xref: /plugin/struct/syntax/output.php (revision ea5ad12ac615a67b2c935ce7472ece1c00189ba7)
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;
12use dokuwiki\plugin\struct\meta\StructException;
13
14if(!defined('DOKU_INC')) die();
15
16class syntax_plugin_struct_output extends DokuWiki_Syntax_Plugin {
17
18    protected $hasBeenRendered = false;
19
20    const XHTML_OPEN = '<div id="plugin__struct_output">';
21    const XHTML_CLOSE = '</div>';
22
23    /**
24     * @return string Syntax mode type
25     */
26    public function getType() {
27        return 'substition';
28    }
29
30    /**
31     * @return string Paragraph type
32     */
33    public function getPType() {
34        return 'block';
35    }
36
37    /**
38     * @return int Sort order - Low numbers go before high numbers
39     */
40    public function getSort() {
41        return 155;
42    }
43
44    /**
45     * Connect lookup pattern to lexer.
46     *
47     * We do not connect any pattern here, because the call to this plugin is not
48     * triggered from syntax but our action component
49     *
50     * @asee action_plugin_struct_output
51     * @param string $mode Parser mode
52     */
53    public function connectTo($mode) {
54
55    }
56
57    /**
58     * Handle matches of the struct syntax
59     *
60     * @param string $match The match of the syntax
61     * @param int $state The state of the handler
62     * @param int $pos The position in the document
63     * @param Doku_Handler $handler The handler
64     * @return array Data for the renderer
65     */
66    public function handle($match, $state, $pos, Doku_Handler $handler) {
67        // this is never called
68        return array();
69    }
70
71    /**
72     * Render schema data
73     *
74     * Currently completely renderer agnostic
75     *
76     * @param string $mode Renderer mode
77     * @param Doku_Renderer $R The renderer
78     * @param array $data The data from the handler() function
79     * @return bool If rendering was successful.
80     */
81    public function render($mode, Doku_Renderer $R, $data) {
82        global $ID;
83        global $INFO;
84        global $REV;
85        if($ID != $INFO['id']) return true;
86        if(!$INFO['exists']) return true;
87        if($this->hasBeenRendered) return true;
88
89        // do not render the output twice on the same page, e.g. when another page has been included
90        $this->hasBeenRendered = true;
91        try {
92            $assignments = Assignments::getInstance();
93        } catch (StructException $e) {
94            return false;
95        }
96        $tables = $assignments->getPageAssignments($ID);
97        if(!$tables) return true;
98
99        if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN;
100
101        $hasdata = false;
102        foreach($tables as $table) {
103            $schemadata = AccessTable::byTableName($table, $ID, $REV);
104            $schemadata->optionSkipEmpty(true);
105            $data = $schemadata->getData();
106            if(!count($data)) continue;
107            $hasdata = true;
108
109            $R->table_open();
110
111            $R->tablethead_open();
112            $R->tablerow_open();
113            $R->tableheader_open(2);
114            $R->cdata($table);
115            $R->tableheader_close();
116            $R->tablerow_close();
117            $R->tablethead_open();
118
119            $R->tabletbody_open();
120            foreach($data as $field) {
121                $R->tablerow_open();
122                $R->tableheader_open();
123                $R->cdata($field->getColumn()->getTranslatedLabel());
124                $R->tableheader_close();
125                $R->tablecell_open();
126                if($mode == 'xhtml') {
127                    $R->doc = substr($R->doc, 0, -1) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">';
128                }
129                $field->render($R, $mode);
130                $R->tablecell_close();
131                $R->tablerow_close();
132            }
133            $R->tabletbody_close();
134            $R->table_close();
135        }
136
137        if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE;
138
139        // if no data has been output, remove empty wrapper again
140        if($mode == 'xhtml' && !$hasdata) {
141            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
142        }
143
144        return true;
145    }
146}
147
148// vim:ts=4:sw=4:et:
149