1<?php
2
3/**
4 * DokuWiki Plugin struct (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10use dokuwiki\plugin\struct\meta\AggregationList;
11use dokuwiki\plugin\struct\meta\ConfigParser;
12use dokuwiki\plugin\struct\meta\SearchConfig;
13use dokuwiki\plugin\struct\meta\StructException;
14
15class syntax_plugin_struct_list extends DokuWiki_Syntax_Plugin
16{
17    /** @var string which class to use for output */
18    protected $tableclass = AggregationList::class;
19
20    /**
21     * @return string Syntax mode type
22     */
23    public function getType()
24    {
25        return 'substition';
26    }
27
28    /**
29     * @return string Paragraph type
30     */
31    public function getPType()
32    {
33        return 'block';
34    }
35
36    /**
37     * @return int Sort order - Low numbers go before high numbers
38     */
39    public function getSort()
40    {
41        return 155;
42    }
43
44    /**
45     * Connect lookup pattern to lexer.
46     *
47     * @param string $mode Parser mode
48     */
49    public function connectTo($mode)
50    {
51        $this->Lexer->addSpecialPattern('----+ *struct list *-+\n.*?\n----+', $mode, 'plugin_struct_list');
52    }
53
54    /**
55     * Handle matches of the struct syntax
56     *
57     * @param string $match The match of the syntax
58     * @param int $state The state of the handler
59     * @param int $pos The position in the document
60     * @param Doku_Handler $handler The handler
61     * @return array Data for the renderer
62     */
63    public function handle($match, $state, $pos, Doku_Handler $handler)
64    {
65        global $conf;
66
67        $lines = explode("\n", $match);
68        array_shift($lines);
69        array_pop($lines);
70
71        try {
72            $parser = new ConfigParser($lines);
73            $config = $parser->getConfig();
74            return $config;
75        } catch (StructException $e) {
76            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
77            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
78            return null;
79        }
80    }
81
82    /**
83     * Checks for options that do not work in a list aggregation
84     *
85     * @param array $config
86     */
87    protected function checkForInvalidOptions($config)
88    {
89        $illegal = ['dynfilters', 'summarize', 'rownumbers', 'widths', 'summary'];
90        foreach ($illegal as $illegalOption)
91        if (!empty($config[$illegalOption])) {
92            throw new StructException('illegal option', $illegalOption);
93        }
94    }
95
96    /**
97     * Render xhtml output or metadata
98     *
99     * @param string $mode Renderer mode (supported modes: xhtml)
100     * @param Doku_Renderer $renderer The renderer
101     * @param array $data The data from the handler() function
102     * @return bool If rendering was successful.
103     */
104    public function render($mode, Doku_Renderer $renderer, $data)
105    {
106        if ($mode != 'xhtml') return false;
107        if (!$data) return false;
108        global $INFO;
109        global $conf;
110
111        try {
112            $this->checkForInvalidOptions($data);
113            $search = new SearchConfig($data);
114
115            /** @var AggregationList $list */
116            $list = new $this->tableclass($INFO['id'], $mode, $renderer, $search);
117            $list->render();
118
119            if ($mode == 'metadata') {
120                /** @var Doku_Renderer_metadata $renderer */
121                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
122            }
123        } catch (StructException $e) {
124            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
125            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
126        }
127
128        return true;
129    }
130}
131
132// vim:ts=4:sw=4:et:
133