xref: /plugin/struct/syntax/table.php (revision 7cbcfbdb68125878b37fede99d5e33997295c2f6)
1<?php
2/**
3 * DokuWiki Plugin struct (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9use dokuwiki\plugin\struct\meta\AggregationTable;
10use dokuwiki\plugin\struct\meta\ConfigParser;
11use dokuwiki\plugin\struct\meta\SearchConfig;
12use dokuwiki\plugin\struct\meta\StructException;
13
14// must be run within Dokuwiki
15if(!defined('DOKU_INC')) die();
16
17class syntax_plugin_struct_table extends DokuWiki_Syntax_Plugin {
18
19    /** @var string which class to use for output */
20    protected $tableclass = AggregationTable::class;
21
22    /**
23     * @return string Syntax mode type
24     */
25    public function getType() {
26        return 'substition';
27    }
28
29    /**
30     * @return string Paragraph type
31     */
32    public function getPType() {
33        return 'block';
34    }
35
36    /**
37     * @return int Sort order - Low numbers go before high numbers
38     */
39    public function getSort() {
40        return 155;
41    }
42
43    /**
44     * Connect lookup pattern to lexer.
45     *
46     * @param string $mode Parser mode
47     */
48    public function connectTo($mode) {
49        $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table');
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        global $conf;
63
64        $lines = explode("\n", $match);
65        array_shift($lines);
66        array_pop($lines);
67
68        try {
69            $parser = new ConfigParser($lines);
70            $config = $parser->getConfig();
71            return $config;
72        } catch(StructException $e) {
73            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
74            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
75            return null;
76        }
77    }
78
79    /**
80     * Render xhtml output or metadata
81     *
82     * @param string $mode Renderer mode (supported modes: xhtml)
83     * @param Doku_Renderer $renderer The renderer
84     * @param array $data The data from the handler() function
85     * @return bool If rendering was successful.
86     */
87    public function render($mode, Doku_Renderer $renderer, $data) {
88        if(!$data) return false;
89        global $ID;
90        global $conf;
91
92        try {
93            $search = new SearchConfig($data);
94            if($mode == 'struct_csv') {
95                // no pagination in export
96                $search->setLimit(0);
97                $search->setOffset(0);
98            }
99
100            /** @var AggregationTable $table */
101            $table = new $this->tableclass($ID, $mode, $renderer, $search);
102            $table->render();
103
104            if($mode == 'metadata') {
105                /** @var Doku_Renderer_metadata $renderer */
106                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
107            }
108
109        } catch(StructException $e) {
110            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
111            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
112        }
113
114        return true;
115    }
116}
117