From 63f714d5a78b765c117ebf6bbdfdbd748dd45644 Mon Sep 17 00:00:00 2001 From: George Halkiadakis Date: Thu, 4 May 2023 03:02:01 +0300 Subject: authentication; invitation; activation; base form setup --- public/app/models/Access_model.php | 57 ++++--- public/app/models/Cms_model.php | 314 ------------------------------------- public/app/models/Office_model.php | 314 +++++++++++++++++++++++++++++++++++++ public/app/models/_info.md | 21 +++ 4 files changed, 372 insertions(+), 334 deletions(-) delete mode 100644 public/app/models/Cms_model.php create mode 100644 public/app/models/Office_model.php (limited to 'public/app/models') diff --git a/public/app/models/Access_model.php b/public/app/models/Access_model.php index 8d446dc..8a2e5ce 100644 --- a/public/app/models/Access_model.php +++ b/public/app/models/Access_model.php @@ -3,6 +3,7 @@ namespace app\models; use \Registry; +use app\extends\App_manager; use app\models\History_model; class Access_model @@ -148,36 +149,52 @@ class Access_model } - /** activate + /** activate_set_password * - * check if activation code is valid; - * if valid, set account active; + * activate user and set password * * @param $ticket (hex/MD5): activation code; * */ - public static function activate($ticket) + public static function activate_set_password($post, $password) { - $user = Registry::use('database')->query( - "SELECT * FROM user WHERE activation = :ticket", - [ 'ticket' => $ticket ] - )->getFirst(); - - // if no user with this activation code, return false - if ($user === false) return false; - - // remove activation code from user record - Registry::use('database')->runQuery( + // Update and set activate = true + $rowCount = Registry::use('database')->query( "UPDATE user - SET active = 1, `activation` = NULL - WHERE activation = :ticket", - [ 'ticket' => $ticket ] - ); + SET first_name = :first_name, + last_name = :last_name, + email = :email, + father_name = :father_name, + registration_number = :registration_number, + sector = :sector, + belonging_school = :belonging_school, + position = :position, + phone = :phone, + `password` = :password, + invitation = NULL, + active = :active + WHERE id = :id AND invitation = :invitation", + [ + ':first_name' => $post['first_name'], + ':last_name' => $post['last_name'], + ':email' => $post['email'], + ':father_name' => $post['father_name'], + ':registration_number' => $post['registration_number'], + ':sector' => $post['sector'], + ':belonging_school' => $post['belonging_school'], + ':position' => $post['position'], + ':phone' => $post['phone'], + ':password' => $password, + ':active' => 1, + ':id' => $post['id'], + ':invitation' => $post['invitation'] + ] + )->rowCount(); // update history - History_model::trackUserAccess($user['id'], TRACK_ACCOUNT, 'User Account Activated'); + History_model::trackUserAccess($post['id'], TRACK_ACCOUNT, 'User Account Activated'); - return true; + return $rowCount; } diff --git a/public/app/models/Cms_model.php b/public/app/models/Cms_model.php deleted file mode 100644 index fad11b8..0000000 --- a/public/app/models/Cms_model.php +++ /dev/null @@ -1,314 +0,0 @@ -runQuery( - "SELECT * FROM course ORDER BY label", - [] - ); - } - - /** courses struct - * - * a super array with almost any info needed about courses - * - * @return (array) ['tree' => ..., 'breadcrumbs' => ... ] - */ - public static function courses_struct() - { - $tree = self::category_tree(); - return [ - 'tree' => $tree, - 'breadcrumbs' => self::breadcrumbs($tree) - ]; - } - - - /** constuct a category_tree - * - * returns a tree representation of the categories - * - * NOTE: - * category_tree() is an expensive method; - * it calls 2 other methods implementing recursive algorithms - * thus it uses many sources to run (particularly RAM). - * Caching the result is strogly recommended. - * - */ - public static function category_tree() - { - $categories = self::get_categories(); // get all categories - - $tree = self::to_tree($categories); // format to a tree - - $tree_wParents = self::tree_parents($tree); // add parents section for each tree-node - - return $tree_wParents; - } - - - /** to_tree - * - * constructs a tree from raw-table data; - * this is a private method and uses a recursive algorithm - * - * @param $dataset (array): flar array of records with id/parent-id pairs - * @return $root (array): id of root category - * - * (**) each node has 2 parts: - * .... .. rec : all record attributes/data as passed into $dataset - * .... .. childs : array of (children) nodes - */ - private static function to_tree($dataset, $root = 0) - { - $return = []; - - // loop data ; search for direct children of root - foreach($dataset as $key => $rec) { - - $child = $rec['id']; - $parent = $rec['parent_id']; - - if ($parent == $root) { // a direct child is found - - unset($dataset[$key]); // remove item (no need to traverse again) - - // Append the child into result array ; parse its children - $return[] = [ - 'rec' => [ - 'id' => $rec['id'], - 'label' => $rec['label'], - 'order' => $rec['order'], - 'parent' => $rec['parent_id'] - ], - 'childs' => self::to_tree($dataset, $child) // recursively - ]; - } - } - return empty($return) ? [] : $return; - } - - - /** tree_parents - * - * adds a section to each tree node with all parents of each node - * - * @param $tree (array) : nodes array (each node has `rec` and `childs` sections ) - * @param $parents (array); DO NOT SET IT (takes values automaticaly) - * @return array of nodes with an extra node[parents] section - * - */ - private static function tree_parents($tree, $parents = []) - { - $tree_with_parents = []; - - foreach($tree as $key => $node) { - // parents to be pushed for node's children - $push_parents = $parents; // parents so far - $push_parents[] = $node['rec']; // this record will be a new parent - - $tree_with_parents[$key] = [ - 'rec' => $node['rec'], - 'parents' => $parents, - 'childs' => ($node['childs'] == []) - ? [] - : self::tree_parents($node['childs'], $push_parents) - ]; - } - - return $tree_with_parents; - } - - - /** all_breadcrumbs - * ------------------------------------------------------------------------- - * - * returns an array of all breadcrumbs - * where array-key of each record is category[id] - * - * NOTE: - * --- - * Cms_model::all_breadcrumbs returns an indexed super-array; - * each array item includes a banch of information: [ - * breadcrumb, - * rec: [ id , title ], - * parents: [ [id, title] , ... ] - * childs: [ [id, title] , ... ], - * level - * ] - * - * Use Cases: - * --- - * as a super-array, the output can be used in many cases - * for example... - * into form elements - * .. while selecting category for a post - * .. or editing a category - * or directry referring to category's parents/childs - * - * Arguments: - * --- - * @param $tree (array) : category tree (with childs and parents parts) - * @param $detimiter (string, optional) : string to split breadcrumb's path-nodes - * @param $exception (int, optional) : id of category to exclude (subcategories shall be excluded too) - * @param $l (int, not-pass) : depth level of the node; DO NOT SET (takes values automaticaly) - * @return array of breadcrumbs - * ------------------------------------------------------------------------- - */ - static public function breadcrumbs($tree, $delimiter = " / ", $exception = 0, $l = 0) - { - $all = []; // results array - - foreach($tree as $node) { // loop through all nodes - - if (intval($node['rec']['id']) != $exception) { // if node is not exception - - // construct breadcrumb html of node - // --- -- -- - - - - $breadcrumb = ""; - foreach($node['parents'] as $par) { // first: join path titles - $breadcrumb .= $par['label'] . $delimiter; - } - $breadcrumb .= $node['rec']['label']; // last: append title - - // make a new super record - // --- -- -- - - - - $all[$node['rec']['id']] = [ // set record is as key - 'breadcrumb' => $breadcrumb, // add breadcrump to results - 'rec' => $node['rec'], // + node info - 'parents' => $node['parents'], // + parents array - 'childs' => self::first_level_childs($node), // + direct childs - 'level' => $l // + level - ]; - - // recursively traverse children nodes - // --- -- -- - - - - if (isset($node['childs']) && $node['childs'] != []) { - $child_breadcrumbs = self::breadcrumbs( - $node['childs'], - $delimiter, - $exception, - $l+1 - ); - - $all = $all + $child_breadcrumbs; // concatenate arrays (keep array-keys) - } - } - - } - return $all; - - } - - /** first_level_childs - * --- -- -- - - - - * used by all_breadcrumbs() - */ - static private function first_level_childs($node) - { - $childs = []; - if ($node['childs'] == []) { - return []; - } - foreach($node['childs'] as $key => $kid) { - $childs[] = [ - 'id' => $kid['rec']['id'], - 'label' => $kid['rec']['label'] - ]; - } - return $childs; - } - - - - - /** LESSONS - * ------------------------------------------------------------------------- - */ - - - /** lessons of course - * - * @param $id (int) : course_id - */ - public static function lessons_of_course($id) - { - // TODO: order results in some way - - return Registry::use('database')->runQuery( - "SELECT lesson.*, lesson_privilege.privilege_id FROM lesson - LEFT JOIN lesson_privilege ON lesson_privilege.lesson_id = lesson.id - WHERE lesson.status = 1 AND lesson.course_id = :id - ORDER BY lesson_privilege.privilege_id ASC", - [':id' => $id] - ); - } - - /** lesson - * - * @param $id (int) : lesson id - */ - public static function lesson($id) - { - // TODO: order results in some way - - return Registry::use('database')->query( - "SELECT lesson.*, lesson_privilege.privilege_id FROM lesson - LEFT JOIN lesson_privilege ON lesson_privilege.lesson_id = lesson.id - WHERE lesson.status = 1 AND lesson.id = :id", - [':id' => $id] - )->getFirst(); - } - - - - /** files - * return all files - * @param void - * @return array - */ - public static function files() - { - return Registry::use('database')->runQuery("SELECT * from media", []); - } - - - /** pages - * - * return all pages as array; - * array key of each item shall be the page-id - * - * @param void - * @return array - */ - public static function pages() - { - $result = []; - $pages = Registry::use('database')->runQuery("SELECT * FROM page", []); - foreach($pages as $key => $page) { - $result[ $page['id'] ] = [ - 'title' => $page['title'], - 'body' => $page['body'], - 'status' => $page['status'] - ]; - } - // print_r($result); die(); - return $result; - } - -} diff --git a/public/app/models/Office_model.php b/public/app/models/Office_model.php new file mode 100644 index 0000000..fad11b8 --- /dev/null +++ b/public/app/models/Office_model.php @@ -0,0 +1,314 @@ +runQuery( + "SELECT * FROM course ORDER BY label", + [] + ); + } + + /** courses struct + * + * a super array with almost any info needed about courses + * + * @return (array) ['tree' => ..., 'breadcrumbs' => ... ] + */ + public static function courses_struct() + { + $tree = self::category_tree(); + return [ + 'tree' => $tree, + 'breadcrumbs' => self::breadcrumbs($tree) + ]; + } + + + /** constuct a category_tree + * + * returns a tree representation of the categories + * + * NOTE: + * category_tree() is an expensive method; + * it calls 2 other methods implementing recursive algorithms + * thus it uses many sources to run (particularly RAM). + * Caching the result is strogly recommended. + * + */ + public static function category_tree() + { + $categories = self::get_categories(); // get all categories + + $tree = self::to_tree($categories); // format to a tree + + $tree_wParents = self::tree_parents($tree); // add parents section for each tree-node + + return $tree_wParents; + } + + + /** to_tree + * + * constructs a tree from raw-table data; + * this is a private method and uses a recursive algorithm + * + * @param $dataset (array): flar array of records with id/parent-id pairs + * @return $root (array): id of root category + * + * (**) each node has 2 parts: + * .... .. rec : all record attributes/data as passed into $dataset + * .... .. childs : array of (children) nodes + */ + private static function to_tree($dataset, $root = 0) + { + $return = []; + + // loop data ; search for direct children of root + foreach($dataset as $key => $rec) { + + $child = $rec['id']; + $parent = $rec['parent_id']; + + if ($parent == $root) { // a direct child is found + + unset($dataset[$key]); // remove item (no need to traverse again) + + // Append the child into result array ; parse its children + $return[] = [ + 'rec' => [ + 'id' => $rec['id'], + 'label' => $rec['label'], + 'order' => $rec['order'], + 'parent' => $rec['parent_id'] + ], + 'childs' => self::to_tree($dataset, $child) // recursively + ]; + } + } + return empty($return) ? [] : $return; + } + + + /** tree_parents + * + * adds a section to each tree node with all parents of each node + * + * @param $tree (array) : nodes array (each node has `rec` and `childs` sections ) + * @param $parents (array); DO NOT SET IT (takes values automaticaly) + * @return array of nodes with an extra node[parents] section + * + */ + private static function tree_parents($tree, $parents = []) + { + $tree_with_parents = []; + + foreach($tree as $key => $node) { + // parents to be pushed for node's children + $push_parents = $parents; // parents so far + $push_parents[] = $node['rec']; // this record will be a new parent + + $tree_with_parents[$key] = [ + 'rec' => $node['rec'], + 'parents' => $parents, + 'childs' => ($node['childs'] == []) + ? [] + : self::tree_parents($node['childs'], $push_parents) + ]; + } + + return $tree_with_parents; + } + + + /** all_breadcrumbs + * ------------------------------------------------------------------------- + * + * returns an array of all breadcrumbs + * where array-key of each record is category[id] + * + * NOTE: + * --- + * Cms_model::all_breadcrumbs returns an indexed super-array; + * each array item includes a banch of information: [ + * breadcrumb, + * rec: [ id , title ], + * parents: [ [id, title] , ... ] + * childs: [ [id, title] , ... ], + * level + * ] + * + * Use Cases: + * --- + * as a super-array, the output can be used in many cases + * for example... + * into form elements + * .. while selecting category for a post + * .. or editing a category + * or directry referring to category's parents/childs + * + * Arguments: + * --- + * @param $tree (array) : category tree (with childs and parents parts) + * @param $detimiter (string, optional) : string to split breadcrumb's path-nodes + * @param $exception (int, optional) : id of category to exclude (subcategories shall be excluded too) + * @param $l (int, not-pass) : depth level of the node; DO NOT SET (takes values automaticaly) + * @return array of breadcrumbs + * ------------------------------------------------------------------------- + */ + static public function breadcrumbs($tree, $delimiter = " / ", $exception = 0, $l = 0) + { + $all = []; // results array + + foreach($tree as $node) { // loop through all nodes + + if (intval($node['rec']['id']) != $exception) { // if node is not exception + + // construct breadcrumb html of node + // --- -- -- - - - + $breadcrumb = ""; + foreach($node['parents'] as $par) { // first: join path titles + $breadcrumb .= $par['label'] . $delimiter; + } + $breadcrumb .= $node['rec']['label']; // last: append title + + // make a new super record + // --- -- -- - - - + $all[$node['rec']['id']] = [ // set record is as key + 'breadcrumb' => $breadcrumb, // add breadcrump to results + 'rec' => $node['rec'], // + node info + 'parents' => $node['parents'], // + parents array + 'childs' => self::first_level_childs($node), // + direct childs + 'level' => $l // + level + ]; + + // recursively traverse children nodes + // --- -- -- - - - + if (isset($node['childs']) && $node['childs'] != []) { + $child_breadcrumbs = self::breadcrumbs( + $node['childs'], + $delimiter, + $exception, + $l+1 + ); + + $all = $all + $child_breadcrumbs; // concatenate arrays (keep array-keys) + } + } + + } + return $all; + + } + + /** first_level_childs + * --- -- -- - - - + * used by all_breadcrumbs() + */ + static private function first_level_childs($node) + { + $childs = []; + if ($node['childs'] == []) { + return []; + } + foreach($node['childs'] as $key => $kid) { + $childs[] = [ + 'id' => $kid['rec']['id'], + 'label' => $kid['rec']['label'] + ]; + } + return $childs; + } + + + + + /** LESSONS + * ------------------------------------------------------------------------- + */ + + + /** lessons of course + * + * @param $id (int) : course_id + */ + public static function lessons_of_course($id) + { + // TODO: order results in some way + + return Registry::use('database')->runQuery( + "SELECT lesson.*, lesson_privilege.privilege_id FROM lesson + LEFT JOIN lesson_privilege ON lesson_privilege.lesson_id = lesson.id + WHERE lesson.status = 1 AND lesson.course_id = :id + ORDER BY lesson_privilege.privilege_id ASC", + [':id' => $id] + ); + } + + /** lesson + * + * @param $id (int) : lesson id + */ + public static function lesson($id) + { + // TODO: order results in some way + + return Registry::use('database')->query( + "SELECT lesson.*, lesson_privilege.privilege_id FROM lesson + LEFT JOIN lesson_privilege ON lesson_privilege.lesson_id = lesson.id + WHERE lesson.status = 1 AND lesson.id = :id", + [':id' => $id] + )->getFirst(); + } + + + + /** files + * return all files + * @param void + * @return array + */ + public static function files() + { + return Registry::use('database')->runQuery("SELECT * from media", []); + } + + + /** pages + * + * return all pages as array; + * array key of each item shall be the page-id + * + * @param void + * @return array + */ + public static function pages() + { + $result = []; + $pages = Registry::use('database')->runQuery("SELECT * FROM page", []); + foreach($pages as $key => $page) { + $result[ $page['id'] ] = [ + 'title' => $page['title'], + 'body' => $page['body'], + 'status' => $page['status'] + ]; + } + // print_r($result); die(); + return $result; + } + +} diff --git a/public/app/models/_info.md b/public/app/models/_info.md index 82143bf..ac6d334 100644 --- a/public/app/models/_info.md +++ b/public/app/models/_info.md @@ -150,3 +150,24 @@ $myobject = new myclass(); call_user_func(array($myobject, 'say_hello')); ?> + + + + +# APPlication + +\ controllers + [ ] App + [ ] Auth + +\ extends + [ ] Cache_service + [*] App_manager + [*] App_user + [ ] SendMail_service + [ ] Form_builder + + +\ models + [ ] Access_model + [ ] History_model \ No newline at end of file -- cgit v1.2.3