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