xref: /plugin/wikicalendar-ng/syntax.php (revision 5d6390e4a0dd870f24469028afcc4cbbc75cb000)
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        if($this->getConf('weekstart') == 'Sunday') {
217            $last = array_pop($this->langDays);
218            array_unshift($this->langDays, $last);
219        }
220        foreach($this->langDays as $day) {
221            $out .= '<td class="weekday">'.$day.'</td>';
222        }
223        $out .= "</tr>\n";
224
225        // create calendar-body
226        for($i=1;$i<=$this->numDays;$i++) {
227            $day = $i;
228            //set day-wikipage - use leading zeros on new pages
229            if($day < 10) {
230                if(page_exists($this->month_ns.':'.$day)) {
231                    $dayWP = $this->month_ns.':'.$day;
232                } else {
233                    $dayWP = $this->month_ns.':0'.$day;
234                }
235            } else {
236                $dayWP = $this->month_ns.':'.$day;
237            }
238            // close row at end of week
239            if($wd == 7) $out .= '</tr>';
240            // set weekday
241            if(!isset($wd) or $wd == 7) { $wd = 0; }
242            // start new row when new week starts
243            if($wd == 0) $out .= '<tr>';
244
245            // create blank fields up to the first day of the month
246            $offset = ($this->getConf('weekstart') == 'Sunday') ? 0 : 1;
247            if(!$this->firstWeek) {
248                while($wd < ($this->MonthStart - $offset)) {
249                    $out .= '<td class="blank">&nbsp;</td>';
250                    $wd++;
251                }
252                // ok - first week is printet
253                $this->firstWeek = true;
254            }
255
256            // check for today
257            if($this->today == $day) {
258                $out .= '<td class="today">'.$this->_calendar_day($dayWP,$day).'</td>';
259            } else {
260                $out .= '<td class="day">'.$this->_calendar_day($dayWP,$day).'</td>';
261            }
262
263            // fill remaining days with blanks
264            if($i == $this->numDays && $wd < 7) {
265                while($wd<7) {
266                    $out .= '<td class="blank">&nbsp;</td>';
267                    $wd++;
268                }
269                $out .= '</tr>';
270            }
271
272            // dont forget to count weekdays
273            $wd++;
274        }
275
276        // finally close the table
277        $out .= '</table>';
278
279        return ($out);
280    }
281
282    /**
283     * Generates the content of each day in the calendar-table.
284     *
285     * @author Michael Klier <chi@chimeric.de>
286     */
287    function _calendar_day($wp, $day) {
288        global $lang;
289        global $ID;
290
291        if(file_exists(wikiFN($wp))) {
292            $out .= '<div class="isevent">';
293            if(auth_quickaclcheck($wp) >= AUTH_READ) {
294                $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;
295            }
296            $out .= '<div class="day_num"><a href="' . wl($wp) . '" class="wikilink1" title="' . $wp . '">'.$day.'</a></div>';
297            $out .= '<div class="abstract">' . p_get_metadata($wp, 'description abstract') . '</div>' . DOKU_LF;
298        } else {
299            $out .= '<div class="noevent">';
300            if(auth_quickaclcheck($wp) >= AUTH_CREATE) {
301                //$out .= $this->_btn_add_day($wp);
302                $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;
303            }
304            $out .= '<div class="day_num">'.$day.'</div>';
305        }
306        $out .= '</div>';
307        return ($out);
308    }
309
310    /**
311     * Generates a From to jump to selected dates
312     *
313     * @author Michael Klier <chi@chimeric.de>
314     */
315    function _form_go2() {
316        global $ID;
317
318        $out .= '<table class="inline plugin_wikicalendar_go2">' . DOKU_LF;
319
320        $out .= '<form action="'.script().'" method="post">' . DOKU_LF;
321        $out .= '<tr class="default">' . DOKU_LF;
322        $out .= '<td><label>'.$this->getLang('year').':</label></td>' . DOKU_LF;
323        $out .= '<td><div class="input">' . DOKU_LF;
324        $out .= '<select id="year" name="plugin_wikicalendar_year">' . DOKU_LF;
325
326        $year_start = ($this->showYear != $this->curDate['year']) ? $this->showYear - 10 : $this->curDate['year'] - 5;
327        $year_end   = $this->showYear + 10;
328
329        for($i=$year_start;$i<=$year_end;$i++) {
330            if($i == $this->showYear || $i == $this->curDate['year']) {
331                $out .= '<option value="'.$i.'" selected="selected">'.$i.'</option>' . DOKU_LF;
332            } else {
333                $out .= '<option value="'.$i.'">'.$i.'</option>' . DOKU_LF;
334            }
335        }
336
337        $out .= '</select>' . DOKU_LF;
338        $out .= '</div>' . DOKU_LF;
339        $out .= '</td>' . DOKU_LF;
340        $out .= '<td><label>'.$this->getLang('mon').':</label></td>' . DOKU_LF;
341        $out .= '<td><div class="input">' . DOKU_LF;
342        $out .= '<select id="month" name="plugin_wikicalendar_month">' . DOKU_LF;
343
344        for($i=1;$i<=12;$i++) {
345            if($i == $this->showMonth) {
346                $out .= '<option value="'.$i.'" selected="selected">'.$this->langMonth[$i].'</option>' . DOKU_LF;
347            } else {
348                $out .= '<option value="'.$i.'">'.$this->langMonth[$i].'</option>' . DOKU_LF;
349            }
350        }
351
352        $out .= '</select>' . DOKU_LF;
353        $out .= '</div>' . DOKU_LF;
354        $out .= '</td>' . DOKU_LF;
355        $out .= '<td><input type="hidden" name="id" value="'.$ID.'" />' . DOKU_LF;
356        $out .= '<input type="submit" class="button" name="go2" value="'.$this->getLang('go').'" />' . DOKU_LF;
357        $out .= '</form>' . DOKU_LF;
358
359        if($this->showYear != $this->curDate['year'] or $this->showMonth != $this->curDate['mon']) {
360            $out .= '<form action="'.script().'" method="post" class="go2" onsubmit="">' . DOKU_LF;
361            $out .= '<input type="hidden" name="id" value="'.$ID.'" />' . DOKU_LF;
362            $out .= '<input type="submit" class="button" name="back2cur" value="'.$this->getLang('current').'" />' . DOKU_LF;
363            $out .= '</form>' . DOKU_LF;
364        } else {
365            $out .= '</td></tr>' . DOKU_LF;
366        }
367
368        $out .= '</table>' . DOKU_LF;
369
370        return ($out);
371    }
372}
373//Setup Vim: tabstop=4 enc=utf8
374