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