summaryrefslogtreecommitdiff
path: root/localwebsite/handlers/RequestHandler.php
blob: 136a23e4e80eacb5916ab34db98f8babd426be75 (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
<?php

class RequestHandler extends request_handler {

    /** @var web_tpl*/
    protected $tpl;

    public function __construct() {
        global $__tpl;
        $__tpl = new web_tpl();
        $this->tpl = $__tpl;

        $this->tpl->add_static('bootstrap.min.css');
        $this->tpl->add_static('bootstrap.min.js');
        $this->tpl->add_static('polyfills.js');
        $this->tpl->add_static('app.js');
        $this->tpl->add_static('app.css');

        if (auth::id()) {
            $this->tpl->set_global([
                'auth_user' => auth::$authorizedUser
            ]);
        }
    }

    public function dispatch(string $act) {
        global $config;
        $this->tpl->set_global([
            '__dev' => $config['is_dev'],
        ]);
        return parent::dispatch($act);
    }

    protected function method_not_found(string $method, string $act)
    {
        global $config;

        if ($act != '404' && $config['is_dev'])
            debugError(get_called_class() . ": act {$method}_{$act} not found.");

        if (!is_xhr_request())
            $this->tpl->render_not_found();
        else
            ajax_error('unknown act "'.$act.'"', 404);

    }

    protected function before_dispatch(string $method, string $act) {
        if (config::get('auth_need') && !auth::id())
            redirect('/auth/');
    }
}