xref: /plugin/struct/syntax/output.php (revision 7938ca48e9616e2e020a922137123f8442612482)
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// must be run within Dokuwiki
10use dokuwiki\plugin\struct\meta\AccessTable;
11use dokuwiki\plugin\struct\meta\Assignments;
12
13if(!defined('DOKU_INC')) die();
14
15class syntax_plugin_struct_output extends DokuWiki_Syntax_Plugin {
16
17    protected $hasBeenRendered = false;
18
19    const XHTML_OPEN = '<div id="plugin__struct_output">';
20    const XHTML_CLOSE = '</div>';
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     * We do not connect any pattern here, because the call to this plugin is not
47     * triggered from syntax but our action component
48     *
49     * @asee action_plugin_struct_output
50     * @param string $mode Parser mode
51     */
52    public function connectTo($mode) {
53
54    }
55
56    /**
57     * Handle matches of the struct syntax
58     *
59     * @param string $match The match of the syntax
60     * @param int $state The state of the handler
61     * @param int $pos The position in the document
62     * @param Doku_Handler $handler The handler
63     * @return array Data for the renderer
64     */
65    public function handle($match, $state, $pos, Doku_Handler $handler) {
66        // this is never called
67        return array();
68    }
69
70    /**
71     * Render schema data
72     *
73     * Currently completely renderer agnostic
74     *
75     * @param string $mode Renderer mode
76     * @param Doku_Renderer $R 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 $R, $data) {
81        global $ID;
82        global $INFO;
83        global $REV;
84        if($ID != $INFO['id']) return true;
85        if(!$INFO['exists']) return true;
86        if($this->hasBeenRendered) return true;
87
88        // do not render the output twice on the same page, e.g. when another page has been included
89        $this->hasBeenRendered = true;
90
91        $assignments = new Assignments();
92        $tables = $assignments->getPageAssignments($ID);
93        if(!$tables) return true;
94
95        if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN;
96
97        $hasdata = false;
98        foreach($tables as $table) {
99            $schemadata = AccessTable::byTableName($table, $ID, $REV);
100            $schemadata->optionSkipEmpty(true);
101            $data = $schemadata->getData();
102            if(!count($data)) continue;
103            $hasdata = true;
104
105            $R->table_open();
106
107            $R->tablethead_open();
108            $R->tablerow_open();
109            $R->tableheader_open(2);
110            $R->cdata($table);
111            $R->tableheader_close();
112            $R->tablerow_close();
113            $R->tablethead_open();
114
115            $R->tabletbody_open();
116            foreach($data as $field) {
117                $R->tablerow_open();
118                $R->tableheader_open();
119                $R->cdata($field->getColumn()->getTranslatedLabel());
120                $R->tableheader_close();
121                $R->tablecell_open();
122                $field->render($R, $mode);
123                $R->tablecell_close();
124                $R->tablerow_close();
125            }
126            $R->tabletbody_close();
127            $R->table_close();
128        }
129
130        if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE;
131
132        // if no data has been output, remove empty wrapper again
133        if($mode == 'xhtml' && !$hasdata) {
134            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
135        }
136
137        return true;
138    }
139}
140
141// vim:ts=4:sw=4:et:
142