lollipop

A PHP-framework
Log | Files | Refs

commit 021ead60bfa23b1ba7e3b12c6b65817fc9c54f67
parent 6d7b483109f2e2c309d24f862181d042ca123d37
Author: MoiBaguette <[email protected]>
Date:   Sat, 24 Jun 2023 13:47:08 +0200

minor changes

Diffstat:
MLollipop/DatabaseObject.php | 44+++++++++++++++++++++++++++++++++++---------
MLollipop/Utils.php | 4++--
MModel/Course.php | 26++++++++++++++++++--------
Mcontroller/templates.php | 32++++++++++++++++++++++++++++++++
Mindex.php | 23++++++++++++-----------
5 files changed, 99 insertions(+), 30 deletions(-)

diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php @@ -171,30 +171,26 @@ namespace Lollipop { } public function notNullable(){ //non-auto-increment not-nullable collumn names query - $not_null = []; $col_names = []; - $sql = " SELECT column_name, is_nullable, extra + $sql = " SELECT is_nullable FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{$this->table}' - AND TABLE_SCHEMA = 'lollipop'"; + AND TABLE_SCHEMA = '{$this->schema}'"; $stmt = $this->db->conn->prepare($sql); $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows == 0) { - return false; + return []; } while($tmp = $result->fetch_assoc()){ if($tmp["is_nullable"] == 'NO'){ if($tmp["extra"] == "auto_increment") continue; - $not_null[] = $tmp["column_name"]; + $col_names[] = $tmp["column_name"]; } - $col_names[] = $tmp["column_name"]; } - $this->not_nullable = $not_null; - $this->column_names = $col_names; - return true; + return $col_names; } public function get_column_names():array{ $column_names = []; @@ -252,5 +248,35 @@ namespace Lollipop { } return $column_names; } + public function get_col_info():array{ + $column_names = []; + $sql = " SELECT column_name, extra, data_type + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_NAME = '$this->table' + AND TABLE_SCHEMA = '$this->schema'"; + $stmt = $this->db->conn->prepare($sql); + $stmt->execute(); + $result = $stmt->get_result(); + + if ($result->num_rows == 0) { + return []; + } + while($tmp = $result->fetch_assoc()){ + if(str_contains($tmp['data_type'], "varchar") || str_contains($tmp['data_type'], "text")){ + $column_names[$tmp["column_name"]]["input_type"] = "text"; + }elseif(str_contains($tmp['data_type'], "date")){ + $column_names[$tmp["column_name"]]["input_type"] = "date"; + }elseif(str_contains($tmp['data_type'], "int")){ + $column_names[$tmp["column_name"]]["input_type"] = "number"; + } + if(str_contains($tmp['extra'], "auto_increment")){ + $column_names[$tmp["column_name"]]['extra'] = "auto_increment"; + } + if(str_contains($tmp['column_name'], "password")){ + $column_names[$tmp["column_name"]]['extra'] = "password"; + } + } + return $column_names; + } } } \ No newline at end of file diff --git a/Lollipop/Utils.php b/Lollipop/Utils.php @@ -9,10 +9,10 @@ namespace Lollipop{ return $arr; } - static function missing_fields($post, $not_nullable){ + static function missing_fields($not_nullable){ $missing = []; foreach($not_nullable as $column){ - if($post[$column] == NULL || $post[$column] == ""){ + if($_POST[$column] == NULL || $_POST[$column] == ""){ $key = 'missing_' . $column; $missing[$key] = "This field cannot be empty!"; } diff --git a/Model/Course.php b/Model/Course.php @@ -1,6 +1,7 @@ <?php namespace Model { +use Lollipop\Utils; class Course extends \Lollipop\DatabaseObject { static function get_table(): string @@ -19,20 +20,29 @@ namespace Model { } public function add_course():bool{ - foreach($_POST as $key => $post){ - if(in_array($key, $this->get_col_names_no_ai())){ - $this->{$key} = $post; + $missing_fields = Utils::missing_fields($this->notNullable()); + if(sizeof($missing_fields) == 0){ + foreach($_POST as $key => $post){ + if(in_array($key, $this->get_col_names_no_ai())){ + $this->{$key} = $post; + } } + return $this->add(); } - return $this->add(); + return false; } public function update_course():bool{ - foreach($_POST as $key => $post){ - if(in_array($key, $this->get_column_names())){ - $this->{$key} = $post; + $missing_fields = Utils::missing_fields($this->notNullable()); + if(sizeof($missing_fields) == 0){ + foreach($_POST as $key => $post){ + if(in_array($key, $this->get_column_names())){ + $this->{$key} = $post; + } } + return $this->save(); } - return $this->save(); + return false; } + } } \ No newline at end of file diff --git a/controller/templates.php b/controller/templates.php @@ -46,6 +46,38 @@ namespace controller{ return $form; } + function form_v2(string $action, array $values = [], array $funcs = [], $response = []): string{ + /*auto-increment fields are automatically hidden*/ + $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] . '">'; + }elseif($info["extra"] == "password"){ + $form .= '<input type="password" name="' . $col . '" placeholder="' . $col . '">'; + }else{ + $form .= '<input type="'. $info["input_type"] .'" name="' . $col . '" placeholder="' . $col . '" value="' . $values[$col] . '">'; + } + $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($funcs as $func){ + if (is_callable($func)) + $form.= $func(); + } + if(sizeof($values) == 0){ + $form_type = "Add"; + }else{ + $form_type = "Update"; + } + $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 . '"> diff --git a/index.php b/index.php @@ -135,12 +135,9 @@ $router->addRoute(["GET"], "/course", function(&$vars){ global $db; $course = $db->get(Model\Course::class); $templates = new controller\templates($db, $course); + var_dump($course->get_col_info()); - if(isset($_POST["search"])){ - $course->load($_POST["search"]); - $data = $course->getData(); - } - $vars["form"] = $templates->form("/course"); + $vars["form"] = $templates->form_v2("/course"); $vars["search"] = $templates->search_form("/course/search"); $vars["table"] = $templates->crud_table("/course", "" ,"name"); return "views/course.html"; @@ -150,7 +147,7 @@ $router->addRoute(["GET"], "/course/search/:search_query", function(&$vars){ global $db; $course = $db->get(Model\Course::class); $templates = new controller\templates($db, $course); - $vars["form"] = $templates->form("/course"); + $vars["form"] = $templates->form_v2("/course"); $vars["search"] = $templates->search_form("/course/search"); $vars["table"] = $templates->crud_table("/course", $vars["search_query"], "name"); return "views/course.html"; @@ -176,11 +173,12 @@ $router->addRoute(["GET"], "/course/:primary_key/edit", function(&$vars){ $course = $db->get(Model\Course::class); $templates = new controller\templates($db, $course); $data = []; + $course->load($vars["primary_key"]); foreach($course->getData() as $key => $col){ $data[$key] = $col; } - $vars["form"] = $templates->form("/course", $data); + $vars["form"] = $templates->form_v2("/course", $data); $vars["search"] = $templates->search_form("/course/search"); $vars["table"] = $templates->crud_table("/course", "", "name"); return "views/course.html"; @@ -192,7 +190,8 @@ $router->addRoute(["GET"], "/course/:primary_key/delete", function(&$vars){ $templates = new controller\templates($db, $course); $course->load($vars["primary_key"]); $course->delete(); - $vars["form"] = $templates->form("/course"); + + $vars["form"] = $templates->form_v2("/course"); $vars["search"] = $templates->search_form("/course"); $vars["table"] = $templates->crud_table("/course" ,"", "name"); return "views/course.html"; @@ -203,16 +202,18 @@ $router->addRoute(["POST"], "/course", function(&$vars){ $course = $db->get(Model\Course::class); $templates = new controller\templates($db, $course); $data = []; - if(isset($_POST["form_type"])){ - if($_POST["form_type"] == 'Add') + if($_POST["form_type"] == 'Add'){ $course->add_course(); + echo "add 'em"; + } elseif($_POST["form_type"] == 'Update'){ + echo "update 'em"; $course->update_course(); } } - $vars["form"] = $templates->form("/course", $data); + $vars["form"] = $templates->form_v2("/course", $data); $vars["search"] = $templates->search_form("/course"); $vars["table"] = $templates->crud_table("/course", "", "name"); return "views/course.html";