lollipop

A PHP-framework
Log | Files | Refs

commit 4ca069a831bc926120c6007345d38d72ac57dbc8
parent 21639de2c3c958109bdacc6fb82c15e69b87e332
Author: MoiBaguette <[email protected]>
Date:   Sat, 24 Jun 2023 16:28:33 +0200

Merge branch 'master' of https://github.com/friedelschoen/lollipop

Diffstat:
AController/Templates.php | 143+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
MLollipop/Template.php | 95++++++-------------------------------------------------------------------------
ALollipop/TemplateMethods.php | 118+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dcontroller/templates.php | 143-------------------------------------------------------------------------------
Mindex.php | 2+-
Mrouting/course.php | 8++++----
Mrouting/index.php | 45+++++++++++++++++++++++++++++++++++++++++++++
Mviews/dashboard.html | 38+++++++++++++++++++++++---------------
8 files changed, 341 insertions(+), 251 deletions(-)

diff --git a/Controller/Templates.php b/Controller/Templates.php @@ -0,0 +1,142 @@ +<?php +namespace Controller{ + class Templates{ + private \Lollipop\SQLDatabase $db; + private \Lollipop\DatabaseObject $table; + private string $schema = 'lollipop'; + private string $table_name; + function __construct(\Lollipop\SQLDatabase $db, \Lollipop\DatabaseObject $table){ + $this->db = $db; + $this->table = $table; + $this->table_name = $table::class; + } + + function form(string $action, array $data = [], array $response = []):string{ + /*auto-increment fields are automatically hidden*/ + $form_type = "Add"; + $form = '<form method="POST" action="'. $action . '">'; + foreach($this->table->get_col_names_ai() as $col){ + if($data == []){ + $value = '-1'; + }else{ + if(in_array($col , array_keys($data))) + $value = $data[$col]; + $form_type = "Update"; + } + $form .= '<input type="hidden" name="' . $col . '" value="' . $value . '">'; + } + $form .= '<input type="hidden" name="form_type" value="' . $form_type . '">'; + foreach($this->table->get_col_names_no_ai() as $col){ + if($data == []){ + $value = ''; + }else{ + if(in_array($col , array_keys($data))) + $value = $data[$col]; + } + $form .= '<input type="text" name="' . $col . '" placeholder="' . $col . '" value="' . $value . '">'; + $miss_key = 'missing_'.$col; + if(array_key_exists($miss_key, $response)){ + $form .= '<div class="form-response"><p style="color:red;"> col: '. $col . ' cannot be empty</p></div>'; + } + } + $form .=' + <input type="submit" value="'. $form_type .'"> + </form>'; + + return $form; + } + + function form_v2(string $action, array $values = [], array $extra = [], array $response = []): string{ + /*auto-increment fields are automatically hidden*/ + if(sizeof($values) == 0){ + $form_type = "Add"; + }else{ + $form_type = "Update"; + } + $form = '<h1>'. $form_type .' '. $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(isset($info["extra"]) && $info["extra"] == "auto_increment"){ + $form .= '<input type="hidden" name="' . $col . '" placeholder="' . $col . '" value="'; + if(isset($values[$col])) + $form .= $values[$col]; + $form .= '">'; + }elseif(isset($info["extra"]) && $info["extra"] == "password"){ + $form .= '<input type="password" name="' . $col . '" placeholder="' . $col . '">'; + }elseif(isset($info["input_type"])){ + $form .= '<input type="'. $info["input_type"] .'" name="' . $col . '" placeholder="' . $col . '" value="'; + if(isset($values[$col])) + $form .= $values[$col]; + $form .= '">'; + } + $miss_key = 'missing_'.$col; + if(array_key_exists($miss_key, $response)){ + $form .= '<div class="form-response"><p style="color:red;"> col: '. $col . ' cannot be empty</p></div>'; + } + } + foreach($extra as $html){ + $form.= $html; + } + $form .= '<input type="hidden" name="form_type" " value="' . $form_type . '">'; + $form .=' + <input type="submit" value="'. $form_type .'"> + </form>'; + return $form; + } + + function search_form(string $action):string{ + return ' + <form method="POST" action="'. $action . '"> + <input type="text" name="search" placeholder="Search..."> + <input type="submit" value="Search"> + </form>'; + } + + public function crud_table(string $action, string $search = "", string $search_key, \Model\PermissionUser $permissionUser = null):string{ + if($search == ""){ + $search = "%"; + }else{ + $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>"; + if($permissionUser != null){ + $table .= "<th>Permissions</th>"; + } + $table .= "</tr> </thead>"; + + $objs = $this->db->all_where($this->table_name, [$search_key => $search]); + $table .= "<tbody>"; + foreach($objs as $obj){ + $table .= "<tr>"; + $col_names = $obj->get_column_names(); + foreach($col_names as $col){ + $table .= '<td>' . $obj->{$col} . '</td>'; + } + $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> + <td>'; + if($permissionUser != null){ + foreach($this->db->all_where(\Model\PermissionUser::class, [$permissionUser->get_primary() => $obj->{$this->table->get_primary()}]) as $perm){ + $table .= $perm->id . ' '; + } + } + $table .= '</td> </tr>'; + } + + $table .= " + </tbody> + </table>"; + return $table; + } + } +} +\ No newline at end of file diff --git a/Lollipop/Template.php b/Lollipop/Template.php @@ -2,6 +2,12 @@ namespace Lollipop { use ErrorException; Class Template{ + private TemplateMethods $methods; + + function __construct(TemplateMethods $methods){ + $this->methods = $methods; + } + function template(string $uri, array $data) : string{ /* this function takes a uri and a string array data */ /* opens a stream to the uri specified file and stores the content in $file*/ @@ -57,97 +63,10 @@ use ErrorException; } private function eval_tokens(array $tokens, array $data) { - $funcs = [ - "add" => function(array &$tokens) { - $right = array_pop($tokens); - $left = array_pop($tokens); - - if (is_null($left) || is_null($right)) - throw new ErrorException("Stack is empty"); - - return $left + $right; - }, - "sub" => function(array &$tokens) { - $right = array_pop($tokens); - $left = array_pop($tokens); - - if (is_null($left) || is_null($right)) - throw new ErrorException("Stack is empty"); - - return intval($left) - intval($right); - }, - "mul" => function(array &$tokens) { - $right = array_pop($tokens); - $left = array_pop($tokens); - - if (is_null($left) || is_null($right)) - throw new ErrorException("Stack is empty"); - - return intval($left) * intval($right); - }, - "div" => function(array &$tokens) { - $right = array_pop($tokens); - $left = array_pop($tokens); - - if (is_null($left) || is_null($right)) - throw new ErrorException("Stack is empty"); - - return intval($left) / intval($right); - }, - "cat" => function(array &$tokens) { - $right = array_pop($tokens); - $left = array_pop($tokens); - - if (is_null($left) || is_null($right)) - throw new ErrorException("Stack is empty"); - - return $left . $right; - }, - - "to_int" => function(array &$tokens) { - $val = array_pop($val); - - if (is_null($val)) - throw new ErrorException("Stack is empty"); - - return inval($val); - }, - - "include" => function (array &$tokens) { - $name = array_pop($tokens); - - if ($name == null) - throw new ErrorException("Stack is empty"); - - include($name); - }, - "eval" => function (array &$tokens) { - $expr = array_pop($tokens); - - if (is_null($expr)) - throw new ErrorException("Stack is empty"); - - return eval("return ($expr);"); - }, - "format_if" => function (array &$stack) { - $format_false = array_pop($stack); - $format_true = array_pop($stack); - $expr = array_pop($stack); - - if (is_null($expr) || is_null($format_true) || is_null($format_false)) - throw new ErrorException("Stack is empty"); - - if ($expr == "") - return $format_false; - else - return str_replace("%%", $expr, $format_true); - }, - ]; - $stack = []; foreach ($tokens as $token) { if ($token && $token[0] == '!') { - $val = $funcs[substr($token, 1)]($stack); + $val = $this->methods->{substr($token, 1)}($stack); if (!is_null($val)) $stack[] = $val; } else if ($token && $token[0] == '$') { diff --git a/Lollipop/TemplateMethods.php b/Lollipop/TemplateMethods.php @@ -0,0 +1,118 @@ +<?php + +namespace Lollipop { + + class TemplateMethods + { + public static function add(array &$tokens) + { + $right = array_pop($tokens); + $left = array_pop($tokens); + + if (is_null($left) || is_null($right)) { + throw new ErrorException("Stack is empty"); + } + + return $left + $right; + + } + + public static function sub(array &$tokens) + { + $right = array_pop($tokens); + $left = array_pop($tokens); + + if (is_null($left) || is_null($right)) { + throw new ErrorException("Stack is empty"); + } + + return intval($left) - intval($right); + } + + public static function mul(array &$tokens) + { + $right = array_pop($tokens); + $left = array_pop($tokens); + + if (is_null($left) || is_null($right)) { + throw new ErrorException("Stack is empty"); + } + + return intval($left) * intval($right); + } + + public static function div(array &$tokens) + { + $right = array_pop($tokens); + $left = array_pop($tokens); + + if (is_null($left) || is_null($right)) { + throw new ErrorException("Stack is empty"); + } + + return intval($left) / intval($right); + } + + public static function cat(array &$tokens) + { + $right = array_pop($tokens); + $left = array_pop($tokens); + + if (is_null($left) || is_null($right)) { + throw new ErrorException("Stack is empty"); + } + + return $left . $right; + } + + public static function to_int(array &$tokens) + { + $val = array_pop($val); + + if (is_null($val)) { + throw new ErrorException("Stack is empty"); + } + + return inval($val); + } + + public static function include(array &$tokens) + { + $name = array_pop($tokens); + + if ($name == null) { + throw new ErrorException("Stack is empty"); + } + + include($name); + } + + public static function eval(array &$tokens) + { + $expr = array_pop($tokens); + + if (is_null($expr)) { + throw new ErrorException("Stack is empty"); + } + + return eval("return ($expr);"); + } + + public static function format_if(array &$stack) + { + $format_false = array_pop($stack); + $format_true = array_pop($stack); + $expr = array_pop($stack); + + if (is_null($expr) || is_null($format_true) || is_null($format_false)) { + throw new ErrorException("Stack is empty"); + } + + if ($expr == "") { + return $format_false; + } else { + return str_replace("%%", $expr, $format_true); + } + } + } +} diff --git a/controller/templates.php b/controller/templates.php @@ -1,142 +0,0 @@ -<?php -namespace controller{ - class templates{ - private \Lollipop\SQLDatabase $db; - private \Lollipop\DatabaseObject $table; - private string $schema = 'lollipop'; - private string $table_name; - function __construct(\Lollipop\SQLDatabase $db, \Lollipop\DatabaseObject $table){ - $this->db = $db; - $this->table = $table; - $this->table_name = $table::class; - } - - function form(string $action, array $data = [], array $response = []):string{ - /*auto-increment fields are automatically hidden*/ - $form_type = "Add"; - $form = '<form method="POST" action="'. $action . '">'; - foreach($this->table->get_col_names_ai() as $col){ - if($data == []){ - $value = '-1'; - }else{ - if(in_array($col , array_keys($data))) - $value = $data[$col]; - $form_type = "Update"; - } - $form .= '<input type="hidden" name="' . $col . '" value="' . $value . '">'; - } - $form .= '<input type="hidden" name="form_type" value="' . $form_type . '">'; - foreach($this->table->get_col_names_no_ai() as $col){ - if($data == []){ - $value = ''; - }else{ - if(in_array($col , array_keys($data))) - $value = $data[$col]; - } - $form .= '<input type="text" name="' . $col . '" placeholder="' . $col . '" value="' . $value . '">'; - $miss_key = 'missing_'.$col; - if(array_key_exists($miss_key, $response)){ - $form .= '<div class="form-response"><p style="color:red;"> col: '. $col . ' cannot be empty</p></div>'; - } - } - $form .=' - <input type="submit" value="'. $form_type .'"> - </form>'; - - return $form; - } - - function form_v2(string $action, array $values = [], array $extra = [], array $response = []): string{ - /*auto-increment fields are automatically hidden*/ - if(sizeof($values) == 0){ - $form_type = "Add"; - }else{ - $form_type = "Update"; - } - $form = '<h1>'. $form_type .' '. $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(isset($info["extra"]) && $info["extra"] == "auto_increment"){ - $form .= '<input type="hidden" name="' . $col . '" placeholder="' . $col . '" value="'; - if(isset($values[$col])) - $form .= $values[$col]; - $form .= '">'; - }elseif(isset($info["extra"]) && $info["extra"] == "password"){ - $form .= '<input type="password" name="' . $col . '" placeholder="' . $col . '">'; - }elseif(isset($info["input_type"])){ - $form .= '<input type="'. $info["input_type"] .'" name="' . $col . '" placeholder="' . $col . '" value="'; - if(isset($values[$col])) - $form .= $values[$col]; - $form .= '">'; - } - $miss_key = 'missing_'.$col; - if(array_key_exists($miss_key, $response)){ - $form .= '<div class="form-response"><p style="color:red;"> col: '. $col . ' cannot be empty</p></div>'; - } - } - foreach($extra as $html){ - $form.= $html; - } - $form .= '<input type="hidden" name="form_type" " value="' . $form_type . '">'; - $form .=' - <input type="submit" value="'. $form_type .'"> - </form>'; - return $form; - } - - function search_form(string $action):string{ - return ' - <form method="POST" action="'. $action . '"> - <input type="text" name="search" placeholder="Search..."> - <input type="submit" value="Search"> - </form>'; - } - - public function crud_table(string $action, string $search = "", string $search_key, \Model\PermissionUser $permissionUser = null):string{ - if($search == ""){ - $search = "%"; - }else{ - $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>"; - if($permissionUser != null){ - $table .= "<th>Permissions</th>"; - } - $table .= "</tr> </thead>"; - - $objs = $this->db->all_where($this->table_name, [$search_key => $search]); - $table .= "<tbody>"; - foreach($objs as $obj){ - $table .= "<tr>"; - $col_names = $obj->get_column_names(); - foreach($col_names as $col){ - $table .= '<td>' . $obj->{$col} . '</td>'; - } - $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> - <td>'; - if($permissionUser != null){ - foreach($this->db->all_where(\Model\PermissionUser::class, [$permissionUser->get_primary() => $obj->{$this->table->get_primary()}]) as $perm){ - $table .= $perm->id . ' '; - } - } - $table .= '</td> </tr>'; - } - - $table .= " - </tbody> - </table>"; - return $table; - } - } -} -\ No newline at end of file diff --git a/index.php b/index.php @@ -4,7 +4,7 @@ require_once "routing/index.php"; require_once "routing/user.php"; require_once "routing/course.php"; -$templater = new Lollipop\Template(); +$templater = new Lollipop\Template(new Lollipop\TemplateMethods()); $router = new Lollipop\Router($templater); $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "lollipop"); diff --git a/routing/course.php b/routing/course.php @@ -2,7 +2,7 @@ $course_get = function(&$vars){ global $db; $course = $db->get(Model\Course::class); - $templates = new controller\templates($db, $course); + $templates = new Controller\Templates($db, $course); if(isset($_POST["search"])){ $course->load($_POST["search"]); @@ -17,7 +17,7 @@ $course_get = function(&$vars){ $course_post = function(&$vars){ global $db; $course = $db->get(Model\Course::class); - $templates = new controller\templates($db, $course); + $templates = new Controller\Templates($db, $course); $data = []; if(isset($_POST["form_type"])){ @@ -67,7 +67,7 @@ $course_search = function(&$vars){ $course_edit = function(&$vars){ global $db; $course = $db->get(Model\Course::class); - $templates = new controller\templates($db, $course); + $templates = new Controller\Templates($db, $course); $data = []; $course->load($vars["primary_key"]); foreach($course->getData() as $key => $col){ @@ -82,7 +82,7 @@ $course_edit = function(&$vars){ $course_delete = function(&$vars){ global $db; $course = $db->get(Model\Course::class); - $templates = new controller\templates($db, $course); + $templates = new Controller\Templates($db, $course); $course->load($vars["primary_key"]); $course->delete(); $vars["form"] = $templates->form_v2("/course"); diff --git a/routing/index.php b/routing/index.php @@ -20,7 +20,52 @@ $index_post = function(&$vars){ }; $dashboard = function(&$vars){ + global $db; $vars += $_SESSION; + $templates = new Controller\Templates($db, $db->get(\Model\Course::class)); + $course = $db->get(Model\Course::class); + + $enrolled = []; + + foreach($db->all_where(Model\CourseUser::class, [ "email" => $_SESSION['email'] ]) as $data) { + $enrolled[] = $data->id; + } + + $table = "<table> <thead> <tr>"; + foreach($course->get_column_names() as $column){ + $table .= "<th>$column</th>"; + } + $table .= "</tr> </thead>"; + + $objs = $db->all(Model\Course::class); + $table .= "<tbody>"; + foreach($objs as $obj){ + if (in_array($obj->id, $enrolled)) { + $enroll_btn = 'Enroll'; + $enroll_action = 'enroll'; + } else { + $enroll_btn = 'Disenroll'; + $enroll_action = 'disenroll'; + } + + $table .= "<tr>"; + $col_names = $obj->get_column_names(); + foreach($col_names as $col){ + $table .= '<td>' . $obj->{$col} . '</td>'; + } + $table .= ' + <td> + <a class="edit" href="/user/'. $_SESSION['email'] . '/course/' . $obj->id . '/' . $enroll_action . '/";>' . $enroll_btn . '</a> + </td>'; + + $table .= '</tr>'; + } + + $table .= " + </tbody></table>"; + + $vars['in_course'] = $table; + return "views/dashboard.html"; }; diff --git a/views/dashboard.html b/views/dashboard.html @@ -1,16 +1,24 @@ -<html> - <head> - <link rel="stylesheet" href="views/css/input.css"> - </head> - <body> - <div class="header"> - <div></div> - <h1>Lollipop</h1> - <form method="post" action="/logout"> - <input type="submit" value="Logout"> - </form> - </div> - - {{$email}} - </body> +<!DOCTYPE html> +<html lang="eng"> + <title>Add User</title> + <link rel="stylesheet" href="/views/css/theme.css"> + <link rel="stylesheet" href="/views/css/form_template.css"> + <link rel="stylesheet" href="/views/css/course.css"> + <meta name="viewport" content="width=device-width, initial-scale=1" /> +</head> +<body> + <div class="flex_container"> + <div class ="side_bar"> + <div class ="form_add"> + <h1>Dashboard</h1> + Welcome {{ $first_name }}! + </div> + </div> + <div class = "courses"> + <div class="table"> + {{ $in_course }} + </div> + </div> + </div> +</body> </html> \ No newline at end of file