From c60f20b34ce9683cfd4cf5aa1b742b2dc6200e09 Mon Sep 17 00:00:00 2001 From: Sveneld Date: Sun, 17 Mar 2024 15:04:51 +0100 Subject: [PATCH] RentSystem add response method for every class, due to the difference --- src/Rent/AbstractRentSystem.php | 8 ++++ src/Rent/RentSystemQR.php | 69 +++++++++++++++++++++++---------- src/Rent/RentSystemSms.php | 60 ++++++++++++++-------------- src/Rent/RentSystemWeb.php | 55 +++++++++++++++++--------- 4 files changed, 124 insertions(+), 68 deletions(-) create mode 100644 src/Rent/AbstractRentSystem.php diff --git a/src/Rent/AbstractRentSystem.php b/src/Rent/AbstractRentSystem.php new file mode 100644 index 0000000..824f0db --- /dev/null +++ b/src/Rent/AbstractRentSystem.php @@ -0,0 +1,8 @@ +query("SELECT bikeNum FROM bikes WHERE bikeNum=$bikeNum"); if ($result->num_rows != 1) { - response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR); - return; + return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR); } if ($force == false) { if (!$creditSystem->isEnoughCreditForRent($userId)) { $minRequiredCredit = $creditSystem->getMinRequiredCredit(); - response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); - - return; + return $this->response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); } checktoomany(0, $userId); @@ -38,11 +35,11 @@ public function rentBike($userId, $bike, $force = false) if ($countRented >= $limit) { if ($limit == 0) { - response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR); + return $this->response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR); } elseif ($limit == 1) { - response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR); + return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR); } else { - response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR); + return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR); } } @@ -57,7 +54,7 @@ public function rentBike($userId, $bike, $force = false) $serviceTag = $row['serviceTag']; if ($serviceTag != 0) { - response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR); + return $this->response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR); } if ($watches['stack'] and $stacktopbike != $bike) { @@ -68,7 +65,7 @@ public function rentBike($userId, $bike, $force = false) notifyAdmins(_('Bike') . ' ' . $bike . ' ' . _('rented out of stack by') . ' ' . $userName . '. ' . $stacktopbike . ' ' . _('was on the top of the stack at') . ' ' . $stand . '.', ERROR); } if ($forcestack and $stacktopbike != $bike) { - response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR); + return $this->response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR); } } } @@ -88,12 +85,10 @@ public function rentBike($userId, $bike, $force = false) if ($force == false) { if ($currentUser == $userId) { - response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '.', ERROR); - return; + return $this->response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '.', ERROR); } if ($currentUser != 0) { - response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR); - return; + return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR); } } @@ -108,7 +103,7 @@ public function rentBike($userId, $bike, $force = false) } else { $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERENT',parameter=$newCode"); } - response($message); + return $this->response($message); } public function returnBike($userId, $bike, $stand, $note = '', $force = false) @@ -122,7 +117,7 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false) $result = $db->query("SELECT standId FROM stands WHERE standName='$stand'"); if (!$result->num_rows) { - response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR); + return $this->response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR); } $row = $result->fetch_assoc(); $standId = $row["standId"]; @@ -132,14 +127,14 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false) $bikenumber = $result->num_rows; if ($bikenumber == 0) { - response(_('You currently have no rented bikes.'), ERROR); + return $this->response(_('You currently have no rented bikes.'), ERROR); } elseif ($bikenumber > 1) { $message = _('You have') . ' ' . $bikenumber . ' ' . _('rented bikes currently. QR code return can be used only when 1 bike is rented. Please, use web'); if ($connectors["sms"]) { $message .= _(' or SMS'); } $message .= _(' to return the bikes.'); - response($message, ERROR); + return $this->response($message, ERROR); } } @@ -173,6 +168,40 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false) $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERETURN',parameter=$standId"); } - response($message); + return $this->response($message); + } + + protected function response($message, $error = 0, $additional = '', $log = 1) + { + global $db, $systemname, $systemURL, $user, $auth; + if ($log == 1 and $message) { + $userid = $auth->getUserId(); + $number = $user->findPhoneNumber($userid); + logresult($number, $message); + } + $db->commit(); + echo '',$systemname,''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + echo ''; + if (file_exists("analytics.php")) require("analytics.php"); + echo '
'; + if ($error) + { + echo ''; + } + else + { + echo ''; + } + echo '
'; + exit; } } \ No newline at end of file diff --git a/src/Rent/RentSystemSms.php b/src/Rent/RentSystemSms.php index 4de1e8b..7ce8833 100644 --- a/src/Rent/RentSystemSms.php +++ b/src/Rent/RentSystemSms.php @@ -2,12 +2,16 @@ namespace BikeShare\Rent; -class RentSystemSms implements RentSystemInterface +class RentSystemSms extends AbstractRentSystem implements RentSystemInterface { + private $number; + public function rentBike($number, $bike, $force = false) { global $db, $forcestack, $watches, $smsSender, $user, $creditSystem; + $this->number = $number; + $stacktopbike = false; $userId = $user->findUserIdByNumber($number); @@ -15,19 +19,16 @@ public function rentBike($number, $bike, $force = false) $result = $db->query("SELECT bikeNum FROM bikes WHERE bikeNum=$bikeNum"); if ($result->num_rows != 1) { - $smsSender->send($number, "Bike $bikeNum does not exist."); - return; + return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR); } if ($force == false) { if (!$creditSystem->isEnoughCreditForRent($userId)) { $minRequiredCredit = $creditSystem->getMinRequiredCredit(); $userRemainingCredit = $creditSystem->getUserCredit($userId); - $smsSender->send( - $number, + return $this->response( _('Please, recharge your credit:') . " " . $userRemainingCredit . $creditSystem->getCreditCurrency() . ". " . _('Credit required:') . " " . $minRequiredCredit . $creditSystem->getCreditCurrency() . "." ); - return; } checktoomany(0, $userId); @@ -42,13 +43,12 @@ public function rentBike($number, $bike, $force = false) if ($countRented >= $limit) { if ($limit == 0) { - $smsSender->send($number, _('You can not rent any bikes. Contact the admins to lift the ban.')); + return $this->response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR); } elseif ($limit == 1) { - $smsSender->send($number, _('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.'); + return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR); } else { - $smsSender->send($number, _('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.'); + return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR); } - return; } if ($forcestack or $watches['stack']) { @@ -62,8 +62,7 @@ public function rentBike($number, $bike, $force = false) $serviceTag = $row['serviceTag']; if ($serviceTag != 0) { - $smsSender->send($number, "Renting from service stands is not allowed: The bike probably waits for a repair."); - return; + return $this->response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR); } if ($watches['stack'] and $stacktopbike != $bike) { @@ -74,8 +73,7 @@ public function rentBike($number, $bike, $force = false) notifyAdmins(_('Bike') . ' ' . $bike . ' ' . _('rented out of stack by') . ' ' . $userName . '. ' . $stacktopbike . ' ' . _('was on the top of the stack at') . ' ' . $stand . '.', ERROR); } if ($forcestack and $stacktopbike != $bike) { - $smsSender->send($number, _('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.'); - return; + return $this->response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR); } } } @@ -95,12 +93,10 @@ public function rentBike($number, $bike, $force = false) if ($force == false) { if ($currentUser == $userId) { - $smsSender->send($number, _('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '. ' . _('Return bike with command:') . ' RETURN ' . _('bikenumber') . ' ' . _('standname') . '.'); - return; + return $this->response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '. ' . _('Return bike with command:') . ' RETURN ' . _('bikenumber') . ' ' . _('standname') . '.'); } if ($currentUser != 0) { - $smsSender->send($number, _('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.'); - return; + return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR); } } @@ -114,22 +110,24 @@ public function rentBike($number, $bike, $force = false) $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='RENT',parameter=$newCode"); } else { $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERENT',parameter=$newCode"); - $smsSender->send($number, _('System override') . ": " . _('Your rented bike') . " " . $bikeNum . " " . _('has been rented by admin') . "."); + $this->response(_('System override') . ": " . _('Your rented bike') . " " . $bikeNum . " " . _('has been rented by admin') . "."); } - $smsSender->send($number, $message); + return $this->response($message); } public function returnBike($number, $bike, $stand, $message = '', $force = false) { global $db, $smsSender, $user, $creditSystem; + + $this->number = $number; + $userId = $user->findUserIdByNumber($number); $bikeNum = intval($bike); $stand = strtoupper($stand); $result = $db->query("SELECT standId FROM stands WHERE standName='$stand'"); if (!$result->num_rows) { - $smsSender->send($number, _('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.')); - return; + return $this->response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR); } $row = $result->fetch_assoc(); $standId = $row["standId"]; @@ -139,8 +137,7 @@ public function returnBike($number, $bike, $stand, $message = '', $force = false $bikenumber = $result->num_rows; if ($bikenumber == 0) { - $smsSender->send($number, _('You currently have no rented bikes.')); - return; + return $this->response(_('You currently have no rented bikes.'), ERROR); } $listBikes = []; @@ -155,14 +152,12 @@ public function returnBike($number, $bike, $stand, $message = '', $force = false if ($force == false) { $result = $db->query("SELECT currentCode FROM bikes WHERE currentUser=$userId AND bikeNum=$bikeNum"); if ($result->num_rows != 1) { - $smsSender->send($number, _('You does not have bike') . " " . $bikeNum . " rented. " . _('You have rented the following') . " " . sprintf(ngettext('%d bike', '%d bikes', $bikenumber), $bikenumber) . ": $listBikes"); - return; + return $this->response(_('You does not have bike') . " " . $bikeNum . " rented. " . _('You have rented the following') . " " . sprintf(ngettext('%d bike', '%d bikes', $bikenumber), $bikenumber) . ": $listBikes"); } } else { $result = $db->query("SELECT currentCode FROM bikes WHERE bikeNum=$bikeNum"); if ($result->num_rows != 1) { - $smsSender->send($number, _('Bike') . " " . $bikeNum . " " . _('is not rented. Saint Thomas, the patronus of all unrented bikes, prohibited returning unrented bikes.')); - return; + return $this->response(_('Bike') . " " . $bikeNum . " " . _('is not rented. Saint Thomas, the patronus of all unrented bikes, prohibited returning unrented bikes.')); } } $row = $result->fetch_assoc(); @@ -201,6 +196,13 @@ public function returnBike($number, $bike, $stand, $message = '', $force = false $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERETURN',parameter=$standId"); } - $smsSender->send($number, $message); + return $this->response($message); + } + + protected function response($message, $error = 0, $additional = '', $log = 1) + { + global $smsSender; + + $smsSender->send($this->number, $message); } } \ No newline at end of file diff --git a/src/Rent/RentSystemWeb.php b/src/Rent/RentSystemWeb.php index 06a6010..88f07b2 100644 --- a/src/Rent/RentSystemWeb.php +++ b/src/Rent/RentSystemWeb.php @@ -2,7 +2,7 @@ namespace BikeShare\Rent; -class RentSystemWeb implements RentSystemInterface +class RentSystemWeb extends AbstractRentSystem implements RentSystemInterface { public function rentBike($userId, $bike, $force = false) { @@ -13,16 +13,13 @@ public function rentBike($userId, $bike, $force = false) $result = $db->query("SELECT bikeNum FROM bikes WHERE bikeNum=$bikeNum"); if ($result->num_rows != 1) { - response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR); - return; + return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('does not exist.'), ERROR); } if ($force == false) { if (!$creditSystem->isEnoughCreditForRent($userId)) { $minRequiredCredit = $creditSystem->getMinRequiredCredit(); - response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); - - return; + return $this->response(_('You are below required credit') . ' ' . $minRequiredCredit . $creditSystem->getCreditCurrency() . '. ' . _('Please, recharge your credit.'), ERROR); } checktoomany(0, $userId); @@ -37,11 +34,11 @@ public function rentBike($userId, $bike, $force = false) if ($countRented >= $limit) { if ($limit == 0) { - response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR); + return $this->response(_('You can not rent any bikes. Contact the admins to lift the ban.'), ERROR); } elseif ($limit == 1) { - response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR); + return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . '.', ERROR); } else { - response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR); + return $this->response(_('You can only rent') . ' ' . sprintf(ngettext('%d bike', '%d bikes', $limit), $limit) . ' ' . _('at once') . ' ' . _('and you have already rented') . ' ' . $limit . '.', ERROR); } } @@ -56,7 +53,7 @@ public function rentBike($userId, $bike, $force = false) $serviceTag = $row['serviceTag']; if ($serviceTag != 0) { - response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR); + return $this->response(_('Renting from service stands is not allowed: The bike probably waits for a repair.'), ERROR); } if ($watches['stack'] and $stacktopbike != $bike) { @@ -67,7 +64,7 @@ public function rentBike($userId, $bike, $force = false) notifyAdmins(_('Bike') . ' ' . $bike . ' ' . _('rented out of stack by') . ' ' . $userName . '. ' . $stacktopbike . ' ' . _('was on the top of the stack at') . ' ' . $stand . '.', ERROR); } if ($forcestack and $stacktopbike != $bike) { - response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR); + return $this->response(_('Bike') . ' ' . $bike . ' ' . _('is not rentable now, you have to rent bike') . ' ' . $stacktopbike . ' ' . _('from this stand') . '.', ERROR); } } } @@ -87,12 +84,10 @@ public function rentBike($userId, $bike, $force = false) if ($force == false) { if ($currentUser == $userId) { - response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '.', ERROR); - return; + return $this->response(_('You have already rented the bike') . ' ' . $bikeNum . '. ' . _('Code is') . ' ' . $currentCode . '.', ERROR); } if ($currentUser != 0) { - response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR); - return; + return $this->response(_('Bike') . ' ' . $bikeNum . ' ' . _('is already rented') . '.', ERROR); } } @@ -107,7 +102,7 @@ public function rentBike($userId, $bike, $force = false) } else { $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERENT',parameter=$newCode"); } - response($message); + return $this->response($message); } public function returnBike($userId, $bike, $stand, $note = '', $force = false) @@ -118,7 +113,7 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false) $result = $db->query("SELECT standId FROM stands WHERE standName='$stand'"); if (!$result->num_rows) { - response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR); + return $this->response(_('Stand name') . " '" . $stand . "' " . _('does not exist. Stands are marked by CAPITALLETTERS.'), ERROR); } $row = $result->fetch_assoc(); $standId = $row["standId"]; @@ -128,7 +123,7 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false) $bikenumber = $result->num_rows; if ($bikenumber == 0) { - response(_('You currently have no rented bikes.'), ERROR); + return $this->response(_('You currently have no rented bikes.'), ERROR); } } @@ -162,6 +157,28 @@ public function returnBike($userId, $bike, $stand, $note = '', $force = false) $result = $db->query("INSERT INTO history SET userId=$userId,bikeNum=$bikeNum,action='FORCERETURN',parameter=$standId"); } - response($message); + return $this->response($message); + } + + protected function response($message, $error = 0, $additional = '', $log = 1) + { + global $db, $user, $auth; + + $json = array('error' => $error, 'content' => $message); + if (is_array($additional)) { + foreach ($additional as $key => $value) { + $json[$key] = $value; + } + } + $json = json_encode($json); + if ($log == 1 && $message) { + $userid = $auth->getUserId(); + + $number = $user->findPhoneNumber($userid); + logresult($number, $message); + } + $db->commit(); + echo $json; + exit; } } \ No newline at end of file