1<?php
2/*
3 * To change this template, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7if(!defined('DOKU_INC')) die();
8
9if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
10require_once(DOKU_PLUGIN.'syntax.php');
11
12require_once(DOKU_PLUGIN."noticeboard/classes/ArrayList.php");
13
14/**
15 * Description of NoticeList
16 *
17 * @author Tomas Zaruba
18 */
19class helper_plugin_noticeboard_NoticeList extends DokuWiki_Syntax_Plugin {
20
21    private $list;
22    private $NoticeboardId;
23    public $hasPrevious;    //previous items in list
24    public $hasNext;        //next items in list
25    public $start;
26    public $end;
27    private $categoryFilter;
28    private $timeFilter;
29    private $sortFilter;
30    private $sortOrder; // 0= desc 1=asc
31
32
33
34    public function hasPrevious(){
35        return $this->hasPrevious();
36    }
37
38     public function hasNext(){
39        return $this->hasNext();
40    }
41
42    public function setCategoryFilter($value){
43        $this->categoryFilter = $value;
44    }
45
46    public function setTimeFilter($value){
47        $this->timeFilter = $value;
48    }
49
50    public function setSortFilter($value){
51        $this->sortFilter = $value;
52    }
53
54    public function setSortOrder($value){
55        $this->sortOrder = $value;
56    }
57
58    function helper_plugin_noticeboard_NoticeList($id){
59        $this->list = new helper_plugin_noticeboard_ArrayList();
60        $this->noticeboardId = $id;
61
62    }
63
64    public static function noticeIdExist($id){
65        $fileNoticeboards = metaFN('','.noticeboard');
66
67
68        if (!@file_exists($fileNoticeboards)){
69            return false;
70        }else{
71            $array = unserialize(io_readFile($fileNoticeboards, false));
72            if(array_key_exists($id,$array)){
73                return true;
74            }else{
75
76                return false;
77            }
78        }
79    }
80
81
82    public function addNotice($notice){
83        $file = metaFN($this->noticeboardId, '.noticeboard');
84        $fileNoticeboards = metaFN('','.noticeboard');
85
86
87        if (!@file_exists($fileNoticeboards)){//insert noticeboard page to list of known noticeboards
88            $array[strtolower($notice->getId())] = $this->noticeboardId;
89            io_saveFile($fileNoticeboards, serialize($array));
90        }else{
91            $array = unserialize(io_readFile($fileNoticeboards, false));
92            if(!array_key_exists($notice->getId(),$array)){
93                $array[strtolower($notice->getId())] = $this->noticeboardId;
94                io_saveFile($fileNoticeboards, serialize($array));
95            }
96        }
97
98        if (!@file_exists($file)){//make new file, insert one notice
99            $this->list->add($notice);
100            io_saveFile($file, serialize($this->list));
101        }else{
102            $this->list = unserialize(io_readFile($file, false));
103            while($this->list->hasNext()){
104                $next = $this->list->next();
105                if($notice->getId() == $next->getId()){
106                    $array = $this->list->getList();
107                    $array[$this->list->getPointer()-1] = $notice;
108                    $this->list->setArrayList($array);
109                    $i = 1;
110                    break;
111                }
112            }
113            $this->list->setPointer(0);
114            if($i != 1){
115                $this->list->add($notice);
116            }
117            io_saveFile($file, serialize($this->list));
118        }
119    }
120
121    public function deleteNotice($noticeId){
122        $file = metaFN($this->noticeboardId, '.noticeboard');
123        $fileNoticeboards = metaFN('','.noticeboard');
124
125
126        if (@file_exists($fileNoticeboards)){//delete from notice list
127            $array  = unserialize(io_readFile($fileNoticeboards, false));
128            unset($array[$noticeId]);
129            //array_unshift($array, array_shift ($array));
130            io_saveFile($fileNoticeboards, serialize($array));
131        }
132
133        if (@file_exists($file)){//delete notice
134            $this->list = unserialize(io_readFile($file, false));
135            //$array = $array();
136            while($this->list->hasNext()){
137                $next = $this->list->next();
138                if($noticeId == $next->getId()){
139                    $array = $this->list->getList();
140                    if(count($array) == 1){
141                        $array = array();
142                    }else{
143                        unset($array[$this->list->getPointer()-1]);
144                        array_unshift ($array, array_shift ($array));
145                    }
146                    $this->list->setArrayList($array);
147                    break;
148                }
149            }
150            $this->list->setPointer(0);
151            io_saveFile($file, serialize($this->list));
152        }
153    }
154
155    public function getNoticeAtDay($time){
156
157        $file = metaFN($this->noticeboardId, '.noticeboard');
158        if (!@file_exists($file)){
159            return false;
160        }
161        // load data
162        if (@file_exists($file)) {
163            $returnList = unserialize(io_readFile($file, false));
164        }
165        $arrayGet = $returnList->getList();
166        $array = array();
167        $startTime = $time;
168
169        $endTime = $time + 86400;//all day
170        for($i = 0; $i < count($arrayGet); $i++){
171            $notice = $arrayGet[$i];
172                if($notice->getDeadline() >= $startTime && $notice->getDeadline() < $endTime){
173                    $notice->setHasStartTime(false);
174                    echo $notice->hasStartTime();
175                    array_push($array, $notice);
176                    echo $array[0]->hasStartTime();
177                }else if($notice->getStartTime() >= $startTime && $notice->getStartTime() < $endTime){
178
179                    array_push($array, $notice);
180                }else if($notice->getEndTime() >= $startTime && $notice->getEndTime() < $endTime){
181
182                    $notice->setHasStartTime(0);
183                    array_push($array, $notice);
184                }else if(($notice->getHasEnd() && $notice->getEndTime() > $startTime)&& $notice->getStartTime() < $startTime  ){
185
186                    $notice->setHasStartTime(0);
187                    array_push($array, $notice);
188                }
189
190        }
191        $array = $this->selectCategory($array); //select categories
192        $array = $this->selectTime($array); // select time (future - past - all)
193        $array = $this->sortList($array);
194        $returnList->setArrayList($array);
195
196        return $returnList;
197    }
198
199    public function getNoticeList($start,$end){
200        $this->start = $start;
201        $this->end = $end;
202        $file = metaFN($this->noticeboardId, '.noticeboard');
203        if (!@file_exists($file)){
204            return false;
205        }
206        // load data
207        if (@file_exists($file)) {
208            $returnList = unserialize(io_readFile($file, false));
209        }
210
211        $array = $returnList->getList();
212
213        $array = $this->selectCategory($array); //select categories
214        $array = $this->selectTime($array); // select time (future - past - all)
215
216        $array = $this->sortList($array);
217
218        for($i = $start;$i<$end && $i<count($array);$i++){
219            $arrayNew[$i-$start] = $array[$i];
220        }
221        $returnList->setArrayList($arrayNew);
222
223        if($start > 0){
224            $this->hasPrevious = true;
225        }else{
226            $this->hasPrevious = false;
227        }
228
229        if($end < count($array)){
230            $this->hasNext = true;
231        }else{
232            $this->hasNext = false;
233        }
234
235        return $returnList;
236
237    }
238
239
240    private function selectTime($array){
241        if($this->timeFilter ==3){
242            return $array;      //show all time - nothing to filter
243        }
244        $currentTime = mktime(0,0,0,date("n"),date("j"),date("Y"));
245        //if past show all past + today
246        $returnArray = array();
247        if($this->timeFilter == 2){
248
249            for($i = 0;$i<count($array);$i++){
250                $notice = $array[$i];
251                if($notice->getStartTime() < $currentTime + 86400){ // all day
252                    array_push($returnArray, $notice);
253                }
254            }
255        //if future show all future + today
256        }else{
257            for($i = 0;$i<count($array);$i++){
258                $notice = $array[$i];
259                if($notice->getStartTime() >= $currentTime){ // all day
260                    array_push($returnArray, $notice);
261                }
262            }
263        }
264        return $returnArray;
265
266    }
267
268    private function selectCategory($array){
269        if($this->categoryFilter > 7){
270            return $array; //nothing to filter ->show all
271        }
272        $returnArray = array();
273        for($i = 0;$i<count($array);$i++){
274            $cat = $this->categoryFilter;
275            $notice = $array[$i];
276
277            if($cat > 3){
278                if($notice->getCategory() == 'meeting'){
279                    array_push($returnArray, $notice);
280                }
281                $cat -= 4;
282            }
283            if($cat > 1){
284                if($notice->getCategory() == 'event'){
285                    array_push($returnArray, $notice);
286                }
287                $cat -= 2;
288            }
289            if($cat == 1){
290                if($notice->getCategory() == 'conference'){
291                    array_push($returnArray, $notice);
292                }
293            }
294        }
295        return $returnArray;
296    }
297
298    private function sortList($array){
299        Global $ID;
300
301        $sortArray = array();
302        $returnArray = array();
303        if(!$array){
304            return $array;
305        }
306        if(!$this->sortFilter || $this->sortFilter == 1){
307            for($i = 0; $i<count($array);$i++){
308                $sortArray[$array[$i]->getId()] = $array[$i]->getStartTime();
309            }
310            if($this->sortOrder){
311                arsort($sortArray);
312            }else{
313                asort($sortArray);
314            }
315            reset($sortArray);
316            for($j = 0; $j<count($sortArray);$j++){
317                $notice = $this->getNoticeById(key($sortArray));
318                next($sortArray);
319                $returnArray[$j] = $notice;
320            }
321        }else if($this->sortFilter == 2){
322            for($i = 0; $i<count($array);$i++){
323                if($array[$i]->getDeadline()){
324                    $sortArray[$array[$i]->getId()] = $array[$i]->getDeadline();
325                }
326            }
327            if($this->sortOrder){
328                arsort($sortArray);
329            }else{
330                asort($sortArray);
331            }
332            reset($sortArray);
333            for($j = 0; $j<count($sortArray);$j++){
334                $notice = $this->getNoticeById(key($sortArray));
335                next($sortArray);
336                $returnArray[$j] = $notice;
337            }
338        }else if($this->sortFilter == 3){
339            for($i = 0; $i<count($array);$i++){
340                $sortArray[$array[$i]->getId()] = $array[$i]->getName();
341            }
342            if($this->sortOrder){
343                arsort($sortArray);
344            }else{
345                asort($sortArray);
346            }
347            reset($sortArray);
348            for($j = 0; $j<count($sortArray);$j++){
349                $notice = $this->getNoticeById(key($sortArray));
350                next($sortArray);
351                $returnArray[$j] = $notice;
352            }
353        }else if($this->sortFilter == 4){
354            for($i = 0; $i<count($array);$i++){
355                $sortArray[$array[$i]->getId()] = $array[$i]->getPlace();
356            }
357            if($this->sortOrder){
358                arsort($sortArray);
359            }else{
360                asort($sortArray);
361            }
362            reset($sortArray);
363            for($j = 0; $j<count($sortArray);$j++){
364                $notice = $this->getNoticeById(key($sortArray));
365                next($sortArray);
366                $returnArray[$j] = $notice;
367            }
368        }
369        return $returnArray;
370    }
371
372
373    public function getNoticeById($id){
374        $f = metaFN('', '.noticeboard');
375        $array = unserialize(io_readFile($f, false));
376
377        $parentId = $array[$id];
378
379        if(!$parentId){
380            return false;
381        }
382        $file = metaFN($parentId, '.noticeboard');
383        if (!@file_exists($file)){
384            return false;
385        }
386        // load data
387        if (@file_exists($file)) {
388            $this->list = unserialize(io_readFile($file, false));
389            while($this->list->hasNext()){
390                $item = $this->list->next();
391                if($item->getId() == $id){
392                    $notice = $item;
393                }
394            }
395
396            return $notice;
397        }
398    }
399
400
401
402}
403?>
404