hanze/iwa-panda2

some late night work i didnt push (ba4f8d91b05f7e362688b8e5a4795d6554331c4f)
Repositories | README.md

commit ba4f8d91b05f7e362688b8e5a4795d6554331c4f
parent d03aa28b43944e4ca84cef3867b3a95694f7af70
Author: Gerco van Woudenbergh <[email protected]>
Date:   Thu,  8 Jun 2023 10:15:01 +0200

some late night work i didnt push

Diffstat:
MLollipop/DatabaseObject.php23+++++++++++++++++++++--
MModel/Add_user.php71+++++++++++++++++++++++------------------------------------------------
MModel/User.php1-
AModel/post_to_array.php9+++++++++
Mindex.php16++++++++++++++++
Mviews/add_user.html2+-
6 files changed, 70 insertions(+), 52 deletions(-)

diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php @@ -7,7 +7,7 @@ namespace Lollipop { { protected string $table; protected string $primary; - + protected SQLDatabase $db; protected array $data = []; protected array $changed_keys = []; @@ -21,7 +21,7 @@ namespace Lollipop { abstract static function get_primary(): string; abstract static function get_table(): string; - + public function setData($data) { $this->data = $data; @@ -165,5 +165,24 @@ namespace Lollipop { { return $this->data; } + public function getCollumnNames(){ + $collumns = []; + $sql = " SELECT COLUMN_NAME + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_NAME = '{$this->table}' + AND TABLE_SCHEMA = 'panda'"; + $stmt = $this->db->conn->prepare($sql); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows == 0) { + return false; + } + while($tmp = $result->fetch_assoc()){ + $collumns[] = $tmp; + } + $this->data["collumn_names"] = $collumns; + return true; + } } } \ No newline at end of file diff --git a/Model/Add_user.php b/Model/Add_user.php @@ -1,49 +1,23 @@ <?php -class Add_user{ - function read_post() :array{ - $errors = array(); // initialize an empty array to store errors - - // Check if first_name is set and not empty - if (isset($_POST['first_name']) && !empty($_POST['first_name'])) { - $first_name = $_POST['first_name']; - } else { - $errors["first_name"] = "first_name is required"; - } - - // Check if last_name is set and not empty - if (isset($_POST['last_name']) && !empty($_POST['last_name'])) { - $last_name = $_POST['last_name']; - } else { - $errors["last_name"] = "last_name is required"; - } - - // Check if email is set and not empty - if (isset($_POST['email']) && !empty($_POST['email'])) { - $email = $_POST['email']; - } else { - $errors["email"] = "E-mail is required"; - } - - // Check if password is set and not empty - if (isset($_POST['password']) && !empty($_POST['password'])) { - $password = $_POST['password']; - } else { - $errors["password"] = "password is required"; - } - - // Check if permissions is set - if (isset($_POST['permissions'])) { - $permissions = $_POST['permissions']; - } else { - $errors["password"] = "Permissions are required"; +class Add_user { + function add_user() :array{ + //this function checks $_POST fields for data if all the data is present adds the user to database + $errors = []; + $data = []; + + foreach ($_POST as $key => $value) { + if($value == ""){ + $errors[$key] = "This field is required"; + }else{ + $arr[$key] = [$value]; + } } - if (count($errors) > 0) { - // Print out the errors + if(siezof($errors) > 0){ return $errors; } else { - create_user($fname, $lname, $email, $password, $permissions); - return ["msg" => "succes! user with email: {$email} was added to the db"]; + create_user($data); + return ["msg" => "succes! user with email: {$data[$email]} was added to the db"]; } } function create_permission_radials():string{ @@ -54,25 +28,26 @@ class Add_user{ foreach($all_p as $db_permission){ $radials .= "<div class=\"mb-3 form-check\"> <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission->id . "\"> + <input type='hidden' value='-1' name='{$db_permission->name}'> <label class=\"form-check-label\" for=" . $db_permission->name . ">" . $db_permission->name . "</label> </div> "; } return $radials; } - function create_user(string $first_name, string $last_name, string $email, string $password, array $permissions){ + function create_user(array $data){ $u = $db->get(User::class); //check if email already exists - if($u->where("email", $email)){ + if($u->where("email", $data["email"])){ $msg = "this email address is taken: " . $email; }else{ $u = $db->get(User::class); //set new user data - $u->email = $email; - $u->first_name = $first_name; - $u->last_name = $last_name; + $u->email = $data["email"]; + $u->first_name = $data["first_name"]; + $u->last_name = $data["last_name"]; //hash the pwd - $hashed_pwd = password_hash($password, PASSWORD_DEFAULT); + $hashed_pwd = password_hash($data["password"], PASSWORD_DEFAULT); $u->password = $hashed_pwd; //add user with the add function @@ -80,7 +55,7 @@ class Add_user{ throw new ErrorException("Could not add user to database"); }; $u = $db->get(User::class); - $u->where("email", $email); + $u->where("email", $data["email"]); //create a database object with table permission for each permission //set the data and execute the add function foreach($permissions as $permission){ diff --git a/Model/User.php b/Model/User.php @@ -11,5 +11,4 @@ class User extends Lollipop\DatabaseObject return "user_id"; } } - ?> \ No newline at end of file diff --git a/Model/post_to_array.php b/Model/post_to_array.php @@ -0,0 +1,8 @@ +<?php +function to_array(){ + $arr = []; + foreach ($_POST as $key => $value) { + $arr[$key] = [$value]; + } + return $arr; +} +\ No newline at end of file diff --git a/index.php b/index.php @@ -8,6 +8,7 @@ $router->addRoute(["GET"], "/", function($vars){ $templater = new Lollipop\Template(); echo $templater->template("views/login.html", ["msg" => ""]); }); + $router->addRoute(["POST"], "/login", function($vars){ $templater = new Lollipop\Template(); $login = new Login_handler(); @@ -33,6 +34,21 @@ $router->addRoute(["GET"], "/user/add", function($vars){ echo $templater->template("views/add_user.html", $template); }); +$router->addRoute(["POST"], "/user/test", function($vars){ + $arr = []; + $post = $_POST; + array_pop($post); + foreach ($post as $key => $value) { + $arr[$key] = [$value]; + } + //var_dump($arr); + $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "panda"); + $tmp = $db->get(User::class); + $tmp->getCollumnNames(); + var_dump($tmp->collumn_names); +}); + + $router->addRoute(["POST"], "/user/add", function($vars){ $add_user = new Add_user(); $templater = new Lollipop\Template(); diff --git a/views/add_user.html b/views/add_user.html @@ -10,7 +10,7 @@ <div class="container"> <h1>Add user</h1> - <form action="add" method="post"> + <form action="test" method="post"> <div class="mb-3"> <label for="first_name" class="form-label"><b>Voornaam:</b></label> <input type="text" class="form-control" name="first_name" id="first_name" placeholder="{{first_name}}first_name">