xref: /plugin/struct/syntax/output.php (revision 5d17398f53f42b0d82bee77abf32606b63e8cb61)
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            try {
104                $schemadata = AccessTable::byTableName($table, $ID, $REV);
105            } catch(StructException $ignored) {
106                continue; // no such schema at this revision
107            }
108            $schemadata->optionSkipEmpty(true);
109            $data = $schemadata->getData();
110            if(!count($data)) continue;
111            $hasdata = true;
112
113            $R->table_open();
114
115            $R->tablethead_open();
116            $R->tablerow_open();
117            $R->tableheader_open(2);
118            $R->cdata($table);
119            $R->tableheader_close();
120            $R->tablerow_close();
121            $R->tablethead_open();
122
123            $R->tabletbody_open();
124            foreach($data as $field) {
125                $R->tablerow_open();
126                $R->tableheader_open();
127                $R->cdata($field->getColumn()->getTranslatedLabel());
128                $R->tableheader_close();
129                $R->tablecell_open();
130                if($mode == 'xhtml') {
131                    $R->doc = substr($R->doc, 0, -1) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">';
132                }
133                $field->render($R, $mode);
134                $R->tablecell_close();
135                $R->tablerow_close();
136            }
137            $R->tabletbody_close();
138            $R->table_close();
139        }
140
141        if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE;
142
143        // if no data has been output, remove empty wrapper again
144        if($mode == 'xhtml' && !$hasdata) {
145            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
146        }
147
148        return true;
149    }
150}
151
152// vim:ts=4:sw=4:et:
153