xref: /plugin/davcal/syntax/calendar.php (revision 82a48dfbf5495f3f2fd216265cb05b6e2f79ee7c)
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');
516fbb086dSAndreas Boehler    }
526fbb086dSAndreas Boehler
536fbb086dSAndreas Boehler    /**
546fbb086dSAndreas Boehler     * Handle the match
556fbb086dSAndreas Boehler     */
566fbb086dSAndreas Boehler    function handle($match, $state, $pos, &$handler){
576fbb086dSAndreas Boehler        global $ID;
586fbb086dSAndreas Boehler        $options = trim(substr($match,9,-2));
596fbb086dSAndreas Boehler        $options = explode(',', $options);
606fbb086dSAndreas Boehler        $data = array('name' => $ID,
616fbb086dSAndreas Boehler                      'description' => $this->getLang('created_by_davcal'),
626fbb086dSAndreas Boehler                      'id' => array(),
636fbb086dSAndreas Boehler                      'settings' => 'show',
64*82a48dfbSAndreas Boehler                      'view' => 'month',
65*82a48dfbSAndreas Boehler                      'forcetimezone' => 'no'
666fbb086dSAndreas Boehler                      );
676fbb086dSAndreas Boehler        $lastid = $ID;
686fbb086dSAndreas Boehler        foreach($options as $option)
696fbb086dSAndreas Boehler        {
706fbb086dSAndreas Boehler            list($key, $val) = explode('=', $option);
716fbb086dSAndreas Boehler            $key = strtolower(trim($key));
726fbb086dSAndreas Boehler            $val = trim($val);
736fbb086dSAndreas Boehler            switch($key)
746fbb086dSAndreas Boehler            {
756fbb086dSAndreas Boehler                case 'id':
766fbb086dSAndreas Boehler                    $lastid = $val;
776fbb086dSAndreas Boehler                    if(!in_array($val, $data['id']))
786fbb086dSAndreas Boehler                        $data['id'][$val] = '#3a87ad';
796fbb086dSAndreas Boehler                break;
806fbb086dSAndreas Boehler                case 'color':
816fbb086dSAndreas Boehler                    $data['id'][$lastid] = $val;
826fbb086dSAndreas Boehler                break;
836fbb086dSAndreas Boehler                case 'view':
846fbb086dSAndreas Boehler                    if(in_array($val, array('month', 'basicDay', 'basicWeek', 'agendaWeek', 'agendaDay')))
856fbb086dSAndreas Boehler                        $data['view'] = $val;
866fbb086dSAndreas Boehler                    else
876fbb086dSAndreas Boehler                        $data['view'] = 'month';
886fbb086dSAndreas Boehler                break;
89*82a48dfbSAndreas Boehler                case 'forcetimezone':
90*82a48dfbSAndreas Boehler                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
91*82a48dfbSAndreas Boehler                    if(in_array($val, $tzlist) || $val === 'no')
92*82a48dfbSAndreas Boehler                        $data['forcetimezone'] = $val;
93*82a48dfbSAndreas Boehler                    else
94*82a48dfbSAndreas Boehler                        msg($this->getLang('error_timezone_not_in_list'), -1);
95*82a48dfbSAndreas Boehler                break;
966fbb086dSAndreas Boehler                default:
976fbb086dSAndreas Boehler                    $data[$key] = $val;
986fbb086dSAndreas Boehler            }
996fbb086dSAndreas Boehler        }
1006fbb086dSAndreas Boehler        // Handle the default case when the user didn't enter a different ID
1016fbb086dSAndreas Boehler        if(empty($data['id']))
1026fbb086dSAndreas Boehler        {
1036fbb086dSAndreas Boehler            $data['id'] = array($ID => '#3a87ad');
1046fbb086dSAndreas Boehler        }
1056fbb086dSAndreas Boehler        // Only update the calendar name/description if the ID matches the page ID.
1066fbb086dSAndreas Boehler        // Otherwise, the calendar is included in another page and we don't want
1076fbb086dSAndreas Boehler        // to interfere with its data.
1086fbb086dSAndreas Boehler        if(in_array($ID, array_keys($data['id'])))
1096fbb086dSAndreas Boehler        {
1106fbb086dSAndreas Boehler            if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
1116fbb086dSAndreas Boehler                $username = $_SERVER['REMOTE_USER'];
1126fbb086dSAndreas Boehler            else
1136fbb086dSAndreas Boehler                $username = uniqid('davcal-');
1146fbb086dSAndreas Boehler            $this->hlp->setCalendarNameForPage($data['name'], $data['description'], $ID, $username);
1156fbb086dSAndreas Boehler            $this->hlp->setCalendarColorForPage($data['id'][$ID], $ID);
1166fbb086dSAndreas Boehler        }
1176fbb086dSAndreas Boehler
1186fbb086dSAndreas Boehler        p_set_metadata($ID, array('plugin_davcal' => $data));
1196fbb086dSAndreas Boehler
1206fbb086dSAndreas Boehler        return $data;
1216fbb086dSAndreas Boehler    }
1226fbb086dSAndreas Boehler
1236fbb086dSAndreas Boehler    /**
1246fbb086dSAndreas Boehler     * Create output
1256fbb086dSAndreas Boehler     */
1266fbb086dSAndreas Boehler    function render($format, &$R, $data) {
1276fbb086dSAndreas Boehler        if($format != 'xhtml') return false;
1286fbb086dSAndreas Boehler        global $ID;
1296fbb086dSAndreas Boehler        $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
1306fbb086dSAndreas Boehler
1316fbb086dSAndreas Boehler        // Render the Calendar. Timezone list is within a hidden div,
1326fbb086dSAndreas Boehler        // the calendar ID is in a data-calendarid tag.
133*82a48dfbSAndreas Boehler        if($data['forcetimezone'] !== 'no')
134*82a48dfbSAndreas Boehler            $R->doc .= '<div id="fullCalendarTimezoneWarning">'.sprintf($this->getLang('this_calendar_uses_timezone'), $data['forcetimezone']).'</div>';
1356fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendar" data-calendarpage="'.$ID.'"></div>';
1366fbb086dSAndreas Boehler        $R->doc .= '<div id="fullCalendarTimezoneList" class="fullCalendarTimezoneList" style="display:none">';
1376fbb086dSAndreas Boehler        $R->doc .= '<select id="fullCalendarTimezoneDropdown">';
1386fbb086dSAndreas Boehler        $R->doc .= '<option value="local">'.$this->getLang('local_time').'</option>';
1396fbb086dSAndreas Boehler        foreach($tzlist as $tz)
1406fbb086dSAndreas Boehler        {
1416fbb086dSAndreas Boehler            $R->doc .= '<option value="'.$tz.'">'.$tz.'</option>';
1426fbb086dSAndreas Boehler        }
1436fbb086dSAndreas Boehler        $R->doc .= '</select></div>';
1446fbb086dSAndreas Boehler        if(($this->getConf('hide_settings') !== 1) && ($data['settings'] !== 'hide'))
1456fbb086dSAndreas Boehler        {
1466fbb086dSAndreas 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>';
1476fbb086dSAndreas Boehler        }
1486fbb086dSAndreas Boehler
1496fbb086dSAndreas Boehler    }
1506fbb086dSAndreas Boehler
1516fbb086dSAndreas Boehler
1526fbb086dSAndreas Boehler
1536fbb086dSAndreas Boehler}
1546fbb086dSAndreas Boehler
1556fbb086dSAndreas Boehler// vim:ts=4:sw=4:et:enc=utf-8:
156