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

Update package updating #135

Merged
merged 1 commit into from
Nov 13, 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
56 changes: 35 additions & 21 deletions app/controllers/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public function packageRegister($parking_ID = null, $parking_name = null){
}

// Remove package
public function packageRemove()
{
public function packageRemove(){
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Submitted form data
// input data
Expand All @@ -103,13 +102,14 @@ public function packageRemove()
}

// Update package
public function packageUpdateForm()
{
public function packageUpdateForm(){
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$data = [
'id' => trim($_POST['id']),
'name' => trim($_POST['name']),
'price' => trim($_POST['price']),
'packageType' => trim($_POST['package_type']),
'package_type' => trim($_POST['package_type']),
'vehicle_type' => trim($_POST['vehicle_type']),
'package_price' => trim($_POST['package_price']),
'err' => ''
];
$this->view('parkingOwner/packages/update', $data);
Expand All @@ -124,35 +124,49 @@ public function packageUpdate()
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

$data = [
'id' => trim($_POST['id']),
'name' => trim($_POST['name']),
'price' => trim($_POST['price']),
'packageType' => trim($_POST['package_type']),
'vehicle_type' => trim($_POST['vehicle_type']),
'old_vehicle_type' => trim($_POST['old_vehicle_type']),
'package_price' => trim($_POST['package_price']),
'package_type' => trim($_POST['package_type']),
'old_package_type' => trim($_POST['old_package_type']),
'err' => ''
];

// Validate data
// Validate email
if (empty($data['name'])) {
$data['err'] = 'Please select package name';
} else if ($data['name'] != 'weekly' and $data['name'] != 'monthly') {
$data['err'] = 'Invalid package name';
// Validate package type
if (empty($data['package_type'])) {
$data['err'] = 'Please select package type';
} else if ($data['package_type'] != 'weekly' and $data['package_type'] != 'monthly') {
$data['err'] = 'Invalid package type';
}

if (empty($data['price'])) {
// Validate package price
if (empty($data['package_price'])) {
$data['err'] = 'Please enter price';
}
}

if (empty($data['packageType'])) {
$data['err'] = 'Please select package type';
} else if ($data['packageType'] != 'car' and $data['packageType'] != 'bike' and $data['packageType'] != '3wheel') {
$data['err'] = 'Invalid package type';
// Validate vehicle 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';
}

if($data['old_vehicle_type'] != $data['vehicle_type'] or $data['old_package_type'] != $data['package_type']){
if ($this->parkingOwnerModel->findPackage($data['id'], $data['package_type'], $data['vehicle_type'])){
$data['err'] = 'Package cannot be duplicate';
}
}



// Validation is completed and no error found
if (empty($data['err'])) {
// Register package
print_r($_SESSION['user_id']);
if ($this->parkingOwnerModel->updatePackage($data)) {
redirect('parkingOwner/packages');
redirect('parkingOwner/packages/'.$data['id'].'/'.$data['name']);
} else {
die('Something went wrong');
}
Expand Down
13 changes: 7 additions & 6 deletions app/models/ParkingOwnerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,15 @@ public function removePackage($data): bool
public function updatePackage($data): bool
{
// Prepare statement
$this->db->query('UPDATE package SET name = :name, price=:price, packageType =:packageType WHERE pid = :pid and name = :old_name');
$this->db->query('UPDATE package SET name = :name, price=:price, packageType =:packageType WHERE pid = :pid and name = :oldName and packageType = :oldPackageType');

// Bind values
$this->db->bind(':name', $data['name']);
$this->db->bind(':old_name', $data['old_name']);
$this->db->bind(':price', $data['price']);
$this->db->bind(':packageType', $data['packageType']);
$this->db->bind(':pid', $_SESSION['user_id']);
$this->db->bind(':oldPackageType', $data['old_vehicle_type']);
$this->db->bind(':packageType', $data['vehicle_type']);
$this->db->bind(':price', $data['package_price']);
$this->db->bind(':oldName', $data['old_package_type']);
$this->db->bind(':name', $data['package_type']);
$this->db->bind(':pid', $data['id']);


// Execute
Expand Down
2 changes: 2 additions & 0 deletions app/views/parkingOwner/packages.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
</div>
<div class="right">
<form action="<?php echo URLROOT ?>/package/packageUpdateForm" method="post">
<input type="text" name="id" id="id" hidden value="<?php echo $data['id'] ?>" />
<input type="text" name="name" id="name" hidden value="<?php echo $data['name'] ?>" />
<input type="text" name="package_type" id="package_type" hidden value="<?php echo $other_data[$i]->name ?>" />
<input type="text" name="package_price" id="package_price" hidden value="<?php echo $other_data[$i]->price ?>" />
<input type="text" name="vehicle_type" id="vehicle_type" hidden value="<?php echo $other_data[$i]->packageType ?>" />
Expand Down
4 changes: 2 additions & 2 deletions app/views/parkingOwner/packages/create.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
<?php } ?>

<form action="<?php echo URLROOT ?>/package/packageRegister" method="post">
<input type="text" name="id" id="id" required hidden value="<?php echo $data['id'] ?>" />
<input type="text" name="name" id="name" required hidden value="<?php echo $data['name'] ?>" />
<input type="text" name="id" id="id" required hidden value="<?php echo $data['id'] ?>" />
<input type="text" name="name" id="name" required hidden value="<?php echo $data['name'] ?>" />
<!-- package name -->
<select name="package_type">
<option value="" hidden disabled selected>Package Type</option>
Expand Down
57 changes: 42 additions & 15 deletions app/views/parkingOwner/packages/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,56 @@
</div>
<?php } ?>

<form action="<?php echo URLROOT ?>/parkingOwner/packageUpdate" method="post">


<form action="<?php echo URLROOT ?>/Package/packageUpdate" method="post">
<input type="text" name="id" id="id" required hidden value="<?php echo $data['id'] ?>" />
<input type="text" name="name" id="name" required hidden value="<?php echo $data['name'] ?>" />
<input type="text" name="old_vehicle_type" id="old_vehicle_type" required hidden value="<?php echo $data['vehicle_type'] ?>" />
<input type="text" name="old_package_type" id="old_package_type" required hidden value="<?php echo $data['package_type'] ?>" />
<!-- package name -->
<input type="text" name="price" id="price" required hidden value="<?php echo $data['name'] ?>" />
<select name="name" >
<option value="" hidden disabled selected>Package Name</option>
<option value="weekly">weekly</option>
<option value="monthly">monthly</option>
<select name="package_type">
<?php if($data['package_type'] == 'weekly') {?>
<option value="weekly" selected>weekly</option>
<option value="monthly">monthly</option>
<?php }
else if($data['package_type'] == 'monthly') {?>
<option value="weekly">weekly</option>
<option value="monthly selected">monthly</option>
<?php }
else{?>
<option value="" hidden disabled selected>Package Type</option>
<option value="weekly">weekly</option>
<option value="monthly">monthly</option>
<?php }?>
</select>

<!-- Price -->
<div class="form-input-title">Price:</div>
<input type="text" name="price" id="price" required value="<?php echo $data['price'] ?>" />
<input type="text" name="package_price" id="package_price" required value="<?php echo $data['package_price'] ?>" />

<br><br>
<!-- package Type -->
<input type="text" name="price" id="price" required hidden="<?php echo $data['packageType'] ?>" />
<select name="package_type" >
<option value="" hidden disabled selected>Package Type</option>
<option value="car">Car</option>
<option value="bike">Bike</option>
<option value="3wheel">Three wheel</option>
<select name="vehicle_type">
<?php if($data['vehicle_type'] == 'car') {?>
<option value="car" selected>Car</option>
<option value="bike">Bike</option>
<option value="3wheel">Three wheel</option>
<?php }
else if($data['vehicle_type'] == 'bike') {?>
<option value="car">Car</option>
<option value="bike" selected>Bike</option>
<option value="3wheel">Three wheel</option>
<?php }
else if($data['vehicle_type'] == '3wheel') {?>
<option value="car">Car</option>
<option value="bike">Bike</option>
<option value="3wheel" selected>Three wheel</option>
<?php }
else{?>
<option value="" hidden disabled selected>Vehicle Type</option>
<option value="car">Car</option>
<option value="bike">Bike</option>
<option value="3wheel">Three wheel</option>
<?php }?>
</select>

<br><br>
Expand Down
Loading