xref: /plugin/struct/syntax/output.php (revision 7c5fd8d6756c15818709cf52cb599c5f987bbb7b)
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            $schemadata->optionSkipEmpty(true);
99            $data = $schemadata->getData();
100            if(!count($data)) continue;
101
102            $R->table_open();
103
104            $R->tablethead_open();
105            $R->tablerow_open();
106            $R->tableheader_open(2);
107            $R->cdata($table);
108            $R->tableheader_close();
109            $R->tablerow_close();
110            $R->tablethead_open();
111
112            $R->tabletbody_open();
113            foreach($data as $field) {
114                $R->tablerow_open();
115                $R->tableheader_open();
116                $R->cdata($field->getColumn()->getTranslatedLabel());
117                $R->tableheader_close();
118                $R->tablecell_open();
119                $field->render($R, $mode);
120                $R->tablecell_close();
121                $R->tablerow_close();
122            }
123            $R->tabletbody_close();
124            $R->table_close();
125        }
126
127        if($mode == 'xhtml') $R->doc .= '</div>';
128
129        return true;
130    }
131}
132
133// vim:ts=4:sw=4:et:
134