xref: /plugin/davcal/syntax/calendar.php (revision 52010ac70e4ea239361d794150d998bba9ab390a)
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                      'forcetimeformat' => 'no'
67                      );
68        $lastid = $ID;
69        foreach($options as $option)
70        {
71            list($key, $val) = explode('=', $option);
72            $key = strtolower(trim($key));
73            $val = trim($val);
74            switch($key)
75            {
76                case 'id':
77                    $lastid = $val;
78                    if(!in_array($val, $data['id']))
79                        $data['id'][$val] = '#3a87ad';
80                break;
81                case 'color':
82                    $data['id'][$lastid] = $val;
83                break;
84                case 'view':
85                    if(in_array($val, array('month', 'basicDay', 'basicWeek', 'agendaWeek', 'agendaDay')))
86                        $data['view'] = $val;
87                    else
88                        $data['view'] = 'month';
89                break;
90                case 'forcetimezone':
91                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
92                    if(in_array($val, $tzlist) || $val === 'no')
93                        $data['forcetimezone'] = $val;
94                    else
95                        msg($this->getLang('error_timezone_not_in_list'), -1);
96                break;
97                case 'forcetimeformat':
98                    $tfopt = array('lang', '24h', '12h');
99                    if(in_array($val, $tfopt) || $val === 'no')
100                        $data['forcetimeformat'] = $val;
101                    else
102                        msg($this->getLang('error_option_error'), -1);
103                break;
104                default:
105                    $data[$key] = $val;
106            }
107        }
108        // Handle the default case when the user didn't enter a different ID
109        if(empty($data['id']))
110        {
111            $data['id'] = array($ID => '#3a87ad');
112        }
113        // Only update the calendar name/description if the ID matches the page ID.
114        // Otherwise, the calendar is included in another page and we don't want
115        // to interfere with its data.
116        if(in_array($ID, array_keys($data['id'])))
117        {
118            if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
119                $username = $_SERVER['REMOTE_USER'];
120            else
121                $username = uniqid('davcal-');
122            $this->hlp->setCalendarNameForPage($data['name'], $data['description'], $ID, $username);
123            $this->hlp->setCalendarColorForPage($data['id'][$ID], $ID);
124        }
125
126        p_set_metadata($ID, array('plugin_davcal' => $data));
127
128        return $data;
129    }
130
131    /**
132     * Create output
133     */
134    function render($format, &$R, $data) {
135        if($format != 'xhtml') return false;
136        global $ID;
137        $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
138
139        // Render the Calendar. Timezone list is within a hidden div,
140        // the calendar ID is in a data-calendarid tag.
141        if($data['forcetimezone'] !== 'no')
142            $R->doc .= '<div id="fullCalendarTimezoneWarning">'.sprintf($this->getLang('this_calendar_uses_timezone'), $data['forcetimezone']).'</div>';
143        $R->doc .= '<div id="fullCalendar" data-calendarpage="'.$ID.'"></div>';
144        $R->doc .= '<div id="fullCalendarTimezoneList" class="fullCalendarTimezoneList" style="display:none">';
145        $R->doc .= '<select id="fullCalendarTimezoneDropdown">';
146        $R->doc .= '<option value="local">'.$this->getLang('local_time').'</option>';
147        foreach($tzlist as $tz)
148        {
149            $R->doc .= '<option value="'.$tz.'">'.$tz.'</option>';
150        }
151        $R->doc .= '</select></div>';
152        if(($this->getConf('hide_settings') !== 1) && ($data['settings'] !== 'hide'))
153        {
154            $R->doc .= '<div class="fullCalendarSettings"><a href="#" class="fullCalendarSettings"><img src="'.DOKU_URL.'lib/plugins/davcal/images/settings.png'.'">'.$this->getLang('settings').'</a></div>';
155        }
156
157    }
158
159
160
161}
162
163// vim:ts=4:sw=4:et:enc=utf-8:
164