diff options
Diffstat (limited to 'public/app/controllers/admin/Course_admin.php')
| -rw-r--r-- | public/app/controllers/admin/Course_admin.php | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/public/app/controllers/admin/Course_admin.php b/public/app/controllers/admin/Course_admin.php new file mode 100644 index 0000000..0e34e00 --- /dev/null +++ b/public/app/controllers/admin/Course_admin.php @@ -0,0 +1,93 @@ +<?php + +namespace app\controllers\admin; + +use Registry; +use Render; +use app\models\cms\Course_model; + +class Course_admin { + + + /** breadcrumbs + * --- + * responds to ajax GET: /admin/api/categories + */ + public static function breadcrumbs() + { + $tree = proxy( // cache-get categories + [\app\models\cms\Course_model::class, 'category_tree'], + [], CACHE_ROOT_TTL + ); + $breadcrumbs = proxy( // cache-get breadcrumbs + [\app\models\cms\Course_model::class, 'breadcrumbs'], + [$tree], CACHE_ROOT_TTL + ); + Render::json($breadcrumbs); // send as json + } + + /** add course + * --- + * @param void (get data from POST) + */ + public static function add_course() + { + // insert new category; id = new category id + $id = Registry::use('database')->query( + "INSERT INTO course (parent_id, label) VALUES (:parent, :label)", + [ + ':parent' => Registry::get('REQUEST')->POST['parent_id'], + ':label' => Registry::get('REQUEST')->POST['label'] + ] + )->lastInsertID(); + + $cache = self::update_courses_cache(); // update category caches + return $cache['breadcrumbs']; // return + } + + + /** update course + * --- + * @param void (get data from POST) + */ + public static function update_course() + { + Registry::use('database')->query( + "UPDATE course SET parent_id = :parent, label = :label + WHERE id = :id", + [ + ':id' => Registry::get('REQUEST')->POST['id'], + ':parent' => Registry::get('REQUEST')->POST['parent_id'], + ':label' => Registry::get('REQUEST')->POST['label'] + ] + ); + $cache = self::update_courses_cache(); + return $cache['breadcrumbs']; + } + + + /** update courses cache + * --- -- -- - - - + * Shall run if anything changes to courses + * + * @param void + * @return (array): the two course caches + */ + public static function update_courses_cache() + { + $tree = proxy( // cache-get categories + [\app\models\cms\Course_model::class, 'category_tree'], + [], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE + ); + $breadcrumbs = proxy( // cache-get breadcrumbs + [\app\models\cms\Course_model::class, 'breadcrumbs'], + [$tree], CACHE_ROOT_TTL, PROXY_IGNORE_CACHE + ); + + return [ + 'tree' => $tree, + 'breadcrumbs' => $breadcrumbs + ]; + } + +}
\ No newline at end of file |
