lollipop

A PHP-framework
Log | Files | Refs

commit 3fb9bb661c264f676109f3dd35ee15b3302f9806
parent c49e14c6b7d5b0df0236dd1835fb8a86a21b6688
Author: MoiBaguette <[email protected]>
Date:   Sat, 24 Jun 2023 15:11:28 +0200

table and login

Diffstat:
MModel/Permission.php | 2+-
MModel/PermissionUser.php | 10++++++++--
MModel/User.php | 11+++++------
Mcontroller/templates.php | 25+++++++++++++++++--------
Mindex.php | 2++
Mrouting/course.php | 1-
Mrouting/user.php | 45+++++++++++++++++++++++++++++++++------------
Mviews/course.html | 2--
Mviews/user.html | 2--
Aviews/user_page.html | 0
10 files changed, 66 insertions(+), 34 deletions(-)

diff --git a/Model/Permission.php b/Model/Permission.php @@ -16,7 +16,7 @@ namespace Model { { return "lollipop"; } - function all_fields(): string{ + function get_checkboxes(): string{ $all_permissions = $this->db->all($this::class); $html = ""; foreach($all_permissions as $permission){ diff --git a/Model/PermissionUser.php b/Model/PermissionUser.php @@ -15,15 +15,21 @@ namespace Model { { return "lollipop"; } - public function add_permissions(User $user):array{ + public function add_permissions(User $user):bool{ if(array_key_exists('permissions', $_POST)){ foreach($_POST['permissions'] as $permission){ $this->{$user->get_primary()} = $user->{$user->get_primary()}; $this->id = $permission; $this->add(); } + return true; } - return []; + return false; + } + public function update_permissions(User $user):array{ + + + return $this->db->all_where($this->get_table(), ["email", $_POST["email"]]); } } } \ No newline at end of file diff --git a/Model/User.php b/Model/User.php @@ -45,12 +45,11 @@ namespace Model { function login():array{ $post_arr = \Lollipop\Utils::post_to_array(); - $missing_fields = \Lollipop\Utils::missing_fields($post_arr , [$this->get_primary(), $this->get_password_field()]); - if(sizeof($missing_fields) == 0){ + if([$this->get_primary() != "" && !$this->get_password_field() == ""]){ return $this->authenticate($post_arr); }else{ - return ["response" => "missing fields"]; + return ["response" => ""]; } } function authenticate(array $post) : array @@ -83,7 +82,7 @@ namespace Model { } } //get permissions form db and set sessions_permissions - $p = $this->db->all_where(Permission_user::class, [$this->get_primary(), $this->{$this->get_primary()}]); + $p = $this->db->all_where(PermissionUser::class, [$this->get_primary(), $this->{$this->get_primary()}]); foreach($p as $permission){ $user_permissions[] = $permission->id; } @@ -92,7 +91,7 @@ namespace Model { function add_user():array{ $post_arr = \Lollipop\Utils::post_to_array(); - $missing_fields = \Lollipop\Utils::missing_fields($post_arr , $this->not_nullable); + $missing_fields = \Lollipop\Utils::missing_fields($this->notNullable()); if(sizeof($missing_fields) == 0){ return $this->add_data_db($post_arr); @@ -111,7 +110,7 @@ namespace Model { if($post_arr[$this->get_password_field()]){ $post_arr[$this->get_password_field()] = password_hash($post_arr[$this->get_password_field()], PASSWORD_DEFAULT); } - foreach($this->column_names as $col){ + foreach($this->get_col_names_no_ai() as $col){ if($post_arr[$col] != ""){ $this->$col = $post_arr[$col]; $user_credentials[$col] = $post_arr[$col]; diff --git a/controller/templates.php b/controller/templates.php @@ -46,9 +46,12 @@ namespace controller{ return $form; } - function form_v2(string $action, array $values = [], array $funcs = [], $response = []): string{ + function form_v2(string $action, array $values = [], array $extra = [], array $response = []): string{ + /*auto-increment fields are automatically hidden*/ - $form = '<form method="POST" action="'. $action . '">'; + $form = '<h1>Add '. $this->table->get_table() .'</h1> + <a href="/'. $this->table->get_table() .'">New</a>'; + $form .= '<form method="POST" action="'. $action . '">'; foreach($this->table->get_col_info() as $col => $info){ if($info["extra"] == "auto_increment"){ $form .= '<input type="hidden" name="' . $col . '" placeholder="' . $col . '" value="' . $values[$col] . '">'; @@ -62,9 +65,8 @@ namespace controller{ $form .= '<div class="form-response"><p style="color:red;"> col: '. $col . ' cannot be empty</p></div>'; } } - foreach($funcs as $func){ - if (is_callable($func)) - $form.= $func(); + foreach($extra as $html){ + $form.= $html; } if(sizeof($values) == 0){ $form_type = "Add"; @@ -86,13 +88,17 @@ namespace controller{ </form>'; } - public function crud_table(string $action, string $search = "", string $search_key):string{ + public function crud_table(string $action, string $search = "", string $search_key, \Model\PermissionUser $permissionUser = null):string{ $search = '%' . $search . '%'; $table = "<table> <thead> <tr>"; foreach($this->table->get_column_names() as $column){ $table .= "<th>$column</th>"; } - $table .= "<th>Alter</th> <th>Delete</th> </tr> </thead>"; + $table .= "<th>Alter</th> <th>Delete</th>"; + if($permissionUser != null){ + $table .= "<th>Permissions</th>"; + } + $table .= "</tr> </thead>"; $objs = $this->db->all_where($this->table_name, [$search_key => $search]); $table .= "<tbody>"; @@ -105,10 +111,13 @@ namespace controller{ $table .= ' <td> <a class="edit" href="' . $action . '/'. $obj->{$this->table->get_primary()} .'/edit/";>Edit</a> + </td> <td> <a class="delete" href="' . $action . '/'. $obj->{$this->table->get_primary()} .'/delete/">Delete</a> </td> - </tr>'; + <td>'; + + $table .= '</td> </tr>'; } $table .= " diff --git a/index.php b/index.php @@ -36,6 +36,8 @@ $router->addRoute(["GET"], "/user/:primary_key/edit", $user_edit); $router->addRoute(["GET"], "/user/:primary_key/delete", $user_delete); +$router->addRoute(["GET"], "/user/:primary_key/page", $user_page); + $router->addRoute(["POST"], "/logout", $logout); $router->addRoute(["GET"], "/course", $course_get); diff --git a/routing/course.php b/routing/course.php @@ -1,5 +1,4 @@ <?php - $course_get = function(&$vars){ global $db; $course = $db->get(Model\Course::class); diff --git a/routing/user.php b/routing/user.php @@ -1,35 +1,43 @@ <?php - $user_get = function(&$vars){ global $db; $user = $db->get(Model\User::class); + $permissions = $db->get(Model\Permission::class); + $permissionUser = $db->get(Model\PermissionUser::class); $templates = new controller\templates($db, $user); if(isset($_POST["search"])){ $user->load($_POST["search"]); $data = $user->getData(); } - $vars["form"] = $templates->form("/user"); + $vars["form"] = $templates->form_v2("/user", [], ["checkboxes" => $permissions->get_checkboxes()]); $vars["search"] = $templates->search_form("/user/search"); - $vars["table"] = $templates->crud_table("/user", "" ,"email"); + $vars["table"] = $templates->crud_table("/user", "" ,"email", $permissionUser); return "views/user.html"; }; $user_post = function(&$vars){ global $db; $user = $db->get(Model\User::class); + $permissions = $db->get(Model\Permission::class); + $permission_user = $db->get(Model\PermissionUser::class); $templates = new controller\templates($db, $user); $data = []; if(isset($_POST["form_type"])){ - if($_POST["form_type"] == 'Add') - $user->add_user(); - elseif($_POST["form_type"] == 'Update'){ - $user->update_user(); + if($_POST["form_type"] == 'Add'){ + if($user->add_user() && $permission_user->add_permissions($user)){ + $vars["response"] = 'succesfully added: ' . $_POST["email"]; + } + } elseif($_POST["form_type"] == 'Update'){ + if($user->update_user()){ + $vars["response"] = 'succesfully updated: ' . $_POST["email"]; + } } } + - $vars["form"] = $templates->form("/user", $data); + $vars["form"] = $templates->form_v2("/user", $data, ["checkboxes" => $permissions->get_checkboxes()]); $vars["search"] = $templates->search_form("/user"); $vars["table"] = $templates->crud_table("/user", "", "email"); return "views/user.html"; @@ -38,8 +46,9 @@ $user_post = function(&$vars){ $user_query = function(&$vars){ global $db; $user = $db->get(Model\User::class); + $permissions = $db->get(Model\Permission::class); $templates = new controller\templates($db, $user); - $vars["form"] = $templates->form("/user"); + $vars["form"] = $templates->form_v2("/user", [], ["checkboxes" => $permissions->get_checkboxes()]); $vars["search"] = $templates->search_form("/user/search"); $vars["table"] = $templates->crud_table("/user", $vars["search_query"], "email"); return "views/user.html"; @@ -63,13 +72,14 @@ $user_search = function(&$vars){ $user_edit = function(&$vars){ global $db; $user = $db->get(Model\User::class); + $permissions = $db->get(Model\Permission::class); $templates = new controller\templates($db, $user); $data = []; $user->load($vars["primary_key"]); foreach($user->getData() as $key => $col){ $data[$key] = $col; } - $vars["form"] = $templates->form("/user", $data); + $vars["form"] = $templates->form_v2("/user", $data, ["checkboxes" => $permissions->get_checkboxes()]); $vars["search"] = $templates->search_form("/user/search"); $vars["table"] = $templates->crud_table("/user", "", "email"); return "views/user.html"; @@ -78,11 +88,21 @@ $user_edit = function(&$vars){ $user_delete = function(&$vars){ global $db; $user = $db->get(Model\User::class); + $permissions = $db->get(Model\Permission::class); $templates = new controller\templates($db, $user); $user->load($vars["primary_key"]); $user->delete(); - $vars["form"] = $templates->form("/user"); + $vars["form"] = $templates->form_v2("/user", [], ["checkboxes" => $permissions->get_checkboxes()]); $vars["search"] = $templates->search_form("/user"); - $vars["table"] = $templates->crud_table("/user" ,"", "email"); + $vars["table"] = $templates->crud_table("/user" ,"", "email"); return "views/user.html"; }; + +$user_page = function(&$vars){ + global $db; + $user = $db->get(Model\User::class); + $permissions = $db->get(Model\Permission::class); + $templates = new controller\templates($db, $user); + + return "views/user_page.html"; +}; +\ No newline at end of file diff --git a/views/course.html b/views/course.html @@ -10,8 +10,6 @@ <div class="flex_container"> <div class ="side_bar"> <div class ="form_add"> - <h1>Add Course</h1> - <a href="/course">New</a> {{$form}} {{$response}} </div> diff --git a/views/user.html b/views/user.html @@ -10,8 +10,6 @@ <div class="flex_container"> <div class ="side_bar"> <div class ="form_add"> - <h1>Add Course</h1> - <a href="/course">New</a> {{$form}} </div> </div> diff --git a/views/user_page.html b/views/user_page.html