Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbird7112 committed Jun 25, 2021
1 parent 0cc430f commit 3d46f0e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app/controller/userrequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions app/controller/usersignup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
Expand Down
46 changes: 24 additions & 22 deletions app/models/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -77,50 +77,52 @@ 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();
$prevUser = $row1["users"];
$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;
}

Expand Down

0 comments on commit 3d46f0e

Please sign in to comment.