alter_user.php (5944B)
1 <!DOCTYPE html> 2 <html lang="eng"> 3 4 <head> 5 <title>User toevoegen</title> 6 <!-- Bootstrap CSS --> 7 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> 8 <link rel="stylesheet" type="text/css" href="/css/homepage.css"> 9 </head> 10 <?php 11 use Lollipop\DatabaseObject; 12 use Lollipop\SQLDatabase; 13 include "views/navbar.php"; 14 include "utils/autoloader.php"; 15 if(!in_array(1, $_SESSION['user_permissions'])){ 16 header('Location: /dashboard'); 17 exit; 18 } 19 $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "wap2"); 20 $msg = ""; 21 //select the available permissions from the database 22 $all_p = $db->all(Permissions::class); 23 $available_permissions = []; 24 foreach($all_p as $tmp){ 25 $available_permissions[] = ['id' => $tmp->permission_id, 'name' => $tmp->permission_name]; 26 } 27 //if not found set to empty if not GET 28 $fname = ""; 29 $lname = ""; 30 $email = ""; 31 $user_permissions = []; 32 33 if($_SERVER["REQUEST_METHOD"] == "GET"){ 34 //if the get var isset and user is found in the database load data into forms 35 if(!isset($_GET['email'])){ 36 echo""; 37 }else{ 38 $get_email = $_GET['email']; 39 $u = $db->get(User::class); 40 $u->where("email", $get_email); 41 if($u->email != null){ 42 $fname = $u->first_name; 43 $lname = $u->last_name; 44 $email = $u->email; 45 $p = $db->all_where(Permission_user::class, array('user_id' => $u->user_id)); 46 foreach($p as $permission){ 47 $user_permissions[] = $permission->permission_id; 48 } 49 } 50 } 51 } 52 if ($_SERVER["REQUEST_METHOD"] == "POST") { 53 $errors = array(); // initialize an empty array to store errors 54 55 // Check if voornaam is set and not empty 56 if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { 57 $fname = $_POST['voornaam']; 58 } else { 59 $errors[] = "First name is required"; 60 } 61 62 // Check if achternaam is set and not empty 63 if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { 64 $lname = $_POST['achternaam']; 65 } else { 66 $errors[] = "Last name is required"; 67 } 68 69 // Check if email is set and not empty 70 if (isset($_POST['email']) && !empty($_POST['email'])) { 71 $email = $_POST['email']; 72 } else { 73 $errors[] = "E-mail is required"; 74 } 75 76 // Check if permissions is set 77 if (isset($_POST['permissions'])) { 78 $permissions = $_POST['permissions']; 79 } 80 81 // Check if there are any errors 82 if (count($errors) > 0) { 83 // Print out the errors 84 foreach ($errors as $error) { 85 $msg .= $error . "<br>"; 86 } 87 } else { 88 //create a database object with table user 89 $u = $db->get(User::class); 90 $u->where("email", $email); 91 $user_id = $u->user_id; 92 //check if email already exists 93 if($u->email == null){ 94 $msg = "this user does not exist " . $email; 95 }else{ 96 $succes = false; 97 //set new user data 98 $u->email = $email; 99 $u->user_id = $user_id; 100 $u->first_name = $fname; 101 $u->last_name = $lname; 102 //add user with the add function 103 if( $u->save()){ 104 $succes = true; 105 }; 106 107 $p = $db->get(Permission_user::class); 108 //delete all permissions 109 foreach($available_permissions as $available){ 110 $p->user_id = $user_id; 111 $p->permission_id = $available['id']; 112 $p->delete(); 113 } 114 115 //add permissions 116 if(isset($permissions)){ 117 foreach($permissions as $keep){ 118 $p->user_id = $user_id; 119 $p->permission_id = (int)$keep; 120 $p->add(); 121 } 122 } 123 if($succes){ 124 $msg = "succes! changes saved to the database"; 125 } 126 } 127 } 128 //if the get var isset and user is found in the database load data into forms 129 if(!isset($_GET['email'])){ 130 echo""; 131 }else{ 132 $get_email = $_GET['email']; 133 $u = $db->get(User::class); 134 $u->where("email", $email); 135 if($u->email != null){ 136 $fname = $u->first_name; 137 $lname = $u->last_name; 138 $email = $u->email; 139 $p = $db->all_where(Permission_user::class, array('user_id' => $u->user_id)); 140 foreach($p as $permission){ 141 $user_permissions[] = $permission->permission_id; 142 } 143 } 144 } 145 } 146 ?> 147 148 <body> 149 <div class="container"> 150 <h1>Alter user</h1> 151 <form action="alter_user?email=<?php echo $email;?>" method="post"> 152 <div class="mb-3"> 153 <label for="voornaam" class="form-label"><b>First name:</b></label> 154 <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="First name" value=<?php echo$fname?>> 155 </div> 156 <div class="mb-3"> 157 <label for="achternaam" class="form-label"><b>Last name:</b></label> 158 <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Last name" value=<?php echo$lname?>> 159 </div> 160 <div class="mb-3"> 161 <label for="email" class="form-label"><b>Email:</b></label> 162 <input type="text" class="form-control" name="email" id="email" placeholder="Email" value=<?php echo$email?>> 163 </div> 164 <p>Please select the user permissions:</p> 165 <?php 166 foreach($available_permissions as $db_permission){ 167 echo "<div class=\"mb-3 form-check\">" . 168 "<input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission['id'] . "\""; 169 if ($user_permissions != null && in_array($db_permission['id'], $user_permissions)) { 170 echo " checked"; 171 } 172 echo "><label class=\"form-check-label\" for=" . $db_permission['name'] . ">" . $db_permission['name'] . "</label>" . 173 "</div>"; 174 } 175 ?> 176 <button type="submit" class="btn btn-primary" name="submit">Alter user</button> 177 </form> 178 <?php echo $msg;?> 179 </div> 180 </body> 181 </html>