1<?php
2/**
3 * statdisplay plugin syntax component
4 *
5 * @author Andreas Gohr <gohr@cosmocode.de>
6 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 */
8class syntax_plugin_statdisplay extends DokuWiki_Syntax_Plugin
9{
10
11    /** @inheritDoc */
12    function getType()
13    {
14        return 'substition';
15    }
16
17    /** @inheritDoc */
18    function getPType()
19    {
20        return 'block';
21    }
22
23    /** @inheritDoc */
24    function getSort()
25    {
26        return 155;
27    }
28
29    /** @inheritDoc */
30    function connectTo($mode)
31    {
32        $this->Lexer->addSpecialPattern('\{\{statdisplay>[^\}]+\}\}', $mode, 'plugin_statdisplay');
33    }
34
35    /** @inheritDoc */
36    function handle($match, $state, $pos, Doku_Handler $handler)
37    {
38        $command = trim(substr($match, 14, -2));
39        list($command, $params) = array_pad(explode('?', $command), 2, '');
40        $params = explode(' ', $params);
41
42        $params = array_map('trim', $params);
43        $params = array_filter($params);
44
45        $pos = array_search('graph', $params);
46        if ($pos !== false) {
47            $graph = true;
48            unset($params[$pos]);
49        } else {
50            $graph = false;
51        }
52
53        // remaining params are dates
54        list($from, $to) = array_pad(array_values($params), 2, '');
55
56        return [
57            'command' => $command,
58            'graph' => $graph,
59            'from' => $this->cleanDate($from),
60            'to' => $this->cleanDate($to),
61        ];
62    }
63
64    /** @inheritDoc */
65    function render($format, Doku_Renderer $renderer, $data)
66    {
67        if ($format != 'xhtml') return true;
68        $command = $data['command'];
69        $graph = $data['graph'];
70        $from = $data['from'];
71        $to = $data['to'];
72
73        /** @var $table helper_plugin_statdisplay_table */
74        if (!$graph) {
75            $table = plugin_load('helper', 'statdisplay_table');
76            $table->table($renderer, $command, $from, $to);
77        } else {
78            $img = array(
79                'src' => DOKU_BASE . 'lib/plugins/statdisplay/graph.php?graph=' . rawurlencode($command) . '&f=' . $from . '&t=' . $to,
80                'class' => 'media',
81            );
82            $renderer->doc .= '<img  ' . buildAttributes($img) . '/>';
83        }
84        return true;
85    }
86
87    /**
88     * Make correct year-month format from the input syntax
89     *
90     * @param $date
91     * @return string
92     */
93    private function cleanDate($date)
94    {
95        $months = array('', 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec');
96        list($month, $year) = array_pad(explode('_', strtolower($date)), 2, '');
97        $year = (int)$year;
98        if ($year < 2000 || $year > 2050) return '';
99        $month = array_search($month, $months);
100        if (!$month) return '';
101        return sprintf("%d-%02d", $year, $month);
102    }
103}
104