aboutsummaryrefslogtreecommitdiff
path: root/app/class/controller.php
blob: 1c3fee584eff5561ee4e5454e95f6130c5ff7fad (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php

class Controller
{

    protected $user;
    protected $router;
    protected $usermanager;
    protected $plates;

	public function __construct($router) {
        $this->setuser();
        $this->router = $router;        
        $this->initplates();
	}

    public function setuser()
    {
        $this->usermanager = new Modeluser;        
        $this->user = $this->usermanager->readsession();
    }

    public function initplates()
    {
        $router = $this->router;
        $this->plates = new League\Plates\Engine(Model::TEMPLATES_DIR);
        $this->plates->registerFunction('url', function (string $string, array $vars = []) use ($router) {
            return $router->generate($string, $vars);
        });
        $this->plates->registerFunction('uart', function (string $string, string $id) use ($router) {
            return $router->generate($string, ['art' => $id]);
        });
    }
    
    public function useriseditor()
    {
        if ($this->user->level() >= $this->usermanager::EDITOR) {
            echo '<h3>Editor access</h3>';
            return true;
        } else {
            echo '<h3>Not enought rights to see more...</h3>';
            return false;
        }
    }


    public function showtemplate($template, $params)
    {
        $params = array_merge($this->commonsparams(), $params);
        echo $this->plates->render($template, $params);
    }

    public function commonsparams()
    {
        $commonsparams = [];
        $commonsparams['router'] = $this->router;
        $commonsparams['user'] = $this->user;
        $commonsparams['css'] = Model::csspath();
        return $commonsparams;
    }

    public function login($redirect = 'home')
    {
        if(isset($_POST['pass'])) {
            $this->user = $this->usermanager->login($_POST['pass']);
            $this->usermanager->writesession($this->user);
        }
        if($redirect == 'art') {
            $this->redirect('?id=' . $this->art->id());
        } else {
            $this->redirect('?aff=' . $redirect);
        }
    }

    public function logout($redirect = 'home')
    {
        $this->user = $this->usermanager->logout();
        $this->usermanager->writesession($this->user);
        if($redirect == 'art') {
            $this->redirect('?id=' . $this->art->id());
        } else {
            $this->redirect('?aff=' . $redirect);
        }
    }




    public function redirect($url)
    {
        header('Location: ' . $url);
    }

    public function routedirect(string $route, array $vars = [])
    {
        $this->redirect($this->router->generate($route, $vars));
    }



}





?>