xref: /plugin/structsection/syntax.php (revision 284f9fc3fe87c819811b9293950be7ba46307b04)
16b380169SMichael Große<?php
26b380169SMichael Große/**
3*284f9fc3SMichael Große * DokuWiki Plugin structsection (Syntax Component)
46b380169SMichael Große *
56b380169SMichael Große * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
66b380169SMichael Große * @author  Michael Große <mic.grosse@googlemail.com>
76b380169SMichael Große */
86b380169SMichael Große
96b380169SMichael Große// must be run within Dokuwiki
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
146b380169SMichael Großeif(!defined('DOKU_INC')) die();
156b380169SMichael Große
16*284f9fc3SMichael Großeclass syntax_plugin_structsection extends DokuWiki_Syntax_Plugin {
176b380169SMichael Große
186b380169SMichael Große    protected $hasBeenRendered = false;
196b380169SMichael Große
20*284f9fc3SMichael Große    const XHTML_OPEN = '<div id="plugin__structsection_output">';
216b380169SMichael Große    const XHTML_CLOSE = '</div>';
226b380169SMichael Große
236b380169SMichael Große    /**
246b380169SMichael Große     * @return string Syntax mode type
256b380169SMichael Große     */
266b380169SMichael Große    public function getType() {
276b380169SMichael Große        return 'substition';
286b380169SMichael Große    }
296b380169SMichael Große
306b380169SMichael Große    /**
316b380169SMichael Große     * @return string Paragraph type
326b380169SMichael Große     */
336b380169SMichael Große    public function getPType() {
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     */
406b380169SMichael Große    public function getSort() {
416b380169SMichael Große        return 155;
426b380169SMichael Große    }
436b380169SMichael Große
446b380169SMichael Große    /**
456b380169SMichael Große     * Connect lookup pattern to lexer.
466b380169SMichael Große     *
476b380169SMichael Große     * We do not connect any pattern here, because the call to this plugin is not
486b380169SMichael Große     * triggered from syntax but our action component
496b380169SMichael Große     *
50*284f9fc3SMichael Große     * @asee action_plugin_structsection
516b380169SMichael Große     * @param string $mode Parser mode
526b380169SMichael Große     */
536b380169SMichael Große    public function connectTo($mode) {
546b380169SMichael Große
556b380169SMichael Große    }
566b380169SMichael Große
576b380169SMichael Große    /**
586b380169SMichael Große     * Handle matches of the struct syntax
596b380169SMichael Große     *
606b380169SMichael Große     * @param string $match The match of the syntax
616b380169SMichael Große     * @param int $state The state of the handler
626b380169SMichael Große     * @param int $pos The position in the document
636b380169SMichael Große     * @param Doku_Handler $handler The handler
646b380169SMichael Große     * @return array Data for the renderer
656b380169SMichael Große     */
666b380169SMichael Große    public function handle($match, $state, $pos, Doku_Handler $handler) {
676b380169SMichael Große        // this is never called
686b380169SMichael Große        return array();
696b380169SMichael Große    }
706b380169SMichael Große
716b380169SMichael Große    /**
726b380169SMichael Große     * Render schema data
736b380169SMichael Große     *
746b380169SMichael Große     * Currently completely renderer agnostic
756b380169SMichael Große     *
766b380169SMichael Große     * @param string $mode Renderer mode
776b380169SMichael Große     * @param Doku_Renderer $R The renderer
786b380169SMichael Große     * @param array $handlerData The data from the handler() function
796b380169SMichael Große     * @return bool If rendering was successful.
806b380169SMichael Große     */
816b380169SMichael Große    public function render($mode, Doku_Renderer $R, $handlerData) {
826b380169SMichael Große        global $ID;
836b380169SMichael Große        global $INFO;
846b380169SMichael Große        global $REV;
856b380169SMichael Große        if($ID != $INFO['id']) return true;
866b380169SMichael Große        if(!$INFO['exists']) return true;
876b380169SMichael Große        if($this->hasBeenRendered) return true;
886b380169SMichael Große
896b380169SMichael Große        // do not render the output twice on the same page, e.g. when another page has been included
906b380169SMichael Große        $this->hasBeenRendered = true;
916b380169SMichael Große        try {
926b380169SMichael Große            $assignments = Assignments::getInstance();
936b380169SMichael Große        } catch (StructException $e) {
946b380169SMichael Große            return false;
956b380169SMichael Große        }
966b380169SMichael Große        $tables = $assignments->getPageAssignments($ID);
976b380169SMichael Große        if(!$tables) return true;
986b380169SMichael Große
996b380169SMichael Große        $pos = $handlerData['pos'];
1006b380169SMichael Große        if($mode == 'xhtml') {
1016b380169SMichael Große            $R->finishSectionEdit($pos-1);
1026b380169SMichael Große            $R->doc .= self::XHTML_OPEN;
1036b380169SMichael Große        }
1046b380169SMichael Große
1056b380169SMichael Große
1066b380169SMichael Große        $hasdata = false;
1076b380169SMichael Große        foreach($tables as $table) {
1086b380169SMichael Große            try {
1096b380169SMichael Große                $schemadata = AccessTable::byTableName($table, $ID, $REV);
1106b380169SMichael Große            } catch(StructException $ignored) {
1116b380169SMichael Große                continue; // no such schema at this revision
1126b380169SMichael Große            }
1136b380169SMichael Große            $schemadata->optionSkipEmpty(false);
1146b380169SMichael Große            $data = $schemadata->getData();
1156b380169SMichael Große            if(!count($data)) continue;
1166b380169SMichael Große            $hasdata = true;
1176b380169SMichael Große
1186b380169SMichael Große            foreach($data as $field) {
119*284f9fc3SMichael Große                if(!is_a($field->getColumn()->getType(), \dokuwiki\plugin\structsection\types\Section::class)) {
1206b380169SMichael Große                    continue;
1216b380169SMichael Große                }
1226b380169SMichael Große                $lvl = 2;
1236b380169SMichael Große                $R->header($field->getColumn()->getTranslatedLabel(), $lvl, $pos);
1246b380169SMichael Große                $pos += strlen($field->getColumn()->getTranslatedLabel());
1256b380169SMichael Große                $R->section_open($lvl);
1266b380169SMichael Große                if($mode === 'xhtml') {
1276b380169SMichael Große                    $R->doc = substr($R->doc, 0, -2) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">'."\n";
1286b380169SMichael Große                }
1296b380169SMichael Große                $field->render($R, $mode);
1306b380169SMichael Große                $R->section_close();
1316b380169SMichael Große            }
1326b380169SMichael Große        }
1336b380169SMichael Große
1346b380169SMichael Große        if($mode == 'xhtml') {
1356b380169SMichael Große            $R->finishSectionEdit($pos);
1366b380169SMichael Große            $R->doc .= self::XHTML_CLOSE;
1376b380169SMichael Große        }
1386b380169SMichael Große
1396b380169SMichael Große        // if no data has been output, remove empty wrapper again
1406b380169SMichael Große        if($mode == 'xhtml' && !$hasdata) {
1416b380169SMichael Große            $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE));
1426b380169SMichael Große        }
1436b380169SMichael Große
1446b380169SMichael Große        return true;
1456b380169SMichael Große    }
1466b380169SMichael Große}
1476b380169SMichael Große
1486b380169SMichael Große// vim:ts=4:sw=4:et:
149