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