xref: /plugin/struct/syntax/output.php (revision 7cbcfbdb68125878b37fede99d5e33997295c2f6)
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;
12*7cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
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
207938ca48SAndreas Gohr    const XHTML_OPEN = '<div id="plugin__struct_output">';
217938ca48SAndreas Gohr    const XHTML_CLOSE = '</div>';
227938ca48SAndreas Gohr
23257dd7f8SAndreas Gohr    /**
24257dd7f8SAndreas Gohr     * @return string Syntax mode type
25257dd7f8SAndreas Gohr     */
26257dd7f8SAndreas Gohr    public function getType() {
27257dd7f8SAndreas Gohr        return 'substition';
28257dd7f8SAndreas Gohr    }
29da30fdd3SAndreas Gohr
30257dd7f8SAndreas Gohr    /**
31257dd7f8SAndreas Gohr     * @return string Paragraph type
32257dd7f8SAndreas Gohr     */
33257dd7f8SAndreas Gohr    public function getPType() {
34257dd7f8SAndreas Gohr        return 'block';
35257dd7f8SAndreas Gohr    }
36da30fdd3SAndreas Gohr
37257dd7f8SAndreas Gohr    /**
38257dd7f8SAndreas Gohr     * @return int Sort order - Low numbers go before high numbers
39257dd7f8SAndreas Gohr     */
40257dd7f8SAndreas Gohr    public function getSort() {
41257dd7f8SAndreas Gohr        return 155;
42257dd7f8SAndreas Gohr    }
43257dd7f8SAndreas Gohr
44257dd7f8SAndreas Gohr    /**
45257dd7f8SAndreas Gohr     * Connect lookup pattern to lexer.
46257dd7f8SAndreas Gohr     *
4782c064c1SAndreas Gohr     * We do not connect any pattern here, because the call to this plugin is not
4882c064c1SAndreas Gohr     * triggered from syntax but our action component
4982c064c1SAndreas Gohr     *
5082c064c1SAndreas Gohr     * @asee action_plugin_struct_output
51257dd7f8SAndreas Gohr     * @param string $mode Parser mode
52257dd7f8SAndreas Gohr     */
53257dd7f8SAndreas Gohr    public function connectTo($mode) {
5482c064c1SAndreas Gohr
55257dd7f8SAndreas Gohr    }
56257dd7f8SAndreas Gohr
57257dd7f8SAndreas Gohr    /**
58257dd7f8SAndreas Gohr     * Handle matches of the struct syntax
59257dd7f8SAndreas Gohr     *
60257dd7f8SAndreas Gohr     * @param string $match The match of the syntax
61257dd7f8SAndreas Gohr     * @param int $state The state of the handler
62257dd7f8SAndreas Gohr     * @param int $pos The position in the document
63257dd7f8SAndreas Gohr     * @param Doku_Handler $handler The handler
64257dd7f8SAndreas Gohr     * @return array Data for the renderer
65257dd7f8SAndreas Gohr     */
66257dd7f8SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler) {
6782c064c1SAndreas Gohr        // this is never called
6882c064c1SAndreas Gohr        return array();
69257dd7f8SAndreas Gohr    }
70257dd7f8SAndreas Gohr
71257dd7f8SAndreas Gohr    /**
7282c064c1SAndreas Gohr     * Render schema data
73257dd7f8SAndreas Gohr     *
7482c064c1SAndreas Gohr     * Currently completely renderer agnostic
7582c064c1SAndreas Gohr     *
7682c064c1SAndreas Gohr     * @param string $mode Renderer mode
77257dd7f8SAndreas Gohr     * @param Doku_Renderer $R The renderer
78257dd7f8SAndreas Gohr     * @param array $data The data from the handler() function
79257dd7f8SAndreas Gohr     * @return bool If rendering was successful.
80257dd7f8SAndreas Gohr     */
81257dd7f8SAndreas Gohr    public function render($mode, Doku_Renderer $R, $data) {
82257dd7f8SAndreas Gohr        global $ID;
8382c064c1SAndreas Gohr        global $INFO;
84257dd7f8SAndreas Gohr        global $REV;
8582c064c1SAndreas Gohr        if($ID != $INFO['id']) return true;
862f1a213bSAndreas Gohr        if(!$INFO['exists']) return true;
8787050b53SMichael Grosse        if($this->hasBeenRendered) return true;
8887050b53SMichael Grosse
8987050b53SMichael Grosse        // do not render the output twice on the same page, e.g. when another page has been included
9087050b53SMichael Grosse        $this->hasBeenRendered = true;
91*7cbcfbdbSAndreas Gohr        try {
92025cb9daSAndreas Gohr            $assignments = Assignments::getInstance();
93*7cbcfbdbSAndreas Gohr        } catch (StructException $e) {
94*7cbcfbdbSAndreas Gohr            return false;
95*7cbcfbdbSAndreas Gohr        }
96257dd7f8SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
97257dd7f8SAndreas Gohr        if(!$tables) return true;
98257dd7f8SAndreas Gohr
997938ca48SAndreas Gohr        if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN;
1005a1eab78SAndreas Gohr
1017938ca48SAndreas Gohr        $hasdata = false;
102257dd7f8SAndreas Gohr        foreach($tables as $table) {
103f411d872SAndreas Gohr            $schemadata = AccessTable::byTableName($table, $ID, $REV);
1040dd23cefSAndreas Gohr            $schemadata->optionSkipEmpty(true);
1050dd23cefSAndreas Gohr            $data = $schemadata->getData();
106da30fdd3SAndreas Gohr            if(!count($data)) continue;
1077938ca48SAndreas Gohr            $hasdata = true;
108257dd7f8SAndreas Gohr
109da30fdd3SAndreas Gohr            $R->table_open();
110da30fdd3SAndreas Gohr
111da30fdd3SAndreas Gohr            $R->tablethead_open();
112da30fdd3SAndreas Gohr            $R->tablerow_open();
113da30fdd3SAndreas Gohr            $R->tableheader_open(2);
114da30fdd3SAndreas Gohr            $R->cdata($table);
115da30fdd3SAndreas Gohr            $R->tableheader_close();
116da30fdd3SAndreas Gohr            $R->tablerow_close();
117da30fdd3SAndreas Gohr            $R->tablethead_open();
118da30fdd3SAndreas Gohr
119da30fdd3SAndreas Gohr            $R->tabletbody_open();
120257dd7f8SAndreas Gohr            foreach($data as $field) {
121257dd7f8SAndreas Gohr                $R->tablerow_open();
122257dd7f8SAndreas Gohr                $R->tableheader_open();
1239e9bee91SAndreas Gohr                $R->cdata($field->getColumn()->getTranslatedLabel());
124257dd7f8SAndreas Gohr                $R->tableheader_close();
125257dd7f8SAndreas Gohr                $R->tablecell_open();
126257dd7f8SAndreas Gohr                $field->render($R, $mode);
127257dd7f8SAndreas Gohr                $R->tablecell_close();
128257dd7f8SAndreas Gohr                $R->tablerow_close();
129257dd7f8SAndreas Gohr            }
130257dd7f8SAndreas Gohr            $R->tabletbody_close();
131257dd7f8SAndreas Gohr            $R->table_close();
132da30fdd3SAndreas Gohr        }
133257dd7f8SAndreas Gohr
1347938ca48SAndreas Gohr        if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE;
1357938ca48SAndreas Gohr
1367938ca48SAndreas Gohr        // if no data has been output, remove empty wrapper again
1377938ca48SAndreas Gohr        if($mode == 'xhtml' && !$hasdata) {
1387938ca48SAndreas Gohr            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
1397938ca48SAndreas Gohr        }
1405a1eab78SAndreas Gohr
141257dd7f8SAndreas Gohr        return true;
142257dd7f8SAndreas Gohr    }
143257dd7f8SAndreas Gohr}
144257dd7f8SAndreas Gohr
145257dd7f8SAndreas Gohr// vim:ts=4:sw=4:et:
146