diff --git a/app/controllers/DashboardController.php b/app/controllers/DashboardController.php index 647d6d5..2066317 100644 --- a/app/controllers/DashboardController.php +++ b/app/controllers/DashboardController.php @@ -1,12 +1,12 @@ getHabitsByUser($_SESSION['user']['id']); + $vehicle = new Vehicle(); + $vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']); $this->view('dashboard/index', [ 'title' => 'Dashboard', - 'habits' => $habits, + 'vehicles' => $vehicles, ]); } diff --git a/app/controllers/HabitController.php b/app/controllers/HabitController.php deleted file mode 100644 index 9221113..0000000 --- a/app/controllers/HabitController.php +++ /dev/null @@ -1,57 +0,0 @@ -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) - } -} diff --git a/app/controllers/VehicleController.php b/app/controllers/VehicleController.php new file mode 100644 index 0000000..d2897ca --- /dev/null +++ b/app/controllers/VehicleController.php @@ -0,0 +1,61 @@ +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) + } +} diff --git a/app/models/Habit.php b/app/models/Habit.php deleted file mode 100644 index f7078b8..0000000 --- a/app/models/Habit.php +++ /dev/null @@ -1,46 +0,0 @@ -db = Database::getInstance()->getConnection(); - } - - public function create($data) { - $stmt = $this->db->prepare(" - INSERT INTO habits (user_id, title, frequency, custom_frequency, reward_points, created_at) - VALUES (?, ?, ?, ?, ?, NOW()) - "); - - $stmt->bind_param( - "isssi", // Bind types: int, string, string, string, int - $data['user_id'], - $data['name'], - $data['frequency'], - $data['custom_frequency'], // Bind the custom_frequency field - $data['reward_points'] - ); - - if ($stmt->execute()) { - return true; - } else { - error_log("Failed to create habit: " . $stmt->error); - return false; - } - } - - public function getHabitsByUser($userId) { - $stmt = $this->db->prepare("SELECT id, title, frequency, custom_frequency, reward_points, created_at FROM habits WHERE user_id = ?"); - $stmt->bind_param("i", $userId); - $stmt->execute(); - $result = $stmt->get_result(); - - $habits = []; - while ($row = $result->fetch_assoc()) { - $habits[] = $row; - } - - return $habits; - } -} diff --git a/app/models/Vehicle.php b/app/models/Vehicle.php new file mode 100644 index 0000000..42dd2f1 --- /dev/null +++ b/app/models/Vehicle.php @@ -0,0 +1,49 @@ +db = Database::getInstance()->getConnection(); + } + + public function create($data) { + try{ + $stmt = $this->db->prepare(" + INSERT INTO vehicles (user_id, name, registration_plate, fuel_type, note, created_at) + VALUES (?, ?, ?, ?, ?, NOW()) + "); + + $stmt->bind_param( + "issss", + $data['user_id'], + $data['name'], + $data['registration_plate'], + $data['fuel_type'], + $data['note'], + ); + + if ($stmt->execute()) { + return true; + } else { + return "Error: " . $stmt->error; + } + } catch(mysqli_sql_exception $e) { + return $e->getMessage(); + } + } + + public function getVehiclesByUser($userId) { + $stmt = $this->db->prepare("SELECT id, name, registration_plate, fuel_type, note, created_at FROM vehicles WHERE user_id = ?"); + $stmt->bind_param("i", $userId); + $stmt->execute(); + $result = $stmt->get_result(); + + $vehicles = []; + while ($row = $result->fetch_assoc()) { + $vehicles[] = $row; + } + + return $vehicles; + } +} diff --git a/app/views/dashboard/index.php b/app/views/dashboard/index.php index ae3273d..93fefaf 100644 --- a/app/views/dashboard/index.php +++ b/app/views/dashboard/index.php @@ -3,8 +3,8 @@

Welcome, !

- Create new habit! - List all habits + Add new refuel record! + List all vehicles
diff --git a/app/views/habits/create.php b/app/views/habits/create.php deleted file mode 100644 index 84aaa73..0000000 --- a/app/views/habits/create.php +++ /dev/null @@ -1,70 +0,0 @@ - - -
-

get('title', 'Create Habit') ?>

- - get('error')): ?> -
- get('error')) ?> -
- - -
- - - - - - - - - -
-
- - diff --git a/app/views/habits/index.php b/app/views/habits/index.php deleted file mode 100644 index b050b5b..0000000 --- a/app/views/habits/index.php +++ /dev/null @@ -1,24 +0,0 @@ - -
- get('habits'))): ?> -

No habits yet. Create your first habit.

- -
- get('habits') as $habit): ?> -
- -

Frequency:

- -

- -

-

- Mark as done | - Edit | - Delete -
- -
- Create new habit! - -
diff --git a/app/views/vehicles/create.php b/app/views/vehicles/create.php new file mode 100644 index 0000000..963bacf --- /dev/null +++ b/app/views/vehicles/create.php @@ -0,0 +1,35 @@ + + +
+

get('title', 'Create Vehicle') ?>

+ + get('error')): ?> +
+ get('error')) ?> +
+ + +
+ + + + + + + + + + + + + +
+
diff --git a/app/views/habits/edit.php b/app/views/vehicles/edit.php similarity index 100% rename from app/views/habits/edit.php rename to app/views/vehicles/edit.php diff --git a/app/views/vehicles/index.php b/app/views/vehicles/index.php new file mode 100644 index 0000000..2ec4ed4 --- /dev/null +++ b/app/views/vehicles/index.php @@ -0,0 +1,19 @@ + +
+ get('vehicles'))): ?> +

No vehicles yet. Add your first vehicle.

+ +
+ get('vehicles') as $vehicle): ?> +
+ + Edit | + Delete +
+ +
+ +
+ Add new vehicle! + +
diff --git a/core/Database.php b/core/Database.php index 66cf280..11e437f 100644 --- a/core/Database.php +++ b/core/Database.php @@ -79,6 +79,7 @@ class Database { registration_plate VARCHAR(50) NOT NULL UNIQUE, fuel_type ENUM('Diesel', 'Gasoline 95', 'Gasoline 98', 'Premium Diesel', 'Premium Gasoline 95', 'Premium Gasoline 98', 'Other') NOT NULL, note VARCHAR(150) NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ) ENGINE=InnoDB; "; @@ -95,7 +96,7 @@ class Database { liters DECIMAL(10, 2) NOT NULL, price_per_liter DECIMAL(10, 2) NOT NULL, total_price DECIMAL(10, 2) NOT NULL, - refueling_date DATE NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (vehicle_id) REFERENCES vehicles(id) ON DELETE CASCADE ) ENGINE=InnoDB; "; diff --git a/public/css/habits_create.css b/public/css/habits_create.css deleted file mode 100644 index 273f803..0000000 --- a/public/css/habits_create.css +++ /dev/null @@ -1,21 +0,0 @@ -.form form .dow_chb_wrapper input[type="checkbox"] { - width: 1rem; -} - -.form form .dow_chb_wrapper { - display: flex; - justify-content: space-between; -} - -#lbl_dow { - margin-bottom: .5rem; -} - -#lbl_dom { - margin-top: .5rem; -} - -#custom-frequency { - flex-direction: column; - justify-content: space-between; -} diff --git a/public/css/vehicle_create.css b/public/css/vehicle_create.css new file mode 100644 index 0000000..6cd184f --- /dev/null +++ b/public/css/vehicle_create.css @@ -0,0 +1,3 @@ +#registration_plate { + text-transform: uppercase; +} diff --git a/public/index.php b/public/index.php index 74908f2..bf8af17 100644 --- a/public/index.php +++ b/public/index.php @@ -20,7 +20,8 @@ require_once '../core/Database.php'; require_once '../core/middlewares/RequireAuth.php'; require_once models . 'User.php'; -require_once models . 'Habit.php'; +require_once models . 'Vehicle.php'; +#require_once models . 'Refueling.php'; // Initialize router $router = new Router(); @@ -41,11 +42,11 @@ $router->group('/auth', [], function ($router) { $router->add('/dashboard', 'DashboardController@index', ['RequireAuth']); // habits routes -$router->group('/habits', ['RequireAuth'], function ($router) { - $router->add('', 'HabitController@index'); - $router->add('/create', 'HabitController@create'); - $router->add('/edit/{id}', 'HabitController@edit'); - $router->add('/delete/{id}', 'HabitController@delete'); +$router->group('/vehicles', ['RequireAuth'], function ($router) { + $router->add('', 'VehicleController@index'); + $router->add('/add', 'VehicleController@create'); + $router->add('/edit/{id}', 'VehicleController@edit'); + $router->add('/delete/{id}', 'VehicleController@delete'); }); $router->dispatch();