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