xref: /plugin/struct/syntax/output.php (revision 0dd23cef92d4871326b68daa31f600a089cdf9ff)
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
10ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaData;
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
19257dd7f8SAndreas Gohr    /**
20257dd7f8SAndreas Gohr     * @return string Syntax mode type
21257dd7f8SAndreas Gohr     */
22257dd7f8SAndreas Gohr    public function getType() {
23257dd7f8SAndreas Gohr        return 'substition';
24257dd7f8SAndreas Gohr    }
25da30fdd3SAndreas Gohr
26257dd7f8SAndreas Gohr    /**
27257dd7f8SAndreas Gohr     * @return string Paragraph type
28257dd7f8SAndreas Gohr     */
29257dd7f8SAndreas Gohr    public function getPType() {
30257dd7f8SAndreas Gohr        return 'block';
31257dd7f8SAndreas Gohr    }
32da30fdd3SAndreas Gohr
33257dd7f8SAndreas Gohr    /**
34257dd7f8SAndreas Gohr     * @return int Sort order - Low numbers go before high numbers
35257dd7f8SAndreas Gohr     */
36257dd7f8SAndreas Gohr    public function getSort() {
37257dd7f8SAndreas Gohr        return 155;
38257dd7f8SAndreas Gohr    }
39257dd7f8SAndreas Gohr
40257dd7f8SAndreas Gohr    /**
41257dd7f8SAndreas Gohr     * Connect lookup pattern to lexer.
42257dd7f8SAndreas Gohr     *
4382c064c1SAndreas Gohr     * We do not connect any pattern here, because the call to this plugin is not
4482c064c1SAndreas Gohr     * triggered from syntax but our action component
4582c064c1SAndreas Gohr     *
4682c064c1SAndreas Gohr     * @asee action_plugin_struct_output
47257dd7f8SAndreas Gohr     * @param string $mode Parser mode
48257dd7f8SAndreas Gohr     */
49257dd7f8SAndreas Gohr    public function connectTo($mode) {
5082c064c1SAndreas Gohr
51257dd7f8SAndreas Gohr    }
52257dd7f8SAndreas Gohr
53257dd7f8SAndreas Gohr    /**
54257dd7f8SAndreas Gohr     * Handle matches of the struct syntax
55257dd7f8SAndreas Gohr     *
56257dd7f8SAndreas Gohr     * @param string $match The match of the syntax
57257dd7f8SAndreas Gohr     * @param int $state The state of the handler
58257dd7f8SAndreas Gohr     * @param int $pos The position in the document
59257dd7f8SAndreas Gohr     * @param Doku_Handler $handler The handler
60257dd7f8SAndreas Gohr     * @return array Data for the renderer
61257dd7f8SAndreas Gohr     */
62257dd7f8SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler) {
6382c064c1SAndreas Gohr        // this is never called
6482c064c1SAndreas Gohr        return array();
65257dd7f8SAndreas Gohr    }
66257dd7f8SAndreas Gohr
67257dd7f8SAndreas Gohr    /**
6882c064c1SAndreas Gohr     * Render schema data
69257dd7f8SAndreas Gohr     *
7082c064c1SAndreas Gohr     * Currently completely renderer agnostic
7182c064c1SAndreas Gohr     *
7282c064c1SAndreas Gohr     * @todo we currently have no schema headlines
7382c064c1SAndreas Gohr     *
7482c064c1SAndreas Gohr     * @param string $mode Renderer mode
75257dd7f8SAndreas Gohr     * @param Doku_Renderer $R The renderer
76257dd7f8SAndreas Gohr     * @param array $data The data from the handler() function
77257dd7f8SAndreas Gohr     * @return bool If rendering was successful.
78257dd7f8SAndreas Gohr     */
79257dd7f8SAndreas Gohr    public function render($mode, Doku_Renderer $R, $data) {
80257dd7f8SAndreas Gohr        global $ID;
8182c064c1SAndreas Gohr        global $INFO;
82257dd7f8SAndreas Gohr        global $REV;
8382c064c1SAndreas Gohr        if($ID != $INFO['id']) return true;
842f1a213bSAndreas Gohr        if(!$INFO['exists']) return true;
8587050b53SMichael Grosse        if($this->hasBeenRendered) return true;
8687050b53SMichael Grosse
8787050b53SMichael Grosse        // do not render the output twice on the same page, e.g. when another page has been included
8887050b53SMichael Grosse        $this->hasBeenRendered = true;
89257dd7f8SAndreas Gohr
90257dd7f8SAndreas Gohr        $assignments = new Assignments();
91257dd7f8SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
92257dd7f8SAndreas Gohr        if(!$tables) return true;
93257dd7f8SAndreas Gohr
945a1eab78SAndreas Gohr        if($mode == 'xhtml') $R->doc .= '<div id="plugin__struct_output">';
955a1eab78SAndreas Gohr
96257dd7f8SAndreas Gohr        foreach($tables as $table) {
97257dd7f8SAndreas Gohr            $schemadata = new SchemaData($table, $ID, $REV);
98*0dd23cefSAndreas Gohr            $schemadata->optionSkipEmpty(true);
99*0dd23cefSAndreas Gohr            $data = $schemadata->getData();
100da30fdd3SAndreas Gohr            if(!count($data)) continue;
101257dd7f8SAndreas Gohr
102da30fdd3SAndreas Gohr            $R->table_open();
103da30fdd3SAndreas Gohr
104da30fdd3SAndreas Gohr            $R->tablethead_open();
105da30fdd3SAndreas Gohr            $R->tablerow_open();
106da30fdd3SAndreas Gohr            $R->tableheader_open(2);
107da30fdd3SAndreas Gohr            $R->cdata($table);
108da30fdd3SAndreas Gohr            $R->tableheader_close();
109da30fdd3SAndreas Gohr            $R->tablerow_close();
110da30fdd3SAndreas Gohr            $R->tablethead_open();
111da30fdd3SAndreas Gohr
112da30fdd3SAndreas Gohr            $R->tabletbody_open();
113257dd7f8SAndreas Gohr            foreach($data as $field) {
114257dd7f8SAndreas Gohr                $R->tablerow_open();
115257dd7f8SAndreas Gohr                $R->tableheader_open();
1169e9bee91SAndreas Gohr                $R->cdata($field->getColumn()->getTranslatedLabel());
117257dd7f8SAndreas Gohr                $R->tableheader_close();
118257dd7f8SAndreas Gohr                $R->tablecell_open();
119257dd7f8SAndreas Gohr                $field->render($R, $mode);
120257dd7f8SAndreas Gohr                $R->tablecell_close();
121257dd7f8SAndreas Gohr                $R->tablerow_close();
122257dd7f8SAndreas Gohr            }
123257dd7f8SAndreas Gohr            $R->tabletbody_close();
124257dd7f8SAndreas Gohr            $R->table_close();
125da30fdd3SAndreas Gohr        }
126257dd7f8SAndreas Gohr
1275a1eab78SAndreas Gohr        if($mode == 'xhtml') $R->doc .= '</div>';
1285a1eab78SAndreas Gohr
129257dd7f8SAndreas Gohr        return true;
130257dd7f8SAndreas Gohr    }
131257dd7f8SAndreas Gohr}
132257dd7f8SAndreas Gohr
133257dd7f8SAndreas Gohr// vim:ts=4:sw=4:et:
134