Added: vehicle creation
This commit is contained in:
		@@ -1,12 +1,12 @@
 | 
			
		||||
<?php
 | 
			
		||||
class DashboardController extends Controller {
 | 
			
		||||
    public function index() {
 | 
			
		||||
        $habit = new Habit();
 | 
			
		||||
        $habits = $habit->getHabitsByUser($_SESSION['user']['id']);
 | 
			
		||||
        $vehicle = new Vehicle();
 | 
			
		||||
        $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
 | 
			
		||||
 | 
			
		||||
        $this->view('dashboard/index', [
 | 
			
		||||
            'title' => 'Dashboard',
 | 
			
		||||
            'habits' => $habits,
 | 
			
		||||
            'vehicles' => $vehicles,
 | 
			
		||||
        ]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,57 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
class HabitController extends Controller {
 | 
			
		||||
    public function index() {
 | 
			
		||||
        $habit = new Habit();
 | 
			
		||||
        $habits = $habit->getHabitsByUser($_SESSION['user']['id']);
 | 
			
		||||
        $this->view('habits/index', ['title' => 'Habits', 'habits' => $habits]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function create() {
 | 
			
		||||
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
 | 
			
		||||
            $name = $_POST['name'] ?? '';
 | 
			
		||||
            $frequency = $_POST['frequency'] ?? 'Daily';
 | 
			
		||||
            $customFrequency = null;
 | 
			
		||||
 | 
			
		||||
            if (empty($name)) {
 | 
			
		||||
                $this->view('habits/create', ['error' => 'Habit name is required.']);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if ($frequency === 'Custom') {
 | 
			
		||||
                $daysOfWeek = $_POST['days_of_week'] ?? [];
 | 
			
		||||
                $daysOfMonth = $_POST['days_of_month'] ?? '*';
 | 
			
		||||
                $months = $_POST['months'] ?? '*';
 | 
			
		||||
 | 
			
		||||
                // Combine into crontab-like string
 | 
			
		||||
                $customFrequency = implode(',', $daysOfWeek) . " $daysOfMonth $months";
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $habit = new Habit();
 | 
			
		||||
            $result = $habit->create([
 | 
			
		||||
                'name' => $name,
 | 
			
		||||
                'frequency' => $frequency,
 | 
			
		||||
                'custom_frequency' => $customFrequency,
 | 
			
		||||
                'reward_points' => intval($_POST['difficulty'] ?? 1),
 | 
			
		||||
                'user_id' => $_SESSION['user']['id'],
 | 
			
		||||
            ]);
 | 
			
		||||
 | 
			
		||||
            if ($result) {
 | 
			
		||||
                $this->redirect('/habits');
 | 
			
		||||
            } else {
 | 
			
		||||
                $this->view('habits/create', ['error' => 'Failed to create habit.']);
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->view('habits/create', ['title' => 'Create Habit']);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function edit() {
 | 
			
		||||
        // Edit habit (to be implemented later)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function delete() {
 | 
			
		||||
        // Delete habit (to be implemented later)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										61
									
								
								app/controllers/VehicleController.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								app/controllers/VehicleController.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
<?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()) {
 | 
			
		||||
                $this->view('vehicle/create', [
 | 
			
		||||
                    'error' => 'Please correct the errors below.',
 | 
			
		||||
                    'validationErrors' => $validator->errors() ?: [],
 | 
			
		||||
                ]);
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            $vehicle = new Vehicle();
 | 
			
		||||
            $result = $vehicle->create([
 | 
			
		||||
                'name' => $name,
 | 
			
		||||
                'registration_plate' => strtoupper($registration_plate),
 | 
			
		||||
                'fuel_type' => $fuel_type,
 | 
			
		||||
                'note' => $note,
 | 
			
		||||
                'user_id' => $_SESSION['user']['id'],
 | 
			
		||||
            ]);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
            if ($result === true) {
 | 
			
		||||
                $this->redirect('/vehicles');
 | 
			
		||||
            } else {
 | 
			
		||||
                $this->view('vehicles/create', ['title' => 'Create vehicle', 'error' => $result, 'validationErrors' => []] );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        } else {
 | 
			
		||||
            $this->view('vehicles/create', ['title' => 'Create Vehicle']);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public function edit() {
 | 
			
		||||
        // Edit vehicle (to be implemented later)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function delete() {
 | 
			
		||||
        // Delete vehicle (to be implemented later)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user