Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete driver's vehicle CRUD #53

Merged
merged 2 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions app/controllers/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function vehicles(){
$this->view('driver/vehicles', $vehicles);
}

// Register Vehicle
public function vehicleRegister(){
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// Submitted form data
Expand Down Expand Up @@ -47,6 +48,9 @@ public function vehicleRegister(){
if (empty($data['vehicle_type'])){
$data['err'] = 'Please select vehicle type';
}
else if ($data['vehicle_type'] != 'car' and $data['vehicle_type'] != 'bike' and $data['vehicle_type'] != '3wheel'){
$data['err'] = 'Invalid vehicle type';
}

// Validation is completed and no error found
if (empty($data['err'])){
Expand Down Expand Up @@ -75,4 +79,84 @@ public function vehicleRegister(){
$this->view('driver/vehicles/create', $data);
}
}

// Remove Vehicle
public function vehicleRemove(){
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// Submitted form data
// input data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

$data = [
'name' => trim($_POST['name'])
];

// Delete the vehicle
if ($this->driverModel->removeVehicle($data)){
redirect('driver/vehicles');
} else {
die('Something went wrong');
}
}
}

// Update Vehicle
public function vehicleUpdateForm(){
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$data = [
'name' => trim($_POST['name']),
'vehicle_type' => trim($_POST['vehicle_type']),
'err' => ''
];
$this->view('driver/vehicles/update', $data);
}
}

public function vehicleUpdate(){
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// Submitted form data
// input data
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

$data = [
'name' => trim($_POST['name']),
'old_name' => trim($_POST['old_name']),
'vehicle_type' => trim($_POST['vehicle_type']),
'err' => ''
];

// Validate data
// Validate email
if (empty($data['name'])){
$data['err'] = 'Please enter name';
} else {
// Check name
if ($this->driverModel->findVehicleByName($data['name']) and $data['name'] != $data['old_name']){
$data['err'] = 'Name cannot be duplicate';
}
}

// Validate user type
if (empty($data['vehicle_type'])){
$data['err'] = 'Please select vehicle type';
}
else if ($data['vehicle_type'] != 'car' and $data['vehicle_type'] != 'bike' and $data['vehicle_type'] != '3wheel'){
$data['err'] = 'Invalid vehicle type'.$data['vehicle_type'];
}

// Validation is completed and no error found
if (empty($data['err'])){
// Register vehicle
print_r($_SESSION['user_id']);
if ($this->driverModel->updateVehicle($data)){
redirect('driver/vehicles');
} else {
die('Something went wrong');
}
} else {
// Load view with errors
$this->view('driver/vehicles/update', $data);
}
}
}
}
43 changes: 43 additions & 0 deletions app/models/DriverModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,47 @@ public function viewVehicles(){

return $row;
}

public function removeVehicle($data): bool
{
// Prepare statement
$this->db->query('DELETE FROM vehicle WHERE name = :name AND id = :id');

// Bind values
$this->db->bind(':name', $data['name']);
$this->db->bind(':id', $_SESSION['user_id']);
print_r($data['name']);
print_r($_SESSION['user_id']);
// Execute
if ($this->db->execute()){
print_r("check 4");
return true;
}
else {
print_r("check 5");
return false;
}
}

public function updateVehicle($data): bool
{
// Prepare statement
$this->db->query('UPDATE vehicle SET name = :name, vehicleType = :vehicleType WHERE id = :id and name = :old_name');

// Bind values
$this->db->bind(':name', $data['name']);
$this->db->bind(':old_name', $data['old_name']);
$this->db->bind(':vehicleType', $data['vehicle_type']);
$this->db->bind(':id', $_SESSION['user_id']);


print_r($data);
// Execute
if ($this->db->execute()){
return true;
}
else {
return false;
}
}
}
28 changes: 22 additions & 6 deletions app/views/driver/vehicles.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<h1>Vehicles</h1>

<br><br>
<a href="<?php echo URLROOT ?>/driver/vehicleRegister">Add Vehicle</a>
<a href="<?php echo URLROOT ?>/driver/vehicleRegister" style="font-weight: 1000; font-size: 20px">+</a>

<?php if (sizeof($data) == 0) {?>
<div class="emptyVehicle">You have no any registered vehicles</div>
Expand All @@ -29,10 +29,22 @@
</tr>
<?php for ($i = 0; $i < sizeof($data); $i++) {?>
<tr>
<td><?php echo $data[$i]->name ?></td>
<td><?php echo $data[$i]->vehicleType ?></td>
<td style="text-align: center"><a class="sub-option" href="#">Update</a><br><br>
<a class="sub-option" href="#">Delete</a>
<td>
<?php echo $data[$i]->name ?>
</td>
<td>
<?php echo $data[$i]->vehicleType ?>
</td>
<td style="text-align: center">
<form action="<?php echo URLROOT ?>/driver/vehicleUpdateForm" method="post">
<input type="text" name="name" id="name" hidden value="<?php echo $data[$i]->name ?>" />
<input type="text" name="vehicle_type" id="vehicle_type" hidden value="<?php echo $data[$i]->vehicleType ?>" />
<input type="submit" class="sub-option" value="Update"/>
</form>
<form action="<?php echo URLROOT ?>/driver/vehicleRemove" method="post">
<input type="text" name="name" id="name" hidden value="<?php echo $data[$i]->name ?>" />
<input type="submit" class="sub-option" onclick="return confirmSubmit();" value="Delete"/>
</form>
</td>
</tr>
<?php } ?>
Expand All @@ -42,5 +54,9 @@
</div>
</section>
</main>

<script>
function confirmSubmit() {
return confirm("Are you sure you want to delete this vehicle?");
}
</script>
<?php require APPROOT.'/views/inc/footer.php'; ?>
54 changes: 54 additions & 0 deletions app/views/driver/vehicles/update.php
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
<?php require APPROOT.'/views/inc/header.php'; ?>
<!-- TOP NAVIGATION -->
<?php require APPROOT.'/views/inc/components/topnavbar.php'; ?>

<!-- SIDE NAVIGATION -->
<?php
$section = 'vehicles';
require APPROOT.'/views/inc/components/sidenavbar.php';
?>

<div class="form-container">
<h1>Update Vehicle</h1>
<?php if (!empty($data['err'])){?>
<div class="error-msg">
<span class="form-invalid"><?php echo $data["err"] ?></span>
</div>
<?php } ?>

<form action="<?php echo URLROOT ?>/driver/vehicleUpdate" method="post">
<!-- Name -->
<div class="form-input-title">Name:</div>
<input type="text" name="name" id="name" required value="<?php echo $data['name'] ?>" />
<input type="text" name="old_name" id="old_name" required value="<?php echo $data['name'] ?>" disable hidden />

<br><br>
<!-- Vehicle Type -->
<div class="form-input-title">User Type:</div>
<div class="user-selection-container">
<ul class="user-selection-list">
<li data-user-type="car">Car</li>
<li data-user-type="bike">Bike</li>
<li data-user-type="3wheel">3wheel</li>
</ul>
</div>

<!-- Hidden Input for User Type -->
<input type="hidden" name="vehicle_type" id="vehicle_type" value="<?php echo $data['vehicle_type'] ?>" />

<br><br>

<!-- Submit -->
<input type="submit" value="Update">
</form>
</div>

<script>
const userSelectionList = document.querySelector('.user-selection-list');

userSelectionList.addEventListener('click', function(event) {
if (event.target.tagName === 'LI') {
document.getElementById('vehicle_type').value = event.target.getAttribute('data-user-type');
}
});
</script>
<?php require APPROOT.'/views/inc/footer.php'; ?>
Loading