xref: /plugin/struct/syntax/table.php (revision 0659dc6476aba807b38d4abff28e59f22867fa24)
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
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     * @return string Syntax mode type
21     */
22    public function getType() {
23        return 'substition';
24    }
25    /**
26     * @return string Paragraph type
27     */
28    public function getPType() {
29        return 'block';
30    }
31    /**
32     * @return int Sort order - Low numbers go before high numbers
33     */
34    public function getSort() {
35        return 155;
36    }
37
38    /**
39     * Connect lookup pattern to lexer.
40     *
41     * @param string $mode Parser mode
42     */
43    public function connectTo($mode) {
44        $this->Lexer->addSpecialPattern('----+ *struct table *-+\n.*?\n----+', $mode, 'plugin_struct_table');
45    }
46
47
48    /**
49     * Handle matches of the struct syntax
50     *
51     * @param string $match The match of the syntax
52     * @param int    $state The state of the handler
53     * @param int    $pos The position in the document
54     * @param Doku_Handler    $handler The handler
55     * @return array Data for the renderer
56     */
57    public function handle($match, $state, $pos, Doku_Handler $handler){
58        global $conf;
59
60        $lines = explode("\n", $match);
61        array_shift($lines);
62        array_pop($lines);
63
64        try {
65            $parser = new ConfigParser($lines);
66            $config = $parser->getConfig();
67            /** @var helper_plugin_struct $struct_helper */
68            $struct_helper = plugin_load('helper', 'struct');
69            foreach ($config['schemas'] as $schema) {
70                $result = $struct_helper->getSchema($schema[0]);
71                if (!$result[$schema[0]]->getId()) {
72                    msg("Schema $schema[0] does not exist!",-1);
73                    return false;
74                }
75            }
76            return $config;
77        } catch (StructException $e) {
78            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
79            if($conf['allowdebug']) msg('<pre>'.hsc($e->getTraceAsString()).'</pre>', -1);
80            return null;
81        }
82    }
83
84    /**
85     * Render xhtml output or metadata
86     *
87     * @param string         $mode      Renderer mode (supported modes: xhtml)
88     * @param Doku_Renderer  $renderer  The renderer
89     * @param array          $data      The data from the handler() function
90     * @return bool If rendering was successful.
91     */
92    public function render($mode, Doku_Renderer $renderer, $data) {
93        if(!$data) return false;
94        global $ID;
95        global $conf;
96
97        try {
98            $search = new SearchConfig($data);
99            $table = new AggregationTable($ID, $mode, $renderer, $search);
100            $table->render();
101
102            if($mode == 'metadata') {
103                /** @var Doku_Renderer_metadata $renderer  */
104                $renderer->meta['plugin']['struct']['hasaggregation'] = $search->getCacheFlag();
105            }
106
107        } catch (StructException $e) {
108            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
109            if($conf['allowdebug']) msg('<pre>'.hsc($e->getTraceAsString()).'</pre>', -1);
110        }
111
112        return true;
113    }
114}
115
116// vim:ts=4:sw=4:et:
117