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