xref: /plugin/struct/syntax/output.php (revision 2dbe71f867df0982fb08a9a8f3cd3d027278de03)
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\AccessTable;
11use dokuwiki\plugin\struct\meta\Assignments;
12use dokuwiki\plugin\struct\meta\StructException;
13
14class syntax_plugin_struct_output extends DokuWiki_Syntax_Plugin
15{
16
17    protected $hasBeenRendered = false;
18
19    const XHTML_OPEN = '<div id="plugin__struct_output">';
20    const XHTML_CLOSE = '</div>';
21
22    /**
23     * Regexp to check on which actions the struct data may be rendered
24     */
25    const WHITELIST_ACTIONS = '/^(show|export_.*)$/';
26
27    /**
28     * @return string Syntax mode type
29     */
30    public function getType()
31    {
32        return 'substition';
33    }
34
35    /**
36     * @return string Paragraph type
37     */
38    public function getPType()
39    {
40        return 'block';
41    }
42
43    /**
44     * @return int Sort order - Low numbers go before high numbers
45     */
46    public function getSort()
47    {
48        return 155;
49    }
50
51    /**
52     * Connect lookup pattern to lexer.
53     *
54     * We do not connect any pattern here, because the call to this plugin is not
55     * triggered from syntax but our action component
56     *
57     * @asee action_plugin_struct_output
58     * @param string $mode Parser mode
59     */
60    public function connectTo($mode)
61    {
62    }
63
64    /**
65     * Handle matches of the struct syntax
66     *
67     * @param string $match The match of the syntax
68     * @param int $state The state of the handler
69     * @param int $pos The position in the document
70     * @param Doku_Handler $handler The handler
71     * @return array Data for the renderer
72     */
73    public function handle($match, $state, $pos, Doku_Handler $handler)
74    {
75        // this is never called
76        return array();
77    }
78
79    /**
80     * Render schema data
81     *
82     * Currently completely renderer agnostic
83     *
84     * @param string $format Renderer format
85     * @param Doku_Renderer $renderer The renderer
86     * @param array $data The data from the handler() function
87     * @return bool If rendering was successful.
88     */
89    public function render($format, Doku_Renderer $renderer, $data)
90    {
91        global $ACT;
92        global $ID;
93        global $INFO;
94        global $REV;
95
96        foreach (helper_plugin_struct::BLACKLIST_RENDERER as $blacklisted) {
97            if ($renderer instanceof $blacklisted) {
98                return true;
99            }
100        }
101        if ($ID != $INFO['id']) return true;
102        if (!$INFO['exists']) return true;
103        if ($this->hasBeenRendered) return true;
104        if (!preg_match(self::WHITELIST_ACTIONS, act_clean($ACT))) return true;
105
106        // do not render the output twice on the same page, e.g. when another page has been included
107        $this->hasBeenRendered = true;
108        try {
109            $assignments = Assignments::getInstance();
110        } catch (StructException $e) {
111            return false;
112        }
113        $tables = $assignments->getPageAssignments($ID);
114        if (!$tables) return true;
115
116        if ($format == 'xhtml') $renderer->doc .= self::XHTML_OPEN;
117
118        $hasdata = false;
119        foreach ($tables as $table) {
120            try {
121                $schemadata = AccessTable::getPageAccess($table, $ID, (int)$REV);
122            } catch (StructException $ignored) {
123                continue; // no such schema at this revision
124            }
125
126            $rendercontext = array(
127                'renderer' => $renderer,
128                'format' => $format,
129                'meta' => p_get_metadata($ID),
130                'schemadata' => $schemadata,
131                'hasdata' => &$hasdata
132            );
133
134            $event = new \Doku_Event(
135                'PLUGIN_STRUCT_RENDER_SCHEMA_DATA',
136                $rendercontext
137            );
138            $event->trigger([$this, 'renderSchemaData']);
139        }
140
141        if ($format == 'xhtml') $renderer->doc .= self::XHTML_CLOSE;
142
143        // if no data has been output, remove empty wrapper again
144        if ($format == 'xhtml' && !$hasdata) {
145            $renderer->doc = substr($renderer->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
146        }
147
148        return true;
149    }
150
151    /**
152     * Default schema data rendering (simple table view)
153     *
154     * @param array The render context including renderer and data
155     */
156    public function renderSchemaData($rendercontext)
157    {
158        $schemadata = $rendercontext['schemadata'];
159        $renderer = $rendercontext['renderer'];
160        $format = $rendercontext['format'];
161
162        $schemadata->optionSkipEmpty(true);
163        $data = $schemadata->getData();
164        if (!count($data))
165            return;
166
167        $rendercontext['hasdata'] = true;
168
169        $renderer->table_open();
170        $renderer->tablethead_open();
171        $renderer->tablerow_open();
172        $renderer->tableheader_open(2);
173        $renderer->cdata($schemadata->getSchema()->getTranslatedLabel());
174        $renderer->tableheader_close();
175        $renderer->tablerow_close();
176        $renderer->tablethead_close();
177
178        $renderer->tabletbody_open();
179        foreach ($data as $field) {
180            $renderer->tablerow_open();
181            $renderer->tableheader_open();
182            $renderer->cdata($field->getColumn()->getTranslatedLabel());
183            $renderer->tableheader_close();
184            $renderer->tablecell_open();
185            if ($format == 'xhtml') {
186                $renderer->doc = substr($renderer->doc, 0, -1) .
187                    ' data-struct="' . hsc($field->getColumn()->getFullQualifiedLabel()) .
188                    '">';
189            }
190            $field->render($renderer, $format);
191            $renderer->tablecell_close();
192            $renderer->tablerow_close();
193        }
194        $renderer->tabletbody_close();
195        $renderer->table_close();
196    }
197}
198
199// vim:ts=4:sw=4:et:
200