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