xref: /plugin/bez/action/default.php (revision ffe3109b3ffb461f7393087200b87a2995522fbf)
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_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
63	public function include_dependencies(Doku_Event $event) {
64		// Adding a stylesheet
65		$event->data["link"][] = array (
66		  "type" => "text/css",
67		  "rel" => "stylesheet",
68		  "href" => DOKU_BASE.
69			"lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.css",
70		);
71
72		// Adding a JavaScript File
73		$event->data["script"][] = array (
74		  "type" => "text/javascript",
75		  "src" => DOKU_BASE.
76			"lib/plugins/bez/lib/jquery.timepicker-1.11.9-0/jquery.timepicker.min.js",
77		  "_data" => "",
78		);
79
80		// Adding a JavaScript File
81		$event->data["script"][] = array (
82		  "type" => "text/javascript",
83		  "src" => DOKU_BASE.
84			"lib/plugins/bez/lib/jquery.datepair/datepair.js",
85		  "_data" => "",
86		);
87
88		// Adding a JavaScript File
89		$event->data["script"][] = array (
90		  "type" => "text/javascript",
91		  "src" => DOKU_BASE.
92			"lib/plugins/bez/lib/jquery.datepair/jquery.datepair.js",
93		  "_data" => "",
94		);
95
96		$event->data["link"][] = array (
97		  "type" => "text/css",
98		  "rel" => "stylesheet",
99		  "href" => DOKU_BASE.
100			"lib/plugins/bez/lib/jquery.form-validator/theme-default.min.css",
101		);
102
103
104		$event->data["script"][] = array (
105		  "type" => "text/javascript",
106		  "src" => DOKU_BASE.
107			"lib/plugins/bez/lib/jquery.form-validator/jquery.form-validator.min.js",
108		  "_data" => "",
109		);
110	}
111
112    public function setup_enviroment(Doku_Event $event, $param) {
113        global $ACT, $auth, $conf, $INFO;
114
115        if ($ACT !== 'show') {
116            return;
117        }
118
119        $id = $_GET['id'];
120		$ex = explode(':', $id);
121
122        //check if we process BEZ
123        if ($ex[0] !== 'bez' && $ex[1] !== 'bez') {
124            return;
125        }
126
127
128        if ($ex[1] === 'bez') {
129            $conf['lang'] = array_shift($ex);
130            //$this->lang_code = $conf['lang'];
131            $this->localised = false;
132        }
133        //throw out "bez"
134        array_shift($ex);
135
136        $this->action = array_shift($ex);
137
138        if (count($ex) % 2 !== 0) {
139            throw new Exception('invalid params');
140        }
141
142        for ($i = 0; $i < count($ex); $i += 2) {
143            $this->params[$ex[$i]] = $ex[$i+1];
144        }
145
146        $this->setupLocale();
147        $this->createObjects();
148
149        if (empty($conf['baseurl'])) {
150            msg($this->getLang('info set baseurl'));
151        }
152    }
153
154	/**
155	 * handle ajax requests
156	 */
157	public function _ajax_call(Doku_Event $event, $param) {
158		global $auth;
159		if ($event->data !== 'plugin_bez') {
160			return;
161		}
162		//no other ajax call handlers needed
163		$event->stopPropagation();
164		$event->preventDefault();
165
166	}
167
168	public function tpl_pagetools_display(Doku_Event $event, $param) {
169		if ($this->action !== '') {
170			$event->preventDefault();
171        }
172	}
173
174	protected $prevent_rendering = false;
175
176	public function action_act_preprocess(Doku_Event $event, $param)
177	{
178        global $conf;
179
180        if ($this->action === '') {
181            return;
182        }
183
184        $event->preventDefault();
185		try {
186            $this->flush_notifications();
187
188			$ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php";
189
190			if (file_exists($ctl)) {
191				include $ctl;
192			}
193        } catch(bez\meta\ValidationException $e) {
194            foreach ($e->get_errors() as $field => $error_code) {
195                $lang = $this->getLang($field);
196                if ($lang != '') {
197                    $field = $lang;
198                }
199                $this->add_error(
200                    $this->getLang('validate_' . $error_code),
201                    $field);
202            }
203
204            $this->tpl->set_values($_POST);
205
206        } catch(bez\meta\PermissionDeniedException $e) {
207            dbglog('plugin_bez', $e);
208            header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login');
209		} catch(Exception $e) {
210            dbglog('plugin_bez', $e);
211            if ($conf['allowdebug']) {
212               dbg($e);
213            } else {
214                msg($e->getMessage(), -1);
215            }
216            $this->prevent_rendering = true;
217		}
218	}
219
220	public function tpl_act_render($event, $param)
221	{
222        global $conf;
223
224        if ($this->action === '') {
225            return false;
226        }
227        $event->preventDefault();
228
229        if ($this->prevent_rendering) return;
230
231		try {
232
233			foreach ($this->errors as $error) {
234				echo '<div class="error">';
235                if ($error['header'] === NULL) {
236					echo $error['value'];
237				} else {
238					echo '<strong>'.$error['header'].'</strong>: '.$error['value'];
239				}
240				echo '</div>';
241			}
242
243            foreach ($this->notifications as $note) {
244                echo '<div class="info">';
245				if ($note['header'] === NULL) {
246					echo $note['value'];
247				} else {
248					echo $note['header'].': <strong>'.$note['value'].'</strong>';
249				}
250				echo '</div>';
251            }
252
253			$this->bez_tpl_include(str_replace('/', '', $this->get_action()));
254
255        } catch(bez\meta\PermissionDeniedException $e) {
256            dbglog('plugin_bez', $e);
257		} catch(Exception $e) {
258			/*exception*/
259            dbglog('plugin_bez', $e);
260            if ($conf['allowdebug']) {
261               dbg($e);
262            }
263		}
264	}
265}
266