xref: /plugin/bez/action/base.php (revision 8968937cbcd0fe0f263f3cb783d6771d8c2cbaea)
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        array_unshift($elms, 'bez');
97
98
99        //pl is default language
100        if ($this->getGlobalConf('lang') != '' && $this->getGlobalConf('lang') != 'pl') {
101            array_unshift($elms, $this->getGlobalConf('lang'));
102        }
103
104//        $elms = array_map(function ($elm) {
105//            return str_replace(array(':', '#'), '', $elm);
106//        }, $elms);
107        return implode(':', $elms);
108    }
109
110    public function url() {
111        global $conf;
112
113        $args = func_get_args();
114        if (count($args) > 0) {
115            $get_parts = array_filter($args, function ($a) {
116                return is_array($a) && is_array($a['GET']);
117            });
118            $id_parts = array_diff($args, $get_parts);
119            $id = call_user_func_array(array($this, 'id'), $id_parts);
120
121            $get = '';
122            if ($get_parts) {
123                $get = $get_parts[1]['GET'];
124                $get = http_build_query($get);
125            }
126
127            if ($conf['userewrite'] == '1') {
128                if ($get) $get = "?$get";
129                return DOKU_URL . $id. $get;
130            } elseif ($conf['userewrite'] == '2') {
131                if ($get) $get = "?$get";
132                return DOKU_URL . 'doku.php/' . $id . $get;
133            } else {
134                if ($get) $get = "&$get";
135                return DOKU_URL . 'doku.php?id=' . $id . $get;
136            }
137
138        }
139    }
140}