xref: /plugin/davcal/syntax/calendar.php (revision 1d5bdcd0daf69d42a648436e4034819d35b1879d)
1<?php
2/**
3 * DokuWiki Plugin DAVCal (Calendar Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Böhler <dev@aboehler.at>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_davcal_calendar extends DokuWiki_Syntax_Plugin {
16
17    protected $hlp = null;
18
19    // Load the helper plugin
20    public function syntax_plugin_davcal_calendar() {
21        $this->hlp =& plugin_load('helper', 'davcal');
22    }
23
24
25    /**
26     * What kind of syntax are we?
27     */
28    function getType(){
29        return 'substition';
30    }
31
32    /**
33     * What about paragraphs?
34     */
35    function getPType(){
36        return 'normal';
37    }
38
39    /**
40     * Where to sort in?
41     */
42    function getSort(){
43        return 165;
44    }
45
46    /**
47     * Connect pattern to lexer
48     */
49    function connectTo($mode) {
50        $this->Lexer->addSpecialPattern('\{\{davcal>[^}]*\}\}',$mode,'plugin_davcal_calendar');
51    }
52
53    /**
54     * Handle the match
55     */
56    function handle($match, $state, $pos, &$handler){
57        global $ID;
58        $options = trim(substr($match,9,-2));
59        $options = explode(',', $options);
60        $data = array('name' => $ID,
61                      'description' => $this->getLang('created_by_davcal'),
62                      'id' => array(),
63                      'settings' => 'show',
64                      'view' => 'month',
65                      'forcetimezone' => 'no'
66                      );
67        $lastid = $ID;
68        foreach($options as $option)
69        {
70            list($key, $val) = explode('=', $option);
71            $key = strtolower(trim($key));
72            $val = trim($val);
73            switch($key)
74            {
75                case 'id':
76                    $lastid = $val;
77                    if(!in_array($val, $data['id']))
78                        $data['id'][$val] = '#3a87ad';
79                break;
80                case 'color':
81                    $data['id'][$lastid] = $val;
82                break;
83                case 'view':
84                    if(in_array($val, array('month', 'basicDay', 'basicWeek', 'agendaWeek', 'agendaDay')))
85                        $data['view'] = $val;
86                    else
87                        $data['view'] = 'month';
88                break;
89                case 'forcetimezone':
90                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
91                    if(in_array($val, $tzlist) || $val === 'no')
92                        $data['forcetimezone'] = $val;
93                    else
94                        msg($this->getLang('error_timezone_not_in_list'), -1);
95                break;
96                default:
97                    $data[$key] = $val;
98            }
99        }
100        // Handle the default case when the user didn't enter a different ID
101        if(empty($data['id']))
102        {
103            $data['id'] = array($ID => '#3a87ad');
104        }
105        // Only update the calendar name/description if the ID matches the page ID.
106        // Otherwise, the calendar is included in another page and we don't want
107        // to interfere with its data.
108        if(in_array($ID, array_keys($data['id'])))
109        {
110            if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
111                $username = $_SERVER['REMOTE_USER'];
112            else
113                $username = uniqid('davcal-');
114            $this->hlp->setCalendarNameForPage($data['name'], $data['description'], $ID, $username);
115            $this->hlp->setCalendarColorForPage($data['id'][$ID], $ID);
116        }
117
118        p_set_metadata($ID, array('plugin_davcal' => $data));
119
120        return $data;
121    }
122
123    /**
124     * Create output
125     */
126    function render($format, &$R, $data) {
127        if($format != 'xhtml') return false;
128        global $ID;
129        $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
130
131        // Render the Calendar. Timezone list is within a hidden div,
132        // the calendar ID is in a data-calendarid tag.
133        if($data['forcetimezone'] !== 'no')
134            $R->doc .= '<div id="fullCalendarTimezoneWarning">'.sprintf($this->getLang('this_calendar_uses_timezone'), $data['forcetimezone']).'</div>';
135        $R->doc .= '<div id="fullCalendar" data-calendarpage="'.$ID.'"></div>';
136        $R->doc .= '<div id="fullCalendarTimezoneList" class="fullCalendarTimezoneList" style="display:none">';
137        $R->doc .= '<select id="fullCalendarTimezoneDropdown">';
138        $R->doc .= '<option value="local">'.$this->getLang('local_time').'</option>';
139        foreach($tzlist as $tz)
140        {
141            $R->doc .= '<option value="'.$tz.'">'.$tz.'</option>';
142        }
143        $R->doc .= '</select></div>';
144        if(($this->getConf('hide_settings') !== 1) && ($data['settings'] !== 'hide'))
145        {
146            $R->doc .= '<div class="fullCalendarSettings"><a href="#" class="fullCalendarSettings"><img src="'.DOKU_URL.'lib/plugins/davcal/images/settings.png'.'">'.$this->getLang('settings').'</a></div>';
147        }
148
149    }
150
151
152
153}
154
155// vim:ts=4:sw=4:et:enc=utf-8:
156