1<?php
2
3use \dokuwiki\plugin\bez;
4
5class action_plugin_bez_base extends DokuWiki_Action_Plugin {
6
7    /** @var  bez\mdl\Model */
8    protected $model;
9
10    /** @var  bez\meta\Tpl */
11    protected $tpl;
12
13    public function getPluginName() {
14        return 'bez';
15    }
16
17    public function register(Doku_Event_Handler $controller) {
18    }
19
20    public function getGlobalConf($key='') {
21        global $conf;
22        if ($key == '') {
23            return $conf;
24        }
25        return $conf[$key];
26    }
27
28    public function get_model() {
29        return $this->model;
30    }
31
32    public function get_client() {
33        global $INFO;
34        return $INFO['client'];
35    }
36
37    public function get_tpl() {
38        return $this->tpl;
39    }
40
41    public function get_level() {
42        return $this->model->get_level();
43    }
44
45    public function bez_tpl_include($tpl_file='', $return=false) {
46        $file = DOKU_PLUGIN . "bez/tpl/$tpl_file.php";
47        if (!file_exists($file)) {
48            throw new Exception("$file doesn't exist");
49        }
50
51        $tpl = $this->tpl;
52        if ($return) ob_start();
53        include $file;
54        if ($return) return ob_get_clean();
55
56    }
57
58    public function createObjects($skip_acl=false) {
59        global $auth;
60        global $INFO;
61
62        if ($skip_acl) {
63            $client = false;
64        } else {
65            $client = $INFO['client'];
66        }
67
68        $this->model = new bez\mdl\Model($auth, $client, $this, $skip_acl);
69        $this->tpl = new bez\meta\Tpl($this);
70    }
71
72    public function id() {
73        $args = func_get_args();
74
75        if (count($args) === 0) {
76            return $_GET['id'];
77        }
78
79        $elms = array();
80        foreach ($args as $arg) {
81            if (is_array($arg)) {
82                foreach ($arg as $k => $v) {
83                    //replace special chars
84                    list($k, $v) = str_replace(array(':', '#'), '', array($k, $v));
85                    //don't create id with empty value
86                    if (empty($k) || empty($v)) {
87                        continue;
88                    }
89                    $elms[] = $k;
90                    $elms[] = $v;
91                }
92            } else {
93                $elms[] = $arg;
94            }
95        }
96        //add bez as a key for controller
97        array_unshift($elms, 'bez');
98
99        //make an $elms key->value pair
100        $params = [];
101        for ($i = 0; $i < count($elms); $i += 2) {
102            $key = $elms[$i];
103            $value = isset($elms[$i+1]) ? $elms[$i+1] : '';
104            $params[$key] = $value;
105        }
106        $params = array_filter($params);
107        $elms = [];
108        foreach ($params as $k => $v) {
109            $elms[] = $k;
110            $elms[] = $v;
111        }
112
113
114
115
116        //pl is default language
117        if ($this->getGlobalConf('lang') != '' && $this->getGlobalConf('lang') != 'pl') {
118            array_unshift($elms, $this->getGlobalConf('lang'));
119        }
120
121        return implode(':', $elms);
122    }
123
124    public function url() {
125        global $conf;
126
127        $args = func_get_args();
128        if (count($args) > 0) {
129            if (isset($args[count($args)-1]['GET'])) {
130                $get = array_pop($args)['GET'];
131                $get = http_build_query($get);
132            }
133            $id = call_user_func_array(array($this, 'id'), $args);
134
135            if ($conf['userewrite'] == '1') {
136                if ($get) $get = "?$get";
137                return DOKU_URL . $id. $get;
138            } elseif ($conf['userewrite'] == '2') {
139                if ($get) $get = "?$get";
140                return DOKU_URL . 'doku.php/' . $id . $get;
141            } else {
142                if ($get) $get = "&$get";
143                return DOKU_URL . 'doku.php?id=' . $id . $get;
144            }
145
146        }
147    }
148}
149