xref: /plugin/structsection/syntax.php (revision adc87508697492918d4a7b7ee1db4e7939294965)
16b380169SMichael Große<?php
26a5d40b1SMichael Große
36b380169SMichael Große/**
4284f9fc3SMichael Große * DokuWiki Plugin structsection (Syntax Component)
56b380169SMichael Große *
66b380169SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
76b380169SMichael Große * @author  Michael Große <mic.grosse@googlemail.com>
86b380169SMichael Große */
96b380169SMichael Große
106b380169SMichael Großeuse dokuwiki\plugin\struct\meta\AccessTable;
116b380169SMichael Großeuse dokuwiki\plugin\struct\meta\Assignments;
126b380169SMichael Großeuse dokuwiki\plugin\struct\meta\StructException;
136b380169SMichael Große
14eadb8fe2SMichael Großeclass syntax_plugin_structsection extends \DokuWiki_Syntax_Plugin
156a5d40b1SMichael Große{
166b380169SMichael Große    protected $hasBeenRendered = false;
176b380169SMichael Große
18*17c7556eSMichael Große    private const XHTML_OPEN = '<div id="plugin__structsection_output">';
19*17c7556eSMichael Große    private const XHTML_CLOSE = '</div>';
206b380169SMichael Große
216b380169SMichael Große    /**
226b380169SMichael Große     * @return string Syntax mode type
236b380169SMichael Große     */
246a5d40b1SMichael Große    public function getType()
256a5d40b1SMichael Große    {
266b380169SMichael Große        return 'substition';
276b380169SMichael Große    }
286b380169SMichael Große
296b380169SMichael Große    /**
306b380169SMichael Große     * @return string Paragraph type
316b380169SMichael Große     */
326a5d40b1SMichael Große    public function getPType()
336a5d40b1SMichael Große    {
346b380169SMichael Große        return 'block';
356b380169SMichael Große    }
366b380169SMichael Große
376b380169SMichael Große    /**
386b380169SMichael Große     * @return int Sort order - Low numbers go before high numbers
396b380169SMichael Große     */
406a5d40b1SMichael Große    public function getSort()
416a5d40b1SMichael Große    {
426b380169SMichael Große        return 155;
436b380169SMichael Große    }
446b380169SMichael Große
456b380169SMichael Große    /**
466b380169SMichael Große     * Connect lookup pattern to lexer.
476b380169SMichael Große     *
486b380169SMichael Große     * We do not connect any pattern here, because the call to this plugin is not
496b380169SMichael Große     * triggered from syntax but our action component
506b380169SMichael Große     *
51284f9fc3SMichael Große     * @asee action_plugin_structsection
526b380169SMichael Große     * @param string $mode Parser mode
536b380169SMichael Große     */
546a5d40b1SMichael Große    public function connectTo($mode)
556a5d40b1SMichael Große    {
566b380169SMichael Große    }
576b380169SMichael Große
586b380169SMichael Große    /**
596b380169SMichael Große     * Handle matches of the struct syntax
606b380169SMichael Große     *
616b380169SMichael Große     * @param string $match The match of the syntax
626b380169SMichael Große     * @param int $state The state of the handler
636b380169SMichael Große     * @param int $pos The position in the document
64eadb8fe2SMichael Große     * @param \Doku_Handler $handler The handler
656b380169SMichael Große     * @return array Data for the renderer
666b380169SMichael Große     */
67eadb8fe2SMichael Große    public function handle($match, $state, $pos, \Doku_Handler $handler)
686a5d40b1SMichael Große    {
696b380169SMichael Große        // this is never called
706b380169SMichael Große        return array();
716b380169SMichael Große    }
726b380169SMichael Große
736b380169SMichael Große    /**
746b380169SMichael Große     * Render schema data
756b380169SMichael Große     *
766b380169SMichael Große     * Currently completely renderer agnostic
776b380169SMichael Große     *
786b380169SMichael Große     * @param string $mode Renderer mode
79eadb8fe2SMichael Große     * @param \Doku_Renderer $R The renderer
806b380169SMichael Große     * @param array $handlerData The data from the handler() function
816b380169SMichael Große     * @return bool If rendering was successful.
826b380169SMichael Große     */
83eadb8fe2SMichael Große    public function render($mode, \Doku_Renderer $R, $handlerData)
846a5d40b1SMichael Große    {
856b380169SMichael Große        global $ID;
866b380169SMichael Große        global $INFO;
876b380169SMichael Große        global $REV;
886a5d40b1SMichael Große        if ($ID != $INFO['id']) {
896a5d40b1SMichael Große            return true;
906a5d40b1SMichael Große        }
916a5d40b1SMichael Große        if (!$INFO['exists']) {
926a5d40b1SMichael Große            return true;
936a5d40b1SMichael Große        }
946a5d40b1SMichael Große        if ($this->hasBeenRendered) {
956a5d40b1SMichael Große            return true;
966a5d40b1SMichael Große        }
976b380169SMichael Große
986b380169SMichael Große        // do not render the output twice on the same page, e.g. when another page has been included
996b380169SMichael Große        $this->hasBeenRendered = true;
1006b380169SMichael Große        try {
1016b380169SMichael Große            $assignments = Assignments::getInstance();
1026b380169SMichael Große        } catch (StructException $e) {
1036b380169SMichael Große            return false;
1046b380169SMichael Große        }
1056b380169SMichael Große        $tables = $assignments->getPageAssignments($ID);
1066a5d40b1SMichael Große        if (!$tables) {
1076a5d40b1SMichael Große            return true;
1086a5d40b1SMichael Große        }
1096b380169SMichael Große
1106b380169SMichael Große        $pos = $handlerData['pos'];
1116b380169SMichael Große        if ($mode == 'xhtml') {
1126b380169SMichael Große            $R->finishSectionEdit($pos - 1);
1136b380169SMichael Große            $R->doc .= self::XHTML_OPEN;
1146b380169SMichael Große        }
1156b380169SMichael Große
1166b380169SMichael Große
1176b380169SMichael Große        $hasdata = false;
1186b380169SMichael Große        foreach ($tables as $table) {
1196b380169SMichael Große            try {
120cb058b5aSMichael Große                $schemadata = AccessTable::getPageAccess($table, $ID, $REV);
1216b380169SMichael Große            } catch (StructException $ignored) {
1226b380169SMichael Große                continue; // no such schema at this revision
1236b380169SMichael Große            }
1246b380169SMichael Große            $schemadata->optionSkipEmpty(false);
1256b380169SMichael Große            $data = $schemadata->getData();
1266a5d40b1SMichael Große            if (!count($data)) {
1276a5d40b1SMichael Große                continue;
1286a5d40b1SMichael Große            }
1296b380169SMichael Große            $hasdata = true;
1306b380169SMichael Große
1316b380169SMichael Große            foreach ($data as $field) {
132284f9fc3SMichael Große                if (!is_a($field->getColumn()->getType(), \dokuwiki\plugin\structsection\types\Section::class)) {
1336b380169SMichael Große                    continue;
1346b380169SMichael Große                }
1356b380169SMichael Große                $lvl = 2;
1366b380169SMichael Große                $R->header($field->getColumn()->getTranslatedLabel(), $lvl, $pos);
1376b380169SMichael Große                $pos += strlen($field->getColumn()->getTranslatedLabel());
1386b380169SMichael Große                $R->section_open($lvl);
1396b380169SMichael Große                if ($mode === 'xhtml') {
14027fdceffSMichael Große                    $structDataAttribute = 'data-struct="' . hsc($field->getColumn()->getFullQualifiedLabel()) . '"';
14127fdceffSMichael Große                    $R->doc = substr($R->doc, 0, -2) . ' ' . $structDataAttribute . '>' . "\n";
1426b380169SMichael Große                }
1436b380169SMichael Große                $field->render($R, $mode);
1446b380169SMichael Große                $R->section_close();
1456b380169SMichael Große            }
1466b380169SMichael Große        }
1476b380169SMichael Große
1486b380169SMichael Große        if ($mode == 'xhtml') {
1496b380169SMichael Große            $R->finishSectionEdit($pos);
1506b380169SMichael Große            $R->doc .= self::XHTML_CLOSE;
1516b380169SMichael Große        }
1526b380169SMichael Große
1536b380169SMichael Große        // if no data has been output, remove empty wrapper again
1546b380169SMichael Große        if ($mode == 'xhtml' && !$hasdata) {
1556b380169SMichael Große            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
1566b380169SMichael Große        }
1576b380169SMichael Große
1586b380169SMichael Große        return true;
1596b380169SMichael Große    }
1606b380169SMichael Große}
1616b380169SMichael Große
1626b380169SMichael Große// vim:ts=4:sw=4:et:
163