1<?php 2/** 3 * DokuWiki Plugin struct (Syntax Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 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_struct_output extends DokuWiki_Syntax_Plugin { 17 18 protected $hasBeenRendered = false; 19 20 const XHTML_OPEN = '<div id="plugin__struct_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_struct_output 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 $data The data from the handler() function 79 * @return bool If rendering was successful. 80 */ 81 public function render($mode, Doku_Renderer $R, $data) { 82 global $ACT; 83 global $ID; 84 global $INFO; 85 global $REV; 86 if (act_clean($ACT) !== 'show') { 87 return true; 88 } 89 if($ID != $INFO['id']) return true; 90 if(!$INFO['exists']) return true; 91 if($this->hasBeenRendered) return true; 92 93 // do not render the output twice on the same page, e.g. when another page has been included 94 $this->hasBeenRendered = true; 95 try { 96 $assignments = Assignments::getInstance(); 97 } catch (StructException $e) { 98 return false; 99 } 100 $tables = $assignments->getPageAssignments($ID); 101 if(!$tables) return true; 102 103 if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN; 104 105 $hasdata = false; 106 foreach($tables as $table) { 107 try { 108 $schemadata = AccessTable::byTableName($table, $ID, $REV); 109 } catch(StructException $ignored) { 110 continue; // no such schema at this revision 111 } 112 $schemadata->optionSkipEmpty(true); 113 $data = $schemadata->getData(); 114 if(!count($data)) continue; 115 $hasdata = true; 116 117 $R->table_open(); 118 119 $R->tablethead_open(); 120 $R->tablerow_open(); 121 $R->tableheader_open(2); 122 $R->cdata($table); 123 $R->tableheader_close(); 124 $R->tablerow_close(); 125 $R->tablethead_open(); 126 127 $R->tabletbody_open(); 128 foreach($data as $field) { 129 $R->tablerow_open(); 130 $R->tableheader_open(); 131 $R->cdata($field->getColumn()->getTranslatedLabel()); 132 $R->tableheader_close(); 133 $R->tablecell_open(); 134 if($mode == 'xhtml') { 135 $R->doc = substr($R->doc, 0, -1) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">'; 136 } 137 $field->render($R, $mode); 138 $R->tablecell_close(); 139 $R->tablerow_close(); 140 } 141 $R->tabletbody_close(); 142 $R->table_close(); 143 } 144 145 if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE; 146 147 // if no data has been output, remove empty wrapper again 148 if($mode == 'xhtml' && !$hasdata) { 149 $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE)); 150 } 151 152 return true; 153 } 154} 155 156// vim:ts=4:sw=4:et: 157