Updated: Improved vehicle and refuel data handling on dashboard
All checks were successful
Build and Deploy Zola Website / build_and_deploy (push) Successful in 14s
All checks were successful
Build and Deploy Zola Website / build_and_deploy (push) Successful in 14s
Updated: - DashboardController now fetches latest refuel record only for the default vehicle - VehicleController - first created vehicle is now set as default automatically - Refuel model - latest_one() now accepts vehicle_id instead of user_id - Dashboard view - improved handling when no vehicles or refuel records exist - CSS styles - adjusted dashboard layout and global action padding
This commit is contained in:
parent
f90c707435
commit
7517bcb78f
@ -3,8 +3,7 @@ class DashboardController extends Controller {
|
||||
public function index() {
|
||||
$vehicle = new Vehicle();
|
||||
$vehicles = $vehicle->getVehiclesByUser($_SESSION['user']['id']);
|
||||
|
||||
$default_car = $vehicle->getDefaultVehicle($_SESSION['user']['id']);
|
||||
$default_car = $vehicle->getDefaultVehicle($_SESSION['user']['id']) ?? null;
|
||||
|
||||
$refuel = new Refuel();
|
||||
$data = [
|
||||
@ -13,7 +12,7 @@ class DashboardController extends Controller {
|
||||
"mileage" => [],
|
||||
"liters" => []
|
||||
];
|
||||
$raw_data = $refuel->latest_data($default_car['id'], 5);
|
||||
$raw_data = $default_car ? $refuel->latest_data($default_car['id'], 5) : [];
|
||||
foreach($raw_data as $one) {
|
||||
array_push($data['date'], date('d. m.', strtotime($one['created_at'])));
|
||||
array_push($data['price'], $one['price_per_liter']);
|
||||
@ -21,7 +20,8 @@ class DashboardController extends Controller {
|
||||
array_push($data['liters'], $one['liters']);
|
||||
}
|
||||
|
||||
$latest_record = $refuel->latest_one($_SESSION['user']['id'])[0];
|
||||
$latest_data = $default_car ? $refuel->latest_one($default_car['id']) : [];
|
||||
$latest_record = !empty($latest_data) ? $latest_data[0] : null;
|
||||
|
||||
$this->view('dashboard/index', [
|
||||
'title' => 'Dashboard',
|
||||
|
@ -30,17 +30,21 @@ class VehicleController extends Controller {
|
||||
}
|
||||
|
||||
$vehicle = new Vehicle();
|
||||
$default_vehicle = $vehicle->getDefaultVehicle($_SESSION['user']['id']);
|
||||
$is_default = $default_vehicle ? 0 : 1;
|
||||
|
||||
$result = $vehicle->create([
|
||||
'name' => $name,
|
||||
'registration_plate' => strtoupper($registration_plate),
|
||||
'fuel_type' => $fuel_type,
|
||||
'note' => $note,
|
||||
'user_id' => $_SESSION['user']['id'],
|
||||
'is_default' => $is_default
|
||||
]);
|
||||
|
||||
|
||||
if ($result === true) {
|
||||
$this->redirect('/vehicles');
|
||||
$this->redirect('/');
|
||||
} else {
|
||||
$this->view('vehicles/create', ['title' => 'Create vehicle', 'error' => $result, 'validationErrors' => []] );
|
||||
}
|
||||
@ -72,7 +76,7 @@ class VehicleController extends Controller {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->view('vehicles/index', ['title' => 'Vehicles', 'vehicles' => $vehicles]);
|
||||
header("Location: /vehicles");
|
||||
}
|
||||
|
||||
public function setDefault() {
|
||||
@ -84,7 +88,7 @@ class VehicleController extends Controller {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->view('vehicles/index', ['title' => 'Vehicles', 'vehicles' => $vehicles]);
|
||||
header("Location: /");
|
||||
}
|
||||
|
||||
public function api_get() {
|
||||
|
@ -60,7 +60,7 @@ class Refuel {
|
||||
}
|
||||
}
|
||||
|
||||
public function latest_one($user_id, $record_count = 1) {
|
||||
public function latest_one($vehicle_id, $record_count = 1) {
|
||||
try {
|
||||
$stmt = $this->db->prepare("
|
||||
SELECT
|
||||
@ -70,15 +70,16 @@ class Refuel {
|
||||
`r`.`price_per_liter`,
|
||||
`r`.`total_price`,
|
||||
`r`.`mileage`,
|
||||
`r`.`note`,
|
||||
`r`.`created_at`
|
||||
FROM `refueling_records` AS `r`
|
||||
JOIN `vehicles` AS `v` ON `r`.`vehicle_id` = `v`.`id`
|
||||
WHERE `r`.`user_id` = ?
|
||||
WHERE `r`.`vehicle_id` = ?
|
||||
ORDER BY `r`.`created_at` DESC
|
||||
LIMIT ?;
|
||||
");
|
||||
|
||||
$stmt->bind_param("ii", $user_id, $record_count);
|
||||
$stmt->bind_param("ii", $vehicle_id, $record_count);
|
||||
if ($stmt->execute()) {
|
||||
$result = $stmt->get_result();
|
||||
$data = $result->fetch_all(MYSQLI_ASSOC);
|
||||
|
@ -10,17 +10,18 @@ class Vehicle {
|
||||
public function create($data) {
|
||||
try{
|
||||
$stmt = $this->db->prepare("
|
||||
INSERT INTO vehicles (user_id, name, registration_plate, fuel_type, note, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, NOW())
|
||||
INSERT INTO vehicles (user_id, name, registration_plate, fuel_type, note, is_default, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, NOW())
|
||||
");
|
||||
|
||||
$stmt->bind_param(
|
||||
"issss",
|
||||
"issssi",
|
||||
$data['user_id'],
|
||||
$data['name'],
|
||||
$data['registration_plate'],
|
||||
$data['fuel_type'],
|
||||
$data['note'],
|
||||
$data['is_default'],
|
||||
);
|
||||
|
||||
if ($stmt->execute()) {
|
||||
|
@ -5,8 +5,15 @@
|
||||
|
||||
<section class="dashboard">
|
||||
<h1>Welcome, <?= htmlspecialchars($_SESSION['user']['username']) ?>!</h1>
|
||||
<?php if(!isset($data['default_car'])): ?>
|
||||
|
||||
<div id="intro">
|
||||
<a href="/vehicles/create">Create your first vehicle</a>
|
||||
</div>
|
||||
<?php elseif (isset($data['latest_record'])): ?>
|
||||
|
||||
<div id="actions">
|
||||
<a href="/refuel/create" class="btn-green">Add new refuel record!</a>
|
||||
<a href="/refuel/create" class="btn-green">Add new refuel record</a>
|
||||
<a href="/vehicles" class="btn-primary">List all vehicles</a>
|
||||
<a class="btn-warning" id="btn-offline-add">Add new offline refuel record</a>
|
||||
</div>
|
||||
@ -25,6 +32,10 @@
|
||||
<p><?= $data['latest_record']['total_price'] ?>,-</p>
|
||||
<b>Mileage:</b>
|
||||
<p><?= $data['latest_record']['mileage'] ?> km</p>
|
||||
<?php if (isset($data['latest_record']['note'])): ?>
|
||||
<b>Note:</b>
|
||||
<p><?= $data['latest_record']['note'] ?></p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
@ -55,6 +66,17 @@
|
||||
<b id="avg-fl-cnsmp"></b>
|
||||
</section>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div id="actions">
|
||||
<a href="/refuel/create" class="btn-green">Add new refuel record</a>
|
||||
<a href="/vehicles" class="btn-primary">List all vehicles</a>
|
||||
<a class="btn-warning" id="btn-offline-add">Add new offline refuel record</a>
|
||||
</div>
|
||||
<div class="alert-warning">
|
||||
<p>Default vehicle <b><i><?= $data['default_car']['name'] ?></i></b> doesn't have any refuel record yet.</p>
|
||||
<p>Select another vehicle or create first refuel record.</p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</section>
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
|
||||
<script>
|
||||
|
@ -4,7 +4,7 @@
|
||||
<p>No vehicles yet. <a href="/vehicles/create">Add your first vehicle</a>.</p>
|
||||
<?php else: ?>
|
||||
<div class="btn-wrapper">
|
||||
<a href="/vehicles/create" class="btn-green">Add new vehicle!</a>
|
||||
<a href="/vehicles/create" class="btn-green">Add new vehicle</a>
|
||||
</div>
|
||||
<div class="vehicle-wrapper">
|
||||
<?php foreach ($this->get('vehicles') as $vehicle): ?>
|
||||
@ -13,15 +13,18 @@
|
||||
<p><?= htmlspecialchars($vehicle['registration_plate']) ?></p>
|
||||
<p><?= htmlspecialchars($vehicle['fuel_type']) ?></p>
|
||||
<p><?= htmlspecialchars($vehicle['note'] ?? "") ?></p>
|
||||
|
||||
<div class="actions">
|
||||
<br>
|
||||
<form method="POST" action="/vehicles/delete">
|
||||
<input type="number" name="vehicle_id" value="<?= $vehicle['id'] ?>" style="display: none">
|
||||
<input type="submit" value="Delete vehicle" class="btn-danger">
|
||||
</form>
|
||||
|
||||
<br>
|
||||
<form method="POST" action="/vehicles/default">
|
||||
<input type="number" name="vehicle_id" value="<?= $vehicle['id'] ?>" style="display: none">
|
||||
<input type="submit" value="Set default" class="btn-primary">
|
||||
<input type="submit" value="Set as default" class="btn-primary">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -9,7 +9,6 @@
|
||||
flex-wrap: wrap;
|
||||
gap: 1rem;
|
||||
justify-content: center;
|
||||
margin-top: 2rem;
|
||||
}
|
||||
|
||||
.card {
|
||||
|
@ -52,3 +52,8 @@ h1 {
|
||||
background-color: var(--clr-warning-muted);
|
||||
border: var(--borderWidth-thin) solid var(--clr-border-danger);
|
||||
}
|
||||
|
||||
#actions {
|
||||
padding-top: 1rem;
|
||||
padding-bottom: 2rem;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user