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
9class syntax_plugin_davcal_calendar extends DokuWiki_Syntax_Plugin {
10
11    protected $hlp = null;
12
13    // Load the helper plugin
14    public function __construct() {
15        $this->hlp =& plugin_load('helper', 'davcal');
16    }
17
18
19    /**
20     * What kind of syntax are we?
21     */
22    function getType(){
23        return 'substition';
24    }
25
26    /**
27     * What about paragraphs?
28     */
29    function getPType(){
30        return 'normal';
31    }
32
33    /**
34     * Where to sort in?
35     */
36    function getSort(){
37        return 165;
38    }
39
40    /**
41     * Connect pattern to lexer
42     */
43    function connectTo($mode) {
44        $this->Lexer->addSpecialPattern('\{\{davcal>[^}]*\}\}',$mode,'plugin_davcal_calendar');
45        $this->Lexer->addSpecialPattern('\{\{davcalclient>[^}]*\}\}',$mode,'plugin_davcal_calendar');
46    }
47
48    /**
49     * Handle the match
50     */
51    function handle($match, $state, $pos, Doku_Handler $handler){
52        global $ID;
53        $data = array('name' => $ID,
54                      'description' => $this->getLang('created_by_davcal'),
55                      'id' => array(),
56                      'settings' => 'show',
57                      'view' => 'month',
58                      'forcetimezone' => 'no',
59                      'forcetimeformat' => 'no',
60                      'fcoptions' => array(),
61                      );
62        if(strpos($match, '{{davcalclient') === 0)
63        {
64            $options = trim(substr($match,15,-2));
65            $defaultId = $this->getConf('default_client_id');
66            if(isset($defaultId) && ($defaultId != ''))
67            {
68                $data['id'][$defaultId] = null;
69                $lastid = $defaultId;
70            }
71        }
72        else
73        {
74            $options = trim(substr($match,9,-2));
75            $lastid = $ID;
76        }
77        $options = explode(',', $options);
78
79        foreach($options as $option)
80        {
81            list($key, $val) = explode('=', $option);
82            $key = strtolower(trim($key));
83            $val = trim($val);
84            switch($key)
85            {
86                case 'id':
87                    $lastid = $val;
88                    if(!in_array($val, $data['id']))
89                        $data['id'][$val] = null;
90                break;
91                case 'color':
92                    $data['id'][$lastid] = $val;
93                break;
94                case 'view':
95                    if(in_array($val, array('month', 'basicDay', 'basicWeek', 'agendaWeek', 'agendaDay', 'listWeek', 'listDay', 'listMonth', 'listYear')))
96                        $data['view'] = $val;
97                    else
98                        $data['view'] = 'month';
99                break;
100                case 'fcoptions':
101                    $fcoptions = explode(';', $val);
102                    foreach($fcoptions as $opt)
103                    {
104                        list($o, $v) = explode(':', $opt, 2);
105                        $data['fcoptions'][$o] = $v;
106                    }
107                break;
108                case 'forcetimezone':
109                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
110                    if(in_array($val, $tzlist) || $val === 'no')
111                        $data['forcetimezone'] = $val;
112                    else
113                        msg($this->getLang('error_timezone_not_in_list'), -1);
114                break;
115                case 'forcetimeformat':
116                    $tfopt = array('lang', '24h', '12h');
117                    if(in_array($val, $tfopt) || $val === 'no')
118                        $data['forcetimeformat'] = $val;
119                    else
120                        msg($this->getLang('error_option_error'), -1);
121                break;
122                default:
123                    $data[$key] = $val;
124            }
125        }
126        // Handle the default case when the user didn't enter a different ID
127        if(empty($data['id']))
128        {
129            $data['id'] = array($ID => null);
130        }
131
132        // Fix up the colors, if no color information is given
133        foreach($data['id'] as $id => $color)
134        {
135            if(is_null($color))
136            {
137                // If this is the current calendar or a WebDAV calendar, use the
138                // default color
139                if(($id === $ID) || (strpos($id, 'webdav://') === 0))
140                {
141                    $data['id'][$id] = '#3a87ad';
142                }
143                // Otherwise, retrieve the color information from the calendar settings
144                else
145                {
146                    $calid = $this->hlp->getCalendarIdForPage($ID);
147                    $settings = $this->hlp->getCalendarSettings($calid);
148                    $color = $settings['calendarcolor'];
149                    $data['id'][$id] = $color;
150                }
151            }
152        }
153
154        // Only update the calendar name/description if the ID matches the page ID.
155        // Otherwise, the calendar is included in another page and we don't want
156        // to interfere with its data.
157        if(in_array($ID, array_keys($data['id'])))
158        {
159            if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
160                $username = $_SERVER['REMOTE_USER'];
161            else
162                $username = uniqid('davcal-');
163            $this->hlp->setCalendarNameForPage($data['name'], $data['description'], $ID, $username);
164            $this->hlp->setCalendarColorForPage($data['id'][$ID], $ID);
165            $this->hlp->enableCalendarForPage($ID);
166        }
167
168        p_set_metadata($ID, array('plugin_davcal' => $data));
169
170        return $data;
171    }
172
173    /**
174     * Create output
175     */
176    function render($format, Doku_Renderer $R, $data) {
177        if($format != 'xhtml') return false;
178        global $ID;
179        $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
180
181        // Render the Calendar. Timezone list is within a hidden div,
182        // the calendar ID is in a data-calendarid tag.
183        if($data['forcetimezone'] !== 'no')
184            $R->doc .= '<div id="fullCalendarTimezoneWarning">'.sprintf($this->getLang('this_calendar_uses_timezone'), $data['forcetimezone']).'</div>';
185        $R->doc .= '<div id="fullCalendar" data-calendarpage="'.$ID.'"></div>';
186        $R->doc .= '<div id="fullCalendarTimezoneList" class="fullCalendarTimezoneList" style="display:none">';
187        $R->doc .= '<select id="fullCalendarTimezoneDropdown">';
188        $R->doc .= '<option value="local">'.$this->getLang('local_time').'</option>';
189        foreach($tzlist as $tz)
190        {
191            $R->doc .= '<option value="'.$tz.'">'.$tz.'</option>';
192        }
193        $R->doc .= '</select></div>';
194        if(($this->getConf('hide_settings') !== 1) && ($data['settings'] !== 'hide'))
195        {
196            $R->doc .= '<div class="fullCalendarSettings"><a href="#" class="fullCalendarSettings"><img src="'.DOKU_URL.'lib/plugins/davcal/images/settings.png'.'">'.$this->getLang('settings').'</a></div>';
197        }
198
199    }
200
201
202
203}
204
205// vim:ts=4:sw=4:et:enc=utf-8:
206