xref: /plugin/struct/syntax/output.php (revision 67b5dd6e74076c01ef194916f126914d7dd90b5e)
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;
127cbcfbdbSAndreas 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     *
744cc5dce0SAnna Dabrowska     * No output with renderers other than descendants of Doku_Renderer_xhtml
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) {
820e4a3e7cSMichael Große        global $ACT;
83257dd7f8SAndreas Gohr        global $ID;
8482c064c1SAndreas Gohr        global $INFO;
85257dd7f8SAndreas Gohr        global $REV;
864cc5dce0SAnna Dabrowska        if (!($R instanceof Doku_Renderer_xhtml)) {
870e4a3e7cSMichael Große            return true;
880e4a3e7cSMichael Große        }
8982c064c1SAndreas Gohr        if($ID != $INFO['id']) return true;
902f1a213bSAndreas Gohr        if(!$INFO['exists']) return true;
9187050b53SMichael Grosse        if($this->hasBeenRendered) return true;
9287050b53SMichael Grosse
9387050b53SMichael Grosse        // do not render the output twice on the same page, e.g. when another page has been included
9487050b53SMichael Grosse        $this->hasBeenRendered = true;
957cbcfbdbSAndreas Gohr        try {
96025cb9daSAndreas Gohr            $assignments = Assignments::getInstance();
977cbcfbdbSAndreas Gohr        } catch (StructException $e) {
987cbcfbdbSAndreas Gohr            return false;
997cbcfbdbSAndreas Gohr        }
100257dd7f8SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
101257dd7f8SAndreas Gohr        if(!$tables) return true;
102257dd7f8SAndreas Gohr
103*67b5dd6eSAnna Dabrowska        if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN;
1045a1eab78SAndreas Gohr
1057938ca48SAndreas Gohr        $hasdata = false;
106257dd7f8SAndreas Gohr        foreach($tables as $table) {
107a28d6152SAndreas Gohr            try {
108f411d872SAndreas Gohr                $schemadata = AccessTable::byTableName($table, $ID, $REV);
109a28d6152SAndreas Gohr            } catch(StructException $ignored) {
110a28d6152SAndreas Gohr                continue; // no such schema at this revision
111a28d6152SAndreas Gohr            }
1120dd23cefSAndreas Gohr            $schemadata->optionSkipEmpty(true);
1130dd23cefSAndreas Gohr            $data = $schemadata->getData();
114da30fdd3SAndreas Gohr            if(!count($data)) continue;
1157938ca48SAndreas Gohr            $hasdata = true;
116257dd7f8SAndreas Gohr
117da30fdd3SAndreas Gohr            $R->table_open();
118da30fdd3SAndreas Gohr
119da30fdd3SAndreas Gohr            $R->tablethead_open();
120da30fdd3SAndreas Gohr            $R->tablerow_open();
121da30fdd3SAndreas Gohr            $R->tableheader_open(2);
122127d6bacSMichael Große            $R->cdata($schemadata->getSchema()->getTranslatedLabel());
123da30fdd3SAndreas Gohr            $R->tableheader_close();
124da30fdd3SAndreas Gohr            $R->tablerow_close();
125da30fdd3SAndreas Gohr            $R->tablethead_open();
126da30fdd3SAndreas Gohr
127da30fdd3SAndreas Gohr            $R->tabletbody_open();
128257dd7f8SAndreas Gohr            foreach($data as $field) {
129257dd7f8SAndreas Gohr                $R->tablerow_open();
130257dd7f8SAndreas Gohr                $R->tableheader_open();
1319e9bee91SAndreas Gohr                $R->cdata($field->getColumn()->getTranslatedLabel());
132257dd7f8SAndreas Gohr                $R->tableheader_close();
133257dd7f8SAndreas Gohr                $R->tablecell_open();
13425852712SAndreas Gohr                if($mode == 'xhtml') {
13525852712SAndreas Gohr                    $R->doc = substr($R->doc, 0, -1) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">';
13625852712SAndreas Gohr                }
137257dd7f8SAndreas Gohr                $field->render($R, $mode);
138257dd7f8SAndreas Gohr                $R->tablecell_close();
139257dd7f8SAndreas Gohr                $R->tablerow_close();
140257dd7f8SAndreas Gohr            }
141257dd7f8SAndreas Gohr            $R->tabletbody_close();
142257dd7f8SAndreas Gohr            $R->table_close();
143da30fdd3SAndreas Gohr        }
144257dd7f8SAndreas Gohr
145*67b5dd6eSAnna Dabrowska        if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE;
1467938ca48SAndreas Gohr
1477938ca48SAndreas Gohr        // if no data has been output, remove empty wrapper again
148*67b5dd6eSAnna Dabrowska        if($mode == 'xhtml' && !$hasdata) {
1497938ca48SAndreas Gohr            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
1507938ca48SAndreas Gohr        }
1515a1eab78SAndreas Gohr
152257dd7f8SAndreas Gohr        return true;
153257dd7f8SAndreas Gohr    }
154257dd7f8SAndreas Gohr}
155257dd7f8SAndreas Gohr
156257dd7f8SAndreas Gohr// vim:ts=4:sw=4:et:
157