xref: /plugin/bez/action/default.php (revision 67fdb4264d29ba654116a7384b9e48f968843f95)
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
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            //pl is default language
130            if ($ex[0] == 'pl') {
131                //throw out "pl" and "bez"
132                array_shift($ex);
133                array_shift($ex);
134
135                $url = call_user_func_array(array($this, 'url'), $ex);
136                header("Location: $url");
137            }
138            $conf['lang'] = array_shift($ex);
139
140            $this->localised = false;
141        }
142        //throw out "bez"
143        array_shift($ex);
144
145        $this->action = array_shift($ex);
146
147        if (count($ex) % 2 !== 0) {
148            throw new Exception('invalid params');
149        }
150
151        for ($i = 0; $i < count($ex); $i += 2) {
152            $this->params[$ex[$i]] = $ex[$i+1];
153        }
154
155        $this->setupLocale();
156        $this->createObjects();
157
158        if (empty($conf['baseurl'])) {
159            msg($this->getLang('info set baseurl'));
160        }
161    }
162
163	/**
164	 * handle ajax requests
165	 */
166	public function _ajax_call(Doku_Event $event, $param) {
167		global $auth;
168		if ($event->data !== 'plugin_bez') {
169			return;
170		}
171		//no other ajax call handlers needed
172		$event->stopPropagation();
173		$event->preventDefault();
174
175	}
176
177	public function tpl_pagetools_display(Doku_Event $event, $param) {
178		if ($this->action !== '') {
179			$event->preventDefault();
180        }
181	}
182
183	protected $prevent_rendering = false;
184
185	public function action_act_preprocess(Doku_Event $event, $param)
186	{
187        global $conf;
188
189        if ($this->action === '') {
190            return;
191        }
192
193        $event->preventDefault();
194		try {
195            $this->flush_notifications();
196
197			$ctl = DOKU_PLUGIN."bez/ctl/".str_replace('/', '', $this->action).".php";
198
199			if (file_exists($ctl)) {
200				include $ctl;
201			}
202        } catch(bez\meta\ValidationException $e) {
203            foreach ($e->get_errors() as $field => $error_code) {
204                $lang = $this->getLang($field);
205                if ($lang != '') {
206                    $field = $lang;
207                }
208                $this->add_error(
209                    $this->getLang('validate_' . $error_code),
210                    $field);
211            }
212
213            $this->tpl->set_values($_POST);
214
215        } catch(bez\meta\PermissionDeniedException $e) {
216            dbglog('plugin_bez', $e);
217            header('Location: ' . DOKU_URL . 'doku.php?id=' . $_GET['id'] . '&do=login');
218        } catch (\PHPMailer\PHPMailer\Exception $e) {
219            msg($e->getMessage(), -1);
220		} catch(Exception $e) {
221            dbglog('plugin_bez', $e);
222            if ($conf['allowdebug']) {
223               dbg($e);
224            } else {
225                msg($e->getMessage(), -1);
226            }
227            $this->prevent_rendering = true;
228		}
229	}
230
231	public function tpl_act_render($event, $param)
232	{
233        global $conf;
234
235        if ($this->action === '') {
236            return false;
237        }
238        $event->preventDefault();
239
240        if ($this->prevent_rendering) return;
241
242		try {
243
244			foreach ($this->errors as $error) {
245				echo '<div class="error">';
246                if ($error['header'] === NULL) {
247					echo $error['value'];
248				} else {
249					echo '<strong>'.$error['header'].'</strong>: '.$error['value'];
250				}
251				echo '</div>';
252			}
253
254            foreach ($this->notifications as $note) {
255                echo '<div class="info">';
256				if ($note['header'] === NULL) {
257					echo $note['value'];
258				} else {
259					echo $note['header'].': <strong>'.$note['value'].'</strong>';
260				}
261				echo '</div>';
262            }
263
264			$this->bez_tpl_include(str_replace('/', '', $this->get_action()));
265
266        } catch(bez\meta\PermissionDeniedException $e) {
267            dbglog('plugin_bez', $e);
268		} catch(Exception $e) {
269			/*exception*/
270            dbglog('plugin_bez', $e);
271            if ($conf['allowdebug']) {
272               dbg($e);
273            }
274		}
275	}
276}
277