xref: /plugin/structsection/syntax.php (revision 6b380169b8d3e302861db4a27bdd55306f1cc68d)
1*6b380169SMichael Große<?php
2*6b380169SMichael Große/**
3*6b380169SMichael Große * DokuWiki Plugin structwiki (Syntax Component)
4*6b380169SMichael Große *
5*6b380169SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*6b380169SMichael Große * @author  Michael Große <mic.grosse@googlemail.com>
7*6b380169SMichael Große */
8*6b380169SMichael Große
9*6b380169SMichael Große// must be run within Dokuwiki
10*6b380169SMichael Großeuse dokuwiki\plugin\struct\meta\AccessTable;
11*6b380169SMichael Großeuse dokuwiki\plugin\struct\meta\Assignments;
12*6b380169SMichael Großeuse dokuwiki\plugin\struct\meta\StructException;
13*6b380169SMichael Große
14*6b380169SMichael Großeif(!defined('DOKU_INC')) die();
15*6b380169SMichael Große
16*6b380169SMichael Großeclass syntax_plugin_structwiki extends DokuWiki_Syntax_Plugin {
17*6b380169SMichael Große
18*6b380169SMichael Große    protected $hasBeenRendered = false;
19*6b380169SMichael Große
20*6b380169SMichael Große    const XHTML_OPEN = '<div id="plugin__structwiki_output">';
21*6b380169SMichael Große    const XHTML_CLOSE = '</div>';
22*6b380169SMichael Große
23*6b380169SMichael Große    /**
24*6b380169SMichael Große     * @return string Syntax mode type
25*6b380169SMichael Große     */
26*6b380169SMichael Große    public function getType() {
27*6b380169SMichael Große        return 'substition';
28*6b380169SMichael Große    }
29*6b380169SMichael Große
30*6b380169SMichael Große    /**
31*6b380169SMichael Große     * @return string Paragraph type
32*6b380169SMichael Große     */
33*6b380169SMichael Große    public function getPType() {
34*6b380169SMichael Große        return 'block';
35*6b380169SMichael Große    }
36*6b380169SMichael Große
37*6b380169SMichael Große    /**
38*6b380169SMichael Große     * @return int Sort order - Low numbers go before high numbers
39*6b380169SMichael Große     */
40*6b380169SMichael Große    public function getSort() {
41*6b380169SMichael Große        return 155;
42*6b380169SMichael Große    }
43*6b380169SMichael Große
44*6b380169SMichael Große    /**
45*6b380169SMichael Große     * Connect lookup pattern to lexer.
46*6b380169SMichael Große     *
47*6b380169SMichael Große     * We do not connect any pattern here, because the call to this plugin is not
48*6b380169SMichael Große     * triggered from syntax but our action component
49*6b380169SMichael Große     *
50*6b380169SMichael Große     * @asee action_plugin_structwiki
51*6b380169SMichael Große     * @param string $mode Parser mode
52*6b380169SMichael Große     */
53*6b380169SMichael Große    public function connectTo($mode) {
54*6b380169SMichael Große
55*6b380169SMichael Große    }
56*6b380169SMichael Große
57*6b380169SMichael Große    /**
58*6b380169SMichael Große     * Handle matches of the struct syntax
59*6b380169SMichael Große     *
60*6b380169SMichael Große     * @param string $match The match of the syntax
61*6b380169SMichael Große     * @param int $state The state of the handler
62*6b380169SMichael Große     * @param int $pos The position in the document
63*6b380169SMichael Große     * @param Doku_Handler $handler The handler
64*6b380169SMichael Große     * @return array Data for the renderer
65*6b380169SMichael Große     */
66*6b380169SMichael Große    public function handle($match, $state, $pos, Doku_Handler $handler) {
67*6b380169SMichael Große        // this is never called
68*6b380169SMichael Große        return array();
69*6b380169SMichael Große    }
70*6b380169SMichael Große
71*6b380169SMichael Große    /**
72*6b380169SMichael Große     * Render schema data
73*6b380169SMichael Große     *
74*6b380169SMichael Große     * Currently completely renderer agnostic
75*6b380169SMichael Große     *
76*6b380169SMichael Große     * @param string $mode Renderer mode
77*6b380169SMichael Große     * @param Doku_Renderer $R The renderer
78*6b380169SMichael Große     * @param array $handlerData The data from the handler() function
79*6b380169SMichael Große     * @return bool If rendering was successful.
80*6b380169SMichael Große     */
81*6b380169SMichael Große    public function render($mode, Doku_Renderer $R, $handlerData) {
82*6b380169SMichael Große        global $ID;
83*6b380169SMichael Große        global $INFO;
84*6b380169SMichael Große        global $REV;
85*6b380169SMichael Große        if($ID != $INFO['id']) return true;
86*6b380169SMichael Große        if(!$INFO['exists']) return true;
87*6b380169SMichael Große        if($this->hasBeenRendered) return true;
88*6b380169SMichael Große
89*6b380169SMichael Große        // do not render the output twice on the same page, e.g. when another page has been included
90*6b380169SMichael Große        $this->hasBeenRendered = true;
91*6b380169SMichael Große        try {
92*6b380169SMichael Große            $assignments = Assignments::getInstance();
93*6b380169SMichael Große        } catch (StructException $e) {
94*6b380169SMichael Große            return false;
95*6b380169SMichael Große        }
96*6b380169SMichael Große        $tables = $assignments->getPageAssignments($ID);
97*6b380169SMichael Große        if(!$tables) return true;
98*6b380169SMichael Große
99*6b380169SMichael Große        $pos = $handlerData['pos'];
100*6b380169SMichael Große        if($mode == 'xhtml') {
101*6b380169SMichael Große            $R->finishSectionEdit($pos-1);
102*6b380169SMichael Große            $R->doc .= self::XHTML_OPEN;
103*6b380169SMichael Große        }
104*6b380169SMichael Große
105*6b380169SMichael Große
106*6b380169SMichael Große        $hasdata = false;
107*6b380169SMichael Große        foreach($tables as $table) {
108*6b380169SMichael Große            try {
109*6b380169SMichael Große                $schemadata = AccessTable::byTableName($table, $ID, $REV);
110*6b380169SMichael Große            } catch(StructException $ignored) {
111*6b380169SMichael Große                continue; // no such schema at this revision
112*6b380169SMichael Große            }
113*6b380169SMichael Große            $schemadata->optionSkipEmpty(false);
114*6b380169SMichael Große            $data = $schemadata->getData();
115*6b380169SMichael Große            if(!count($data)) continue;
116*6b380169SMichael Große            $hasdata = true;
117*6b380169SMichael Große
118*6b380169SMichael Große            foreach($data as $field) {
119*6b380169SMichael Große                if(!is_a($field->getColumn()->getType(), \dokuwiki\plugin\structwiki\types\Section::class)) {
120*6b380169SMichael Große                    continue;
121*6b380169SMichael Große                }
122*6b380169SMichael Große                $lvl = 2;
123*6b380169SMichael Große                $R->header($field->getColumn()->getTranslatedLabel(), $lvl, $pos);
124*6b380169SMichael Große                $pos += strlen($field->getColumn()->getTranslatedLabel());
125*6b380169SMichael Große                $R->section_open($lvl);
126*6b380169SMichael Große                if($mode === 'xhtml') {
127*6b380169SMichael Große                    $R->doc = substr($R->doc, 0, -2) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">'."\n";
128*6b380169SMichael Große                }
129*6b380169SMichael Große                $field->render($R, $mode);
130*6b380169SMichael Große                $R->section_close();
131*6b380169SMichael Große            }
132*6b380169SMichael Große        }
133*6b380169SMichael Große
134*6b380169SMichael Große        if($mode == 'xhtml') {
135*6b380169SMichael Große            $R->finishSectionEdit($pos);
136*6b380169SMichael Große            $R->doc .= self::XHTML_CLOSE;
137*6b380169SMichael Große        }
138*6b380169SMichael Große
139*6b380169SMichael Große        // if no data has been output, remove empty wrapper again
140*6b380169SMichael Große        if($mode == 'xhtml' && !$hasdata) {
141*6b380169SMichael Große            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
142*6b380169SMichael Große        }
143*6b380169SMichael Große
144*6b380169SMichael Große        return true;
145*6b380169SMichael Große    }
146*6b380169SMichael Große}
147*6b380169SMichael Große
148*6b380169SMichael Große// vim:ts=4:sw=4:et:
149