summaryrefslogtreecommitdiff
path: root/public/app/controllers/admin/Course_admin.php
blob: 0e34e008d716963871a0e0d9f295b69f20647b08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
        ];
    }

}