xref: /plugin/wikicalendar-ng/syntax.php (revision 180fbbb2e03e2958a4909454776e1684b9091573)
1<?php
2/**
3 * DokuWiki Syntax Plugin WikiCalendar
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Michael Klier <chi@chimeric.de>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13/**
14 * All DokuWiki plugins to extend the parser/rendering mechanism
15 * need to inherit from this class
16 */
17class syntax_plugin_wikicalendar extends DokuWiki_Syntax_Plugin {
18
19    /**
20     * namespace of the calendar
21     */
22    var $calendar_ns    = '';
23
24    /**
25     * namespace of the current viewed month
26     */
27    var $month_ns       = '';
28
29    /**
30     * indicator if first week of the month has been generated
31     */
32    var $firstWeek      = false;
33
34    /**
35     * array with localisations of weekdays
36     */
37    var $langDays       = array();
38
39    /**
40     * array with localistaions of month
41     */
42    var $langMonth      = array();
43
44    /**
45     * the current date
46     */
47    var $curDate        = array();
48
49    /**
50     * the month to show
51     */
52    var $showMonth      = '';
53
54    /**
55     * the year to show
56     */
57    var $showYear       = '';
58
59    /**
60     * the global timestamp for date-operations
61     */
62    var $gTimestamp     = '';
63
64    /**
65     * number days of the current month
66     */
67    var $numDays        = '';
68
69    /**
70     * date-date to generate the calendar
71     */
72    var $viewDate       = array();
73
74    /**
75     * return some info
76     */
77    function getInfo(){
78        return array(
79            'author' => 'Michael Klier (chi)',
80            'email'  => 'chi@chimeric.de',
81            'date'   => @file_get_contents(DOKU_PLUGIN.'wikicalendar/VERSION'),
82            'name'   => 'WikiCalendar Plugin',
83            'desc'   => 'Implements a simple Calendar with links to wikipages.',
84            'url'    => 'http://dokuwiki.org/plugin:wikicalendar'
85        );
86    }
87
88    /**
89     * Some Information first.
90     */
91    function getType() { return 'substition'; }
92    function getPType() { return 'block'; }
93    function getSort() { return 125; }
94
95    /**
96     * Connect pattern to lexer
97     */
98    function connectTo($mode) {
99        $this->Lexer->addSpecialPattern('{{cal>.+?}}',$mode,'plugin_wikicalendar');
100    }
101
102    /**
103     * Handle the match
104     */
105    function handle($match, $state, $pos, &$handler){
106        $match = substr($match,6,-2);
107        return array($match);
108    }
109
110    /**
111     * Create output
112     */
113    function render($mode, &$renderer, $data) {
114        global $ID;
115        global $conf;
116
117        // define some variables first
118        $this->calendar_ns  = ($data[0]) ? $data[0] : $ID;
119        $this->langDays     = $this->getLang('days');
120        $this->langMonth    = $this->getLang('month');
121        $this->curDate      = getdate(time());
122        $this->showMonth    = (is_numeric($_REQUEST['plugin_wikicalendar_month'])) ? $_REQUEST['plugin_wikicalendar_month'] : $this->curDate['mon'];
123        $this->showYear     = (is_numeric($_REQUEST['plugin_wikicalendar_year']))  ? $_REQUEST['plugin_wikicalendar_year']  : $this->curDate['year'];
124        $this->gTimestamp   = mktime(0,0,0,$this->showMonth,1,$this->showYear);
125        $this->numDays      = date('t',$this->gTimestamp);
126        $this->viewDate     = getdate($this->gTimestamp);
127        $this->today        = ($this->viewDate['mon'] == $this->curDate['mon'] &&
128                               $this->viewDate['year'] == $this->curDate['year']) ?
129                               $this->curDate['mday'] : null;
130
131        // if month directory exists we keep the old scheme
132        if(is_dir($conf['datadir'].'/'.str_replace(':','/',$this->calendar_ns.':'.$this->showYear.':'.$this->showMonth))) {
133            $this->month_ns = $this->calendar_ns.':'.$this->showYear.':'.$this->showMonth;
134        } else {
135            if($this->showMonth < 10) {
136                $this->month_ns = $this->calendar_ns.':'.$this->showYear.':0'.$this->showMonth;
137            } else {
138                $this->month_ns = $this->calendar_ns.':'.$this->showYear.':'.$this->showMonth;
139            }
140        }
141
142        if($this->MonthStart == 7 && $this->getConf('weekstart') == 'Sunday') {
143            $this->MonthStart = 0;
144        } else {
145            $this->MonthStart = ($this->viewDate['wday'] == 0) ? 7 : $this->viewDate['wday'];
146        }
147
148        if($mode == 'xhtml'){
149            // turn off caching
150            $renderer->info['cache'] = false;
151
152            $renderer->doc .= '<div class="plugin_wikicalendar">' . DOKU_LF;
153            $renderer->doc .= $this->_month_xhtml();
154            $renderer->doc .= $this->_form_go2();
155            $renderer->doc .= '</div>' . DOKU_LF;
156
157            return true;
158        }
159        return false;
160    }
161
162    /**
163     * Renders the Calendar (month-view)
164     *
165     * @author Michael Klier <chi@chimeric.de>
166     */
167    function _month_xhtml() {
168        global $ID;
169
170        $script = script();
171
172        $prevMonth  = ($this->showMonth-1 > 0)  ? ($this->showMonth-1) : 12;
173        $nextMonth  = ($this->showMonth+1 < 13) ? ($this->showMonth+1) : 1;
174
175        switch(true) {
176            case($prevMonth == 12):
177                $prevYear = ($this->showYear-1);
178                $nextYear = $this->showYear;
179                break;
180            case($nextMonth == 1):
181                $nextYear = ($this->showYear+1);
182                $prevYear = $this->showYear;
183                break;
184            default:
185                $prevYear = $this->showYear;
186                $nextYear = $this->showYear;
187                break;
188        }
189
190        // create calendar-header
191        $out .= <<<CALHEAD
192<table class="plugin_wikicalendar">
193  <tr>
194    <th class="month">
195      <form action="{$script}" method="post" class="prevnext">
196        <input type="hidden" name="id" value="{$ID}" />
197        <input type="hidden" name="plugin_wikicalendar_year" value="{$prevYear}" />
198        <input type="hidden" name="plugin_wikicalendar_month" value="{$prevMonth}" />
199        <input type="submit" class="btn_prev_month" name="submit" value="&larr;" accesskey="P" title="{$this->langMonth[$prevMonth]} {$prevYear}" />
200      </form>
201    </th>
202    <th class="blank">&nbsp;</th>
203    <th class="blank">&nbsp;</th>
204    <th class="month">{$this->langMonth[$this->viewDate['mon']]}<br />{$this->showYear}<br /></th>
205    <th class="blank">&nbsp;</th>
206    <th class="blank">&nbsp;</th>
207    <th class="month">
208      <form action="{$script}" method="post" class="prevnext">
209        <input type="hidden" name="id" value="{$ID}" />
210        <input type="hidden" name="plugin_wikicalendar_year" value="{$nextYear}" />
211        <input type="hidden" name="plugin_wikicalendar_month" value="{$nextMonth}" />
212        <input type="submit" class="btn_next_month" name="submit" value="&rarr;" accesskey="N" title="{$this->langMonth[$nextMonth]} {$nextYear}" />
213      </form>
214    </th>
215  </tr>
216CALHEAD;
217
218        // create calendar weekday-headers
219        $out .= "<tr>";
220        if($this->getConf('weekstart') == 'Sunday') {
221            $last = array_pop($this->langDays);
222            array_unshift($this->langDays, $last);
223        }
224        foreach($this->langDays as $day) {
225            $out .= '<td class="weekday">'.$day.'</td>';
226        }
227        $out .= "</tr>\n";
228
229        // create calendar-body
230        for($i=1;$i<=$this->numDays;$i++) {
231            $day = $i;
232            //set day-wikipage - use leading zeros on new pages
233            if($day < 10) {
234                if(page_exists($this->month_ns.':'.$day)) {
235                    $dayWP = $this->month_ns.':'.$day;
236                } else {
237                    $dayWP = $this->month_ns.':0'.$day;
238                }
239            } else {
240                $dayWP = $this->month_ns.':'.$day;
241            }
242            // close row at end of week
243            if($wd == 7) $out .= '</tr>';
244            // set weekday
245            if(!isset($wd) or $wd == 7) { $wd = 0; }
246            // start new row when new week starts
247            if($wd == 0) $out .= '<tr>';
248
249            // create blank fields up to the first day of the month
250            $offset = ($this->getConf('weekstart') == 'Sunday') ? 0 : 1;
251            if(!$this->firstWeek) {
252                while($wd < ($this->MonthStart - $offset)) {
253                    $out .= '<td class="blank">&nbsp;</td>';
254                    $wd++;
255                }
256                // ok - first week is printet
257                $this->firstWeek = true;
258            }
259
260            // check for today
261            if($this->today == $day) {
262                $out .= '<td class="today">'.$this->_calendar_day($dayWP,$day).'</td>';
263            } else {
264                $out .= '<td class="day">'.$this->_calendar_day($dayWP,$day).'</td>';
265            }
266
267            // fill remaining days with blanks
268            if($i == $this->numDays && $wd < 7) {
269                while($wd<7) {
270                    $out .= '<td class="blank">&nbsp;</td>';
271                    $wd++;
272                }
273                $out .= '</tr>';
274            }
275
276            // dont forget to count weekdays
277            $wd++;
278        }
279
280        // finally close the table
281        $out .= '</table>';
282
283        return ($out);
284    }
285
286    /**
287     * Generates the content of each day in the calendar-table.
288     *
289     * @author Michael Klier <chi@chimeric.de>
290     */
291    function _calendar_day($wp, $day) {
292        global $lang;
293        global $ID;
294
295        if(file_exists(wikiFN($wp))) {
296            $out .= '<div class="isevent">';
297            if(auth_quickaclcheck($wp) >= AUTH_READ) {
298                $out .= '<a href="' . wl($wp, array('do' => 'edit', 'plugin_wikicalendar_redirect_id' => $ID, 'plugin_wikicalendar_month' => $this->showMonth, 'plugin_wikicalendar_year' => $this->showYear)) . '" class="plugin_wikicalendar_btn" title="' . $lang['btn_edit'] . '"></a>' . DOKU_LF;
299            }
300            $out .= '<div class="day_num"><a href="' . wl($wp) . '" class="wikilink1" title="' . $wp . '">'.$day.'</a></div>';
301            $out .= '<div class="abstract">' . p_get_metadata($wp, 'description abstract') . '</div>' . DOKU_LF;
302        } else {
303            $out .= '<div class="noevent">';
304            if(auth_quickaclcheck($wp) >= AUTH_CREATE) {
305                //$out .= $this->_btn_add_day($wp);
306                $out .= '<a href="' . wl($wp, array('do' => 'edit', 'plugin_wikicalendar_redirect_id' => $ID, 'plugin_wikicalendar_month' => $this->showMonth, 'plugin_wikicalendar_year' => $this->showYear)) . '" class="plugin_wikicalendar_btn" title="' . $lang['btn_create'] . '"></a>' . DOKU_LF;
307            }
308            $out .= '<div class="day_num">'.$day.'</div>';
309        }
310        $out .= '</div>';
311        return ($out);
312    }
313
314    /**
315     * Generates a From to jump to selected dates
316     *
317     * @author Michael Klier <chi@chimeric.de>
318     */
319    function _form_go2() {
320        global $ID;
321
322        $out .= '<table class="inline plugin_wikicalendar_go2">' . DOKU_LF;
323
324        $out .= '<form action="'.script().'" method="post">' . DOKU_LF;
325        $out .= '<tr class="default">' . DOKU_LF;
326        $out .= '<td><label>'.$this->getLang('year').':</label></td>' . DOKU_LF;
327        $out .= '<td><div class="input">' . DOKU_LF;
328        $out .= '<select id="year" name="plugin_wikicalendar_year">' . DOKU_LF;
329
330        $year_start = ($this->showYear != $this->curDate['year']) ? $this->showYear - 10 : $this->curDate['year'] - 5;
331        $year_end   = $this->showYear + 10;
332
333        for($i=$year_start;$i<=$year_end;$i++) {
334            if($i == $this->showYear || $i == $this->curDate['year']) {
335                $out .= '<option value="'.$i.'" selected="selected">'.$i.'</option>' . DOKU_LF;
336            } else {
337                $out .= '<option value="'.$i.'">'.$i.'</option>' . DOKU_LF;
338            }
339        }
340
341        $out .= '</select>' . DOKU_LF;
342        $out .= '</div>' . DOKU_LF;
343        $out .= '</td>' . DOKU_LF;
344        $out .= '<td><label>'.$this->getLang('mon').':</label></td>' . DOKU_LF;
345        $out .= '<td><div class="input">' . DOKU_LF;
346        $out .= '<select id="month" name="plugin_wikicalendar_month">' . DOKU_LF;
347
348        for($i=1;$i<=12;$i++) {
349            if($i == $this->showMonth) {
350                $out .= '<option value="'.$i.'" selected="selected">'.$this->langMonth[$i].'</option>' . DOKU_LF;
351            } else {
352                $out .= '<option value="'.$i.'">'.$this->langMonth[$i].'</option>' . DOKU_LF;
353            }
354        }
355
356        $out .= '</select>' . DOKU_LF;
357        $out .= '</div>' . DOKU_LF;
358        $out .= '</td>' . DOKU_LF;
359        $out .= '<td><input type="hidden" name="id" value="'.$ID.'" />' . DOKU_LF;
360        $out .= '<input type="submit" class="button" name="go2" value="'.$this->getLang('go').'" />' . DOKU_LF;
361        $out .= '</form>' . DOKU_LF;
362
363        if($this->showYear != $this->curDate['year'] or $this->showMonth != $this->curDate['mon']) {
364            $out .= '<form action="'.script().'" method="post" class="go2" onsubmit="">' . DOKU_LF;
365            $out .= '<input type="hidden" name="id" value="'.$ID.'" />' . DOKU_LF;
366            $out .= '<input type="submit" class="button" name="back2cur" value="'.$this->getLang('current').'" />' . DOKU_LF;
367            $out .= '</form>' . DOKU_LF;
368        } else {
369            $out .= '</td></tr>' . DOKU_LF;
370        }
371
372        $out .= '</table>' . DOKU_LF;
373
374        return ($out);
375    }
376}
377//Setup Vim: tabstop=4 enc=utf8
378