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