xref: /plugin/bez/action/default.php (revision addc91ee3302cfabdbe6588b98874f0b355a06f3)
1<?php
2
3use \dokuwiki\plugin\bez;
4
5if(!defined('DOKU_INC')) die();
6
7function bez_tpl_include(bez\meta\Tpl $tpl) {
8    $file = DOKU_PLUGIN."bez/tpl/".str_replace('/', '', $tpl->action()).".php";
9    if (file_exists($file)) {
10        include $file;
11    }
12}
13
14define('BEZ_NOTIFICATIONS_COOKIE_NAME', 'bez_notifications');
15
16class action_plugin_bez_default extends DokuWiki_Action_Plugin {
17
18	protected $action = '';
19    protected $params = array();
20
21    /** @var  bez\mdl\Model */
22    protected $model;
23
24    /** @var  bez\meta\Tpl */
25    protected $tpl;
26
27    protected $notifications = array();
28
29    protected $errors = array();
30
31    public function get_action() {
32        return $this->action;
33    }
34
35    public function get_param($id, $default='') {
36        return (isset($this->params[$id]) ? $this->params[$id] : $default);
37    }
38
39    public static function id() {
40        global $conf;
41
42		$args = func_get_args();
43
44        if (count($args) === 0) {
45            return $_GET['id'];
46        }
47
48        $elms = array();
49        foreach ($args as $arg) {
50            if (is_array($arg)) {
51                foreach ($arg as $k => $v) {
52                    $elms[] = $k;
53                    $elms[] = $v;
54                }
55            } else {
56                $elms[] = $arg;
57            }
58        }
59		array_unshift($elms, 'bez');
60
61
62		if ($conf['lang'] != '') {
63			array_unshift($elms, $conf['lang']);
64        }
65
66		return implode(':', $elms);
67    }
68
69    public static function url() {
70        $args = func_get_args();
71        if (count($args) > 0) {
72            $id = call_user_func_array('action_plugin_bez_default::id', $args);
73            return DOKU_URL . 'doku.php?id=' . $id;
74        } else {
75            //https://stackoverflow.com/questions/6768793/get-the-full-url-in-php
76            return (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
77        }
78
79
80    }
81
82    /**
83     * @return mixed
84     */
85    public function get_model() {
86        return $this->model;
87    }
88
89    private function add_notification($value, $header=NULL) {
90        if (isset($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME])) {
91            $notifs = unserialize($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME]);
92        } else {
93            $notifs = array();
94        }
95        $notifs[] = array('value' => $value, 'header' => $header);
96        setcookie(BEZ_NOTIFICATIONS_COOKIE_NAME, serialize($notifs));
97    }
98
99    private function flush_notifications() {
100        if (!isset($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME])) {
101            return array();
102        }
103        $this->notifications = unserialize($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME]);
104
105        //remove cookie
106        setcookie(BEZ_NOTIFICATIONS_COOKIE_NAME, serialize(array()));
107    }
108
109    private function add_error($value, $header=NULL) {
110        $this->errors[] = array('value' => $value, 'header' => $header);
111    }
112
113	/**
114	 * Register its handlers with the DokuWiki's event controller
115	 */
116	public function register(Doku_Event_Handler $controller)
117	{
118        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'setup_enviroment');
119		$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'action_act_preprocess');
120		$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'tpl_act_render');
121		$controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'tpl_pagetools_display');
122		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'include_dependencies', array());
123		$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call');
124	}
125
126	public function include_dependencies(Doku_Event $event) {
127		// Adding a stylesheet
128		$event->data["link"][] = array (
129		  "type" => "text/css",
130		  "rel" => "stylesheet",
131		  "href" => DOKU_BASE.
132			"lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.css",
133		);
134
135		// Adding a JavaScript File
136		$event->data["script"][] = array (
137		  "type" => "text/javascript",
138		  "src" => DOKU_BASE.
139			"lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.min.js",
140		  "_data" => "",
141		);
142
143		// Adding a JavaScript File
144		$event->data["script"][] = array (
145		  "type" => "text/javascript",
146		  "src" => DOKU_BASE.
147			"lib/plugins/bez/lib/jquery.datepair/datepair.js",
148		  "_data" => "",
149		);
150
151		// Adding a JavaScript File
152		$event->data["script"][] = array (
153		  "type" => "text/javascript",
154		  "src" => DOKU_BASE.
155			"lib/plugins/bez/lib/jquery.datepair/jquery.datepair.js",
156		  "_data" => "",
157		);
158
159		$event->data["link"][] = array (
160		  "type" => "text/css",
161		  "rel" => "stylesheet",
162		  "href" => DOKU_BASE.
163			"lib/plugins/bez/lib/jquery.form-validator/theme-default.min.css",
164		);
165
166
167		$event->data["script"][] = array (
168		  "type" => "text/javascript",
169		  "src" => DOKU_BASE.
170			"lib/plugins/bez/lib/jquery.form-validator/jquery.form-validator.min.js",
171		  "_data" => "",
172		);
173	}
174
175    public function setup_enviroment(Doku_Event $event, $param) {
176        global $ACT, $auth, $conf, $INFO;
177
178        if ($ACT !== 'show') {
179            return;
180        }
181
182        $id = $_GET['id'];
183		$ex = explode(':', $id);
184
185        //check if we process BEZ
186        if ($ex[0] !== 'bez' && $ex[1] !== 'bez') {
187            return;
188        }
189
190
191        if ($ex[1] === 'bez') {
192            $conf['lang'] = array_shift($ex);
193            //$this->lang_code = $conf['lang'];
194            $this->localised = false;
195        }
196        //throw out "bez"
197        array_shift($ex);
198
199        $this->action = array_shift($ex);
200
201        if (count($ex) % 2 !== 0) {
202            throw new Exception('invalid params');
203        }
204
205        for ($i = 0; $i < count($ex); $i += 2) {
206            $this->params[$ex[$i]] = $ex[$i+1];
207        }
208
209        $this->setupLocale();
210
211        $this->model = new bez\mdl\Model($auth, $INFO['client'], $this, $conf);
212        $this->tpl = new bez\meta\Tpl($this, $conf);
213
214    }
215
216	/**
217	 * handle ajax requests
218	 */
219	public function _ajax_call(Doku_Event $event, $param) {
220		global $auth;
221		if ($event->data !== 'plugin_bez') {
222			return;
223		}
224		//no other ajax call handlers needed
225		$event->stopPropagation();
226		$event->preventDefault();
227
228	}
229
230
231
232	public function tpl_pagetools_display(Doku_Event $event, $param) {
233		if ($this->action !== '') {
234			$event->preventDefault();
235        }
236	}
237
238	public function action_act_preprocess(Doku_Event $event, $param)
239	{
240        global $conf;
241
242        if ($this->action === '') {
243            return;
244        }
245
246        $event->preventDefault();
247		try {
248            $this->flush_notifications();
249
250			$ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php";
251
252			if (file_exists($ctl)) {
253				include $ctl;
254			}
255        } catch(bez\meta\ValidationException $e) {
256            foreach ($e->get_errors() as $field => $error_code) {
257                $lang = $this->getLang($field);
258                if ($lang != '') {
259                    $field = $lang;
260                }
261                $this->add_error(
262                    $this->getLang('validate_' . $error_code),
263                    $field);
264            }
265
266            $this->tpl->set_values($_POST);
267
268        } catch(bez\meta\PermissionDeniedException $e) {
269            dbglog('plugin_bez', $e);
270            if ($conf['allowdebug']) {
271                dbg($e);
272                $this->tpl->prevent_rendering();
273            } else {
274                header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login');
275            }
276		} catch(Exception $e) {
277            dbglog('plugin_bez', $e);
278            if ($conf['allowdebug']) {
279               dbg($e);
280            }
281            $this->tpl->prevent_rendering();
282		}
283	}
284
285	public function tpl_act_render($event, $param)
286	{
287        global $conf;
288
289        if ($this->action === '') {
290            return false;
291        }
292        $event->preventDefault();
293
294		try {
295
296			foreach ($this->errors as $error) {
297				echo '<div class="error">';
298                if ($error['header'] === NULL) {
299					echo $error['value'];
300				} else {
301					echo '<strong>'.$error['header'].'</strong>: '.$error['value'];
302				}
303				echo '</div>';
304			}
305
306            foreach ($this->notifications as $note) {
307                echo '<div class="info">';
308				if ($note['header'] === NULL) {
309					echo $note['value'];
310				} else {
311					echo $note['header'].': <strong>'.$note['value'].'</strong>';
312				}
313				echo '</div>';
314            }
315
316			bez_tpl_include($this->tpl);
317
318        } catch(bez\meta\PermissionDeniedException $e) {
319            dbglog('plugin_bez', $e);
320		} catch(Exception $e) {
321			/*exception*/
322            dbglog('plugin_bez', $e);
323            if ($conf['allowdebug']) {
324               dbg($e);
325            }
326		}
327	}
328}
329