user.php (4583B)
1 <?php 2 $user_get = function (&$vars) { 3 global $db; 4 $user = $db->get(Model\User::class); 5 $permissions = $db->get(Model\Permission::class); 6 $permission_user = $db->get(Model\PermissionUser::class); 7 $vars += get_vars($user, "/user", "/user/search", "", "email", [], $permissions->get_checkboxes(), $permission_user); 8 return "views/user.html"; 9 }; 10 11 $user_post = function (&$vars) { 12 global $db; 13 $user = $db->get(Model\User::class); 14 $permissions = $db->get(Model\Permission::class); 15 $permission_user = $db->get(Model\PermissionUser::class); 16 17 if(isset($_POST["form_type"])) { 18 if($_POST["form_type"] == 'Add') { 19 if($user->add_user() && $permission_user->add_permissions($user)) { 20 $vars["response"] = 'succesfully added: ' . $_POST["email"]; 21 } 22 } elseif($_POST["form_type"] == 'Update') { 23 if($user->update_user() && $permission_user->update_permissions($user)) { 24 $vars["response"] = 'succesfully updated: ' . $_POST["email"]; 25 } else { 26 echo"something went wrong"; 27 } 28 } 29 } 30 $vars += get_vars($user, "/user", "/user/search", "", "email", [], $permissions->get_checkboxes(), $permission_user); 31 return "views/user.html"; 32 }; 33 34 $user_query = function (&$vars) { 35 global $db; 36 $user = $db->get(Model\User::class); 37 $permissions = $db->get(Model\Permission::class); 38 $permission_user = $db->get(Model\PermissionUser::class); 39 $query = '%' . $vars['search_query'] . '%'; 40 $vars += get_vars($user, "/user", "/user/search", $query, "email", [], $permissions->get_checkboxes(), $permission_user); 41 return "views/user.html"; 42 }; 43 44 $user_search = function (&$vars) { 45 if(isset($_POST['search'])) { 46 if($_POST['search'] == "") { 47 $search = "%"; 48 } else { 49 $search = $_POST['search']; 50 } 51 $header = '/user/search/' . $search; 52 header('Location: ' . $header); 53 } else { 54 echo "wtF?"; 55 var_dump($_POST); 56 } 57 }; 58 59 $user_edit = function (&$vars) { 60 global $db; 61 $user = $db->get(Model\User::class); 62 $permissions = $db->get(Model\Permission::class); 63 $permission_user = $db->get(Model\PermissionUser::class); 64 $templates = new controller\templates($db, $user); 65 $data = []; 66 $user->load($vars["primary_key"]); 67 foreach($user->getData() as $key => $col) { 68 $data[$key] = $col; 69 } 70 $vars += get_vars($user, "/user", "/user/search", "", "email", $data, $permissions->get_checkboxes(), $permission_user); 71 return "views/user.html"; 72 }; 73 74 $user_delete = function (&$vars) { 75 global $db; 76 $user = $db->get(Model\User::class); 77 $permissions = $db->get(Model\Permission::class); 78 79 $permission_user = $db->get(Model\PermissionUser::class); 80 $user->load($vars["primary_key"]); 81 $user->delete(); 82 $vars += get_vars($user, "/user", "/user/search", "", "email", $permissions->get_checkboxes(), [], $permission_user); 83 return "views/user.html"; 84 }; 85 86 $user_page = function (&$vars) { 87 global $db; 88 $user = $db->get(Model\User::class); 89 $exam = $db->get(Model\Exam::class); 90 $user->load($vars['primary_key']); 91 $data = $user->getData(); 92 if(in_array(3, $_SESSION['user_permissions'])) { 93 foreach($data as $key => $d) { 94 $vars['user_data'] .= "<p>your $key = $d<p><br>"; 95 } 96 } elseif($vars['primary_key'] == $_SESSION['email']) { 97 foreach($data as $key => $d) { 98 $vars['user_data'] .= "<p>your $key = $d<p><br>"; 99 } 100 $vars['user_data'] .= "<h1>your grades</h1><br>"; 101 foreach($db->all_where(Model\Grade::class, ['email' => $vars['primary_key']]) as $grade) { 102 $exam->load($grade->exam); 103 $exam_name =$exam->name; 104 $vars['user_data'] .= "<p>your grade for $exam_name = $grade->grade </p><br>"; 105 } 106 } 107 return "views/user_page.html"; 108 }; 109 function get_vars(Lollipop\DatabaseObject $table, string $action, string $search_action, string $search_string, string $search_key, array $form_data = [], array $extra = [], Model\PermissionUser $permissionUser = null): array 110 { 111 global $db; 112 $templates = new Controller\Templates($db, $table); 113 114 $array = []; 115 $array["form"] = $templates->form_v2($action, $form_data, $extra); 116 $array["search"] = $templates->search_form($search_action); 117 $array["table"] = $templates->crud_table($action, $search_string, $search_key, $permissionUser); 118 $array['first_name'] = $_SESSION['first_name']; 119 return $array; 120 }