lollipop

A PHP-framework
Log | Files | Refs

commit 793efdf4b9f0046997c2fb5e172428a98e87cc89
parent bbbca2dcd96af88e91caa765b626301942c8d2a5
Author: MoiBaguette <[email protected]>
Date:   Sun, 16 Apr 2023 01:31:31 +0200

orm overal geeeen gubs

Diffstat:
MLollipop/DatabaseObject.php | 4++--
Madd_user.php | 15+++++++++++----
Malter_user.php | 203+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
3 files changed, 135 insertions(+), 87 deletions(-)

diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php @@ -62,7 +62,7 @@ namespace Lollipop { $values[] = $this->data[$key]; } - $sql .= " WHERE $this->primary = ?"; + $sql .= " WHERE {$this->primary} = ?"; $values[] = $this->data[$this->primary]; $stmt = $this->db->conn->prepare($sql); @@ -95,7 +95,7 @@ namespace Lollipop { $stmt = $this->db->conn->prepare($sql); $this->changed_keys = []; - + if($stmt->execute($values)) return true; else diff --git a/add_user.php b/add_user.php @@ -45,7 +45,7 @@ <?php foreach($available_permissions as $db_permission){ echo "<div class=\"mb-3 form-check\"> - <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions\" value=" . $db_permission['id'] . "\"> + <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission['id'] . "\"> <label class=\"form-check-label\" for=" . $db_permission['name'] . ">" . $db_permission['name'] . "</label> </div>"; } @@ -109,6 +109,7 @@ if($u->load($email)){ echo"this email address is taken: " . $email; }else{ + $succes = false; //set new user data $u->email = $email; $u->fname = $fname; @@ -117,7 +118,7 @@ //add user with the add function if($u->add()){ - echo"succes!"; + $succes = true; }; //create a database object with table permission for each permission @@ -125,8 +126,14 @@ foreach($permissions as $permission){ $p = $db->get(Permission_user::class); $p->email = $email; - $p->id = $permission; - $p->add(); + $p->id = (int) $permission; + if($p->add()) + { + $succes = true; + } + } + if($succes){ + echo"succes!"; } } } diff --git a/alter_user.php b/alter_user.php @@ -7,6 +7,8 @@ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> </head> <?php +use Lollipop\DatabaseObject; +use Lollipop\SQLDatabase; include "navbar.php"; include "utils/autoloader.php"; if(!in_array(0, $_SESSION['permissions'])){ @@ -21,33 +23,128 @@ foreach($all_p as $tmp){ $available_permissions[] = ['id' => $tmp->id, 'name' => $tmp->name]; } + //if not found set to empty if not GET + $fname = ""; + $lname = ""; + $email = ""; + $user_permissions = []; if($_SERVER["REQUEST_METHOD"] == "GET"){ - $u = $db->get(User::class); - //if the get var isset and user is found in the database load data into forms - if(isset($_GET['email']) && $u->load($_GET['email'])){ - $fname = $u->fname; - $lname = $u->lname; - $email = $u->email; - $p = $db->all_where(Permission_user::class, array('email' => $email)); - foreach($p as $permission){ - $user_permissions[] = $permission->id; - } + if(!isset($_GET['email'])){ + echo""; }else{ - //if not found set to empty - $fname = ""; - $lname = ""; - $email = ""; - $user_permissions = []; + $get_email = $_GET['email']; + $u = $db->get(User::class); + if($u->load($get_email)){ + $fname = $u->fname; + $lname = $u->lname; + $email = $u->email; + $p = $db->all_where(Permission_user::class, array('email' => $email)); + foreach($p as $permission){ + $user_permissions[] = $permission->id; + } + } } } + if ($_SERVER["REQUEST_METHOD"] == "POST") { + $errors = array(); // initialize an empty array to store errors + + // Check if voornaam is set and not empty + if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { + $fname = $_POST['voornaam']; + } else { + $errors[] = "Voornaam is required"; + } + + // Check if achternaam is set and not empty + if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { + $lname = $_POST['achternaam']; + } else { + $errors[] = "Achternaam is required"; + } + + // Check if email is set and not empty + if (isset($_POST['email']) && !empty($_POST['email'])) { + $email = $_POST['email']; + } else { + $errors[] = "E-mail is required"; + } + + // Check if permissions is set + if (isset($_POST['permissions'])) { + $permissions = $_POST['permissions']; + } else { + $errors[] = "Permissies zijn vereist"; + } + + // Check if there are any errors + if (count($errors) > 0) { + // Print out the errors + foreach ($errors as $error) { + echo $error . "<br>"; + } + } else { + //create a database object with table user + $u = $db->get(User::class); + + //check if email already exists + if(!$u->load($email)){ + echo"this user does not exist " . $email; + }else{ + $succes = false; + //set new user data + $u->email = $email; + $u->fname = $fname; + $u->lname = $lname; + echo $u->save(); + //add user with the add function + if(true){ + $succes = true; + }; + + $p = $db->get(Permission_user::class); + //delete all permissions + foreach($available_permissions as $available){ + $p->email = $email; + $p->id = $available['id']; + $p->delete(); + } + + //add permissions + foreach($permissions as $keep){ + $p->email = $email; + $p->id = (int)$keep; + $p->add(); + } + if($succes){ + echo"succes!"; + } + } + } + //if the get var isset and user is found in the database load data into forms + if(!isset($_GET['email'])){ + echo""; + }else{ + $get_email = $_GET['email']; + $u = $db->get(User::class); + if($u->load($get_email)){ + $fname = $u->fname; + $lname = $u->lname; + $email = $u->email; + $p = $db->all_where(Permission_user::class, array('email' => $email)); + foreach($p as $permission){ + $user_permissions[] = $permission->id; + } + } + } + } ?> <body> <div class="container"> <h1>Alter user</h1> - <form action="alter_user.php" method="post"> + <form action="alter_user.php?email=<?php echo $email;?>" method="post"> <div class="mb-3"> <label for="voornaam" class="form-label"><b>Voornaam:</b></label> <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam" value=<?php echo$fname?>> @@ -62,74 +159,18 @@ </div> <p>Please select the user permissions:</p> <?php - foreach($available_permissions as $db_permission){ - echo "<div class=\"mb-3 form-check\">" . - "<input type=\"checkbox\" class=\"form-check-input\" name=\"permissions\" value=" . $db_permission['id'] . "\""; - if ($user_permissions != null && in_array($db_permission['id'], $user_permissions)) { - echo " checked"; - } - echo "><label class=\"form-check-label\" for=" . $db_permission['name'] . ">" . $db_permission['name'] . "</label>" . - "</div>"; - } + foreach($available_permissions as $db_permission){ + echo "<div class=\"mb-3 form-check\">" . + "<input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission['id'] . "\""; + if ($user_permissions != null && in_array($db_permission['id'], $user_permissions)) { + echo " checked"; + } + echo "><label class=\"form-check-label\" for=" . $db_permission['name'] . ">" . $db_permission['name'] . "</label>" . + "</div>"; + } ?> - <button type="submit" class="btn btn-primary" name="submit">Alter user</button> </form> </div> - <?php - if ($_SERVER["REQUEST_METHOD"] == "POST") { - $errors = array(); // initialize an empty array to store errors - - // Check if voornaam is set and not empty - if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { - $fname = $_POST['voornaam']; - } else { - $errors[] = "Voornaam is required"; - } - - // Check if achternaam is set and not empty - if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { - $lname = $_POST['achternaam']; - } else { - $errors[] = "Achternaam is required"; - } - - // Check if email is set and not empty - if (isset($_POST['email']) && !empty($_POST['email'])) { - $email = $_POST['email']; - } else { - $errors[] = "E-mail is required"; - } - - // Check if password is set and not empty - if (isset($_POST['password']) && !empty($_POST['password'])) { - $password = $_POST['password']; - } else { - $password = null; - } - - // Check if permissions is set - if (isset($_POST['permissions'])) { - $permissions = $_POST['permissions']; - } else { - $errors[] = "Permissies zijn vereist"; - } - - // Check if there are any errors - if (count($errors) > 0) { - // Print out the errors - foreach ($errors as $error) { - echo $error . "<br>"; - } - } else { - // Pass the password through a hashing function - - // Making a sql statement to add user to the database, preparing it and excuting - - //Excecuting a sql statement for all the user permissions - } - } - ?> </body> - </html> \ No newline at end of file