index.php (3522B)
1 <?php 2 3 $index_get = function (&$vars) { 4 if(isset($_SESSION['user_permissions'])) { 5 header('Location: /dashboard'); 6 } 7 global $db; 8 $templates = new Controller\Templates($db, $db->get(\Model\User::class)); 9 $vars["header"] = $templates->header(); 10 $vars["login-fields"] = $db->get(Model\User::class)->login_fields(); 11 return "views/login.html"; 12 }; 13 14 $index_post = function (&$vars) { 15 global $db; 16 $res = $db->get(Model\User::class)->login(); 17 $templates = new Controller\Templates($db, $db->get(\Model\Course::class)); 18 $vars["header"] = $templates->header(); 19 if($res == []) { 20 header("Location: dashboard"); 21 exit(); 22 } else { 23 $vars["login-fields"] = $db->get(Model\User::class)->login_fields(); 24 $vars["response"] = $res["response"] ; 25 return "views/login.html"; 26 } 27 }; 28 29 $dashboard = function (&$vars) { 30 global $db; 31 $vars += $_SESSION; 32 $templates = new Controller\Templates($db, $db->get(\Model\Course::class)); 33 $vars["header"] = $templates->header(); 34 $course = $db->get(Model\Course::class); 35 36 $vars['links'] = $templates->links(); 37 $enrolled = []; 38 39 foreach($db->all_where(Model\CourseUser::class, [ "email" => $_SESSION['email'] ]) as $data) { 40 $enrolled[] = $data->id; 41 } 42 43 $table = "<table> <thead> <tr>"; 44 foreach($course->get_column_names() as $column) { 45 $table .= "<th>$column</th>"; 46 } 47 $table .= "<th>registered</th></tr> </thead>"; 48 49 $objs = $db->all(Model\Course::class); 50 $table .= "<tbody>"; 51 foreach($objs as $obj) { 52 if (in_array($obj->id, $enrolled)) { 53 $enroll_btn = 'Disenroll'; 54 $enroll_action = 'disenroll'; 55 } else { 56 $enroll_btn = 'Enroll'; 57 $enroll_action = 'enroll'; 58 } 59 60 $table .= "<tr>"; 61 $col_names = $obj->get_column_names(); 62 foreach($col_names as $col) { 63 $table .= '<td>' . $obj->{$col} . '</td>'; 64 } 65 $table .= ' 66 <td> 67 <a class="edit" href="/course/' . $obj->id . '/' . $enroll_action . '/";>' . $enroll_btn . '</a> 68 </td>'; 69 70 $table .= '</tr>'; 71 } 72 73 $table .= " 74 </tbody></table>"; 75 76 $vars['in_course'] = $table; 77 78 return "views/dashboard.html"; 79 }; 80 81 $logout = function (&$vars) { 82 session_unset(); 83 session_destroy(); 84 header("Location: /"); 85 }; 86 $test = function (&$vars) { 87 global $db; 88 $user = $db->get(Model\User::class); 89 $permissions = $db->get(Model\Permission::class); 90 91 $permission_user = $db->get(Model\PermissionUser::class); 92 var_dump($db->all_where(Model\User::class, ["email" => ""])); 93 var_dump(get_vars($user, "/user", "/user/search", "", "email", [], $permissions->get_checkboxes(), $permission_user)); 94 }; 95 96 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 97 { 98 global $db; 99 $templates = new Controller\Templates($db, $table); 100 101 $array = []; 102 $array["header"] = $templates->header(); 103 $array["form"] = $templates->form_v2($action, $form_data, $extra); 104 $array["search"] = $templates->search_form($search_action); 105 $array["table"] = $templates->crud_table($action, $search_string, $search_key, $permissionUser); 106 $array['links'] = $templates->links(); 107 $array['first_name'] = $_SESSION['first_name']; 108 return $array; 109 }