xref: /plugin/struct/syntax/filter.php (revision 4fc1424af829d05c69a00a0c2ae33facc7342115)
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\AggregationCloud;
11use dokuwiki\plugin\struct\meta\ConfigParser;
12use dokuwiki\plugin\struct\meta\Filter;
13use dokuwiki\plugin\struct\meta\SearchConfig;
14use dokuwiki\plugin\struct\meta\StructException;
15
16class syntax_plugin_struct_filter extends DokuWiki_Syntax_Plugin
17{
18    /**
19     * @return string Syntax mode type
20     */
21    public function getType()
22    {
23        return 'substition';
24    }
25
26    /**
27     * @return string Paragraph type
28     */
29    public function getPType()
30    {
31        return 'block';
32    }
33
34    /**
35     * @return int Sort order - Low numbers go before high numbers
36     */
37    public function getSort()
38    {
39        return 155;
40    }
41
42    /**
43     * Connect lookup pattern to lexer.
44     *
45     * @param string $mode Parser mode
46     */
47    public function connectTo($mode)
48    {
49        $this->Lexer->addSpecialPattern('----+ *struct filter *-+\n.*?\n----+', $mode, 'plugin_struct_filter');
50    }
51
52    /**
53     * Handle matches of the struct syntax
54     *
55     * @param string $match The match of the syntax
56     * @param int $state The state of the handler
57     * @param int $pos The position in the document
58     * @param Doku_Handler $handler The handler
59     * @return array Data for the renderer
60     */
61    public function handle($match, $state, $pos, Doku_Handler $handler)
62    {
63        global $conf;
64        $lines = explode("\n", $match);
65        array_shift($lines);
66        array_pop($lines);
67
68        try {
69            $parser = new ConfigParser($lines);
70            return $parser->getConfig();
71        } catch (StructException $e) {
72            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
73            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
74            return null;
75        }
76    }
77
78    /**
79     * Render filter form
80     *
81     * @param string $mode Renderer mode (supported modes: xhtml)
82     * @param Doku_Renderer $renderer The renderer
83     * @param array $data The data from the handler() function
84     * @return bool If rendering was successful.
85     */
86    public function render($mode, Doku_Renderer $renderer, $data)
87    {
88        if ($mode != 'xhtml') return false;
89
90        global $conf;
91
92        $lang = [
93            'title' => $this->getLang('filter_title'),
94            'intro' => $this->getLang('filter_intro'),
95            'button' => $this->getLang('filter_button')
96        ];
97
98        try {
99            $search = new SearchConfig($data);
100            $filter = new Filter($renderer, $search);
101
102            $filter->render($lang);
103
104            if ($mode === 'metadata') {
105                /** @var Doku_Renderer_metadata $renderer */
106                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
107            }
108        } catch (StructException $e) {
109            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
110            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
111        }
112
113        return true;
114    }
115}
116