xref: /plugin/wikicalendar-ng/syntax.php (revision 6d279233ed5b0cf7c259f7c90ba94b1776ea121c)
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        $this->MonthStart   = ($this->viewDate['wday'] == 0) ? 7 : $this->viewDate['wday'];
143
144        if($mode == 'xhtml'){
145            // turn off caching
146            $renderer->info['cache'] = false;
147
148            $renderer->doc .= '<div class="plugin_wikicalendar">' . DOKU_LF;
149            $renderer->doc .= $this->_month_xhtml();
150            $renderer->doc .= $this->_form_go2();
151            $renderer->doc .= '</div>' . DOKU_LF;
152
153            return true;
154        }
155        return false;
156    }
157
158    /**
159     * Renders the Calendar (month-view)
160     *
161     * @author Michael Klier <chi@chimeric.de>
162     */
163    function _month_xhtml() {
164        global $ID;
165
166        $script = script();
167
168        $prevMonth  = ($this->showMonth-1 > 0)  ? ($this->showMonth-1) : 12;
169        $nextMonth  = ($this->showMonth+1 < 13) ? ($this->showMonth+1) : 1;
170
171        switch(true) {
172            case($prevMonth == 12):
173                $prevYear = ($this->showYear-1);
174                $nextYear = $this->showYear;
175                break;
176            case($nextMonth == 1):
177                $nextYear = ($this->showYear+1);
178                $prevYear = $this->showYear;
179                break;
180            default:
181                $prevYear = $this->showYear;
182                $nextYear = $this->showYear;
183                break;
184        }
185
186        // create calendar-header
187        $out .= <<<CALHEAD
188<table class="plugin_wikicalendar">
189  <tr>
190    <th class="month">
191      <form action="{$script}" method="post" class="prevnext">
192        <input type="hidden" name="id" value="{$ID}" />
193        <input type="hidden" name="plugin_wikicalendar_year" value="{$prevYear}" />
194        <input type="hidden" name="plugin_wikicalendar_month" value="{$prevMonth}" />
195        <input type="submit" class="btn_prev_month" name="submit" value="&larr;" accesskey="P" title="{$this->langMonth[$prevMonth]} {$prevYear}" />
196      </form>
197    </th>
198    <th class="blank">&nbsp;</th>
199    <th class="blank">&nbsp;</th>
200    <th class="month">{$this->langMonth[$this->viewDate['mon']]}<br />{$this->showYear}<br /></th>
201    <th class="blank">&nbsp;</th>
202    <th class="blank">&nbsp;</th>
203    <th class="month">
204      <form action="{$script}" method="post" class="prevnext">
205        <input type="hidden" name="id" value="{$ID}" />
206        <input type="hidden" name="plugin_wikicalendar_year" value="{$nextYear}" />
207        <input type="hidden" name="plugin_wikicalendar_month" value="{$nextMonth}" />
208        <input type="submit" class="btn_next_month" name="submit" value="&rarr;" accesskey="N" title="{$this->langMonth[$nextMonth]} {$nextYear}" />
209      </form>
210    </th>
211  </tr>
212CALHEAD;
213
214        // create calendar weekday-headers
215        $out .= "<tr>";
216        foreach($this->langDays as $day) {
217            $out .= '<td class="weekday">'.$day.'</td>';
218        }
219        $out .= "</tr>\n";
220
221        // create calendar-body
222        for($i=1;$i<=$this->numDays;$i++) {
223            $day = $i;
224            //set day-wikipage - use leading zeros on new pages
225            if($day < 10) {
226                if(page_exists($this->month_ns.':'.$day)) {
227                    $dayWP = $this->month_ns.':'.$day;
228                } else {
229                    $dayWP = $this->month_ns.':0'.$day;
230                }
231            } else {
232                $dayWP = $this->month_ns.':'.$day;
233            }
234            // close row at end of week
235            if($wd == 7) $out .= '</tr>';
236            // set weekday
237            if(!isset($wd) or $wd == 7) { $wd = 0; }
238            // start new row when new week starts
239            if($wd == 0) $out .= '<tr>';
240
241            // create blank fields up to the first day of the month
242            if(!$this->firstWeek) {
243                while($wd < ($this->MonthStart-1)) {
244                    $out .= '<td class="blank">&nbsp;</td>';
245                    $wd++;
246                }
247                // ok - first week is printet
248                $this->firstWeek = true;
249            }
250
251            // check for today
252            if($this->today == $day) {
253                $out .= '<td class="today">'.$this->_calendar_day($dayWP,$day).'</td>';
254            } else {
255                $out .= '<td class="day">'.$this->_calendar_day($dayWP,$day).'</td>';
256            }
257
258            // fill remaining days with blanks
259            if($i == $this->numDays && $wd < 7) {
260                while($wd<7) {
261                    $out .= '<td class="blank">&nbsp;</td>';
262                    $wd++;
263                }
264                $out .= '</tr>';
265            }
266
267            // dont forget to count weekdays
268            $wd++;
269        }
270
271        // finally close the table
272        $out .= '</table>';
273
274        return ($out);
275    }
276
277    /**
278     * Generates the content of each day in the calendar-table.
279     *
280     * @author Michael Klier <chi@chimeric.de>
281     */
282    function _calendar_day($wp, $day) {
283        global $lang;
284        global $ID;
285
286        if(file_exists(wikiFN($wp))) {
287            $out .= '<div class="isevent">';
288            if(auth_quickaclcheck($wp) >= AUTH_READ) {
289                $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;
290            }
291            $out .= '<div class="day_num"><a href="' . wl($wp) . '" class="wikilink1" title="' . $wp . '">'.$day.'</a></div>';
292            $out .= '<div class="abstract">' . p_get_metadata($wp, 'description abstract') . '</div>' . DOKU_LF;
293        } else {
294            $out .= '<div class="noevent">';
295            if(auth_quickaclcheck($wp) >= AUTH_CREATE) {
296                //$out .= $this->_btn_add_day($wp);
297                $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;
298            }
299            $out .= '<div class="day_num">'.$day.'</div>';
300        }
301        $out .= '</div>';
302        return ($out);
303    }
304
305    /**
306     * Generates a From to jump to selected dates
307     *
308     * @author Michael Klier <chi@chimeric.de>
309     */
310    function _form_go2() {
311        global $ID;
312
313        $out .= '<table class="inline plugin_wikicalendar_go2">' . DOKU_LF;
314
315        $out .= '<form action="'.script().'" method="post">' . DOKU_LF;
316        $out .= '<tr class="default">' . DOKU_LF;
317        $out .= '<td><label>'.$this->getLang('year').':</label></td>' . DOKU_LF;
318        $out .= '<td><div class="input">' . DOKU_LF;
319        $out .= '<select id="year" name="plugin_wikicalendar_year">' . DOKU_LF;
320
321        $year_start = ($this->showYear != $this->curDate['year']) ? $this->showYear - 10 : $this->curDate['year'] - 5;
322        $year_end   = $this->showYear + 10;
323
324        for($i=$year_start;$i<=$year_end;$i++) {
325            if($i == $this->showYear || $i == $this->curDate['year']) {
326                $out .= '<option value="'.$i.'" selected="selected">'.$i.'</option>' . DOKU_LF;
327            } else {
328                $out .= '<option value="'.$i.'">'.$i.'</option>' . DOKU_LF;
329            }
330        }
331
332        $out .= '</select>' . DOKU_LF;
333        $out .= '</div>' . DOKU_LF;
334        $out .= '</td>' . DOKU_LF;
335        $out .= '<td><label>'.$this->getLang('mon').':</label></td>' . DOKU_LF;
336        $out .= '<td><div class="input">' . DOKU_LF;
337        $out .= '<select id="month" name="plugin_wikicalendar_month">' . DOKU_LF;
338
339        for($i=1;$i<=12;$i++) {
340            if($i == $this->showMonth) {
341                $out .= '<option value="'.$i.'" selected="selected">'.$this->langMonth[$i].'</option>' . DOKU_LF;
342            } else {
343                $out .= '<option value="'.$i.'">'.$this->langMonth[$i].'</option>' . DOKU_LF;
344            }
345        }
346
347        $out .= '</select>' . DOKU_LF;
348        $out .= '</div>' . DOKU_LF;
349        $out .= '</td>' . DOKU_LF;
350        $out .= '<td><input type="hidden" name="id" value="'.$ID.'" />' . DOKU_LF;
351        $out .= '<input type="submit" class="button" name="go2" value="'.$this->getLang('go').'" />' . DOKU_LF;
352        $out .= '</form>' . DOKU_LF;
353
354        if($this->showYear != $this->curDate['year'] or $this->showMonth != $this->curDate['mon']) {
355            $out .= '<form action="'.script().'" method="post" class="go2" onsubmit="">' . DOKU_LF;
356            $out .= '<input type="hidden" name="id" value="'.$ID.'" />' . DOKU_LF;
357            $out .= '<input type="submit" class="button" name="back2cur" value="'.$this->getLang('current').'" />' . DOKU_LF;
358            $out .= '</form>' . DOKU_LF;
359        } else {
360            $out .= '</td></tr>' . DOKU_LF;
361        }
362
363        $out .= '</table>' . DOKU_LF;
364
365        return ($out);
366    }
367}
368//Setup Vim: tabstop=4 enc=utf8
369