diff --git a/app/controllers/Driver.php b/app/controllers/Driver.php index ee45646a..e682e2db 100644 --- a/app/controllers/Driver.php +++ b/app/controllers/Driver.php @@ -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 @@ -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'])){ @@ -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); + } + } + } } \ No newline at end of file diff --git a/app/models/DriverModel.php b/app/models/DriverModel.php index 36b54333..a908f7e5 100644 --- a/app/models/DriverModel.php +++ b/app/models/DriverModel.php @@ -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; + } + } } \ No newline at end of file diff --git a/app/views/driver/vehicles.php b/app/views/driver/vehicles.php index 7c11bf0a..9a45b96e 100644 --- a/app/views/driver/vehicles.php +++ b/app/views/driver/vehicles.php @@ -14,7 +14,7 @@

Vehicles



- Add Vehicle + +
You have no any registered vehicles
@@ -29,10 +29,22 @@ - name ?> - vehicleType ?> - Update

- Delete + + name ?> + + + vehicleType ?> + + +
+ + + +
+
+ + +
@@ -42,5 +54,9 @@ - + diff --git a/app/views/driver/vehicles/update.php b/app/views/driver/vehicles/update.php index b3d9bbc7..4c64bd46 100644 --- a/app/views/driver/vehicles/update.php +++ b/app/views/driver/vehicles/update.php @@ -1 +1,55 @@ + + + + + + +
+

Update Vehicle

+ +
+ +
+ + +
+ +
Name:
+ + + +

+ +
User Type:
+
+
    +
  • Car
  • +
  • Bike
  • +
  • 3wheel
  • +
+
+ + + + +

+ + + +
+
+ + +