2024-12-31 15:53:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class VehicleController extends Controller {
|
|
|
|
public function index() {
|
|
|
|
$vehicle = new Vehicle();
|
|
|
|
$vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
|
|
|
$this->view('vehicles/index', ['title' => 'Vehicles', 'vehicles' => $vehicles]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create() {
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
$name = $_POST['name'] ?? '';
|
|
|
|
$registration_plate = $_POST['registration_plate'] ?? '';
|
|
|
|
$fuel_type = $_POST['fuel_type'] ?? '';
|
|
|
|
$note = $_POST['note'] ?? '';
|
|
|
|
|
|
|
|
$validator = new Validator();
|
|
|
|
$validator->required('name', $name);
|
|
|
|
$validator->required('registration_plate', $registration_plate);
|
|
|
|
$validator->required('fuel_type', $fuel_type);
|
|
|
|
|
|
|
|
if($note == "") $note = NULL;
|
|
|
|
|
|
|
|
if (!$validator->passes()) {
|
2025-01-05 18:30:29 +01:00
|
|
|
$this->view('vehicles/create', [
|
2024-12-31 15:53:15 +01:00
|
|
|
'error' => 'Please correct the errors below.',
|
|
|
|
'validationErrors' => $validator->errors() ?: [],
|
|
|
|
]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$vehicle = new Vehicle();
|
2025-02-01 18:38:49 +01:00
|
|
|
$default_vehicle = $vehicle->getDefaultVehicle($_SESSION['user']['id']);
|
|
|
|
$is_default = $default_vehicle ? 0 : 1;
|
|
|
|
|
2024-12-31 15:53:15 +01:00
|
|
|
$result = $vehicle->create([
|
|
|
|
'name' => $name,
|
|
|
|
'registration_plate' => strtoupper($registration_plate),
|
|
|
|
'fuel_type' => $fuel_type,
|
|
|
|
'note' => $note,
|
|
|
|
'user_id' => $_SESSION['user']['id'],
|
2025-02-01 18:38:49 +01:00
|
|
|
'is_default' => $is_default
|
2024-12-31 15:53:15 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
if ($result === true) {
|
2025-02-01 18:38:49 +01:00
|
|
|
$this->redirect('/');
|
2024-12-31 15:53:15 +01:00
|
|
|
} else {
|
|
|
|
$this->view('vehicles/create', ['title' => 'Create vehicle', 'error' => $result, 'validationErrors' => []] );
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->view('vehicles/create', ['title' => 'Create Vehicle']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function edit() {
|
2025-01-26 22:22:12 +01:00
|
|
|
// TODO: Edit vehicle (to be implemented later)
|
2024-12-31 15:53:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete() {
|
2025-01-26 23:01:35 +01:00
|
|
|
if(!$_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
echo "Wrong method";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Validate the request
|
|
|
|
$vehicle_id = $_POST['vehicle_id'];
|
|
|
|
|
|
|
|
$vehicle = new Vehicle();
|
|
|
|
$result = $vehicle->delete($vehicle_id, $_SESSION['user']['id']);
|
|
|
|
|
|
|
|
if($result != true) {
|
|
|
|
echo "Something went wrong";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-01 18:38:49 +01:00
|
|
|
header("Location: /vehicles");
|
2025-01-26 22:22:12 +01:00
|
|
|
}
|
|
|
|
|
2025-01-27 00:38:22 +01:00
|
|
|
public function setDefault() {
|
|
|
|
$vehicle = new Vehicle();
|
|
|
|
// TODO: Validate the request
|
|
|
|
$result = $vehicle->setDefaultVehicle($_POST['vehicle_id'], $_SESSION['user']['id']);
|
|
|
|
if($result != true) {
|
|
|
|
echo "Something went wrong";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-02-01 18:38:49 +01:00
|
|
|
header("Location: /");
|
2025-01-27 00:38:22 +01:00
|
|
|
}
|
|
|
|
|
2025-01-26 22:22:12 +01:00
|
|
|
public function api_get() {
|
|
|
|
if(!$_SERVER['REQUEST_METHOD'] === 'GET') {
|
|
|
|
echo "Wrong method, use GET";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$vehicle = new Vehicle();
|
|
|
|
$result = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
|
|
|
echo json_encode($result);
|
2024-12-31 15:53:15 +01:00
|
|
|
}
|
|
|
|
}
|