1<?php 2 3/** 4 * DokuWiki Plugin struct (Syntax Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 8 */ 9 10use dokuwiki\plugin\struct\meta\AccessTable; 11use dokuwiki\plugin\struct\meta\Assignments; 12use dokuwiki\plugin\struct\meta\StructException; 13 14class syntax_plugin_struct_output extends DokuWiki_Syntax_Plugin 15{ 16 17 protected $hasBeenRendered = false; 18 19 const XHTML_OPEN = '<div id="plugin__struct_output">'; 20 const XHTML_CLOSE = '</div>'; 21 22 /** 23 * Regexp to check on which actions the struct data may be rendered 24 */ 25 const WHITELIST_ACTIONS = '/^(show|export_.*)$/'; 26 27 /** 28 * @return string Syntax mode type 29 */ 30 public function getType() 31 { 32 return 'substition'; 33 } 34 35 /** 36 * @return string Paragraph type 37 */ 38 public function getPType() 39 { 40 return 'block'; 41 } 42 43 /** 44 * @return int Sort order - Low numbers go before high numbers 45 */ 46 public function getSort() 47 { 48 return 155; 49 } 50 51 /** 52 * Connect lookup pattern to lexer. 53 * 54 * We do not connect any pattern here, because the call to this plugin is not 55 * triggered from syntax but our action component 56 * 57 * @asee action_plugin_struct_output 58 * @param string $mode Parser mode 59 */ 60 public function connectTo($mode) 61 { 62 } 63 64 /** 65 * Handle matches of the struct syntax 66 * 67 * @param string $match The match of the syntax 68 * @param int $state The state of the handler 69 * @param int $pos The position in the document 70 * @param Doku_Handler $handler The handler 71 * @return array Data for the renderer 72 */ 73 public function handle($match, $state, $pos, Doku_Handler $handler) 74 { 75 // this is never called 76 return array(); 77 } 78 79 /** 80 * Render schema data 81 * 82 * Currently completely renderer agnostic 83 * 84 * @param string $format Renderer format 85 * @param Doku_Renderer $renderer The renderer 86 * @param array $data The data from the handler() function 87 * @return bool If rendering was successful. 88 */ 89 public function render($format, Doku_Renderer $renderer, $data) 90 { 91 global $ACT; 92 global $ID; 93 global $INFO; 94 global $REV; 95 96 foreach (helper_plugin_struct::BLACKLIST_RENDERER as $blacklisted) { 97 if ($renderer instanceof $blacklisted) { 98 return true; 99 } 100 } 101 if ($ID != $INFO['id']) return true; 102 if (!$INFO['exists']) return true; 103 if ($this->hasBeenRendered) return true; 104 if (!preg_match(self::WHITELIST_ACTIONS, act_clean($ACT))) return true; 105 106 // do not render the output twice on the same page, e.g. when another page has been included 107 $this->hasBeenRendered = true; 108 try { 109 $assignments = Assignments::getInstance(); 110 } catch (StructException $e) { 111 return false; 112 } 113 $tables = $assignments->getPageAssignments($ID); 114 if (!$tables) return true; 115 116 if ($format == 'xhtml') $renderer->doc .= self::XHTML_OPEN; 117 118 $hasdata = false; 119 foreach ($tables as $table) { 120 try { 121 $schemadata = AccessTable::getPageAccess($table, $ID, (int)$REV); 122 } catch (StructException $ignored) { 123 continue; // no such schema at this revision 124 } 125 $schemadata->optionSkipEmpty(true); 126 $data = $schemadata->getData(); 127 if (!count($data)) continue; 128 $hasdata = true; 129 130 $renderer->table_open(); 131 132 $renderer->tablethead_open(); 133 $renderer->tablerow_open(); 134 $renderer->tableheader_open(2); 135 $renderer->cdata($schemadata->getSchema()->getTranslatedLabel()); 136 $renderer->tableheader_close(); 137 $renderer->tablerow_close(); 138 $renderer->tablethead_close(); 139 140 $renderer->tabletbody_open(); 141 foreach ($data as $field) { 142 $renderer->tablerow_open(); 143 $renderer->tableheader_open(); 144 $renderer->cdata($field->getColumn()->getTranslatedLabel()); 145 $renderer->tableheader_close(); 146 $renderer->tablecell_open(); 147 if ($format == 'xhtml') { 148 $renderer->doc = substr($renderer->doc, 0, -1) . 149 ' data-struct="' . hsc($field->getColumn()->getFullQualifiedLabel()) . 150 '">'; 151 } 152 $field->render($renderer, $format); 153 $renderer->tablecell_close(); 154 $renderer->tablerow_close(); 155 } 156 $renderer->tabletbody_close(); 157 $renderer->table_close(); 158 } 159 160 if ($format == 'xhtml') $renderer->doc .= self::XHTML_CLOSE; 161 162 // if no data has been output, remove empty wrapper again 163 if ($format == 'xhtml' && !$hasdata) { 164 $renderer->doc = substr($renderer->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE)); 165 } 166 167 return true; 168 } 169} 170 171// vim:ts=4:sw=4:et: 172