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