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