xref: /plugin/davcal/syntax/table.php (revision 1d5bdcd0daf69d42a648436e4034819d35b1879d)
1<?php
2/**
3 * DokuWiki Plugin DAVCal (Table 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_table extends DokuWiki_Syntax_Plugin {
16
17    protected $hlp = null;
18
19    // Load the helper plugin
20    public function syntax_plugin_davcal_table() {
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('\{\{davcaltable>[^}]*\}\}',$mode,'plugin_davcal_table');
51    }
52
53    /**
54     * Handle the match
55     */
56    function handle($match, $state, $pos, &$handler){
57        global $ID;
58        $options = trim(substr($match,14,-2));
59        $options = explode(',', $options);
60
61        $data = array('id' => array(),
62                      'startdate' => 'today',
63                      'numdays' => 30,
64                      'dateformat' => 'Y-m-d H:i',
65                      'onlystart' => false,
66                      'sort' => 'desc',
67                      'timezone' => 'local'
68                      );
69        $lastid = $ID;
70
71        foreach($options as $option)
72        {
73            list($key, $val) = explode('=', $option);
74            $key = strtolower(trim($key));
75            $val = trim($val);
76            switch($key)
77            {
78                case 'id':
79                    $lastid = $val;
80                    if(!in_array($val, $data['id']))
81                        $data['id'][$val] = '#3a87ad';
82                break;
83                case 'onlystart':
84                    if(($val === 'on') || ($val === 'true'))
85                        $data['onlystart'] = true;
86                break;
87                case 'timezone':
88                    $tzlist = \DateTimeZone::listIdentifiers(DateTimeZone::ALL);
89                    if(in_array($val, $tzlist) || $val === 'no')
90                        $data['timezone'] = $val;
91                    else
92                        msg($this->getLang('error_timezone_not_in_list'), -1);
93                break;
94                default:
95                    $data[$key] = $val;
96            }
97        }
98
99        // Handle the default case when the user didn't enter a different ID
100        if(empty($data['id']))
101        {
102            $data['id'] = array($ID => '#3a87ad');
103        }
104
105        return $data;
106    }
107
108    private static function sort_events_asc($a, $b)
109    {
110        $from1 = new \DateTime($a['start']);
111        $from2 = new \DateTime($b['start']);
112        return $from2 < $from1;
113    }
114
115    private static function sort_events_desc($a, $b)
116    {
117        $from1 = new \DateTime($a['start']);
118        $from2 = new \DateTime($b['start']);
119        return $from1 < $from2;
120    }
121
122    /**
123     * Create output
124     */
125    function render($format, &$R, $data) {
126        if($format == 'metadata')
127        {
128            $R->meta['plugin_davcal']['table'] = true;
129            return true;
130        }
131        if(($format != 'xhtml') && ($format != 'odt')) return false;
132        global $ID;
133
134        $events = array();
135        $from = $data['startdate'];
136        if($from === 'today')
137            $from = new \DateTime();
138        else
139            $from = new \DateTime($from);
140        $to = clone $from;
141        $to->add(new \DateInterval('P'.$data['numdays'].'D'));
142        $timezone = $data['timezone'];
143        foreach($data['id'] as $calPage => $color)
144        {
145            $events = array_merge($events, $this->hlp->getEventsWithinDateRange($calPage,
146                                      $user, $from->format('Y-m-d'), $to->format('Y-m-d'),
147                                      $timezone));
148        }
149        if($data['sort'] === 'desc')
150            usort($events, array("syntax_plugin_davcal_table", "sort_events_desc"));
151        else
152            usort($events, array("syntax_plugin_davcal_table", "sort_events_asc"));
153
154        $R->table_open();
155        $R->tablethead_open();
156        $R->tableheader_open();
157        $R->doc .= $data['onlystart'] ? $this->getLang('at') : $this->getLang('from');
158        $R->tableheader_close();
159        if(!$data['onlystart'])
160        {
161            $R->tableheader_open();
162            $R->doc .= $this->getLang('to');
163            $R->tableheader_close();
164        }
165        $R->tableheader_open();
166        $R->doc .= $this->getLang('title');
167        $R->tableheader_close();
168        $R->tableheader_open();
169        $R->doc .= $this->getLang('description');
170        $R->tableheader_close();
171        $R->tablethead_close();
172        foreach($events as $event)
173        {
174            $R->tablerow_open();
175            $R->tablecell_open();
176            $from = new \DateTime($event['start']);
177            $R->doc .= $from->format($data['dateformat']);
178            $R->tablecell_close();
179            if(!$data['onlystart'])
180            {
181                $to = new \DateTime($event['end']);
182                // Fixup all day events, which have one day in excess
183                if($event['allDay'] === true)
184                {
185                    $to->sub(new \DateInterval('P1D'));
186                }
187                $R->tablecell_open();
188                $R->doc .= $to->format($data['dateformat']);
189                $R->tablecell_close();
190            }
191            $R->tablecell_open();
192            $R->doc .= $event['title'];
193            $R->tablecell_close();
194            $R->tablecell_open();
195            $R->doc .= $event['description'];
196            $R->tablecell_close();
197            $R->tablerow_close();
198        }
199        $R->table_close();
200    }
201
202
203
204}
205
206// vim:ts=4:sw=4:et:enc=utf-8:
207