mirror of
https://github.com/michaelstaake/mainty.git
synced 2026-04-16 19:40:13 +00:00
Initial version upload
This commit is contained in:
150
controllers/VehicleController.php
Normal file
150
controllers/VehicleController.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
class VehicleController extends Controller {
|
||||
private Vehicle $vehicleModel;
|
||||
private Maintenance $maintenanceModel;
|
||||
|
||||
public function __construct() {
|
||||
$this->vehicleModel = new Vehicle();
|
||||
$this->maintenanceModel = new Maintenance();
|
||||
}
|
||||
|
||||
public function index(): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$vehicles = $this->vehicleModel->getAll();
|
||||
$viewMode = $_GET['view'] ?? 'grid'; // grid or list
|
||||
|
||||
$this->view('index', [
|
||||
'vehicles' => $vehicles,
|
||||
'viewMode' => $viewMode
|
||||
]);
|
||||
}
|
||||
|
||||
public function show(string $id): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$vehicle = $this->vehicleModel->getById((int)$id);
|
||||
|
||||
if (!$vehicle) {
|
||||
$_SESSION['error'] = 'Vehicle not found';
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$maintenanceItems = $this->maintenanceModel->getByVehicleId((int)$id);
|
||||
|
||||
$this->view('vehicle', [
|
||||
'vehicle' => $vehicle,
|
||||
'maintenanceItems' => $maintenanceItems
|
||||
]);
|
||||
}
|
||||
|
||||
public function add(): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
|
||||
if (empty($name)) {
|
||||
$_SESSION['error'] = 'Vehicle name is required';
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'year' => trim($_POST['year'] ?? ''),
|
||||
'make' => trim($_POST['make'] ?? ''),
|
||||
'model' => trim($_POST['model'] ?? ''),
|
||||
'color' => trim($_POST['color'] ?? ''),
|
||||
'license_plate' => trim($_POST['license_plate'] ?? '')
|
||||
];
|
||||
|
||||
$id = $this->vehicleModel->create($data);
|
||||
$_SESSION['success'] = 'Vehicle added successfully';
|
||||
$this->redirect('/vehicles/' . $id);
|
||||
}
|
||||
|
||||
public function edit(string $id): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
|
||||
if (empty($name)) {
|
||||
$_SESSION['error'] = 'Vehicle name is required';
|
||||
$this->redirect('/vehicles/' . $id);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'year' => trim($_POST['year'] ?? ''),
|
||||
'make' => trim($_POST['make'] ?? ''),
|
||||
'model' => trim($_POST['model'] ?? ''),
|
||||
'color' => trim($_POST['color'] ?? ''),
|
||||
'license_plate' => trim($_POST['license_plate'] ?? '')
|
||||
];
|
||||
|
||||
if ($this->vehicleModel->update((int)$id, $data)) {
|
||||
$_SESSION['success'] = 'Vehicle updated successfully';
|
||||
} else {
|
||||
$_SESSION['error'] = 'Failed to update vehicle';
|
||||
}
|
||||
|
||||
$this->redirect('/vehicles/' . $id);
|
||||
}
|
||||
|
||||
public function delete(string $id): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
if ($this->vehicleModel->delete((int)$id)) {
|
||||
$_SESSION['success'] = 'Vehicle deleted successfully';
|
||||
} else {
|
||||
$_SESSION['error'] = 'Failed to delete vehicle';
|
||||
}
|
||||
|
||||
$this->redirect('/home');
|
||||
}
|
||||
|
||||
public function export(string $id, string $format): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$vehicle = $this->vehicleModel->getById((int)$id);
|
||||
|
||||
if (!$vehicle) {
|
||||
$_SESSION['error'] = 'Vehicle not found';
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$maintenanceItems = $this->maintenanceModel->getByVehicleId((int)$id);
|
||||
|
||||
if ($format === 'json') {
|
||||
header('Content-Type: application/json');
|
||||
header('Content-Disposition: attachment; filename="vehicle-' . $id . '-export.json"');
|
||||
echo json_encode([
|
||||
'vehicle' => $vehicle,
|
||||
'maintenance_items' => $maintenanceItems
|
||||
], JSON_PRETTY_PRINT);
|
||||
exit;
|
||||
} elseif ($format === 'html') {
|
||||
header('Content-Type: text/html');
|
||||
header('Content-Disposition: attachment; filename="vehicle-' . $id . '-export.html"');
|
||||
|
||||
$this->view('export', [
|
||||
'vehicle' => $vehicle,
|
||||
'maintenanceItems' => $maintenanceItems
|
||||
]);
|
||||
exit;
|
||||
} else {
|
||||
$_SESSION['error'] = 'Invalid export format';
|
||||
$this->redirect('/vehicles/' . $id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user