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