xref: /plugin/davcal/syntax/calendar.php (revision d00683a2d4bc1bcb8b416ce661c66c84166fab4d)
16fbb086dSAndreas Boehler<?php
26fbb086dSAndreas Boehler/**
36fbb086dSAndreas Boehler * DokuWiki Plugin DAVCal (Calendar Syntax Component)
46fbb086dSAndreas Boehler *
56fbb086dSAndreas Boehler * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
66fbb086dSAndreas Boehler * @author  Andreas Böhler <dev@aboehler.at>
76fbb086dSAndreas Boehler */
86fbb086dSAndreas Boehler
96fbb086dSAndreas Boehlerclass syntax_plugin_davcal_calendar extends DokuWiki_Syntax_Plugin {
106fbb086dSAndreas Boehler
116fbb086dSAndreas Boehler    protected $hlp = null;
126fbb086dSAndreas Boehler
136fbb086dSAndreas Boehler    // Load the helper plugin
140462f62eSGerrit Uitslag    public function __construct() {
156fbb086dSAndreas Boehler        $this->hlp =& plugin_load('helper', 'davcal');
166fbb086dSAndreas Boehler    }
176fbb086dSAndreas Boehler
186fbb086dSAndreas Boehler
196fbb086dSAndreas Boehler    /**
206fbb086dSAndreas Boehler     * What kind of syntax are we?
216fbb086dSAndreas Boehler     */
226fbb086dSAndreas Boehler    function getType(){
236fbb086dSAndreas Boehler        return 'substition';
246fbb086dSAndreas Boehler    }
256fbb086dSAndreas Boehler
266fbb086dSAndreas Boehler    /**
276fbb086dSAndreas Boehler     * What about paragraphs?
286fbb086dSAndreas Boehler     */
296fbb086dSAndreas Boehler    function getPType(){
306fbb086dSAndreas Boehler        return 'normal';
316fbb086dSAndreas Boehler    }
326fbb086dSAndreas Boehler
336fbb086dSAndreas Boehler    /**
346fbb086dSAndreas Boehler     * Where to sort in?
356fbb086dSAndreas Boehler     */
366fbb086dSAndreas Boehler    function getSort(){
376fbb086dSAndreas Boehler        return 165;
386fbb086dSAndreas Boehler    }
396fbb086dSAndreas Boehler
406fbb086dSAndreas Boehler    /**
416fbb086dSAndreas Boehler     * Connect pattern to lexer
426fbb086dSAndreas Boehler     */
436fbb086dSAndreas Boehler    function connectTo($mode) {
446fbb086dSAndreas Boehler        $this->Lexer->addSpecialPattern('\{\{davcal>[^}]*\}\}',$mode,'plugin_davcal_calendar');
451bb22c2bSAndreas Boehler        $this->Lexer->addSpecialPattern('\{\{davcalclient>[^}]*\}\}',$mode,'plugin_davcal_calendar');
466fbb086dSAndreas Boehler    }
476fbb086dSAndreas Boehler
486fbb086dSAndreas Boehler    /**
496fbb086dSAndreas Boehler     * Handle the match
506fbb086dSAndreas Boehler     */
519f1dab8cSAndreas Boehler    function handle($match, $state, $pos, Doku_Handler $handler){
526fbb086dSAndreas Boehler        global $ID;
536fbb086dSAndreas Boehler        $data = array('name' => $ID,
546fbb086dSAndreas Boehler                      'description' => $this->getLang('created_by_davcal'),
556fbb086dSAndreas Boehler                      'id' => array(),
566fbb086dSAndreas Boehler                      'settings' => 'show',
5782a48dfbSAndreas Boehler                      'view' => 'month',
5852010ac7SAndreas Boehler                      'forcetimezone' => 'no',
59b0737c19SAndreas Boehler                      'forcetimeformat' => 'no',
60b0737c19SAndreas Boehler                      'fcoptions' => array(),
616fbb086dSAndreas Boehler                      );
621bb22c2bSAndreas Boehler        if(strpos($match, '{{davcalclient') === 0)
631bb22c2bSAndreas Boehler        {
641bb22c2bSAndreas Boehler            $options = trim(substr($match,15,-2));
651bb22c2bSAndreas Boehler            $defaultId = $this->getConf('default_client_id');
661bb22c2bSAndreas Boehler            if(isset($defaultId) && ($defaultId != ''))
671bb22c2bSAndreas Boehler            {
68cd2f100dSAndreas Boehler                $data['id'][$defaultId] = null;
691bb22c2bSAndreas Boehler                $lastid = $defaultId;
701bb22c2bSAndreas Boehler            }
711bb22c2bSAndreas Boehler        }
721bb22c2bSAndreas Boehler        else
731bb22c2bSAndreas Boehler        {
741bb22c2bSAndreas Boehler            $options = trim(substr($match,9,-2));
756fbb086dSAndreas Boehler            $lastid = $ID;
761bb22c2bSAndreas Boehler        }
771bb22c2bSAndreas Boehler        $options = explode(',', $options);
781bb22c2bSAndreas Boehler
796fbb086dSAndreas Boehler        foreach($options as $option)
806fbb086dSAndreas Boehler        {
81*d00683a2Sscottleechua            $option = trim($option);
82*d00683a2Sscottleechua            if(empty($option)) continue;
83*d00683a2Sscottleechua
84*d00683a2Sscottleechua            $parts = explode('=', $option, 2);
85*d00683a2Sscottleechua            if(count($parts) < 2) continue;
86*d00683a2Sscottleechua
87*d00683a2Sscottleechua            list($key, $val) = $parts;
886fbb086dSAndreas Boehler            $key = strtolower(trim($key));
896fbb086dSAndreas Boehler            $val = trim($val);
906fbb086dSAndreas Boehler            switch($key)
916fbb086dSAndreas Boehler            {
926fbb086dSAndreas Boehler                case 'id':
936fbb086dSAndreas Boehler                    $lastid = $val;
946fbb086dSAndreas Boehler                    if(!in_array($val, $data['id']))
95cd2f100dSAndreas Boehler                        $data['id'][$val] = null;
966fbb086dSAndreas Boehler                break;
976fbb086dSAndreas Boehler                case 'color':
986fbb086dSAndreas Boehler                    $data['id'][$lastid] = $val;
996fbb086dSAndreas Boehler                break;
1006fbb086dSAndreas Boehler                case 'view':
101f1215668SAndreas Boehler                    if(in_array($val, array('month', 'basicDay', 'basicWeek', 'agendaWeek', 'agendaDay', 'listWeek', 'listDay', 'listMonth', 'listYear')))
1026fbb086dSAndreas Boehler                        $data['view'] = $val;
1036fbb086dSAndreas Boehler                    else
1046fbb086dSAndreas Boehler                        $data['view'] = 'month';
1056fbb086dSAndreas Boehler                break;
106b0737c19SAndreas Boehler                case 'fcoptions':
107b0737c19SAndreas Boehler                    $fcoptions = explode(';', $val);
108b0737c19SAndreas Boehler                    foreach($fcoptions as $opt)
109b0737c19SAndreas Boehler                    {
110*d00683a2Sscottleechua                        $opt = trim($opt);
111*d00683a2Sscottleechua                        if(empty($opt)) continue;
112*d00683a2Sscottleechua
113*d00683a2Sscottleechua                        $parts = explode(':', $opt, 2);
114*d00683a2Sscottleechua                        if(count($parts) < 2) continue;
115*d00683a2Sscottleechua
116*d00683a2Sscottleechua                        list($o, $v) = $parts;
117b0737c19SAndreas Boehler                        $data['fcoptions'][$o] = $v;
118b0737c19SAndreas Boehler                    }
119b0737c19SAndreas Boehler                break;
12082a48dfbSAndreas Boehler                case 'forcetimezone':
12182a48dfbSAndreas Boehler                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
12282a48dfbSAndreas Boehler                    if(in_array($val, $tzlist) || $val === 'no')
12382a48dfbSAndreas Boehler                        $data['forcetimezone'] = $val;
12482a48dfbSAndreas Boehler                    else
12582a48dfbSAndreas Boehler                        msg($this->getLang('error_timezone_not_in_list'), -1);
12682a48dfbSAndreas Boehler                break;
12752010ac7SAndreas Boehler                case 'forcetimeformat':
12852010ac7SAndreas Boehler                    $tfopt = array('lang', '24h', '12h');
12952010ac7SAndreas Boehler                    if(in_array($val, $tfopt) || $val === 'no')
13052010ac7SAndreas Boehler                        $data['forcetimeformat'] = $val;
13152010ac7SAndreas Boehler                    else
13252010ac7SAndreas Boehler                        msg($this->getLang('error_option_error'), -1);
13352010ac7SAndreas Boehler                break;
1346fbb086dSAndreas Boehler                default:
1356fbb086dSAndreas Boehler                    $data[$key] = $val;
1366fbb086dSAndreas Boehler            }
1376fbb086dSAndreas Boehler        }
1386fbb086dSAndreas Boehler        // Handle the default case when the user didn't enter a different ID
1396fbb086dSAndreas Boehler        if(empty($data['id']))
1406fbb086dSAndreas Boehler        {
141cd2f100dSAndreas Boehler            $data['id'] = array($ID => null);
1426fbb086dSAndreas Boehler        }
143cd2f100dSAndreas Boehler
144cd2f100dSAndreas Boehler        // Fix up the colors, if no color information is given
145cd2f100dSAndreas Boehler        foreach($data['id'] as $id => $color)
146cd2f100dSAndreas Boehler        {
147cd2f100dSAndreas Boehler            if(is_null($color))
148cd2f100dSAndreas Boehler            {
149cd2f100dSAndreas Boehler                // If this is the current calendar or a WebDAV calendar, use the
150cd2f100dSAndreas Boehler                // default color
151cd2f100dSAndreas Boehler                if(($id === $ID) || (strpos($id, 'webdav://') === 0))
152cd2f100dSAndreas Boehler                {
153cd2f100dSAndreas Boehler                    $data['id'][$id] = '#3a87ad';
154cd2f100dSAndreas Boehler                }
155cd2f100dSAndreas Boehler                // Otherwise, retrieve the color information from the calendar settings
156cd2f100dSAndreas Boehler                else
157cd2f100dSAndreas Boehler                {
158cd2f100dSAndreas Boehler                    $calid = $this->hlp->getCalendarIdForPage($ID);
159cd2f100dSAndreas Boehler                    $settings = $this->hlp->getCalendarSettings($calid);
160cd2f100dSAndreas Boehler                    $color = $settings['calendarcolor'];
161cd2f100dSAndreas Boehler                    $data['id'][$id] = $color;
162cd2f100dSAndreas Boehler                }
163cd2f100dSAndreas Boehler            }
164cd2f100dSAndreas Boehler        }
165cd2f100dSAndreas Boehler
1666fbb086dSAndreas Boehler        // Only update the calendar name/description if the ID matches the page ID.
1676fbb086dSAndreas Boehler        // Otherwise, the calendar is included in another page and we don't want
1686fbb086dSAndreas Boehler        // to interfere with its data.
1696fbb086dSAndreas Boehler        if(in_array($ID, array_keys($data['id'])))
1706fbb086dSAndreas Boehler        {
1716fbb086dSAndreas Boehler            if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
1726fbb086dSAndreas Boehler                $username = $_SERVER['REMOTE_USER'];
1736fbb086dSAndreas Boehler            else
1746fbb086dSAndreas Boehler                $username = uniqid('davcal-');
1756fbb086dSAndreas Boehler            $this->hlp->setCalendarNameForPage($data['name'], $data['description'], $ID, $username);
1766fbb086dSAndreas Boehler            $this->hlp->setCalendarColorForPage($data['id'][$ID], $ID);
17713b16484SAndreas Boehler            $this->hlp->enableCalendarForPage($ID);
1786fbb086dSAndreas Boehler        }
1796fbb086dSAndreas Boehler
1806fbb086dSAndreas Boehler        p_set_metadata($ID, array('plugin_davcal' => $data));
1816fbb086dSAndreas Boehler
1826fbb086dSAndreas Boehler        return $data;
1836fbb086dSAndreas Boehler    }
1846fbb086dSAndreas Boehler
1856fbb086dSAndreas Boehler    /**
1866fbb086dSAndreas Boehler     * Create output
1876fbb086dSAndreas Boehler     */
1889f1dab8cSAndreas Boehler    function render($format, Doku_Renderer $R, $data) {
1896fbb086dSAndreas Boehler        if($format != 'xhtml') return false;
1906fbb086dSAndreas Boehler        global $ID;
1916fbb086dSAndreas Boehler        $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
1926fbb086dSAndreas Boehler
1936fbb086dSAndreas Boehler        // Render the Calendar. Timezone list is within a hidden div,
1946fbb086dSAndreas Boehler        // the calendar ID is in a data-calendarid tag.
19582a48dfbSAndreas Boehler        if($data['forcetimezone'] !== 'no')
19682a48dfbSAndreas Boehler            $R->doc .= '<div id="fullCalendarTimezoneWarning">'.sprintf($this->getLang('this_calendar_uses_timezone'), $data['forcetimezone']).'</div>';
1976fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendar" data-calendarpage="'.$ID.'"></div>';
1986fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendarTimezoneList" class="fullCalendarTimezoneList" style="display:none">';
1996fbb086dSAndreas Boehler        $R->doc .= '<select id="fullCalendarTimezoneDropdown">';
2006fbb086dSAndreas Boehler        $R->doc .= '<option value="local">'.$this->getLang('local_time').'</option>';
2016fbb086dSAndreas Boehler        foreach($tzlist as $tz)
2026fbb086dSAndreas Boehler        {
2036fbb086dSAndreas Boehler            $R->doc .= '<option value="'.$tz.'">'.$tz.'</option>';
2046fbb086dSAndreas Boehler        }
2056fbb086dSAndreas Boehler        $R->doc .= '</select></div>';
2066fbb086dSAndreas Boehler        if(($this->getConf('hide_settings') !== 1) && ($data['settings'] !== 'hide'))
2076fbb086dSAndreas Boehler        {
2086fbb086dSAndreas Boehler            $R->doc .= '<div class="fullCalendarSettings"><a href="#" class="fullCalendarSettings"><img src="'.DOKU_URL.'lib/plugins/davcal/images/settings.png'.'">'.$this->getLang('settings').'</a></div>';
2096fbb086dSAndreas Boehler        }
2106fbb086dSAndreas Boehler
2116fbb086dSAndreas Boehler    }
2126fbb086dSAndreas Boehler
2136fbb086dSAndreas Boehler
2146fbb086dSAndreas Boehler
2156fbb086dSAndreas Boehler}
2166fbb086dSAndreas Boehler
2176fbb086dSAndreas Boehler// vim:ts=4:sw=4:et:enc=utf-8:
218