1<?php
2
3/**
4 * DokuWiki Plugin structsection (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Michael Große <mic.grosse@googlemail.com>
8 */
9
10use dokuwiki\plugin\struct\meta\AccessTable;
11use dokuwiki\plugin\struct\meta\Assignments;
12use dokuwiki\plugin\struct\meta\StructException;
13
14class syntax_plugin_structsection extends \DokuWiki_Syntax_Plugin
15{
16
17    protected $hasBeenRendered = false;
18
19    private const XHTML_OPEN = '<div id="plugin__structsection_output">';
20    private const XHTML_CLOSE = '</div>';
21
22    /**
23     * @return string Syntax mode type
24     */
25    public function getType()
26    {
27        return 'substition';
28    }
29
30    /**
31     * @return string Paragraph type
32     */
33    public function getPType()
34    {
35        return 'block';
36    }
37
38    /**
39     * @return int Sort order - Low numbers go before high numbers
40     */
41    public function getSort()
42    {
43        return 155;
44    }
45
46    /**
47     * Connect lookup pattern to lexer.
48     *
49     * We do not connect any pattern here, because the call to this plugin is not
50     * triggered from syntax but our action component
51     *
52     * @asee action_plugin_structsection
53     * @param string $mode Parser mode
54     */
55    public function connectTo($mode)
56    {
57    }
58
59    /**
60     * Handle matches of the struct syntax
61     *
62     * @param string $match The match of the syntax
63     * @param int $state The state of the handler
64     * @param int $pos The position in the document
65     * @param \Doku_Handler $handler The handler
66     * @return array Data for the renderer
67     */
68    public function handle($match, $state, $pos, \Doku_Handler $handler)
69    {
70        // this is never called
71        return array();
72    }
73
74    /**
75     * Render schema data
76     *
77     * Currently completely renderer agnostic
78     *
79     * @param string $mode Renderer mode
80     * @param \Doku_Renderer $R The renderer
81     * @param array $handlerData The data from the handler() function
82     * @return bool If rendering was successful.
83     */
84    public function render($mode, \Doku_Renderer $R, $handlerData)
85    {
86        global $ID;
87        global $INFO;
88        global $REV;
89        if ($ID != $INFO['id']) {
90            return true;
91        }
92        if (!$INFO['exists']) {
93            return true;
94        }
95        if ($this->hasBeenRendered) {
96            return true;
97        }
98
99        // do not render the output twice on the same page, e.g. when another page has been included
100        $this->hasBeenRendered = true;
101        try {
102            $assignments = Assignments::getInstance();
103        } catch (StructException $e) {
104            return false;
105        }
106        $tables = $assignments->getPageAssignments($ID);
107        if (!$tables) {
108            return true;
109        }
110
111        $pos = $handlerData['pos'];
112        if ($mode == 'xhtml') {
113            $R->finishSectionEdit($pos - 1);
114            $R->doc .= self::XHTML_OPEN;
115        }
116
117
118        $hasdata = false;
119        foreach ($tables as $table) {
120            try {
121                $schemadata = AccessTable::getPageAccess($table, $ID, $REV);
122            } catch (StructException $ignored) {
123                continue; // no such schema at this revision
124            }
125            $schemadata->optionSkipEmpty(false);
126            $data = $schemadata->getData();
127            if (!count($data)) {
128                continue;
129            }
130            $hasdata = true;
131
132            foreach ($data as $field) {
133                if (!is_a($field->getColumn()->getType(), \dokuwiki\plugin\structsection\types\Section::class)) {
134                    continue;
135                }
136                $lvl = 2;
137                $R->header($field->getColumn()->getTranslatedLabel(), $lvl, $pos);
138                $pos += strlen($field->getColumn()->getTranslatedLabel());
139                $R->section_open($lvl);
140                if ($mode === 'xhtml') {
141                    $structDataAttribute = 'data-struct="' . hsc($field->getColumn()->getFullQualifiedLabel()) . '"';
142                    $R->doc = substr($R->doc, 0, -2) . ' ' . $structDataAttribute . '>' . "\n";
143                }
144                $field->render($R, $mode);
145                $R->section_close();
146            }
147        }
148
149        if ($mode == 'xhtml') {
150            $R->finishSectionEdit($pos);
151            $R->doc .= self::XHTML_CLOSE;
152        }
153
154        // if no data has been output, remove empty wrapper again
155        if ($mode == 'xhtml' && !$hasdata) {
156            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
157        }
158
159        return true;
160    }
161}
162
163// vim:ts=4:sw=4:et:
164