From b52e052bd3741c1ddf1bdaa43079ba5a9eafc8bd Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Thu, 27 Apr 2023 18:16:44 +0300 Subject: setting blueprint in action --- public/app/models/Cms_model.php | 314 +++++++++++++++++++++++++++++++++ public/app/models/User_model.php | 311 ++++++++++++++++++++++++++++++++ public/app/models/admin/User_model.php | 299 ------------------------------- public/app/models/cms/Course_model.php | 314 --------------------------------- public/app/models/cms/Media_model.php | 36 ---- public/app/models/cms/Page_model.php | 18 -- public/app/models/ideas.md | 24 +++ public/app/models/todo.md | 24 +-- 8 files changed, 662 insertions(+), 678 deletions(-) create mode 100644 public/app/models/Cms_model.php create mode 100644 public/app/models/User_model.php delete mode 100644 public/app/models/admin/User_model.php delete mode 100644 public/app/models/cms/Course_model.php delete mode 100644 public/app/models/cms/Media_model.php delete mode 100644 public/app/models/cms/Page_model.php create mode 100644 public/app/models/ideas.md (limited to 'public/app/models') diff --git a/public/app/models/Cms_model.php b/public/app/models/Cms_model.php new file mode 100644 index 0000000..fad11b8 --- /dev/null +++ b/public/app/models/Cms_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/User_model.php b/public/app/models/User_model.php new file mode 100644 index 0000000..f799dd9 --- /dev/null +++ b/public/app/models/User_model.php @@ -0,0 +1,311 @@ +query( + "SELECT * FROM user WHERE email = :email AND active = 1", + [ ':email' => $email ] + )->getFirst(); + + // if no user, return false + if ($user === false) return false; + + return $user; + } + + + /** get user (by index key) + * + * user detailed array + * includes all user properties + granted roles + privileges + * + * @param $id (int) : user id + * @param $value (string) + */ + public static function getUser($id) + { + $user = Registry::use('database')->query( + "SELECT user.*, + ( -- construct array (json) of roles granted to user + SELECT CONCAT( + '[', + GROUP_CONCAT(role.id), + ']' + ) + FROM `role` + WHERE role.id IN ( + SELECT user_role.role_id + FROM user_role + WHERE user_role.user_id = :id + ) + ) AS Roles_json, + ( -- construct array of (root-)privileges granted to user + SELECT CONCAT( + '[', + GROUP_CONCAT(privilege.id), + ']' + ) + FROM privilege + WHERE privilege.id IN ( + SELECT user_privilege.privilege_id + FROM user_privilege + WHERE user_privilege.user_id = :id + ) + ) AS RootPrivileges_json, + ( -- construct array of (root-)privileges granted to user + SELECT CONCAT( + '[', + GROUP_CONCAT(privilege.includes), + ']' + ) + FROM privilege + WHERE privilege.id IN ( + SELECT user_privilege.privilege_id + FROM user_privilege + WHERE user_privilege.user_id = :id + ) + ) AS SubPrivileges_json + FROM user + WHERE id = :id", + [ ':id' => $id ] + )->getFirst(); + + + // if no user, return false + if ($user === false) return false; + + + // TODO: + // * merge root+sub privilede lists + // * convert json strings to php arrays + + + // TODO: + // cache user super array + + return $user; + } + + + /** create user + * + * creates user record; + * assigns privileged (usualy defaults); + * creates activation_code + * + * @param $data (array): Request->POST array + * @param $password (string): secure hashed password + * + * @return $activation_code + * + */ + public static function registerUser($data, $password, $privileges = DEFAULT_PRIVILEGES) + { + $required_fields = [ + 'name', + 'surname', + 'email', + 'password' + ]; + + // check required fields + $isOK = true; + foreach($required_fields as $fi) { + if (empty($data[$fi])) $isOK = false; + } + // if empty required fields exists ... return false + if (!$isOK) { + return [ "success" => false, 'error' => EMPTY_REQUIRED_FIELDS ]; + } + + + // TODO: + // check if email exists + // ... + + // create an activation code + $activation_code = md5($data['email'].time().rand(0, 10000)); + + // if isOK go on and... + // create user record + $new_user_id = Registry::use('database')->query( + "INSERT INTO user + (`first_name`, `last_name`, `email`, `password`, `active`, `activation`) + VALUES + (:nam, :surname, :email, :pass, :act, :actcode)", + [ + ':nam' => $data['name'], + ':surname' => $data['surname'], + ':email' => $data['email'], + ':pass' => $password, + ':act' => 0, // needs email confirmation to be activated ... + ':actcode' => $activation_code // ... with the activation code + ] + )->lastInsertID(); + + // set default privileges + self::set_user_privileges($new_user_id, $privileges); + + // set reader role + self::set_user_role($new_user_id, 5); + + // update history + History_model::trackUserAccess($new_user_id, TRACK_ACCOUNT, 'Create User Account'); + + // return success and user id + return [ + "success" => true, + 'id' => $new_user_id , + 'activation' => $activation_code + ]; + } + + + + ## Set permission methods + ## ------------------------------------------------------------------------- + + + /** set_user_privileges + * + * @param $privileges (array) + */ + public static function set_user_privileges($user, $privileges) + { + $db = Registry::use('database'); // database connection + foreach($privileges as $pri) { // pri = privilege id + $db->runQuery( + "INSERT INTO user_privilege (user_id, privilege_id) VALUES (:user, :pri)", + [ ':user' => $user, ":pri" => $pri ] + ); + } + return true; + } + + + /** set_user_role + * + * user, role are (int) IDs + */ + public static function set_user_role($user, $role) + { + + Registry::use('database')->runQuery( + "INSERT INTO user_role (user_id, role_id) VALUES (:user, :role)", + [ ':user' => $user, ":role" => $role ] + ); + + return true; + } + + + /** activate + * + * check if activation code is valid; + * if valid, set account active; + * + * @param $ticket (hex/MD5): activation code; + * + */ + public static function activate($ticket) + { + $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 user + SET active = 1, `activation` = NULL + WHERE activation = :ticket", + [ 'ticket' => $ticket ] + ); + + // update history + History_model::trackUserAccess($user['id'], TRACK_ACCOUNT, 'User Account Activated'); + + return true; + + } + + + ## documention methods + ## methods related to properties that determin user's access + ##-------------------------------------------------------------------------- + +} + +/* example query getUser (super-array) +--- -- -- - - - + +SELECT user.*, +( -- array (json) of roles granted to user + SELECT CONCAT('[', GROUP_CONCAT(role.id), ']') + FROM `role` + WHERE role.id IN ( + SELECT user_role.role_id + FROM user_role + WHERE user_role.user_id = 1 + ) +) AS Roles_json, +( + SELECT CONCAT( + '[', + GROUP_CONCAT(privilege.id), + ']' + ) + FROM privilege + WHERE privilege.id IN ( + SELECT user_privilege.privilege_id + FROM user_privilege + WHERE user_privilege.user_id = 1 + ) +) AS RootPrivileges_json, +( + SELECT CONCAT( -- array of array of sub-privileges + '[', + GROUP_CONCAT( -- array (json) of subprivileges + ( + SELECT CONCAT( + '[', + GROUP_CONCAT(included_id), + ']' + ) + FROM privilege_includes + WHERE privilege_id = privilege.id + ) + ), + ']' + ) + FROM privilege + WHERE privilege.id IN ( + SELECT user_privilege.privilege_id + FROM user_privilege + WHERE user_id = 1 + ) +) AS SubPrivileges_json +FROM user +WHERE id = 1 +--- */ \ No newline at end of file diff --git a/public/app/models/admin/User_model.php b/public/app/models/admin/User_model.php deleted file mode 100644 index 2885dc1..0000000 --- a/public/app/models/admin/User_model.php +++ /dev/null @@ -1,299 +0,0 @@ -query( - "SELECT * FROM user WHERE email = :email AND active = 1", - [ ':email' => $email ] - )->getFirst(); - - // if no user, return false - if ($user === false) return false; - - return $user; - } - - - /** get user (by index key) - * - * user detailed array - * includes all user properties + granted roles + privileges - * - * @param $id (int) : user id - * @param $value (string) - */ - public static function getUser($id) - { - $user = Registry::use('database')->query( - "SELECT user.*, - ( -- construct array (json) of roles granted to user - SELECT CONCAT( - '[', - GROUP_CONCAT(role.id), - ']' - ) - FROM `role` - WHERE role.id IN ( - SELECT user_role.role_id - FROM user_role - WHERE user_role.user_id = :id - ) - ) AS Roles_json, - ( -- construct array of (root-)privileges granted to user - SELECT CONCAT( - '[', - GROUP_CONCAT(privilege.id), - ']' - ) - FROM privilege - WHERE privilege.id IN ( - SELECT user_privilege.privilege_id - FROM user_privilege - WHERE user_privilege.user_id = :id - ) - ) AS RootPrivileges_json, - ( -- construct array of (root-)privileges granted to user - SELECT CONCAT( - '[', - GROUP_CONCAT(privilege.includes), - ']' - ) - FROM privilege - WHERE privilege.id IN ( - SELECT user_privilege.privilege_id - FROM user_privilege - WHERE user_privilege.user_id = :id - ) - ) AS SubPrivileges_json - FROM user - WHERE id = :id", - [ ':id' => $id ] - )->getFirst(); - - - // if no user, return false - if ($user === false) return false; - - - // TODO: - // * merge root+sub privilede lists - // * convert json strings to php arrays - - - // TODO: - // cache user super array - - return $user; - } - - - /** create user - * - * creates user record; - * assigns privileged (usualy defaults); - * creates activation_code - * - * @param $data (array): Request->POST array - * @param $password (string): secure hashed password - * - * @return $activation_code - * - */ - public static function registerUser($data, $password, $privileges = DEFAULT_PRIVILEGES) - { - $required_fields = [ - 'name', - 'surname', - 'email', - 'password' - ]; - - // check required fields - $isOK = true; - foreach($required_fields as $fi) { - if (empty($data[$fi])) $isOK = false; - } - // if empty required fields exists ... return false - if (!$isOK) { - return [ "success" => false, 'error' => EMPTY_REQUIRED_FIELDS ]; - } - - - // TODO: - // check if email exists - // ... - - // create an activation code - $activation_code = md5($data['email'].time().rand(0, 10000)); - - // if isOK go on and... - // create user record - $new_user_id = Registry::use('database')->query( - "INSERT INTO user - (`first_name`, `last_name`, `email`, `password`, `active`, `activation`) - VALUES - (:nam, :surname, :email, :pass, :act, :actcode)", - [ - ':nam' => $data['name'], - ':surname' => $data['surname'], - ':email' => $data['email'], - ':pass' => $password, - ':act' => 0, // needs email confirmation to be activated ... - ':actcode' => $activation_code // ... with the activation code - ] - )->lastInsertID(); - - // set default privileges - self::set_user_privileges($new_user_id, $privileges); - - // set reader role - self::set_user_role($new_user_id, 5); - - // update history - History::trackUserAccess($new_user_id, TRACK_ACCOUNT, 'Create User Account'); - - // return success and user id - return [ - "success" => true, - 'id' => $new_user_id , - 'activation' => $activation_code - ]; - } - - - /** set_user_privileges - * - * @param $privileges (array) - */ - public static function set_user_privileges($user, $privileges) - { - $db = Registry::use('database'); // database connection - foreach($privileges as $pri) { // pri = privilege id - $db->runQuery( - "INSERT INTO user_privilege (user_id, privilege_id) VALUES (:user, :pri)", - [ ':user' => $user, ":pri" => $pri ] - ); - } - return true; - } - - - /** set_user_role - * - * user, role are (int) IDs - */ - public static function set_user_role($user, $role) - { - - Registry::use('database')->runQuery( - "INSERT INTO user_role (user_id, role_id) VALUES (:user, :role)", - [ ':user' => $user, ":role" => $role ] - ); - - return true; - } - - - /** activate - * - * check if activation code is valid; - * if valid, set account active; - * - * @param $ticket (hex/MD5): activation code; - * - */ - public static function activate($ticket) - { - $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 user - SET active = 1, `activation` = NULL - WHERE activation = :ticket", - [ 'ticket' => $ticket ] - ); - - // update history - History::trackUserAccess($user['id'], TRACK_ACCOUNT, 'User Account Activated'); - - return true; - - } - - -} - -/* example query getUser (super-array) ---- -- -- - - - - -SELECT user.*, -( -- array (json) of roles granted to user - SELECT CONCAT('[', GROUP_CONCAT(role.id), ']') - FROM `role` - WHERE role.id IN ( - SELECT user_role.role_id - FROM user_role - WHERE user_role.user_id = 1 - ) -) AS Roles_json, -( - SELECT CONCAT( - '[', - GROUP_CONCAT(privilege.id), - ']' - ) - FROM privilege - WHERE privilege.id IN ( - SELECT user_privilege.privilege_id - FROM user_privilege - WHERE user_privilege.user_id = 1 - ) -) AS RootPrivileges_json, -( - SELECT CONCAT( -- array of array of sub-privileges - '[', - GROUP_CONCAT( -- array (json) of subprivileges - ( - SELECT CONCAT( - '[', - GROUP_CONCAT(included_id), - ']' - ) - FROM privilege_includes - WHERE privilege_id = privilege.id - ) - ), - ']' - ) - FROM privilege - WHERE privilege.id IN ( - SELECT user_privilege.privilege_id - FROM user_privilege - WHERE user_id = 1 - ) -) AS SubPrivileges_json -FROM user -WHERE id = 1 ---- */ \ No newline at end of file diff --git a/public/app/models/cms/Course_model.php b/public/app/models/cms/Course_model.php deleted file mode 100644 index 6614993..0000000 --- a/public/app/models/cms/Course_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: - * --- - * Course_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/cms/Media_model.php b/public/app/models/cms/Media_model.php deleted file mode 100644 index 6a4a681..0000000 --- a/public/app/models/cms/Media_model.php +++ /dev/null @@ -1,36 +0,0 @@ -, rights =>, path => ] - - // validate ticket - // ValidateAccess::for($ticket) - - // serve - // header("Content-type: type"); - // passthru('cat $media_path'); - - - return Registry::use('database')->query( - "SELECT * FROM pages - WHERE FullFriendlyUrl = :url AND IsActive = 1", - [ ':url' => $url ] - )->getFirst(); - } - -} - - - -// check: -// https://stackoverflow.com/questions/1353850/serve-image-with-php-script-vs-direct-loading-an-image \ No newline at end of file diff --git a/public/app/models/cms/Page_model.php b/public/app/models/cms/Page_model.php deleted file mode 100644 index fda5fb1..0000000 --- a/public/app/models/cms/Page_model.php +++ /dev/null @@ -1,18 +0,0 @@ -query( - "SELECT * FROM pages - WHERE FullFriendlyUrl = :url AND IsActive = 1", - [ ':url' => $url ] - )->getFirst(); - } - -} \ No newline at end of file diff --git a/public/app/models/ideas.md b/public/app/models/ideas.md new file mode 100644 index 0000000..300612d --- /dev/null +++ b/public/app/models/ideas.md @@ -0,0 +1,24 @@ +# interesting projects + +projects for brainstorming + +## php ORM projects + +* [riverside\php-orm](https://github.com/riverside/php-orm) seems the lightest and most promissing + +* https://github.com/mareimorsy/DB + +* https://propelorm.org/ + +* https://redbeanphp.com/index.php?p=/download + +* you can find more in a [related github topic](https://github.com/topics/php-orm) + + +Thoughts: +create a DatabaseModel class (??) based on the riverside\php-orm then use it in anom framerok + + +## php micro-framework + +* [riverside\php-express](https://github.com/riverside/php-express) seems quite interesting diff --git a/public/app/models/todo.md b/public/app/models/todo.md index 89ea113..5a18f38 100644 --- a/public/app/models/todo.md +++ b/public/app/models/todo.md @@ -3,36 +3,38 @@ Rearange -- #1 -- -app\controllers\cms\Course -> [*] app\controllers\Course +[ok] app\controllers\cms\Course -> [*] app\controllers\Course -app\controllers\admin\Users_admin -> [delete] +[ok] app\controllers\admin\Users_admin -> [delete] -app\controllers\Admin -> [delete] +[ok] app\controllers\Admin -> [delete] -app\controllers\admin\Course_admin -> [*] app\controllers\Course_admin +[ok] app\controllers\admin\Course_admin -> [*] app\controllers\Course_admin -app\models\admin\User_model -> [*] app\models\User_model +[ok] app\models\admin\User_model -> [*] app\models\User_model app\models\admin\Privilege_model -> [merge_to] -> [**] app\models\User_model app\models\admin\History_model -> [*] app\models\History_model -app\models\cms\Page_model -> [delete] +[ok] app\models\cms\Page_model -> [delete] -app\models\cms\Media_model -> [delete] +[ok] app\models\cms\Media_model -> [delete] -app\models\cms\Course_model -> [*] app\models\Course_model +[ok] app\models\cms\Course_model -> [*] app\models\Course_model [*] = move -- #2 -- -app\controllers\Course -> [r] app\controllers\Cms +[ok] app\controllers\Course -> [rename] app\controllers\Cms + +[ok] app\controllers\Course_admin -> [rename] app\controllers\CmsAdmin + +[ok] app\models\Course_model -> [rename] app\models\Cms_model -app\controllers\Course_admin -> [r] app\controllers\Cms_admin -app\models\Course_model -> app\models\Cms_model -- cgit v1.2.3