xref: /plugin/davcal/syntax/calendar.php (revision 13b16484544ce9af366ba2deb0a3e1be2c3f2947)
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 Boehler// must be run within Dokuwiki
106fbb086dSAndreas Boehlerif(!defined('DOKU_INC')) die();
116fbb086dSAndreas Boehler
126fbb086dSAndreas Boehlerif(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
136fbb086dSAndreas Boehlerrequire_once(DOKU_PLUGIN.'syntax.php');
146fbb086dSAndreas Boehler
156fbb086dSAndreas Boehlerclass syntax_plugin_davcal_calendar extends DokuWiki_Syntax_Plugin {
166fbb086dSAndreas Boehler
176fbb086dSAndreas Boehler    protected $hlp = null;
186fbb086dSAndreas Boehler
196fbb086dSAndreas Boehler    // Load the helper plugin
206fbb086dSAndreas Boehler    public function syntax_plugin_davcal_calendar() {
216fbb086dSAndreas Boehler        $this->hlp =& plugin_load('helper', 'davcal');
226fbb086dSAndreas Boehler    }
236fbb086dSAndreas Boehler
246fbb086dSAndreas Boehler
256fbb086dSAndreas Boehler    /**
266fbb086dSAndreas Boehler     * What kind of syntax are we?
276fbb086dSAndreas Boehler     */
286fbb086dSAndreas Boehler    function getType(){
296fbb086dSAndreas Boehler        return 'substition';
306fbb086dSAndreas Boehler    }
316fbb086dSAndreas Boehler
326fbb086dSAndreas Boehler    /**
336fbb086dSAndreas Boehler     * What about paragraphs?
346fbb086dSAndreas Boehler     */
356fbb086dSAndreas Boehler    function getPType(){
366fbb086dSAndreas Boehler        return 'normal';
376fbb086dSAndreas Boehler    }
386fbb086dSAndreas Boehler
396fbb086dSAndreas Boehler    /**
406fbb086dSAndreas Boehler     * Where to sort in?
416fbb086dSAndreas Boehler     */
426fbb086dSAndreas Boehler    function getSort(){
436fbb086dSAndreas Boehler        return 165;
446fbb086dSAndreas Boehler    }
456fbb086dSAndreas Boehler
466fbb086dSAndreas Boehler    /**
476fbb086dSAndreas Boehler     * Connect pattern to lexer
486fbb086dSAndreas Boehler     */
496fbb086dSAndreas Boehler    function connectTo($mode) {
506fbb086dSAndreas Boehler        $this->Lexer->addSpecialPattern('\{\{davcal>[^}]*\}\}',$mode,'plugin_davcal_calendar');
511bb22c2bSAndreas Boehler        $this->Lexer->addSpecialPattern('\{\{davcalclient>[^}]*\}\}',$mode,'plugin_davcal_calendar');
526fbb086dSAndreas Boehler    }
536fbb086dSAndreas Boehler
546fbb086dSAndreas Boehler    /**
556fbb086dSAndreas Boehler     * Handle the match
566fbb086dSAndreas Boehler     */
579f1dab8cSAndreas Boehler    function handle($match, $state, $pos, Doku_Handler $handler){
586fbb086dSAndreas Boehler        global $ID;
596fbb086dSAndreas Boehler        $data = array('name' => $ID,
606fbb086dSAndreas Boehler                      'description' => $this->getLang('created_by_davcal'),
616fbb086dSAndreas Boehler                      'id' => array(),
626fbb086dSAndreas Boehler                      'settings' => 'show',
6382a48dfbSAndreas Boehler                      'view' => 'month',
6452010ac7SAndreas Boehler                      'forcetimezone' => 'no',
65b0737c19SAndreas Boehler                      'forcetimeformat' => 'no',
66b0737c19SAndreas Boehler                      'fcoptions' => array(),
676fbb086dSAndreas Boehler                      );
681bb22c2bSAndreas Boehler        if(strpos($match, '{{davcalclient') === 0)
691bb22c2bSAndreas Boehler        {
701bb22c2bSAndreas Boehler            $options = trim(substr($match,15,-2));
711bb22c2bSAndreas Boehler            $defaultId = $this->getConf('default_client_id');
721bb22c2bSAndreas Boehler            if(isset($defaultId) && ($defaultId != ''))
731bb22c2bSAndreas Boehler            {
74cd2f100dSAndreas Boehler                $data['id'][$defaultId] = null;
751bb22c2bSAndreas Boehler                $lastid = $defaultId;
761bb22c2bSAndreas Boehler            }
771bb22c2bSAndreas Boehler        }
781bb22c2bSAndreas Boehler        else
791bb22c2bSAndreas Boehler        {
801bb22c2bSAndreas Boehler            $options = trim(substr($match,9,-2));
816fbb086dSAndreas Boehler            $lastid = $ID;
821bb22c2bSAndreas Boehler        }
831bb22c2bSAndreas Boehler        $options = explode(',', $options);
841bb22c2bSAndreas Boehler
856fbb086dSAndreas Boehler        foreach($options as $option)
866fbb086dSAndreas Boehler        {
876fbb086dSAndreas Boehler            list($key, $val) = explode('=', $option);
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':
1016fbb086dSAndreas Boehler                    if(in_array($val, array('month', 'basicDay', 'basicWeek', 'agendaWeek', 'agendaDay')))
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                    {
110b0737c19SAndreas Boehler                        list($o, $v) = explode(':', $opt, 2);
111b0737c19SAndreas Boehler                        $data['fcoptions'][$o] = $v;
112b0737c19SAndreas Boehler                    }
113b0737c19SAndreas Boehler                break;
11482a48dfbSAndreas Boehler                case 'forcetimezone':
11582a48dfbSAndreas Boehler                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
11682a48dfbSAndreas Boehler                    if(in_array($val, $tzlist) || $val === 'no')
11782a48dfbSAndreas Boehler                        $data['forcetimezone'] = $val;
11882a48dfbSAndreas Boehler                    else
11982a48dfbSAndreas Boehler                        msg($this->getLang('error_timezone_not_in_list'), -1);
12082a48dfbSAndreas Boehler                break;
12152010ac7SAndreas Boehler                case 'forcetimeformat':
12252010ac7SAndreas Boehler                    $tfopt = array('lang', '24h', '12h');
12352010ac7SAndreas Boehler                    if(in_array($val, $tfopt) || $val === 'no')
12452010ac7SAndreas Boehler                        $data['forcetimeformat'] = $val;
12552010ac7SAndreas Boehler                    else
12652010ac7SAndreas Boehler                        msg($this->getLang('error_option_error'), -1);
12752010ac7SAndreas Boehler                break;
1286fbb086dSAndreas Boehler                default:
1296fbb086dSAndreas Boehler                    $data[$key] = $val;
1306fbb086dSAndreas Boehler            }
1316fbb086dSAndreas Boehler        }
1326fbb086dSAndreas Boehler        // Handle the default case when the user didn't enter a different ID
1336fbb086dSAndreas Boehler        if(empty($data['id']))
1346fbb086dSAndreas Boehler        {
135cd2f100dSAndreas Boehler            $data['id'] = array($ID => null);
1366fbb086dSAndreas Boehler        }
137cd2f100dSAndreas Boehler
138cd2f100dSAndreas Boehler        // Fix up the colors, if no color information is given
139cd2f100dSAndreas Boehler        foreach($data['id'] as $id => $color)
140cd2f100dSAndreas Boehler        {
141cd2f100dSAndreas Boehler            if(is_null($color))
142cd2f100dSAndreas Boehler            {
143cd2f100dSAndreas Boehler                // If this is the current calendar or a WebDAV calendar, use the
144cd2f100dSAndreas Boehler                // default color
145cd2f100dSAndreas Boehler                if(($id === $ID) || (strpos($id, 'webdav://') === 0))
146cd2f100dSAndreas Boehler                {
147cd2f100dSAndreas Boehler                    $data['id'][$id] = '#3a87ad';
148cd2f100dSAndreas Boehler                }
149cd2f100dSAndreas Boehler                // Otherwise, retrieve the color information from the calendar settings
150cd2f100dSAndreas Boehler                else
151cd2f100dSAndreas Boehler                {
152cd2f100dSAndreas Boehler                    $calid = $this->hlp->getCalendarIdForPage($ID);
153cd2f100dSAndreas Boehler                    $settings = $this->hlp->getCalendarSettings($calid);
154cd2f100dSAndreas Boehler                    $color = $settings['calendarcolor'];
155cd2f100dSAndreas Boehler                    $data['id'][$id] = $color;
156cd2f100dSAndreas Boehler                }
157cd2f100dSAndreas Boehler            }
158cd2f100dSAndreas Boehler        }
159cd2f100dSAndreas Boehler
1606fbb086dSAndreas Boehler        // Only update the calendar name/description if the ID matches the page ID.
1616fbb086dSAndreas Boehler        // Otherwise, the calendar is included in another page and we don't want
1626fbb086dSAndreas Boehler        // to interfere with its data.
1636fbb086dSAndreas Boehler        if(in_array($ID, array_keys($data['id'])))
1646fbb086dSAndreas Boehler        {
1656fbb086dSAndreas Boehler            if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
1666fbb086dSAndreas Boehler                $username = $_SERVER['REMOTE_USER'];
1676fbb086dSAndreas Boehler            else
1686fbb086dSAndreas Boehler                $username = uniqid('davcal-');
1696fbb086dSAndreas Boehler            $this->hlp->setCalendarNameForPage($data['name'], $data['description'], $ID, $username);
1706fbb086dSAndreas Boehler            $this->hlp->setCalendarColorForPage($data['id'][$ID], $ID);
171*13b16484SAndreas Boehler            $this->hlp->enableCalendarForPage($ID);
1726fbb086dSAndreas Boehler        }
1736fbb086dSAndreas Boehler
1746fbb086dSAndreas Boehler        p_set_metadata($ID, array('plugin_davcal' => $data));
1756fbb086dSAndreas Boehler
1766fbb086dSAndreas Boehler        return $data;
1776fbb086dSAndreas Boehler    }
1786fbb086dSAndreas Boehler
1796fbb086dSAndreas Boehler    /**
1806fbb086dSAndreas Boehler     * Create output
1816fbb086dSAndreas Boehler     */
1829f1dab8cSAndreas Boehler    function render($format, Doku_Renderer $R, $data) {
1836fbb086dSAndreas Boehler        if($format != 'xhtml') return false;
1846fbb086dSAndreas Boehler        global $ID;
1856fbb086dSAndreas Boehler        $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
1866fbb086dSAndreas Boehler
1876fbb086dSAndreas Boehler        // Render the Calendar. Timezone list is within a hidden div,
1886fbb086dSAndreas Boehler        // the calendar ID is in a data-calendarid tag.
18982a48dfbSAndreas Boehler        if($data['forcetimezone'] !== 'no')
19082a48dfbSAndreas Boehler            $R->doc .= '<div id="fullCalendarTimezoneWarning">'.sprintf($this->getLang('this_calendar_uses_timezone'), $data['forcetimezone']).'</div>';
1916fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendar" data-calendarpage="'.$ID.'"></div>';
1926fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendarTimezoneList" class="fullCalendarTimezoneList" style="display:none">';
1936fbb086dSAndreas Boehler        $R->doc .= '<select id="fullCalendarTimezoneDropdown">';
1946fbb086dSAndreas Boehler        $R->doc .= '<option value="local">'.$this->getLang('local_time').'</option>';
1956fbb086dSAndreas Boehler        foreach($tzlist as $tz)
1966fbb086dSAndreas Boehler        {
1976fbb086dSAndreas Boehler            $R->doc .= '<option value="'.$tz.'">'.$tz.'</option>';
1986fbb086dSAndreas Boehler        }
1996fbb086dSAndreas Boehler        $R->doc .= '</select></div>';
2006fbb086dSAndreas Boehler        if(($this->getConf('hide_settings') !== 1) && ($data['settings'] !== 'hide'))
2016fbb086dSAndreas Boehler        {
2026fbb086dSAndreas 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>';
2036fbb086dSAndreas Boehler        }
2046fbb086dSAndreas Boehler
2056fbb086dSAndreas Boehler    }
2066fbb086dSAndreas Boehler
2076fbb086dSAndreas Boehler
2086fbb086dSAndreas Boehler
2096fbb086dSAndreas Boehler}
2106fbb086dSAndreas Boehler
2116fbb086dSAndreas Boehler// vim:ts=4:sw=4:et:enc=utf-8:
212