lollipop

A PHP-framework
Log | Files | Refs

commit 5b58d5537292a9ec1e1dbff163a78b2bf7a9dab4
parent a6e17687e428248f613b0948557737d5fb8d0c98
Author: Gerco van Woudenbergh <[email protected]>
Date:   Thu, 22 Jun 2023 14:25:40 +0200

more appliction

Diffstat:
MModel/Permission.php | 14++++++++++++++
AModel/PermissionUser.php | 24++++++++++++++++++++++++
DModel/Permission_User.php | 16----------------
MModel/User.php | 25++++++++++++++++---------
Mindex.php | 21+++++++++++++--------
Mviews/add_user.html | 5+++--
6 files changed, 70 insertions(+), 35 deletions(-)

diff --git a/Model/Permission.php b/Model/Permission.php @@ -12,5 +12,19 @@ namespace Model { { return "id"; } + + function all_fields(): string{ + $all_permissions = $this->db->all($this::class); + $html = ""; + foreach($all_permissions as $permission){ + $html .= '<input type="checkbox" id="'. $permission->name .'" name="permissions[]" value="'. $permission->id .'"'; + if($permission->id == 0){ + $html .= ' checked'; + } + $html .= '>'; + $html .= '<label for="'. $permission->name .'">'. $permission->name .'</label>'; + } + return $html; + } } } \ No newline at end of file diff --git a/Model/PermissionUser.php b/Model/PermissionUser.php @@ -0,0 +1,23 @@ +<?php +namespace Model { + class PermissionUser extends \Lollipop\DatabaseObject + { + static function get_table(): string + { + return "permission_user"; + } + + static function get_primary(): string + { + return 'id'; + } + public function add_permissions():array{ + if(array_key_exists('permissions', $_POST)){ + foreach($_POST['permissions'] as $permission){ + $this-> + } + } + return []; + } + } +} +\ No newline at end of file diff --git a/Model/Permission_User.php b/Model/Permission_User.php @@ -1,15 +0,0 @@ -<?php -namespace Model { - class Permission_User extends \Lollipop\DatabaseObject - { - static function get_table(): string - { - return "permission_user"; - } - - static function get_primary(): string - { - return 'id'; - } - } -} -\ No newline at end of file diff --git a/Model/User.php b/Model/User.php @@ -22,15 +22,16 @@ namespace Model { return $html; } - function all_fields(): string{ + function all_fields(array $res = []): string{ $html = ""; foreach($this->column_names as $field){ if($field == $this->get_password_field()){ - $html .= '<input type="password" name="' . $field . '" placeholder="' . $field . '">'; - $html .= '<div class="form-response"><p style="color:red;">{{$error-'. $field .'}}</p></div>'; + $html .= '<input type="password" name="' . $field . '" placeholder="' . $field . '">'; }else{ $html .= '<input type="text" name="' . $field . '" placeholder="' . $field . '">'; - $html .= '<div class="form-response"><p style="color:red;">{{$error-'. $field .'}}</p></div>'; + } + if(array_key_exists($field, $res)){ + $html .= '<div class="form-response"><p style="color:red;"> Field: '. $field . ' cannot be empty</p></div>'; } } return $html; @@ -98,8 +99,9 @@ namespace Model { } private function add_data_db(array $post_arr): array{ + $user_credentials = []; if($this->load($post_arr[$this->get_primary()])){ - return ["msg" => "<p style=\"color:red;\">this email address is already taken: {$post_arr[$this->get_primary()]} </p>"]; + return ["response" => "<p style=\"color:red;\">this email address is already taken: {$post_arr[$this->get_primary()]} </p>"]; }else{ if($post_arr[$this->get_password_field()]){ $post_arr[$this->get_password_field()] = password_hash($post_arr[$this->get_password_field()], PASSWORD_DEFAULT); @@ -107,12 +109,17 @@ namespace Model { foreach($this->column_names as $col){ if($post_arr[$col] != ""){ $this->$col = $post_arr[$col]; + $user_credentials[$col] = $post_arr[$col]; } } - if($this->add()) - return ["msg" => "<p style=\"color:green;\">succes</p>"]; - else - return ["msg" => "<p style=\"color:red;\">could not add user to database</p>"]; + $repsonse = []; + $response["response"] = "<p style=\"color:green;\">succes</p>"; + + if($this->add()){ + return $user_credentials; + }else{ + return ["repsonse" => "<p style=\"color:red;\">could not add user to database</p>"]; + } } } } diff --git a/index.php b/index.php @@ -40,19 +40,24 @@ $router->addRoute(["GET"], "/dashboard", function(&$vars){ $router->addRoute(["GET"], "/user/add", function(&$vars){ global $db; - $vars["all-fields"] = $db->get(Model\User::class)->all_fields(); + + $vars["user-data-fields"] = $db->get(Model\User::class)->all_fields(); + $vars["permission-fields"] = $db->get(Model\Permission::class)->all_fields(); return "views/add_user.html"; }); $router->addRoute(["POST"], "/user/add", function(&$vars){ global $db; - $vars["all-fields"] = $db->get(Model\User::class)->all_fields(); - foreach($db->get(Model\User::class)->add_user() as $err_key => $err){ - $vars[$err_key] = $err; - echo $err_key . $err . '<br>'; - } - foreach(Utils::post_to_array() as $key => $data){ - echo $key . $data . '<br>'; + $user = $db->get(Model\User::class); + $permission = $db->get(Model\Permission::class); + $permission_user = $db->get(Model\PermissionUser::class); + + $res = $user->add_user(); + $permission_user->add_permissions(); + $vars["user-data-fields"] = $user->all_fields($res); + $vars["permission-fields"] = $permission->all_fields(); + if(array_key_exists("succes", $res)){ + $vars["succes"] = $res["succes"]; } return "views/add_user.html"; }); diff --git a/views/add_user.html b/views/add_user.html @@ -15,9 +15,10 @@ <div class="flex-middle"> <div class="form-title"><h2>Enter credentials:</h1></div> <form method="POST" action="/user/add"> - {{$all-fields}} - + {{$user-data-fields}} + {{$permission-fields}} <input type="submit" value="Submit"> + {{$response}} </form> </div>