From 3d46f0ede02cf95e9847c8fe5d8b3d773a6cc500 Mon Sep 17 00:00:00 2001 From: Rohith Varma Buddaraju Date: Fri, 25 Jun 2021 23:29:55 +0530 Subject: [PATCH] Minor changes --- app/controller/userrequest.php | 4 +-- app/controller/usersignup.php | 10 ++++---- app/models/admin.php | 46 ++++++++++++++++++---------------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/app/controller/userrequest.php b/app/controller/userrequest.php index bf5f404..13bf5c2 100644 --- a/app/controller/userrequest.php +++ b/app/controller/userrequest.php @@ -10,8 +10,8 @@ public function post(){ $reqtype = $data['type']; $user = $_SESSION['name']; $datetime = date("Y-m-d h:i:sa"); - $res = \Models\User::set_req($user,$bookid,$reqtype,$datetime); - if(res){ + $response = \Models\User::set_req($user,$bookid,$reqtype,$datetime); + if($response){ echo "{\"status\":\"Request successful\"}"; } else{ diff --git a/app/controller/usersignup.php b/app/controller/usersignup.php index 5af42ab..4b60d9d 100644 --- a/app/controller/usersignup.php +++ b/app/controller/usersignup.php @@ -14,11 +14,11 @@ public function post(){ $email = $_POST["email"]; $phone = $_POST["phone"]; $user = $_POST["usr"]; - $pass = $_POST["pass"]; - $repass = $_POST["repass"]; - if($pass==$repass && preg_match("/^[a-zA-Z0-9 ]+$/",$name) && preg_match("/^[a-zA-Z0-9]+$/",$user) && preg_match("/^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$/",$email) && preg_match("/^[0-9]+$/",$phone)){ - $pass = hash("sha256",$pass); - if(\Models\User::user_reg($user,$name,$email,$phone,$pass)){ + $password = $_POST["pass"]; + $repassword = $_POST["repass"]; + if($password==$repassword && preg_match("/^[a-zA-Z0-9 ]+$/",$name) && preg_match("/^[a-zA-Z0-9]+$/",$user) && preg_match("/^[a-zA-Z0-9+_.-]+@[a-zA-Z0-9.-]+$/",$email) && preg_match("/^[0-9]+$/",$phone)){ + $password = hash("sha256",$password); + if(\Models\User::user_reg($user,$name,$email,$phone,$password)){ echo \View\Loader::make()->render("templates/usersignup.twig", array( error => 1 )); diff --git a/app/models/admin.php b/app/models/admin.php index 9c4af26..a5f450a 100644 --- a/app/models/admin.php +++ b/app/models/admin.php @@ -45,16 +45,16 @@ public static function book_reg($name,$author,$publisher,$maxqty){ } return true; } - public static function book_del($bid){ + public static function book_del($bookid){ $db = \DB::get_instance(); $sql = $db->prepare("select * from book where bid = ?;"); - $sql->execute([$bid]); + $sql->execute([$bookid]); if($sql->rowCount()){ $sql1 = $db->prepare("delete from book where bid = ?;"); - $sql1->execute([$bid]); + $sql1->execute([$bookid]); if($sql1->rowCount()){ $sql2 = $db->prepare("delete from request where bid= ? and status='P';"); - $sql2->execute([$bid]); + $sql2->execute([$bookid]); if($sql2->rowCount()){ return true; } @@ -77,14 +77,17 @@ public static function admin_req($reqid,$action){ if($sql->rowCount()){ $row = $sql->fetch(); $user = $row["usr"]; - $bid = $row["bid"]; + $bookid = $row["bid"]; $reqtype = $row["reqtype"]; - $condition1 = $reqtype == "out"; - $condition2 = $reqtype == "in"; + $condition = $reqtype == "out"; + if($reqtype!="in" && $reqtype!="out"){ + return false; + } + $sql1 = $db->prepare("select * from book where bid= ?;"); - $sql1->execute([$bid]); + $sql1->execute([$bookid]); if($sql1->rowCount()){ $row1 = $sql1->fetch(); @@ -92,35 +95,34 @@ public static function admin_req($reqid,$action){ $prevUser1 = explode(";",$prevUser); $avail = (int) $row1["avail"]; - if($condition1 && (in_array($user,$prevUser1) || $avail==0)){ + if($condition && (in_array($user,$prevUser1) || $avail==0)){ $action = "D"; } - if($condition2 && !in_array($user,$prevUser1)){ + if(!$condition && !in_array($user,$prevUser1)){ $action = "D"; } $sql2 = $db->prepare("update request set status= ? where reqid= ?;"); $sql2 -> execute([$action,$reqid]); if($sql2->rowCount()){ - if($condition1 && $action == "A"){ + if($condition && $action == "A"){ $avail -= 1; array_push($prevUser1,$user); - $prevUser2 = implode(";",$prevUser1); - $sql3 = $db -> prepare("update book set avail= ?,users = ? where bid= ?;"); - $sql3->execute([$avail,$prevUser2,$bid]); - if($sql3->rowCount()){ - return true; - } - } else if($condition2 && $action == "A"){ + $prevUser1 = implode(";",$prevUser1); + + } else if(!$condition && $action == "A"){ $key = array_search($user,$prevUser1); unset($prevUser1[$key]); - $prevUser2 = implode(";",$prevUser1); - $avail += 1; + $prevUser1 = implode(";",$prevUser1); + $avail += 1; + } + if($action == "A"){ $sql3 = $db -> prepare("update book set avail= ?,users = ? where bid= ?;"); - $sql3->execute([$avail,$prevUser2,$bid]); + $sql3->execute([$avail,$prevUser1,$bookid]); if($sql3->rowCount()){ return true; } - } else if($action == "D"){ + } + if($action == "D"){ return true; }