xref: /plugin/struct/syntax/output.php (revision ab7b42a5d812b91e6ebdc1617781a4b267009ba4)
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    /**
24bdefb930SAnna Dabrowska     * Class names of renderers which should NOT render struct data.
25bdefb930SAnna Dabrowska     * All descendants are also blacklisted.
26bdefb930SAnna Dabrowska     */
27bdefb930SAnna Dabrowska    const BLACKLIST_RENDERER = array('Doku_Renderer_metadata');
28bdefb930SAnna Dabrowska
29bdefb930SAnna Dabrowska    /**
30257dd7f8SAndreas Gohr     * @return string Syntax mode type
31257dd7f8SAndreas Gohr     */
32257dd7f8SAndreas Gohr    public function getType() {
33257dd7f8SAndreas Gohr        return 'substition';
34257dd7f8SAndreas Gohr    }
35da30fdd3SAndreas Gohr
36257dd7f8SAndreas Gohr    /**
37257dd7f8SAndreas Gohr     * @return string Paragraph type
38257dd7f8SAndreas Gohr     */
39257dd7f8SAndreas Gohr    public function getPType() {
40257dd7f8SAndreas Gohr        return 'block';
41257dd7f8SAndreas Gohr    }
42da30fdd3SAndreas Gohr
43257dd7f8SAndreas Gohr    /**
44257dd7f8SAndreas Gohr     * @return int Sort order - Low numbers go before high numbers
45257dd7f8SAndreas Gohr     */
46257dd7f8SAndreas Gohr    public function getSort() {
47257dd7f8SAndreas Gohr        return 155;
48257dd7f8SAndreas Gohr    }
49257dd7f8SAndreas Gohr
50257dd7f8SAndreas Gohr    /**
51257dd7f8SAndreas Gohr     * Connect lookup pattern to lexer.
52257dd7f8SAndreas Gohr     *
5382c064c1SAndreas Gohr     * We do not connect any pattern here, because the call to this plugin is not
5482c064c1SAndreas Gohr     * triggered from syntax but our action component
5582c064c1SAndreas Gohr     *
5682c064c1SAndreas Gohr     * @asee action_plugin_struct_output
57257dd7f8SAndreas Gohr     * @param string $mode Parser mode
58257dd7f8SAndreas Gohr     */
59257dd7f8SAndreas Gohr    public function connectTo($mode) {
6082c064c1SAndreas Gohr
61257dd7f8SAndreas Gohr    }
62257dd7f8SAndreas Gohr
63257dd7f8SAndreas Gohr    /**
64257dd7f8SAndreas Gohr     * Handle matches of the struct syntax
65257dd7f8SAndreas Gohr     *
66257dd7f8SAndreas Gohr     * @param string $match The match of the syntax
67257dd7f8SAndreas Gohr     * @param int $state The state of the handler
68257dd7f8SAndreas Gohr     * @param int $pos The position in the document
69257dd7f8SAndreas Gohr     * @param Doku_Handler $handler The handler
70257dd7f8SAndreas Gohr     * @return array Data for the renderer
71257dd7f8SAndreas Gohr     */
72257dd7f8SAndreas Gohr    public function handle($match, $state, $pos, Doku_Handler $handler) {
7382c064c1SAndreas Gohr        // this is never called
7482c064c1SAndreas Gohr        return array();
75257dd7f8SAndreas Gohr    }
76257dd7f8SAndreas Gohr
77257dd7f8SAndreas Gohr    /**
7882c064c1SAndreas Gohr     * Render schema data
79257dd7f8SAndreas Gohr     *
80564e138bSAnna Dabrowska     * Currently completely renderer agnostic
8182c064c1SAndreas Gohr     *
8282c064c1SAndreas Gohr     * @param string $mode Renderer mode
83257dd7f8SAndreas Gohr     * @param Doku_Renderer $R The renderer
84257dd7f8SAndreas Gohr     * @param array $data The data from the handler() function
85257dd7f8SAndreas Gohr     * @return bool If rendering was successful.
86257dd7f8SAndreas Gohr     */
87257dd7f8SAndreas Gohr    public function render($mode, Doku_Renderer $R, $data) {
880e4a3e7cSMichael Große        global $ACT;
89257dd7f8SAndreas Gohr        global $ID;
9082c064c1SAndreas Gohr        global $INFO;
91257dd7f8SAndreas Gohr        global $REV;
92bdefb930SAnna Dabrowska
93bdefb930SAnna Dabrowska        foreach (self::BLACKLIST_RENDERER as $blacklisted) {
94bdefb930SAnna Dabrowska            if ($R instanceof $blacklisted) {
950e4a3e7cSMichael Große                return true;
960e4a3e7cSMichael Große            }
97bdefb930SAnna Dabrowska        }
9882c064c1SAndreas Gohr        if($ID != $INFO['id']) return true;
992f1a213bSAndreas Gohr        if(!$INFO['exists']) return true;
10087050b53SMichael Grosse        if($this->hasBeenRendered) return true;
10187050b53SMichael Grosse
10287050b53SMichael Grosse        // do not render the output twice on the same page, e.g. when another page has been included
10387050b53SMichael Grosse        $this->hasBeenRendered = true;
1047cbcfbdbSAndreas Gohr        try {
105025cb9daSAndreas Gohr            $assignments = Assignments::getInstance();
1067cbcfbdbSAndreas Gohr        } catch (StructException $e) {
1077cbcfbdbSAndreas Gohr            return false;
1087cbcfbdbSAndreas Gohr        }
109257dd7f8SAndreas Gohr        $tables = $assignments->getPageAssignments($ID);
110257dd7f8SAndreas Gohr        if(!$tables) return true;
111257dd7f8SAndreas Gohr
11267b5dd6eSAnna Dabrowska        if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN;
1135a1eab78SAndreas Gohr
1147938ca48SAndreas Gohr        $hasdata = false;
115257dd7f8SAndreas Gohr        foreach($tables as $table) {
116a28d6152SAndreas Gohr            try {
117f411d872SAndreas Gohr                $schemadata = AccessTable::byTableName($table, $ID, $REV);
118a28d6152SAndreas Gohr            } catch(StructException $ignored) {
119a28d6152SAndreas Gohr                continue; // no such schema at this revision
120a28d6152SAndreas Gohr            }
1210dd23cefSAndreas Gohr            $schemadata->optionSkipEmpty(true);
1220dd23cefSAndreas Gohr            $data = $schemadata->getData();
123da30fdd3SAndreas Gohr            if(!count($data)) continue;
1247938ca48SAndreas Gohr            $hasdata = true;
125257dd7f8SAndreas Gohr
126da30fdd3SAndreas Gohr            $R->table_open();
127da30fdd3SAndreas Gohr
128da30fdd3SAndreas Gohr            $R->tablethead_open();
129da30fdd3SAndreas Gohr            $R->tablerow_open();
130da30fdd3SAndreas Gohr            $R->tableheader_open(2);
131127d6bacSMichael Große            $R->cdata($schemadata->getSchema()->getTranslatedLabel());
132da30fdd3SAndreas Gohr            $R->tableheader_close();
133da30fdd3SAndreas Gohr            $R->tablerow_close();
134*ab7b42a5SAnika Henke            $R->tablethead_close();
135da30fdd3SAndreas Gohr
136da30fdd3SAndreas Gohr            $R->tabletbody_open();
137257dd7f8SAndreas Gohr            foreach($data as $field) {
138257dd7f8SAndreas Gohr                $R->tablerow_open();
139257dd7f8SAndreas Gohr                $R->tableheader_open();
1409e9bee91SAndreas Gohr                $R->cdata($field->getColumn()->getTranslatedLabel());
141257dd7f8SAndreas Gohr                $R->tableheader_close();
142257dd7f8SAndreas Gohr                $R->tablecell_open();
14325852712SAndreas Gohr                if($mode == 'xhtml') {
14425852712SAndreas Gohr                    $R->doc = substr($R->doc, 0, -1) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">';
14525852712SAndreas Gohr                }
146257dd7f8SAndreas Gohr                $field->render($R, $mode);
147257dd7f8SAndreas Gohr                $R->tablecell_close();
148257dd7f8SAndreas Gohr                $R->tablerow_close();
149257dd7f8SAndreas Gohr            }
150257dd7f8SAndreas Gohr            $R->tabletbody_close();
151257dd7f8SAndreas Gohr            $R->table_close();
152da30fdd3SAndreas Gohr        }
153257dd7f8SAndreas Gohr
15467b5dd6eSAnna Dabrowska        if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE;
1557938ca48SAndreas Gohr
1567938ca48SAndreas Gohr        // if no data has been output, remove empty wrapper again
15767b5dd6eSAnna Dabrowska        if($mode == 'xhtml' && !$hasdata) {
1587938ca48SAndreas Gohr            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
1597938ca48SAndreas Gohr        }
1605a1eab78SAndreas Gohr
161257dd7f8SAndreas Gohr        return true;
162257dd7f8SAndreas Gohr    }
163257dd7f8SAndreas Gohr}
164257dd7f8SAndreas Gohr
165257dd7f8SAndreas Gohr// vim:ts=4:sw=4:et:
166