xref: /plugin/struct/syntax/table.php (revision 30bf61e6ee06783dba12a485133923c61e03f3bd)
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\AggregationTable;
11use dokuwiki\plugin\struct\meta\ConfigParser;
12use dokuwiki\plugin\struct\meta\SearchConfig;
13use dokuwiki\plugin\struct\meta\StructException;
14
15class syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin
16{
17
18    /** @var string which class to use for output */
19    protected $tableclass = AggregationTable::class;
20
21    /**
22     * @return string Syntax mode type
23     */
24    public function getType()
25    {
26        return 'substition';
27    }
28
29    /**
30     * @return string Paragraph type
31     */
32    public function getPType()
33    {
34        return 'block';
35    }
36
37    /**
38     * @return int Sort order - Low numbers go before high numbers
39     */
40    public function getSort()
41    {
42        return 155;
43    }
44
45    /**
46     * Connect lookup pattern to lexer.
47     *
48     * @param string $mode Parser mode
49     */
50    public function connectTo($mode)
51    {
52        $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table');
53    }
54
55    /**
56     * Handle matches of the struct syntax
57     *
58     * @param string $match The match of the syntax
59     * @param int $state The state of the handler
60     * @param int $pos The position in the document
61     * @param Doku_Handler $handler The handler
62     * @return array Data for the renderer
63     */
64    public function handle($match, $state, $pos, Doku_Handler $handler)
65    {
66        global $conf;
67
68        $lines = explode("\n", $match);
69        array_shift($lines);
70        array_pop($lines);
71
72        try {
73            $parser = new ConfigParser($lines);
74            $config = $parser->getConfig();
75            return $config;
76        } catch (StructException $e) {
77            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
78            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
79            return null;
80        }
81    }
82
83    /**
84     * Render xhtml output or metadata
85     *
86     * @param string $format Renderer mode (supported modes: xhtml)
87     * @param Doku_Renderer $renderer The renderer
88     * @param array $config The parsed config data from the handler() function
89     * @return bool If rendering was successful.
90     */
91    public function render($format, Doku_Renderer $renderer, $config)
92    {
93        global $INFO;
94        global $conf;
95
96        if (!$config) return false;
97        $config = $this->addTypeFilter($config); // add type specific filters
98
99        try {
100            $search = new SearchConfig($config);
101            if ($format === 'struct_csv') {
102                // no pagination in export
103                $search->setLimit(0);
104                $search->setOffset(0);
105            }
106
107            /** @var AggregationTable $table */
108            $table = new $this->tableclass($INFO['id'], $format, $renderer, $search);
109            $table->render();
110
111            if ($format === 'metadata') {
112                /** @var Doku_Renderer_metadata $renderer */
113                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
114            }
115        } catch (StructException $e) {
116            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
117            if ($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
118        }
119
120        return true;
121    }
122
123    /**
124     * Filter based on primary key columns, applicable in child classes
125     *
126     * @param array $config
127     * @return array
128     */
129    protected function addTypeFilter($config)
130    {
131        return $config;
132    }
133}
134