xref: /plugin/struct/syntax/output.php (revision 025cb9da4274aac00be48202c8eb49058f2dd283)
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
10f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
12257dd7f8SAndreas Gohr
13257dd7f8SAndreas Gohrif(!defined('DOKU_INC')) die();
14257dd7f8SAndreas Gohr
1582c064c1SAndreas Gohrclass syntax_plugin_struct_output extends DokuWiki_Syntax_Plugin {
1687050b53SMichael Grosse
1787050b53SMichael Grosse    protected $hasBeenRendered = false;
1887050b53SMichael Grosse
197938ca48SAndreas Gohr    const XHTML_OPEN = '<div id="plugin__struct_output">';
207938ca48SAndreas Gohr    const XHTML_CLOSE = '</div>';
217938ca48SAndreas Gohr
22257dd7f8SAndreas Gohr    /**
23257dd7f8SAndreas Gohr     * @return string Syntax mode type
24257dd7f8SAndreas Gohr     */
25257dd7f8SAndreas Gohr    public function getType() {
26257dd7f8SAndreas Gohr        return 'substition';
27257dd7f8SAndreas Gohr    }
28da30fdd3SAndreas Gohr
29257dd7f8SAndreas Gohr    /**
30257dd7f8SAndreas Gohr     * @return string Paragraph type
31257dd7f8SAndreas Gohr     */
32257dd7f8SAndreas Gohr    public function getPType() {
33257dd7f8SAndreas Gohr        return 'block';
34257dd7f8SAndreas Gohr    }
35da30fdd3SAndreas Gohr
36257dd7f8SAndreas Gohr    /**
37257dd7f8SAndreas Gohr     * @return int Sort order - Low numbers go before high numbers
38257dd7f8SAndreas Gohr     */
39257dd7f8SAndreas Gohr    public function getSort() {
40257dd7f8SAndreas Gohr        return 155;
41257dd7f8SAndreas Gohr    }
42257dd7f8SAndreas Gohr
43257dd7f8SAndreas Gohr    /**
44257dd7f8SAndreas Gohr     * Connect lookup pattern to lexer.
45257dd7f8SAndreas Gohr     *
4682c064c1SAndreas Gohr     * We do not connect any pattern here, because the call to this plugin is not
4782c064c1SAndreas Gohr     * triggered from syntax but our action component
4882c064c1SAndreas Gohr     *
4982c064c1SAndreas Gohr     * @asee action_plugin_struct_output
50257dd7f8SAndreas Gohr     * @param string $mode Parser mode
51257dd7f8SAndreas Gohr     */
52257dd7f8SAndreas Gohr    public function connectTo($mode) {
5382c064c1SAndreas Gohr
54257dd7f8SAndreas Gohr    }
55257dd7f8SAndreas Gohr
56257dd7f8SAndreas Gohr    /**
57257dd7f8SAndreas Gohr     * Handle matches of the struct syntax
58257dd7f8SAndreas Gohr     *
59257dd7f8SAndreas Gohr     * @param string $match The match of the syntax
60257dd7f8SAndreas Gohr     * @param int $state The state of the handler
61257dd7f8SAndreas Gohr     * @param int $pos The position in the document
62257dd7f8SAndreas Gohr     * @param Doku_Handler $handler The handler
63257dd7f8SAndreas Gohr     * @return array Data for the renderer
64257dd7f8SAndreas Gohr     */
65257dd7f8SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler) {
6682c064c1SAndreas Gohr        // this is never called
6782c064c1SAndreas Gohr        return array();
68257dd7f8SAndreas Gohr    }
69257dd7f8SAndreas Gohr
70257dd7f8SAndreas Gohr    /**
7182c064c1SAndreas Gohr     * Render schema data
72257dd7f8SAndreas Gohr     *
7382c064c1SAndreas Gohr     * Currently completely renderer agnostic
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
91*025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
92257dd7f8SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
93257dd7f8SAndreas Gohr        if(!$tables) return true;
94257dd7f8SAndreas Gohr
957938ca48SAndreas Gohr        if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN;
965a1eab78SAndreas Gohr
977938ca48SAndreas Gohr        $hasdata = false;
98257dd7f8SAndreas Gohr        foreach($tables as $table) {
99f411d872SAndreas Gohr            $schemadata = AccessTable::byTableName($table, $ID, $REV);
1000dd23cefSAndreas Gohr            $schemadata->optionSkipEmpty(true);
1010dd23cefSAndreas Gohr            $data = $schemadata->getData();
102da30fdd3SAndreas Gohr            if(!count($data)) continue;
1037938ca48SAndreas Gohr            $hasdata = true;
104257dd7f8SAndreas Gohr
105da30fdd3SAndreas Gohr            $R->table_open();
106da30fdd3SAndreas Gohr
107da30fdd3SAndreas Gohr            $R->tablethead_open();
108da30fdd3SAndreas Gohr            $R->tablerow_open();
109da30fdd3SAndreas Gohr            $R->tableheader_open(2);
110da30fdd3SAndreas Gohr            $R->cdata($table);
111da30fdd3SAndreas Gohr            $R->tableheader_close();
112da30fdd3SAndreas Gohr            $R->tablerow_close();
113da30fdd3SAndreas Gohr            $R->tablethead_open();
114da30fdd3SAndreas Gohr
115da30fdd3SAndreas Gohr            $R->tabletbody_open();
116257dd7f8SAndreas Gohr            foreach($data as $field) {
117257dd7f8SAndreas Gohr                $R->tablerow_open();
118257dd7f8SAndreas Gohr                $R->tableheader_open();
1199e9bee91SAndreas Gohr                $R->cdata($field->getColumn()->getTranslatedLabel());
120257dd7f8SAndreas Gohr                $R->tableheader_close();
121257dd7f8SAndreas Gohr                $R->tablecell_open();
122257dd7f8SAndreas Gohr                $field->render($R, $mode);
123257dd7f8SAndreas Gohr                $R->tablecell_close();
124257dd7f8SAndreas Gohr                $R->tablerow_close();
125257dd7f8SAndreas Gohr            }
126257dd7f8SAndreas Gohr            $R->tabletbody_close();
127257dd7f8SAndreas Gohr            $R->table_close();
128da30fdd3SAndreas Gohr        }
129257dd7f8SAndreas Gohr
1307938ca48SAndreas Gohr        if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE;
1317938ca48SAndreas Gohr
1327938ca48SAndreas Gohr        // if no data has been output, remove empty wrapper again
1337938ca48SAndreas Gohr        if($mode == 'xhtml' && !$hasdata) {
1347938ca48SAndreas Gohr            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
1357938ca48SAndreas Gohr        }
1365a1eab78SAndreas Gohr
137257dd7f8SAndreas Gohr        return true;
138257dd7f8SAndreas Gohr    }
139257dd7f8SAndreas Gohr}
140257dd7f8SAndreas Gohr
141257dd7f8SAndreas Gohr// vim:ts=4:sw=4:et:
142