xref: /plugin/bez/action/default.php (revision 16bc480c2551a44015600178c423a765b86208da)
1<?php
2
3use \dokuwiki\plugin\bez;
4
5if(!defined('DOKU_INC')) die();
6
7define('BEZ_NOTIFICATIONS_COOKIE_NAME', 'bez_notifications');
8
9class action_plugin_bez_default extends action_plugin_bez_base {
10
11	protected $action = '';
12    protected $params = array();
13
14    protected $notifications = array();
15
16    protected $errors = array();
17
18    public function get_action() {
19        return $this->action;
20    }
21
22    public function get_param($id, $default='') {
23        return (isset($this->params[$id]) ? $this->params[$id] : $default);
24    }
25
26    private function add_notification($value, $header=NULL) {
27        if (isset($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME])) {
28            $notifs = unserialize($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME]);
29        } else {
30            $notifs = array();
31        }
32        $notifs[] = array('value' => $value, 'header' => $header);
33        setcookie(BEZ_NOTIFICATIONS_COOKIE_NAME, serialize($notifs));
34    }
35
36    private function flush_notifications() {
37        if (!isset($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME])) {
38            return array();
39        }
40        $this->notifications = unserialize($_COOKIE[BEZ_NOTIFICATIONS_COOKIE_NAME]);
41
42        //remove cookie
43        setcookie(BEZ_NOTIFICATIONS_COOKIE_NAME, serialize(array()));
44    }
45
46    private function add_error($value, $header=NULL) {
47        $this->errors[] = array('value' => $value, 'header' => $header);
48    }
49
50	/**
51	 * Register its handlers with the DokuWiki's event controller
52	 */
53	public function register(Doku_Event_Handler $controller)
54	{
55        $controller->register_hook('DOKUWIKI_STARTED', 'BEFORE', $this, 'setup_id');
56        $controller->register_hook('DOKUWIKI_STARTED', 'AFTER', $this, 'setup_enviroment');
57		$controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'action_act_preprocess');
58		$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'tpl_act_render');
59		$controller->register_hook('TEMPLATE_PAGETOOLS_DISPLAY', 'BEFORE', $this, 'tpl_pagetools_display');
60		$controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'include_dependencies', array());
61		$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call');
62
63        $controller->register_hook('PLUGIN_NOTIFICATION_REGISTER_SOURCE', 'AFTER', $this, 'add_notifications_source');
64        $controller->register_hook('PLUGIN_NOTIFICATION_GATHER', 'AFTER', $this, 'add_notifications');
65        $controller->register_hook('PLUGIN_NOTIFICATION_CACHE_DEPENDENCIES', 'AFTER', $this, 'add_notification_cache_dependencies');
66	}
67
68	public function include_dependencies(Doku_Event $event) {
69		// Adding a stylesheet
70		$event->data["link"][] = array (
71		  "type" => "text/css",
72		  "rel" => "stylesheet",
73		  "href" => DOKU_BASE.
74			"lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.css",
75		);
76
77		// Adding a JavaScript File
78		$event->data["script"][] = array (
79		  "type" => "text/javascript",
80		  "src" => DOKU_BASE.
81			"lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.min.js",
82		  "_data" => "",
83		);
84
85		// Adding a JavaScript File
86		$event->data["script"][] = array (
87		  "type" => "text/javascript",
88		  "src" => DOKU_BASE.
89			"lib/plugins/bez/lib/jquery.datepair/datepair.js",
90		  "_data" => "",
91		);
92
93		// Adding a JavaScript File
94		$event->data["script"][] = array (
95		  "type" => "text/javascript",
96		  "src" => DOKU_BASE.
97			"lib/plugins/bez/lib/jquery.datepair/jquery.datepair.js",
98		  "_data" => "",
99		);
100
101		$event->data["link"][] = array (
102		  "type" => "text/css",
103		  "rel" => "stylesheet",
104		  "href" => DOKU_BASE.
105			"lib/plugins/bez/lib/jquery.form-validator/theme-default.min.css",
106		);
107
108
109		$event->data["script"][] = array (
110		  "type" => "text/javascript",
111		  "src" => DOKU_BASE.
112			"lib/plugins/bez/lib/jquery.form-validator/jquery.form-validator.min.js",
113		  "_data" => "",
114		);
115	}
116
117	public function setup_id(Doku_Event $event, $param) {
118	    global $INFO, $ID;
119
120        $id = $_GET['id'];
121        $ex = explode(':', $id);
122
123        //check if we process BEZ
124        if ($ex[0] !== 'bez' && $ex[1] !== 'bez') {
125            return;
126        }
127
128        $INFO['id'] = $id;
129        $ID = $id;
130    }
131
132    public function setup_enviroment(Doku_Event $event, $param) {
133        global $ACT, $conf, $ID;
134
135        if ($ACT !== 'show') {
136            return;
137        }
138
139		$ex = explode(':', $ID);
140
141        //check if we process BEZ
142        if ($ex[0] !== 'bez' && $ex[1] !== 'bez') {
143            return;
144        }
145
146        if ($ex[1] === 'bez') {
147            //pl is default language
148            if ($ex[0] == 'pl') {
149                //throw out "pl" and "bez"
150                array_shift($ex);
151                array_shift($ex);
152
153                $url = call_user_func_array(array($this, 'url'), $ex);
154                header("Location: $url");
155            }
156            $conf['lang'] = array_shift($ex);
157
158            $this->localised = false;
159        }
160        //throw out "bez"
161        array_shift($ex);
162
163        $this->action = array_shift($ex);
164
165        if (count($ex) % 2 !== 0) {
166            throw new Exception('invalid params');
167        }
168
169        for ($i = 0; $i < count($ex); $i += 2) {
170            $this->params[$ex[$i]] = $ex[$i+1];
171        }
172
173        $this->setupLocale();
174        $this->createObjects();
175
176        if (empty($conf['baseurl'])) {
177            msg($this->getLang('info set baseurl'));
178        }
179    }
180
181	/**
182	 * handle ajax requests
183	 */
184	public function _ajax_call(Doku_Event $event, $param) {
185		global $auth;
186		if ($event->data !== 'plugin_bez') {
187			return;
188		}
189		//no other ajax call handlers needed
190		$event->stopPropagation();
191		$event->preventDefault();
192
193	}
194
195	public function tpl_pagetools_display(Doku_Event $event, $param) {
196		if ($this->action !== '') {
197			$event->preventDefault();
198        }
199	}
200
201	protected $prevent_rendering = false;
202
203	public function action_act_preprocess(Doku_Event $event, $param)
204	{
205        global $conf;
206
207        if ($this->action === '') {
208            return;
209        }
210
211        $event->preventDefault();
212		try {
213            $this->flush_notifications();
214
215			$ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php";
216
217			if (file_exists($ctl)) {
218				include $ctl;
219			}
220        } catch(bez\meta\ValidationException $e) {
221            foreach ($e->get_errors() as $field => $error_code) {
222                $lang = $this->getLang($field);
223                if ($lang != '') {
224                    $field = $lang;
225                }
226                $this->add_error(
227                    $this->getLang('validate_' . $error_code),
228                    $field);
229            }
230
231            $this->tpl->set_values($_POST);
232
233        } catch(bez\meta\PermissionDeniedException $e) {
234            dbglog('plugin_bez', $e);
235            header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login');
236        } catch (\PHPMailer\PHPMailer\Exception $e) {
237            msg($e->getMessage(), -1);
238		} catch(Exception $e) {
239            dbglog('plugin_bez', $e);
240            if ($conf['allowdebug']) {
241               dbg($e);
242            } else {
243                msg($e->getMessage(), -1);
244            }
245            $this->prevent_rendering = true;
246		}
247	}
248
249	public function tpl_act_render($event, $param)
250	{
251        global $conf;
252
253        if ($this->action === '') {
254            return false;
255        }
256        $event->preventDefault();
257
258        if ($this->prevent_rendering) return;
259
260		try {
261
262			foreach ($this->errors as $error) {
263				echo '<div class="error">';
264                if ($error['header'] === NULL) {
265					echo $error['value'];
266				} else {
267					echo '<strong>'.$error['header'].'</strong>: '.$error['value'];
268				}
269				echo '</div>';
270			}
271
272            foreach ($this->notifications as $note) {
273                echo '<div class="info">';
274				if ($note['header'] === NULL) {
275					echo $note['value'];
276				} else {
277					echo $note['header'].': <strong>'.$note['value'].'</strong>';
278				}
279				echo '</div>';
280            }
281
282			$this->bez_tpl_include(str_replace('/', '', $this->get_action()));
283
284        } catch(bez\meta\PermissionDeniedException $e) {
285            dbglog('plugin_bez', $e);
286		} catch(Exception $e) {
287			/*exception*/
288            dbglog('plugin_bez', $e);
289            if ($conf['allowdebug']) {
290               dbg($e);
291            }
292		}
293	}
294
295	public function add_notifications_source(Doku_Event $event)
296    {
297        $event->data[] = 'bez:problems_without_tasks';
298        $event->data[] = 'bez:problems_coming';
299        $event->data[] = 'bez:problems_outdated';
300        $event->data[] = 'bez:tasks_coming';
301        $event->data[] = 'bez:tasks_outdated';
302    }
303
304    public function add_notification_cache_dependencies(Doku_Event $event)
305    {
306        if (!preg_grep('/^bez:.*/', $event->data['plugins'])) return;
307
308        /** @var \helper_plugin_bez_db $db_helper */
309        $db_helper = plugin_load('helper', 'bez_db');
310        $event->data['dependencies'][] = $db_helper->getDB()->getAdapter()->getDbFile();
311    }
312
313    public function add_notifications(Doku_Event $event)
314    {
315        if (!preg_grep('/^bez:.*/', $event->data['plugins'])) return;
316
317        $user = $event->data['user'];
318        $this->createObjects(true);
319
320        if (in_array('bez:problems_without_tasks', $event->data['plugins'])) {
321            $threads = $this->get_model()->factory('thread')->get_all(array(
322                                                                          'type' => 'issue',
323                                                                          'task_count' => '0',
324                                                                          'coordinator' => $user
325                                                                      ));
326            /** @var bez\mdl\Thread $thread */
327            foreach ($threads as $thread) {
328                $link = '<a href="' . $this->url('thread', 'id', $thread->id) . '">';
329                $link .= '#' . $thread->id;
330                $link .= '</a>';
331
332                $full = sprintf($this->getLang('notification problems_without_tasks'), $link);
333                $event->data['notifications'][] = [
334                    'plugin' => 'bez:problems_without_tasks',
335                    'id' => 'thread:' . $thread->id,
336                    'full' => $full,
337                    'brief' => $link,
338                    'timestamp' => strtotime($thread->last_activity_date)
339                ];
340            }
341        }
342
343        if (in_array('bez:problems_coming', $event->data['plugins'])) {
344            $threads = $this->get_model()->factory('thread')->get_all(array(
345                'type' => 'issue',
346                'priority' => '1',
347                'coordinator' => $user
348            ));
349            /** @var bez\mdl\Thread $thread */
350            foreach ($threads as $thread) {
351                $link = '<a href="' . $this->url('thread', 'id', $thread->id) . '">';
352                $link .= '#' . $thread->id;
353                $link .= '</a>';
354
355                $full = sprintf($this->getLang('notification problems_coming'), $link);
356                $event->data['notifications'][] = [
357                    'plugin' => 'bez:problems_coming',
358                    'id' => 'thread:' . $thread->id,
359                    'full' => $full,
360                    'brief' => $link,
361                    'timestamp' => strtotime($thread->last_activity_date)
362                ];
363            }
364        }
365
366        if (in_array('bez:problems_outdated', $event->data['plugins'])) {
367            $threads = $this->get_model()->threadFactory->get_all(array(
368                'type' => 'issue',
369                'priority' => '2',
370                'coordinator' => $user
371            ));
372            /** @var bez\mdl\Thread $thread */
373            foreach ($threads as $thread) {
374                $link = '<a href="' . $this->url('thread', 'id', $thread->id) . '">';
375                $link .= '#' . $thread->id;
376                $link .= '</a>';
377
378                $full = sprintf($this->getLang('notification problems_outdated'), $link);
379                $event->data['notifications'][] = [
380                    'plugin' => 'bez:problems_outdated',
381                    'id' => 'thread:' . $thread->id,
382                    'full' => $full,
383                    'brief' => $link,
384                    'timestamp' => strtotime($thread->last_activity_date)
385                ];
386            }
387        }
388
389        if (in_array('bez:tasks_coming', $event->data['plugins'])) {
390            $tasks = $this->get_model()->factory('task')->get_all(array(
391                'priority' => '1',
392                'assignee' => $user
393            ));
394            /** @var bez\mdl\Thread $thread */
395            foreach ($tasks as $task) {
396                $link = '<a href="' . $this->url('task', 'tid', $task->id) . '">';
397                $link .= '#z' . $task->id;
398                $link .= '</a>';
399
400                $full = sprintf($this->getLang('notification tasks_coming'), $link);
401                $event->data['notifications'][] = [
402                    'plugin' => 'bez:tasks_coming',
403                    'id' => 'task:' . $task->id,
404                    'full' => $full,
405                    'brief' => $link,
406                    'timestamp' => strtotime($task->plan_date)
407                ];
408            }
409        }
410
411        if (in_array('bez:tasks_outdated', $event->data['plugins'])) {
412            $tasks = $this->get_model()->factory('task')->get_all(array(
413                'priority' => '2',
414                'assignee' => $user
415            ));
416            /** @var bez\mdl\Thread $thread */
417            foreach ($tasks as $task) {
418                $link = '<a href="' . $this->url('task', 'tid', $task->id) . '">';
419                $link .= '#z' . $task->id;
420                $link .= '</a>';
421
422                $full = sprintf($this->getLang('notification tasks_outdated'), $link);
423                $event->data['notifications'][] = [
424                    'plugin' => 'bez:tasks_outdated',
425                    'id' => 'task:' . $task->id,
426                    'full' => $full,
427                    'brief' => $link,
428                    'timestamp' => strtotime($task->plan_date)
429                ];
430            }
431        }
432    }
433}
434