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