xref: /plugin/struct/syntax/output.php (revision f411d87222d57469b74e2810a1ff3b1f60d1daa8)
1257dd7f8SAndreas Gohr<?php
2257dd7f8SAndreas Gohr/**
3257dd7f8SAndreas Gohr * DokuWiki Plugin struct (Syntax Component)
4257dd7f8SAndreas Gohr *
5257dd7f8SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6257dd7f8SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7257dd7f8SAndreas Gohr */
8257dd7f8SAndreas Gohr
9257dd7f8SAndreas Gohr// must be run within Dokuwiki
10*f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaData;
13257dd7f8SAndreas Gohr
14257dd7f8SAndreas Gohrif(!defined('DOKU_INC')) die();
15257dd7f8SAndreas Gohr
1682c064c1SAndreas Gohrclass syntax_plugin_struct_output extends DokuWiki_Syntax_Plugin {
1787050b53SMichael Grosse
1887050b53SMichael Grosse    protected $hasBeenRendered = false;
1987050b53SMichael Grosse
20257dd7f8SAndreas Gohr    /**
21257dd7f8SAndreas Gohr     * @return string Syntax mode type
22257dd7f8SAndreas Gohr     */
23257dd7f8SAndreas Gohr    public function getType() {
24257dd7f8SAndreas Gohr        return 'substition';
25257dd7f8SAndreas Gohr    }
26da30fdd3SAndreas Gohr
27257dd7f8SAndreas Gohr    /**
28257dd7f8SAndreas Gohr     * @return string Paragraph type
29257dd7f8SAndreas Gohr     */
30257dd7f8SAndreas Gohr    public function getPType() {
31257dd7f8SAndreas Gohr        return 'block';
32257dd7f8SAndreas Gohr    }
33da30fdd3SAndreas Gohr
34257dd7f8SAndreas Gohr    /**
35257dd7f8SAndreas Gohr     * @return int Sort order - Low numbers go before high numbers
36257dd7f8SAndreas Gohr     */
37257dd7f8SAndreas Gohr    public function getSort() {
38257dd7f8SAndreas Gohr        return 155;
39257dd7f8SAndreas Gohr    }
40257dd7f8SAndreas Gohr
41257dd7f8SAndreas Gohr    /**
42257dd7f8SAndreas Gohr     * Connect lookup pattern to lexer.
43257dd7f8SAndreas Gohr     *
4482c064c1SAndreas Gohr     * We do not connect any pattern here, because the call to this plugin is not
4582c064c1SAndreas Gohr     * triggered from syntax but our action component
4682c064c1SAndreas Gohr     *
4782c064c1SAndreas Gohr     * @asee action_plugin_struct_output
48257dd7f8SAndreas Gohr     * @param string $mode Parser mode
49257dd7f8SAndreas Gohr     */
50257dd7f8SAndreas Gohr    public function connectTo($mode) {
5182c064c1SAndreas Gohr
52257dd7f8SAndreas Gohr    }
53257dd7f8SAndreas Gohr
54257dd7f8SAndreas Gohr    /**
55257dd7f8SAndreas Gohr     * Handle matches of the struct syntax
56257dd7f8SAndreas Gohr     *
57257dd7f8SAndreas Gohr     * @param string $match The match of the syntax
58257dd7f8SAndreas Gohr     * @param int $state The state of the handler
59257dd7f8SAndreas Gohr     * @param int $pos The position in the document
60257dd7f8SAndreas Gohr     * @param Doku_Handler $handler The handler
61257dd7f8SAndreas Gohr     * @return array Data for the renderer
62257dd7f8SAndreas Gohr     */
63257dd7f8SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler) {
6482c064c1SAndreas Gohr        // this is never called
6582c064c1SAndreas Gohr        return array();
66257dd7f8SAndreas Gohr    }
67257dd7f8SAndreas Gohr
68257dd7f8SAndreas Gohr    /**
6982c064c1SAndreas Gohr     * Render schema data
70257dd7f8SAndreas Gohr     *
7182c064c1SAndreas Gohr     * Currently completely renderer agnostic
7282c064c1SAndreas Gohr     *
7382c064c1SAndreas Gohr     * @todo we currently have no schema headlines
7482c064c1SAndreas Gohr     *
7582c064c1SAndreas Gohr     * @param string $mode Renderer mode
76257dd7f8SAndreas Gohr     * @param Doku_Renderer $R The renderer
77257dd7f8SAndreas Gohr     * @param array $data The data from the handler() function
78257dd7f8SAndreas Gohr     * @return bool If rendering was successful.
79257dd7f8SAndreas Gohr     */
80257dd7f8SAndreas Gohr    public function render($mode, Doku_Renderer $R, $data) {
81257dd7f8SAndreas Gohr        global $ID;
8282c064c1SAndreas Gohr        global $INFO;
83257dd7f8SAndreas Gohr        global $REV;
8482c064c1SAndreas Gohr        if($ID != $INFO['id']) return true;
852f1a213bSAndreas Gohr        if(!$INFO['exists']) return true;
8687050b53SMichael Grosse        if($this->hasBeenRendered) return true;
8787050b53SMichael Grosse
8887050b53SMichael Grosse        // do not render the output twice on the same page, e.g. when another page has been included
8987050b53SMichael Grosse        $this->hasBeenRendered = true;
90257dd7f8SAndreas Gohr
91257dd7f8SAndreas Gohr        $assignments = new Assignments();
92257dd7f8SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
93257dd7f8SAndreas Gohr        if(!$tables) return true;
94257dd7f8SAndreas Gohr
955a1eab78SAndreas Gohr        if($mode == 'xhtml') $R->doc .= '<div id="plugin__struct_output">';
965a1eab78SAndreas Gohr
97257dd7f8SAndreas Gohr        foreach($tables as $table) {
98*f411d872SAndreas Gohr            $schemadata = AccessTable::byTableName($table, $ID, $REV);
990dd23cefSAndreas Gohr            $schemadata->optionSkipEmpty(true);
1000dd23cefSAndreas Gohr            $data = $schemadata->getData();
101da30fdd3SAndreas Gohr            if(!count($data)) continue;
102257dd7f8SAndreas Gohr
103da30fdd3SAndreas Gohr            $R->table_open();
104da30fdd3SAndreas Gohr
105da30fdd3SAndreas Gohr            $R->tablethead_open();
106da30fdd3SAndreas Gohr            $R->tablerow_open();
107da30fdd3SAndreas Gohr            $R->tableheader_open(2);
108da30fdd3SAndreas Gohr            $R->cdata($table);
109da30fdd3SAndreas Gohr            $R->tableheader_close();
110da30fdd3SAndreas Gohr            $R->tablerow_close();
111da30fdd3SAndreas Gohr            $R->tablethead_open();
112da30fdd3SAndreas Gohr
113da30fdd3SAndreas Gohr            $R->tabletbody_open();
114257dd7f8SAndreas Gohr            foreach($data as $field) {
115257dd7f8SAndreas Gohr                $R->tablerow_open();
116257dd7f8SAndreas Gohr                $R->tableheader_open();
1179e9bee91SAndreas Gohr                $R->cdata($field->getColumn()->getTranslatedLabel());
118257dd7f8SAndreas Gohr                $R->tableheader_close();
119257dd7f8SAndreas Gohr                $R->tablecell_open();
120257dd7f8SAndreas Gohr                $field->render($R, $mode);
121257dd7f8SAndreas Gohr                $R->tablecell_close();
122257dd7f8SAndreas Gohr                $R->tablerow_close();
123257dd7f8SAndreas Gohr            }
124257dd7f8SAndreas Gohr            $R->tabletbody_close();
125257dd7f8SAndreas Gohr            $R->table_close();
126da30fdd3SAndreas Gohr        }
127257dd7f8SAndreas Gohr
1285a1eab78SAndreas Gohr        if($mode == 'xhtml') $R->doc .= '</div>';
1295a1eab78SAndreas Gohr
130257dd7f8SAndreas Gohr        return true;
131257dd7f8SAndreas Gohr    }
132257dd7f8SAndreas Gohr}
133257dd7f8SAndreas Gohr
134257dd7f8SAndreas Gohr// vim:ts=4:sw=4:et:
135