2024-12-31 15:53:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Vehicle {
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
$this->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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-05 21:24:55 +01:00
|
|
|
public function getVehiclesByUser($user_id) {
|
2024-12-31 15:53:15 +01:00
|
|
|
$stmt = $this->db->prepare("SELECT id, name, registration_plate, fuel_type, note, created_at FROM vehicles WHERE user_id = ?");
|
2025-01-05 21:24:55 +01:00
|
|
|
$stmt->bind_param("i", $user_id);
|
2024-12-31 15:53:15 +01:00
|
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->get_result();
|
|
|
|
|
|
|
|
$vehicles = [];
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
|
|
|
$vehicles[] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $vehicles;
|
|
|
|
}
|
2025-01-05 21:24:55 +01:00
|
|
|
|
|
|
|
public function getDefaultVehicle($user_id) {
|
|
|
|
$stmt = $this->db->prepare("
|
|
|
|
SELECT id, name, registration_plate, fuel_type, note, is_default
|
|
|
|
FROM vehicles
|
|
|
|
WHERE user_id = ? AND is_default = TRUE
|
|
|
|
LIMIT 1
|
|
|
|
");
|
|
|
|
$stmt->bind_param("i", $user_id);
|
|
|
|
$stmt->execute();
|
|
|
|
$result = $stmt->get_result();
|
|
|
|
|
|
|
|
return $result->fetch_assoc();
|
|
|
|
}
|
2024-12-31 15:53:15 +01:00
|
|
|
}
|