1<?php
2
3use \dokuwiki\plugin\bez\mdl\Model;
4use \dokuwiki\plugin\bez\struct\BezSearch;
5
6use dokuwiki\plugin\struct\meta\ConfigParser;
7use dokuwiki\plugin\struct\meta\AggregationTable;
8use dokuwiki\plugin\struct\meta\StructException;
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13class syntax_plugin_bez_struct extends DokuWiki_Syntax_Plugin {
14
15    /**
16     * @return string Syntax mode type
17     */
18    public function getType() {
19        return 'substition';
20    }
21
22    /**
23     * @return string Paragraph type
24     */
25    public function getPType() {
26        return 'block';
27    }
28
29    /**
30     * @return int Sort order - Low numbers go before high numbers
31     */
32    public function getSort() {
33        return 155;
34    }
35
36    /**
37     * Connect lookup pattern to lexer.
38     *
39     * @param string $mode Parser mode
40     */
41    public function connectTo($mode) {
42        $this->Lexer->addSpecialPattern('----+ *struct bez *-+\n.*?\n----+', $mode, 'plugin_bez_struct');
43    }
44
45    /**
46     * Handle matches of the struct syntax
47     *
48     * @param string $match The match of the syntax
49     * @param int $state The state of the handler
50     * @param int $pos The position in the document
51     * @param Doku_Handler $handler The handler
52     * @return array Data for the renderer
53     */
54    public function handle($match, $state, $pos, Doku_Handler $handler) {
55        global $conf;
56
57        $lines = explode("\n", $match);
58        array_shift($lines);
59        array_pop($lines);
60
61        try {
62            $parser = new ConfigParser($lines);
63            $config = $parser->getConfig();
64            return $config;
65        } catch(StructException $e) {
66            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
67            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
68            return null;
69        }
70    }
71
72    /**
73     * Render xhtml output or metadata
74     *
75     * @param string $mode Renderer mode (supported modes: xhtml)
76     * @param Doku_Renderer $renderer The renderer
77     * @param array $data The data from the handler() function
78     * @return bool If rendering was successful.
79     */
80    public function render($mode, Doku_Renderer $renderer, $data) {
81        if($mode != 'xhtml') return true;
82        if(!$data) return false;
83        global $INFO;
84        global $conf;
85        global $auth;
86
87        return true;
88
89        try {
90            $schema = $data['schemas'][0][0];
91            /** @var Model $model */
92            $model = new Model($auth, $INFO['client'], $this, $conf);
93
94            $factory = $model->factory($schema);
95            $search = new BezSearch($data, $factory);
96
97            /** @var AggregationTable $table */
98            $table = new AggregationTable($INFO['id'], $mode, $renderer, $search);
99            $table->render();
100
101        } catch(Exception $e) {
102            msg($e->getMessage(), -1, $e->getLine(), $e->getFile());
103            if($conf['allowdebug']) msg('<pre>' . hsc($e->getTraceAsString()) . '</pre>', -1);
104        }
105
106        return true;
107    }
108}
109