1<?php
2/**
3 * Noticeboard Plugin
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  Zaruba Tomas <zatomik@gmail.com>
7 */
8
9if (!defined('DOKU_INC')) die();
10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
11require_once (DOKU_PLUGIN . 'action.php');
12
13require_once(DOKU_PLUGIN."noticeboard/classes/EditForm.php");
14require_once(DOKU_PLUGIN."noticeboard/classes/Notice.php");
15require_once(DOKU_PLUGIN."noticeboard/classes/NoticeList.php");
16require_once(DOKU_PLUGIN."noticeboard/classes/rss.php");
17require_once(DOKU_PLUGIN."noticeboard/classes/ICal.php");
18
19class action_plugin_noticeboard extends DokuWiki_Action_Plugin {
20
21
22    private $form; // edit Form
23    private $start; // start time of event
24    private $end; // end time of event
25    private $deadline; // deadline of event
26    private $noticeboardId; //id of root page with plugin
27    private $isValid;   //edit for is valid -> notice can be saved
28    private $showDetail; // page is detail of some notice
29    private $notice; // notice to show
30    /**
31     * Return some info
32     */
33    function getInfo() {
34        return array (
35            'author' => 'Some name',
36            'email' => 'foo@bar.org',
37            'date' => '2007-04-05',
38            'name' => 'Toolbar Action Plugin',
39            'desc' => 'Inserts a button into the toolbar',
40            'url' => 'http://www.example.com/plugin/toolbar',
41        );
42    }
43
44    /**
45     * Register the eventhandlers
46     */
47    function register(&$controller) {
48        $controller->register_hook('HTML_EDITFORM_OUTPUT',
49                                   'BEFORE',
50                                   $this,
51                                   'editForm');
52        $controller->register_hook('ACTION_ACT_PREPROCESS',
53                                   'BEFORE',
54                                   $this,
55                                   'handle_act_preprocess',
56                                   array());
57        $controller->register_hook('TPL_ACT_RENDER',
58                                   'BEFORE',
59                                   $this,
60                                   'handle_act_render',
61                                   array());
62        $controller->register_hook('ACTION_HEADERS_SEND',
63                                   'BEFORE',
64                                   $this,
65                                   'handle_header',
66                                   array());
67
68
69    }
70
71    /**
72     * CHECK if user want RSS or iCAL
73     */
74    function handle_header(& $event, $param) {
75        global $ID;
76        //RSS
77        if($_GET['noticeboard_rss_category']){
78            $category = htmlspecialchars($_GET['noticeboard_rss_category'],ENT_QUOTES);
79            $parentId = htmlspecialchars($_GET['noticeboard_rss_parent'],ENT_QUOTES);
80            $rss = new helper_plugin_noticeboard_Rss($category,$ID);
81            $rss->generateOutput();
82            die();
83        }else if($_GET['noticeboard_get_ical']){
84            $cal = new helper_plugin_noticeboard_ICal($ID);
85            $cal->generateOutput();
86            die();
87        }
88
89
90    }
91
92    public function editForm(& $event, $param) {
93        global $ID;
94        global $TEXT;
95        global $INFO;
96
97
98
99        if($INFO['perm'] < 2){
100            return;             //edit form only for authorized users
101        }
102        $this->noticeboardId = substr($ID,0,strlen($ID)-10);
103                                //check if page is notice
104
105        $isNotice = helper_plugin_noticeboard_NoticeList::noticeIdExist($ID);
106        if(!($_GET['noticeboard_newnotice'] == true || $isNotice)){
107            return;             // not a noticeboard page, nothing to do
108        }
109
110        $headerPosition = $event->data->findElementByAttribute("name","wikitext");
111        $noticePosition = $event->data->findElementByAttribute("class","editButtons");
112
113        if($TEXT != ''){
114            $a =  form_makeWikiText($TEXT);
115            $event->data->replaceElement($headerPosition,$a);
116        }
117        if(!$this->form){
118            $this->form = new helper_plugin_noticeboard_EditForm();
119        }
120        $out = $this->form->getForm();
121        $event->data->insertElement($noticePosition,$out);
122    }
123
124    public function handle_act_preprocess(& $event, $param) {
125        Global $ACT;
126        Global $ID;
127        Global $TEXT;
128        Global $INFO;
129        $act = $this->_act_clean($event->data);
130
131
132        if($act=='show'){
133            if($_POST['noticeboard_mode']){
134                session_register('noticeboard_mode');
135                $_SESSION['noticeboard_mode'] = $_POST['noticeboard_mode'];
136            }
137
138            if($_POST['noticeboard_list_start'] !== null){
139                session_register('noticeboard_list_start');
140                $_SESSION['noticeboard_list_start'] = $_POST['noticeboard_list_start'];
141            }
142
143            if($_POST['noticeboard_sort'] !== null){
144                session_register('noticeboard_sort');
145                $_SESSION['noticeboard_sort'] = $_POST['noticeboard_sort'];
146            }
147
148            //detail of page
149            $noticeList = new helper_plugin_noticeboard_NoticeList($ID);
150            $notice = $noticeList->getNoticeById($ID);
151            if($notice){
152                $this->showDetail = true;
153                $this->notice = $notice;
154            }
155
156            //list filter
157            if($_POST['noticeboard_list_filter']){
158                if($_SESSION['noticeboard_list_start']){
159                    $_SESSION['noticeboard_list_start'] = 0; // set list to first page
160                }
161                session_register('noticeboard_list_category');
162                $_SESSION['noticeboard_list_category'] = 0;
163                if($_POST['noticeboard_show_category_all'] == 8){
164                    $_SESSION['noticeboard_list_category'] = 8;
165                }
166                if($_POST['noticeboard_show_category_meeting'] == 4){
167                    $_SESSION['noticeboard_list_category'] += 4;
168                }
169                if($_POST['noticeboard_show_category_event'] == 2){
170                    $_SESSION['noticeboard_list_category'] += 2;
171                }
172                if($_POST['noticeboard_show_category_conference'] == 1){
173                    $_SESSION['noticeboard_list_category'] += 1;
174                }
175
176                session_register('noticeboard_sort_order');
177                $_SESSION['noticeboard_sort_order'] = $_POST['noticeboard_sort_order'];
178
179                session_register('noticeboard_show_time');
180                $_SESSION['noticeboard_show_time'] = $_POST['noticeboard_show_time'];
181            }
182
183
184            //calendar month
185            if($_POST['noticeboard_year']){
186                session_register('noticeboard_year');
187                $_SESSION['noticeboard_year'] = $_POST['noticeboard_year'];
188                session_register('noticeboard_month');
189                $_SESSION['noticeboard_month'] = $_POST['noticeboard_month'];
190            }
191
192        }
193
194        //delete post - only for auth
195        if($_POST['noticeboard_delete'] && $INFO['perm'] >= 2){
196           $noticeList->deleteNotice($_POST['noticeboard_delete']);
197        }
198
199
200
201
202        //check save - > save notice
203        $act = $this->_act_clean($event->data);
204        if(($act=='save' && $_REQUEST['noticeboard_category']) && $INFO['perm'] > 1){
205            $this->form = new helper_plugin_noticeboard_EditForm();
206            $this->isValid = $this->_validateForm();
207            if($this->isValid){ //form is corectly filled in
208                if(!$_REQUEST['noticeboard_parrentId']){
209                    $this->noticeboardId = substr($ID,0,strlen($ID)-10);
210                    $ID = cleanID(substr($ID,0,strlen($ID)-10).":"
211                        .htmlspecialchars($_REQUEST['noticeboard_category'],ENT_QUOTES).":"
212                        .date("Y:m:d",$this->start).":"
213                        .str_replace(" ","-",htmlspecialchars($_REQUEST['noticeboard_name'],ENT_QUOTES)));
214                }else{
215                   $this->noticeboardId = $_REQUEST['noticeboard_parrentId'];
216                }
217                $this->_saveNotice();
218            }else{ // form is filled bad, return back to edit page to correct it
219                 $ACT = "edit";
220            }
221        }
222    }
223
224    public function handle_act_render(& $event, $param) {
225        if($this->showDetail == true){
226            $this->_showDetail();
227        }
228    }
229
230
231    private function _validateForm(){
232        $valid = true;
233
234        //check if name is empty
235        if($_REQUEST['noticeboard_name'] == ''){
236            $this->form->setEMName($this->getLang('errorName'));
237            $valid = false;
238        }else{
239            $this->form->setEMName('');
240        }
241
242
243        if( !$_REQUEST['noticeboard_start_time'] ||(
244            preg_match("/^[0-9][0-9]:[0-9][0-9]$/",$_REQUEST['noticeboard_start_time']) &&
245            (substr($_REQUEST['noticeboard_start_time'],0,2) < 24 &&
246            substr($_REQUEST['noticeboard_start_time'],3,2) < 60))){
247
248            $this->form->setEMStartTime('');
249        }else{
250           $this->form->setEMStartTime($this->getLang('errorStartTime'));
251           $valid = false;
252        }
253
254        if( !$_REQUEST['noticeboard_end_time'] ||(
255            preg_match("/^[0-9][0-9]:[0-9][0-9]$/",$_REQUEST['noticeboard_end_time']) &&
256            (substr($_REQUEST['noticeboard_end_time'],0,2) < 24 &&
257            substr($_REQUEST['noticeboard_end_time'],3,2) < 60))){
258
259            $this->form->setEMEndTime('');
260        }else{
261           $this->form->setEMEndTime($this->getLang('errorEndTime'));
262           $valid = false;
263        }
264
265
266        //check correct form of Start date
267        if(preg_match("/^[0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]$/",$_REQUEST['noticeboard_start_date']) &&
268            checkdate(  substr($_REQUEST['noticeboard_start_date'],3,2),
269                        substr($_REQUEST['noticeboard_start_date'],0,2),
270                        substr($_REQUEST['noticeboard_start_date'],6,4))){
271            $this->start = mktime(substr($_REQUEST['noticeboard_start_time'],0,2),
272                        substr($_REQUEST['noticeboard_start_time'],3,2),
273                        0,
274                        substr($_REQUEST['noticeboard_start_date'],3,2),
275                        substr($_REQUEST['noticeboard_start_date'],0,2),
276                        substr($_REQUEST['noticeboard_start_date'],6,4));
277            $this->form->setEMStartDate('');
278
279        }else{
280            $this->form->setEMStartDate($this->getLang('errorStartDate'));
281            $valid = false;
282        }
283
284        //check correct form of End date - if filled in
285        if(!$_REQUEST['noticeboard_end_date'] || (preg_match("/^[0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]$/",$_REQUEST['noticeboard_end_date']) &&
286            checkdate(  substr($_REQUEST['noticeboard_end_date'],3,2),
287                        substr($_REQUEST['noticeboard_end_date'],0,2),
288                        substr($_REQUEST['noticeboard_end_date'],6,4)))){
289
290            $this->end = mktime(substr($_REQUEST['noticeboard_end_time'],0,2),
291                        substr($_REQUEST['noticeboard_end_time'],3,2),
292                        0,
293                        substr($_REQUEST['noticeboard_end_date'],3,2),
294                        substr($_REQUEST['noticeboard_end_date'],0,2),
295                        substr($_REQUEST['noticeboard_end_date'],6,4));
296            if(!$_REQUEST['noticeboard_end_date'] || $this->end > $this->start){
297                $this->form->setEMEndDate('');
298            }else{
299                $this->form->setEMEndDate($this->getLang('errorEndDateBigger'));
300            }
301
302        }else{
303            $this->form->setEMEndDate($this->getLang('errorEndDate'));
304            $valid = false;
305        }
306
307        if(!$_REQUEST['noticeboard_deadline'] || (preg_match("/^[0-9][0-9].[0-9][0-9].[0-9][0-9][0-9][0-9]$/",$_REQUEST['noticeboard_deadline']) &&
308            checkdate(  substr($_REQUEST['noticeboard_deadline'],3,2),
309                        substr($_REQUEST['noticeboard_deadline'],0,2),
310                        substr($_REQUEST['noticeboard_deadline'],6,4)))){
311
312            $this->deadline = mktime(0,0,0,
313                        substr($_REQUEST['noticeboard_deadline'],3,2),
314                        substr($_REQUEST['noticeboard_deadline'],0,2),
315                        substr($_REQUEST['noticeboard_deadline'],6,4));
316             $this->form->setEMDeadline('');
317
318        }else{
319            $this->form->setEMDeadline($this->getLang('errorDeadline'));
320            $valid = false;
321        }
322        return $valid;
323    }
324
325
326    private function _saveNotice(){
327         Global $ID;
328
329         $notice = new helper_plugin_noticeboard_Notice();
330         $notice->setCategory(htmlspecialchars($_REQUEST['noticeboard_category'], ENT_QUOTES));
331         $notice->setName(htmlspecialchars($_REQUEST['noticeboard_name'], ENT_QUOTES));
332         $notice->setPlace(htmlspecialchars($_REQUEST['noticeboard_place'], ENT_QUOTES));
333         $notice->setStartTime($this->start);
334         if($_REQUEST['noticeboard_start_time']){
335             $notice->setHasStartTime(true);
336         }
337         if($_REQUEST['noticeboard_end_time']){
338             $notice->setHasEndTime(true);
339         }
340         if($_REQUEST['noticeboard_end_date']){
341            $notice->setEndTime($this->end);
342         }
343         if($_REQUEST['noticeboard_deadline']){
344            $notice->setDeadline($this->deadline);
345         }
346         $notice->setId(strtolower($ID));
347         $notice->setParentId($this->noticeboardId);
348         $noticeList = new helper_plugin_noticeboard_NoticeList($this->noticeboardId);
349         $noticeList->addNotice($notice);
350
351    }
352
353    private function _showDetail(){
354        Global $INFO;
355        Global $ID;
356            $out .= "<table class='noticeboard-show-detail' cellspacing='0'><thead><tr><th colspan='2'>";
357            $out .= $this->notice->getName();
358            $out .= "</th><th width='80' class='cat'>";
359            $out .= $this->notice->getCategory();
360            $out .= "</th></tr></thead><tbody><tr><td class='left'>";
361            $out .= "<strong>".$this->getLang('startTime').":</strong></td><td colspan='2'> ";
362            if($this->notice->hasStartTime()){
363                $out .= date("d.m.Y H:i",$this->notice->getStartTime());
364            }else{
365                $out .= date("d.m.Y",$this->notice->getStartTime());
366            }
367            $out .= "</td></tr>";
368
369            if($this->notice->getEndTime()){
370                $out .= "<tr><td class='left'><strong>".$this->getLang('endTime').":</strong></td><td colspan='2'>";
371                if($this->notice->hasEndTime()){
372                    $out .= date("d.m.Y H:i",$this->notice->getEndTime());
373                }else{
374                    $out .= date("d.m.Y",$this->notice->getEndTime());
375                }
376                $out .= "</td></tr>";
377            }
378
379            if($this->notice->getDeadline()){
380                 $out .= "<tr><td class='left'><strong>".$this->getLang('deadline').":</strong></td><td colspan='2'>";
381                 $out .= date("d.m.Y",$this->notice->getDeadline());
382                 $out .= "</td></tr>";
383            }
384
385            if($this->notice->getPlace()){
386                 $out .= "<tr><td class='left'><strong>".$this->getLang('place').":</strong></td><td colspan='2'>";
387                 $out .= $this->notice->getPlace();
388                 $out .= "</td></tr>";
389            }
390
391            $out .= "</tbody></table>";
392        echo $out;
393    }
394
395
396    private function _act_clean($act){
397         // check if the action was given as array key
398         if(is_array($act)){
399           list($act) = array_keys($act);
400         }
401
402         //remove all bad chars
403         $act = strtolower($act);
404         $act = preg_replace('/[^a-z_]+/','',$act);
405
406         return $act;
407     }
408
409
410}