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 protected $hasBeenRendered = false; 17 18 protected const XHTML_OPEN = '<div id="plugin__struct_output">'; 19 protected const XHTML_CLOSE = '</div>'; 20 21 /** 22 * Regexp to check on which actions the struct data may be rendered 23 */ 24 protected const WHITELIST_ACTIONS = '/^(show|export_.*)$/'; 25 26 /** 27 * @return string Syntax mode type 28 */ 29 public function getType() 30 { 31 return 'substition'; 32 } 33 34 /** 35 * @return string Paragraph type 36 */ 37 public function getPType() 38 { 39 return 'block'; 40 } 41 42 /** 43 * @return int Sort order - Low numbers go before high numbers 44 */ 45 public function getSort() 46 { 47 return 155; 48 } 49 50 /** 51 * Connect lookup pattern to lexer. 52 * 53 * We do not connect any pattern here, because the call to this plugin is not 54 * triggered from syntax but our action component 55 * 56 * @asee action_plugin_struct_output 57 * @param string $mode Parser mode 58 */ 59 public function connectTo($mode) 60 { 61 } 62 63 /** 64 * Handle matches of the struct syntax 65 * 66 * @param string $match The match of the syntax 67 * @param int $state The state of the handler 68 * @param int $pos The position in the document 69 * @param Doku_Handler $handler The handler 70 * @return array Data for the renderer 71 */ 72 public function handle($match, $state, $pos, Doku_Handler $handler) 73 { 74 // this is never called 75 return array(); 76 } 77 78 /** 79 * Render schema data 80 * 81 * Currently completely renderer agnostic 82 * 83 * @param string $format Renderer format 84 * @param Doku_Renderer $renderer The renderer 85 * @param array $data The data from the handler() function 86 * @return bool If rendering was successful. 87 */ 88 public function render($format, Doku_Renderer $renderer, $data) 89 { 90 global $ACT; 91 global $ID; 92 global $INFO; 93 global $REV; 94 95 foreach (helper_plugin_struct::BLACKLIST_RENDERER as $blacklisted) { 96 if ($renderer instanceof $blacklisted) { 97 return true; 98 } 99 } 100 if ($ID != $INFO['id']) return true; 101 if (!$INFO['exists']) return true; 102 if ($this->hasBeenRendered) return true; 103 if (!preg_match(self::WHITELIST_ACTIONS, act_clean($ACT))) return true; 104 105 // do not render the output twice on the same page, e.g. when another page has been included 106 $this->hasBeenRendered = true; 107 try { 108 $assignments = Assignments::getInstance(); 109 } catch (StructException $e) { 110 return false; 111 } 112 $tables = $assignments->getPageAssignments($ID); 113 if (!$tables) return true; 114 115 if ($format == 'xhtml') $renderer->doc .= self::XHTML_OPEN; 116 117 $hasdata = false; 118 foreach ($tables as $table) { 119 try { 120 $schemadata = AccessTable::getPageAccess($table, $ID, (int)$REV); 121 } catch (StructException $ignored) { 122 continue; // no such schema at this revision 123 } 124 125 $rendercontext = array( 126 'renderer' => $renderer, 127 'format' => $format, 128 'meta' => p_get_metadata($ID), 129 'schemadata' => $schemadata, 130 'hasdata' => &$hasdata 131 ); 132 133 $event = new \Doku_Event( 134 'PLUGIN_STRUCT_RENDER_SCHEMA_DATA', 135 $rendercontext 136 ); 137 $event->trigger([$this, 'renderSchemaData']); 138 } 139 140 if ($format == 'xhtml') $renderer->doc .= self::XHTML_CLOSE; 141 142 // if no data has been output, remove empty wrapper again 143 if ($format == 'xhtml' && !$hasdata) { 144 $renderer->doc = substr($renderer->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE)); 145 } 146 147 return true; 148 } 149 150 /** 151 * Default schema data rendering (simple table view) 152 * 153 * @param array The render context including renderer and data 154 */ 155 public function renderSchemaData($rendercontext) 156 { 157 $schemadata = $rendercontext['schemadata']; 158 $renderer = $rendercontext['renderer']; 159 $format = $rendercontext['format']; 160 161 $schemadata->optionSkipEmpty(true); 162 $data = $schemadata->getData(); 163 if (!count($data)) 164 return; 165 166 $rendercontext['hasdata'] = true; 167 168 if ($format == 'xhtml') { 169 $renderer->doc .= '<div class="struct_output_' . $schemadata->getSchema()->getTable() . '">'; 170 } 171 172 $renderer->table_open(); 173 $renderer->tablethead_open(); 174 $renderer->tablerow_open(); 175 $renderer->tableheader_open(2); 176 $renderer->cdata($schemadata->getSchema()->getTranslatedLabel()); 177 $renderer->tableheader_close(); 178 $renderer->tablerow_close(); 179 $renderer->tablethead_close(); 180 181 $renderer->tabletbody_open(); 182 foreach ($data as $field) { 183 $renderer->tablerow_open(); 184 $renderer->tableheader_open(); 185 $renderer->cdata($field->getColumn()->getTranslatedLabel()); 186 $renderer->tableheader_close(); 187 $renderer->tablecell_open(); 188 if ($format == 'xhtml') { 189 $renderer->doc = substr($renderer->doc, 0, -1) . 190 ' data-struct="' . hsc($field->getColumn()->getFullQualifiedLabel()) . 191 '">'; 192 } 193 $field->render($renderer, $format); 194 $renderer->tablecell_close(); 195 $renderer->tablerow_close(); 196 } 197 $renderer->tabletbody_close(); 198 $renderer->table_close(); 199 200 if ($format == 'xhtml') { 201 $renderer->doc .= '</div>'; 202 } 203 } 204} 205 206// vim:ts=4:sw=4:et: 207