xref: /plugin/bez/action/default.php (revision fe5d6d1ebd253c129098b67fff8cf438a54d8650)
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	private $action = '';
33	private $params = array();
34    private $lang_code = '';
35
36    /** @var  bez\mdl\Model */
37	private $model;
38
39    /** @var  bez\meta\Tpl */
40    private $tpl;
41
42    private $notifications = array();
43
44    private $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		//data
248		$data = array();
249
250		//json library of DokuWiki
251		$json = new JSON();
252
253		$action = $_POST['action'];
254		try {
255			if ($action === 'commcause_delete') {
256				$kid = $_POST['kid'];
257
258				$commcause = $this->model->commcauses->get_one($kid);
259				$this->model->commcauses->delete($commcause);
260
261				$data['state'] = 'ok';
262			}
263		} catch(Exception $e) {
264			$data['state'] = 'error';
265			$data['msg'] = strval($e);
266		}
267
268		//set content type
269		header('Content-Type: application/json');
270		echo $json->encode($data);
271	}
272
273
274
275	public function tpl_pagetools_display(Doku_Event $event, $param) {
276		if ($this->action !== '') {
277			$event->preventDefault();
278        }
279	}
280
281	public function action_act_preprocess(Doku_Event $event, $param)
282	{
283        global $conf;
284
285        if ($this->action === '') {
286            return;
287        }
288
289        $event->preventDefault();
290		try {
291            $this->flush_notifications();
292
293			$ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php";
294
295			if (file_exists($ctl)) {
296				include $ctl;
297			}
298        } catch(bez\meta\ValidationException $e) {
299            foreach ($e->get_errors() as $field => $error_code) {
300                $lang = $this->getLang($field);
301                if ($lang != '') {
302                    $field = $lang;
303                }
304                $this->add_error(
305                    $this->getLang('validate_' . $error_code),
306                    $field);
307            }
308
309            $this->tpl->set_values($_POST);
310
311        } catch(bez\meta\PermissionDeniedException $e) {
312            dbglog('plugin_bez', $e);
313            header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login');
314
315		} catch(Exception $e) {
316            dbglog('plugin_bez', $e);
317            if ($conf['allowdebug']) {
318               dbg($e);
319            }
320            $this->tpl->prevent_rendering();
321		}
322	}
323
324	public function tpl_act_render($event, $param)
325	{
326        global $conf;
327
328        if ($this->action === '') {
329            return false;
330        }
331        $event->preventDefault();
332
333		try {
334
335			foreach ($this->errors as $error) {
336				echo '<div class="error">';
337                if ($error['header'] === NULL) {
338					echo $error['value'];
339				} else {
340					echo '<strong>'.$error['header'].'</strong>: '.$error['value'];
341				}
342				echo '</div>';
343			}
344
345            foreach ($this->notifications as $note) {
346                echo '<div class="info">';
347				if ($note['header'] === NULL) {
348					echo $note['value'];
349				} else {
350					echo $note['header'].': <strong>'.$note['value'].'</strong>';
351				}
352				echo '</div>';
353            }
354
355			bez_tpl_include($this->tpl);
356
357        } catch(bez\meta\PermissionDeniedException $e) {
358            dbglog('plugin_bez', $e);
359            header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login');
360		} catch(Exception $e) {
361			/*exception*/
362            dbglog('plugin_bez', $e);
363            if ($conf['allowdebug']) {
364               dbg($e);
365            }
366		}
367	}
368}
369