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