mirror of
https://github.com/michaelstaake/mainty.git
synced 2026-04-16 11:30:13 +00:00
Initial version upload
This commit is contained in:
41
controllers/AuthController.php
Normal file
41
controllers/AuthController.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
class AuthController extends Controller {
|
||||
public function login(): void {
|
||||
$this->requireSetup();
|
||||
|
||||
if (isset($_SESSION['user_id'])) {
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->view('login');
|
||||
}
|
||||
|
||||
public function authenticate(): void {
|
||||
$this->requireSetup();
|
||||
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
if (empty($password)) {
|
||||
$_SESSION['error'] = 'Password is required';
|
||||
$this->redirect('/login');
|
||||
return;
|
||||
}
|
||||
|
||||
$user = new User();
|
||||
|
||||
if ($user->verifyPassword($password)) {
|
||||
$_SESSION['user_id'] = $user->getId();
|
||||
$this->redirect('/home');
|
||||
} else {
|
||||
$_SESSION['error'] = 'Invalid password';
|
||||
$this->redirect('/login');
|
||||
}
|
||||
}
|
||||
|
||||
public function logout(): void {
|
||||
session_destroy();
|
||||
$this->redirect('/login');
|
||||
}
|
||||
}
|
||||
119
controllers/MaintenanceController.php
Normal file
119
controllers/MaintenanceController.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
class MaintenanceController extends Controller {
|
||||
private Maintenance $maintenanceModel;
|
||||
|
||||
public function __construct() {
|
||||
$this->maintenanceModel = new Maintenance();
|
||||
}
|
||||
|
||||
public function add(): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$vehicleId = (int)($_POST['vehicle_id'] ?? 0);
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
$date = trim($_POST['date'] ?? '');
|
||||
$mileage = str_replace(',', '', trim($_POST['mileage'] ?? ''));
|
||||
|
||||
if (empty($name) || empty($date) || empty($mileage)) {
|
||||
$_SESSION['error'] = 'Name, date, and mileage are required';
|
||||
$this->redirect('/vehicles/' . $vehicleId);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'vehicle_id' => $vehicleId,
|
||||
'name' => $name,
|
||||
'date' => $date,
|
||||
'mileage' => (int)$mileage,
|
||||
'description' => trim($_POST['description'] ?? ''),
|
||||
'cost' => !empty($_POST['cost']) ? (float)$_POST['cost'] : null,
|
||||
'parts_list' => trim($_POST['parts_list'] ?? ''),
|
||||
'performed_by' => trim($_POST['performed_by'] ?? '')
|
||||
];
|
||||
|
||||
$this->maintenanceModel->create($data);
|
||||
$_SESSION['success'] = 'Maintenance item added successfully';
|
||||
$this->redirect('/vehicles/' . $vehicleId);
|
||||
}
|
||||
|
||||
public function edit(string $id): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$item = $this->maintenanceModel->getById((int)$id);
|
||||
|
||||
if (!$item) {
|
||||
$_SESSION['error'] = 'Maintenance item not found';
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
$date = trim($_POST['date'] ?? '');
|
||||
$mileage = str_replace(',', '', trim($_POST['mileage'] ?? ''));
|
||||
|
||||
if (empty($name) || empty($date) || empty($mileage)) {
|
||||
$_SESSION['error'] = 'Name, date, and mileage are required';
|
||||
$this->redirect('/vehicles/' . $item['vehicle_id']);
|
||||
return;
|
||||
}
|
||||
|
||||
$data = [
|
||||
'name' => $name,
|
||||
'date' => $date,
|
||||
'mileage' => (int)$mileage,
|
||||
'description' => trim($_POST['description'] ?? ''),
|
||||
'cost' => !empty($_POST['cost']) ? (float)$_POST['cost'] : null,
|
||||
'parts_list' => trim($_POST['parts_list'] ?? ''),
|
||||
'performed_by' => trim($_POST['performed_by'] ?? '')
|
||||
];
|
||||
|
||||
if ($this->maintenanceModel->update((int)$id, $data)) {
|
||||
$_SESSION['success'] = 'Maintenance item updated successfully';
|
||||
} else {
|
||||
$_SESSION['error'] = 'Failed to update maintenance item';
|
||||
}
|
||||
|
||||
$this->redirect('/vehicles/' . $item['vehicle_id']);
|
||||
}
|
||||
|
||||
public function delete(string $id): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$item = $this->maintenanceModel->getById((int)$id);
|
||||
|
||||
if (!$item) {
|
||||
$_SESSION['error'] = 'Maintenance item not found';
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$vehicleId = $item['vehicle_id'];
|
||||
|
||||
if ($this->maintenanceModel->delete((int)$id)) {
|
||||
$_SESSION['success'] = 'Maintenance item deleted successfully';
|
||||
} else {
|
||||
$_SESSION['error'] = 'Failed to delete maintenance item';
|
||||
}
|
||||
|
||||
$this->redirect('/vehicles/' . $vehicleId);
|
||||
}
|
||||
|
||||
public function search(): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$query = $_GET['q'] ?? '';
|
||||
|
||||
if (strlen($query) < 2) {
|
||||
$this->json([]);
|
||||
return;
|
||||
}
|
||||
|
||||
$results = $this->maintenanceModel->searchByName($query);
|
||||
$this->json($results);
|
||||
}
|
||||
}
|
||||
99
controllers/SettingsController.php
Normal file
99
controllers/SettingsController.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
class SettingsController extends Controller {
|
||||
private QuickTask $quickTaskModel;
|
||||
private User $userModel;
|
||||
|
||||
public function __construct() {
|
||||
$this->quickTaskModel = new QuickTask();
|
||||
$this->userModel = new User();
|
||||
}
|
||||
|
||||
public function index(): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$quickTasks = $this->quickTaskModel->getAll();
|
||||
|
||||
$this->view('settings', [
|
||||
'quickTasks' => $quickTasks
|
||||
]);
|
||||
}
|
||||
|
||||
public function changePassword(): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$currentPassword = $_POST['current_password'] ?? '';
|
||||
$newPassword = $_POST['new_password'] ?? '';
|
||||
$confirmPassword = $_POST['confirm_password'] ?? '';
|
||||
|
||||
if (empty($currentPassword) || empty($newPassword) || empty($confirmPassword)) {
|
||||
$_SESSION['error'] = 'All password fields are required';
|
||||
$this->redirect('/settings');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->userModel->verifyPassword($currentPassword)) {
|
||||
$_SESSION['error'] = 'Current password is incorrect';
|
||||
$this->redirect('/settings');
|
||||
return;
|
||||
}
|
||||
|
||||
if ($newPassword !== $confirmPassword) {
|
||||
$_SESSION['error'] = 'New passwords do not match';
|
||||
$this->redirect('/settings');
|
||||
return;
|
||||
}
|
||||
|
||||
if (strlen($newPassword) < 6) {
|
||||
$_SESSION['error'] = 'New password must be at least 6 characters';
|
||||
$this->redirect('/settings');
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->userModel->updatePassword($newPassword)) {
|
||||
$_SESSION['success'] = 'Password changed successfully';
|
||||
} else {
|
||||
$_SESSION['error'] = 'Failed to change password';
|
||||
}
|
||||
|
||||
$this->redirect('/settings');
|
||||
}
|
||||
|
||||
public function addQuickTask(): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
|
||||
if (empty($name)) {
|
||||
$_SESSION['error'] = 'Task name is required';
|
||||
$this->redirect('/settings');
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $this->quickTaskModel->create($name);
|
||||
|
||||
if ($id > 0) {
|
||||
$_SESSION['success'] = 'Quick task added successfully';
|
||||
} else {
|
||||
$_SESSION['error'] = 'Task already exists or could not be added';
|
||||
}
|
||||
|
||||
$this->redirect('/settings');
|
||||
}
|
||||
|
||||
public function deleteQuickTask(string $id): void {
|
||||
$this->requireSetup();
|
||||
$this->requireAuth();
|
||||
|
||||
if ($this->quickTaskModel->delete((int)$id)) {
|
||||
$_SESSION['success'] = 'Quick task deleted successfully';
|
||||
} else {
|
||||
$_SESSION['error'] = 'Failed to delete quick task';
|
||||
}
|
||||
|
||||
$this->redirect('/settings');
|
||||
}
|
||||
}
|
||||
49
controllers/SetupController.php
Normal file
49
controllers/SetupController.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
class SetupController extends Controller {
|
||||
public function index(): void {
|
||||
// Check if database is already initialized
|
||||
if (Database::isInitialized()) {
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->view('setup');
|
||||
}
|
||||
|
||||
public function setup(): void {
|
||||
if (Database::isInitialized()) {
|
||||
$this->redirect('/home');
|
||||
return;
|
||||
}
|
||||
|
||||
$password = $_POST['password'] ?? '';
|
||||
$confirmPassword = $_POST['confirm_password'] ?? '';
|
||||
|
||||
if (empty($password)) {
|
||||
$_SESSION['error'] = 'Password is required';
|
||||
$this->redirect('/setup');
|
||||
return;
|
||||
}
|
||||
|
||||
if ($password !== $confirmPassword) {
|
||||
$_SESSION['error'] = 'Passwords do not match';
|
||||
$this->redirect('/setup');
|
||||
return;
|
||||
}
|
||||
|
||||
if (strlen($password) < 6) {
|
||||
$_SESSION['error'] = 'Password must be at least 6 characters';
|
||||
$this->redirect('/setup');
|
||||
return;
|
||||
}
|
||||
|
||||
if (Database::initialize($password)) {
|
||||
$_SESSION['success'] = 'Setup completed successfully! Please login.';
|
||||
$this->redirect('/login');
|
||||
} else {
|
||||
$_SESSION['error'] = 'Setup failed. Please try again.';
|
||||
$this->redirect('/setup');
|
||||
}
|
||||
}
|
||||
}
|
||||
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