xref: /plugin/davcal/syntax/calendar.php (revision cd2f100d4748850e85c06a57cfc90768de29ce0a)
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',
6552010ac7SAndreas Boehler                      'forcetimeformat' => 'no'
666fbb086dSAndreas Boehler                      );
671bb22c2bSAndreas Boehler        if(strpos($match, '{{davcalclient') === 0)
681bb22c2bSAndreas Boehler        {
691bb22c2bSAndreas Boehler            $options = trim(substr($match,15,-2));
701bb22c2bSAndreas Boehler            $defaultId = $this->getConf('default_client_id');
711bb22c2bSAndreas Boehler            if(isset($defaultId) && ($defaultId != ''))
721bb22c2bSAndreas Boehler            {
73*cd2f100dSAndreas Boehler                $data['id'][$defaultId] = null;
741bb22c2bSAndreas Boehler                $lastid = $defaultId;
751bb22c2bSAndreas Boehler            }
761bb22c2bSAndreas Boehler        }
771bb22c2bSAndreas Boehler        else
781bb22c2bSAndreas Boehler        {
791bb22c2bSAndreas Boehler            $options = trim(substr($match,9,-2));
806fbb086dSAndreas Boehler            $lastid = $ID;
811bb22c2bSAndreas Boehler        }
821bb22c2bSAndreas Boehler        $options = explode(',', $options);
831bb22c2bSAndreas Boehler
846fbb086dSAndreas Boehler        foreach($options as $option)
856fbb086dSAndreas Boehler        {
866fbb086dSAndreas Boehler            list($key, $val) = explode('=', $option);
876fbb086dSAndreas Boehler            $key = strtolower(trim($key));
886fbb086dSAndreas Boehler            $val = trim($val);
896fbb086dSAndreas Boehler            switch($key)
906fbb086dSAndreas Boehler            {
916fbb086dSAndreas Boehler                case 'id':
926fbb086dSAndreas Boehler                    $lastid = $val;
936fbb086dSAndreas Boehler                    if(!in_array($val, $data['id']))
94*cd2f100dSAndreas Boehler                        $data['id'][$val] = null;
956fbb086dSAndreas Boehler                break;
966fbb086dSAndreas Boehler                case 'color':
976fbb086dSAndreas Boehler                    $data['id'][$lastid] = $val;
986fbb086dSAndreas Boehler                break;
996fbb086dSAndreas Boehler                case 'view':
1006fbb086dSAndreas Boehler                    if(in_array($val, array('month', 'basicDay', 'basicWeek', 'agendaWeek', 'agendaDay')))
1016fbb086dSAndreas Boehler                        $data['view'] = $val;
1026fbb086dSAndreas Boehler                    else
1036fbb086dSAndreas Boehler                        $data['view'] = 'month';
1046fbb086dSAndreas Boehler                break;
10582a48dfbSAndreas Boehler                case 'forcetimezone':
10682a48dfbSAndreas Boehler                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
10782a48dfbSAndreas Boehler                    if(in_array($val, $tzlist) || $val === 'no')
10882a48dfbSAndreas Boehler                        $data['forcetimezone'] = $val;
10982a48dfbSAndreas Boehler                    else
11082a48dfbSAndreas Boehler                        msg($this->getLang('error_timezone_not_in_list'), -1);
11182a48dfbSAndreas Boehler                break;
11252010ac7SAndreas Boehler                case 'forcetimeformat':
11352010ac7SAndreas Boehler                    $tfopt = array('lang', '24h', '12h');
11452010ac7SAndreas Boehler                    if(in_array($val, $tfopt) || $val === 'no')
11552010ac7SAndreas Boehler                        $data['forcetimeformat'] = $val;
11652010ac7SAndreas Boehler                    else
11752010ac7SAndreas Boehler                        msg($this->getLang('error_option_error'), -1);
11852010ac7SAndreas Boehler                break;
1196fbb086dSAndreas Boehler                default:
1206fbb086dSAndreas Boehler                    $data[$key] = $val;
1216fbb086dSAndreas Boehler            }
1226fbb086dSAndreas Boehler        }
1236fbb086dSAndreas Boehler        // Handle the default case when the user didn't enter a different ID
1246fbb086dSAndreas Boehler        if(empty($data['id']))
1256fbb086dSAndreas Boehler        {
126*cd2f100dSAndreas Boehler            $data['id'] = array($ID => null);
1276fbb086dSAndreas Boehler        }
128*cd2f100dSAndreas Boehler
129*cd2f100dSAndreas Boehler        // Fix up the colors, if no color information is given
130*cd2f100dSAndreas Boehler        foreach($data['id'] as $id => $color)
131*cd2f100dSAndreas Boehler        {
132*cd2f100dSAndreas Boehler            if(is_null($color))
133*cd2f100dSAndreas Boehler            {
134*cd2f100dSAndreas Boehler                // If this is the current calendar or a WebDAV calendar, use the
135*cd2f100dSAndreas Boehler                // default color
136*cd2f100dSAndreas Boehler                if(($id === $ID) || (strpos($id, 'webdav://') === 0))
137*cd2f100dSAndreas Boehler                {
138*cd2f100dSAndreas Boehler                    $data['id'][$id] = '#3a87ad';
139*cd2f100dSAndreas Boehler                }
140*cd2f100dSAndreas Boehler                // Otherwise, retrieve the color information from the calendar settings
141*cd2f100dSAndreas Boehler                else
142*cd2f100dSAndreas Boehler                {
143*cd2f100dSAndreas Boehler                    $calid = $this->hlp->getCalendarIdForPage($ID);
144*cd2f100dSAndreas Boehler                    $settings = $this->hlp->getCalendarSettings($calid);
145*cd2f100dSAndreas Boehler                    $color = $settings['calendarcolor'];
146*cd2f100dSAndreas Boehler                    $data['id'][$id] = $color;
147*cd2f100dSAndreas Boehler                }
148*cd2f100dSAndreas Boehler            }
149*cd2f100dSAndreas Boehler        }
150*cd2f100dSAndreas Boehler
1516fbb086dSAndreas Boehler        // Only update the calendar name/description if the ID matches the page ID.
1526fbb086dSAndreas Boehler        // Otherwise, the calendar is included in another page and we don't want
1536fbb086dSAndreas Boehler        // to interfere with its data.
1546fbb086dSAndreas Boehler        if(in_array($ID, array_keys($data['id'])))
1556fbb086dSAndreas Boehler        {
1566fbb086dSAndreas Boehler            if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
1576fbb086dSAndreas Boehler                $username = $_SERVER['REMOTE_USER'];
1586fbb086dSAndreas Boehler            else
1596fbb086dSAndreas Boehler                $username = uniqid('davcal-');
1606fbb086dSAndreas Boehler            $this->hlp->setCalendarNameForPage($data['name'], $data['description'], $ID, $username);
1616fbb086dSAndreas Boehler            $this->hlp->setCalendarColorForPage($data['id'][$ID], $ID);
1626fbb086dSAndreas Boehler        }
1636fbb086dSAndreas Boehler
1646fbb086dSAndreas Boehler        p_set_metadata($ID, array('plugin_davcal' => $data));
1656fbb086dSAndreas Boehler
1666fbb086dSAndreas Boehler        return $data;
1676fbb086dSAndreas Boehler    }
1686fbb086dSAndreas Boehler
1696fbb086dSAndreas Boehler    /**
1706fbb086dSAndreas Boehler     * Create output
1716fbb086dSAndreas Boehler     */
1729f1dab8cSAndreas Boehler    function render($format, Doku_Renderer $R, $data) {
1736fbb086dSAndreas Boehler        if($format != 'xhtml') return false;
1746fbb086dSAndreas Boehler        global $ID;
1756fbb086dSAndreas Boehler        $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
1766fbb086dSAndreas Boehler
1776fbb086dSAndreas Boehler        // Render the Calendar. Timezone list is within a hidden div,
1786fbb086dSAndreas Boehler        // the calendar ID is in a data-calendarid tag.
17982a48dfbSAndreas Boehler        if($data['forcetimezone'] !== 'no')
18082a48dfbSAndreas Boehler            $R->doc .= '<div id="fullCalendarTimezoneWarning">'.sprintf($this->getLang('this_calendar_uses_timezone'), $data['forcetimezone']).'</div>';
1816fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendar" data-calendarpage="'.$ID.'"></div>';
1826fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendarTimezoneList" class="fullCalendarTimezoneList" style="display:none">';
1836fbb086dSAndreas Boehler        $R->doc .= '<select id="fullCalendarTimezoneDropdown">';
1846fbb086dSAndreas Boehler        $R->doc .= '<option value="local">'.$this->getLang('local_time').'</option>';
1856fbb086dSAndreas Boehler        foreach($tzlist as $tz)
1866fbb086dSAndreas Boehler        {
1876fbb086dSAndreas Boehler            $R->doc .= '<option value="'.$tz.'">'.$tz.'</option>';
1886fbb086dSAndreas Boehler        }
1896fbb086dSAndreas Boehler        $R->doc .= '</select></div>';
1906fbb086dSAndreas Boehler        if(($this->getConf('hide_settings') !== 1) && ($data['settings'] !== 'hide'))
1916fbb086dSAndreas Boehler        {
1926fbb086dSAndreas 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>';
1936fbb086dSAndreas Boehler        }
1946fbb086dSAndreas Boehler
1956fbb086dSAndreas Boehler    }
1966fbb086dSAndreas Boehler
1976fbb086dSAndreas Boehler
1986fbb086dSAndreas Boehler
1996fbb086dSAndreas Boehler}
2006fbb086dSAndreas Boehler
2016fbb086dSAndreas Boehler// vim:ts=4:sw=4:et:enc=utf-8:
202