1257dd7f8SAndreas Gohr<?php 2257dd7f8SAndreas Gohr/** 3257dd7f8SAndreas Gohr * DokuWiki Plugin struct (Syntax Component) 4257dd7f8SAndreas Gohr * 5257dd7f8SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6257dd7f8SAndreas Gohr * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 7257dd7f8SAndreas Gohr */ 8257dd7f8SAndreas Gohr 9257dd7f8SAndreas Gohr// must be run within Dokuwiki 10f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable; 11ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 127cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\StructException; 13257dd7f8SAndreas Gohr 14257dd7f8SAndreas Gohrif(!defined('DOKU_INC')) die(); 15257dd7f8SAndreas Gohr 1682c064c1SAndreas Gohrclass syntax_plugin_struct_output extends DokuWiki_Syntax_Plugin { 1787050b53SMichael Grosse 1887050b53SMichael Grosse protected $hasBeenRendered = false; 1987050b53SMichael Grosse 207938ca48SAndreas Gohr const XHTML_OPEN = '<div id="plugin__struct_output">'; 217938ca48SAndreas Gohr const XHTML_CLOSE = '</div>'; 227938ca48SAndreas Gohr 23257dd7f8SAndreas Gohr /** 24bdefb930SAnna Dabrowska * Class names of renderers which should NOT render struct data. 25bdefb930SAnna Dabrowska * All descendants are also blacklisted. 26bdefb930SAnna Dabrowska */ 27bdefb930SAnna Dabrowska const BLACKLIST_RENDERER = array('Doku_Renderer_metadata'); 28bdefb930SAnna Dabrowska 29bdefb930SAnna Dabrowska /** 30979b16f8SAndreas Gohr * Regexp to check on which actions the struct data may be rendered 31979b16f8SAndreas Gohr */ 32979b16f8SAndreas Gohr const WHITELIST_ACTIONS = '/^(show|export_.*)$/'; 33979b16f8SAndreas Gohr 34979b16f8SAndreas Gohr /** 35257dd7f8SAndreas Gohr * @return string Syntax mode type 36257dd7f8SAndreas Gohr */ 37257dd7f8SAndreas Gohr public function getType() { 38257dd7f8SAndreas Gohr return 'substition'; 39257dd7f8SAndreas Gohr } 40da30fdd3SAndreas Gohr 41257dd7f8SAndreas Gohr /** 42257dd7f8SAndreas Gohr * @return string Paragraph type 43257dd7f8SAndreas Gohr */ 44257dd7f8SAndreas Gohr public function getPType() { 45257dd7f8SAndreas Gohr return 'block'; 46257dd7f8SAndreas Gohr } 47da30fdd3SAndreas Gohr 48257dd7f8SAndreas Gohr /** 49257dd7f8SAndreas Gohr * @return int Sort order - Low numbers go before high numbers 50257dd7f8SAndreas Gohr */ 51257dd7f8SAndreas Gohr public function getSort() { 52257dd7f8SAndreas Gohr return 155; 53257dd7f8SAndreas Gohr } 54257dd7f8SAndreas Gohr 55257dd7f8SAndreas Gohr /** 56257dd7f8SAndreas Gohr * Connect lookup pattern to lexer. 57257dd7f8SAndreas Gohr * 5882c064c1SAndreas Gohr * We do not connect any pattern here, because the call to this plugin is not 5982c064c1SAndreas Gohr * triggered from syntax but our action component 6082c064c1SAndreas Gohr * 6182c064c1SAndreas Gohr * @asee action_plugin_struct_output 62257dd7f8SAndreas Gohr * @param string $mode Parser mode 63257dd7f8SAndreas Gohr */ 64257dd7f8SAndreas Gohr public function connectTo($mode) { 6582c064c1SAndreas Gohr 66257dd7f8SAndreas Gohr } 67257dd7f8SAndreas Gohr 68257dd7f8SAndreas Gohr /** 69257dd7f8SAndreas Gohr * Handle matches of the struct syntax 70257dd7f8SAndreas Gohr * 71257dd7f8SAndreas Gohr * @param string $match The match of the syntax 72257dd7f8SAndreas Gohr * @param int $state The state of the handler 73257dd7f8SAndreas Gohr * @param int $pos The position in the document 74257dd7f8SAndreas Gohr * @param Doku_Handler $handler The handler 75257dd7f8SAndreas Gohr * @return array Data for the renderer 76257dd7f8SAndreas Gohr */ 77257dd7f8SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) { 7882c064c1SAndreas Gohr // this is never called 7982c064c1SAndreas Gohr return array(); 80257dd7f8SAndreas Gohr } 81257dd7f8SAndreas Gohr 82257dd7f8SAndreas Gohr /** 8382c064c1SAndreas Gohr * Render schema data 84257dd7f8SAndreas Gohr * 85564e138bSAnna Dabrowska * Currently completely renderer agnostic 8682c064c1SAndreas Gohr * 8782c064c1SAndreas Gohr * @param string $mode Renderer mode 88257dd7f8SAndreas Gohr * @param Doku_Renderer $R The renderer 89257dd7f8SAndreas Gohr * @param array $data The data from the handler() function 90257dd7f8SAndreas Gohr * @return bool If rendering was successful. 91257dd7f8SAndreas Gohr */ 92257dd7f8SAndreas Gohr public function render($mode, Doku_Renderer $R, $data) { 930e4a3e7cSMichael Große global $ACT; 94257dd7f8SAndreas Gohr global $ID; 9582c064c1SAndreas Gohr global $INFO; 96257dd7f8SAndreas Gohr global $REV; 97bdefb930SAnna Dabrowska 98bdefb930SAnna Dabrowska foreach (self::BLACKLIST_RENDERER as $blacklisted) { 99bdefb930SAnna Dabrowska if ($R instanceof $blacklisted) { 1000e4a3e7cSMichael Große return true; 1010e4a3e7cSMichael Große } 102bdefb930SAnna Dabrowska } 10382c064c1SAndreas Gohr if($ID != $INFO['id']) return true; 1042f1a213bSAndreas Gohr if(!$INFO['exists']) return true; 10587050b53SMichael Grosse if($this->hasBeenRendered) return true; 106979b16f8SAndreas Gohr if(!preg_match(self::WHITELIST_ACTIONS, act_clean($ACT))) return true; 10787050b53SMichael Grosse 10887050b53SMichael Grosse // do not render the output twice on the same page, e.g. when another page has been included 10987050b53SMichael Grosse $this->hasBeenRendered = true; 1107cbcfbdbSAndreas Gohr try { 111025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 1127cbcfbdbSAndreas Gohr } catch (StructException $e) { 1137cbcfbdbSAndreas Gohr return false; 1147cbcfbdbSAndreas Gohr } 115257dd7f8SAndreas Gohr $tables = $assignments->getPageAssignments($ID); 116257dd7f8SAndreas Gohr if(!$tables) return true; 117257dd7f8SAndreas Gohr 11867b5dd6eSAnna Dabrowska if($mode == 'xhtml') $R->doc .= self::XHTML_OPEN; 1195a1eab78SAndreas Gohr 1207938ca48SAndreas Gohr $hasdata = false; 121257dd7f8SAndreas Gohr foreach($tables as $table) { 122a28d6152SAndreas Gohr try { 123*00bff81cSAnna Dabrowska // use the current revision if none is specified, otherwise we can't access page data 124*00bff81cSAnna Dabrowska $ts = $REV ?: $INFO['currentrev']; 125*00bff81cSAnna Dabrowska $schemadata = AccessTable::byTableName($table, $ID, $ts); 126a28d6152SAndreas Gohr } catch(StructException $ignored) { 127a28d6152SAndreas Gohr continue; // no such schema at this revision 128a28d6152SAndreas Gohr } 1290dd23cefSAndreas Gohr $schemadata->optionSkipEmpty(true); 1300dd23cefSAndreas Gohr $data = $schemadata->getData(); 131da30fdd3SAndreas Gohr if(!count($data)) continue; 1327938ca48SAndreas Gohr $hasdata = true; 133257dd7f8SAndreas Gohr 134da30fdd3SAndreas Gohr $R->table_open(); 135da30fdd3SAndreas Gohr 136da30fdd3SAndreas Gohr $R->tablethead_open(); 137da30fdd3SAndreas Gohr $R->tablerow_open(); 138da30fdd3SAndreas Gohr $R->tableheader_open(2); 139127d6bacSMichael Große $R->cdata($schemadata->getSchema()->getTranslatedLabel()); 140da30fdd3SAndreas Gohr $R->tableheader_close(); 141da30fdd3SAndreas Gohr $R->tablerow_close(); 142ab7b42a5SAnika Henke $R->tablethead_close(); 143da30fdd3SAndreas Gohr 144da30fdd3SAndreas Gohr $R->tabletbody_open(); 145257dd7f8SAndreas Gohr foreach($data as $field) { 146257dd7f8SAndreas Gohr $R->tablerow_open(); 147257dd7f8SAndreas Gohr $R->tableheader_open(); 1489e9bee91SAndreas Gohr $R->cdata($field->getColumn()->getTranslatedLabel()); 149257dd7f8SAndreas Gohr $R->tableheader_close(); 150257dd7f8SAndreas Gohr $R->tablecell_open(); 15125852712SAndreas Gohr if($mode == 'xhtml') { 15225852712SAndreas Gohr $R->doc = substr($R->doc, 0, -1) . ' data-struct="'.hsc($field->getColumn()->getFullQualifiedLabel()).'">'; 15325852712SAndreas Gohr } 154257dd7f8SAndreas Gohr $field->render($R, $mode); 155257dd7f8SAndreas Gohr $R->tablecell_close(); 156257dd7f8SAndreas Gohr $R->tablerow_close(); 157257dd7f8SAndreas Gohr } 158257dd7f8SAndreas Gohr $R->tabletbody_close(); 159257dd7f8SAndreas Gohr $R->table_close(); 160da30fdd3SAndreas Gohr } 161257dd7f8SAndreas Gohr 16267b5dd6eSAnna Dabrowska if($mode == 'xhtml') $R->doc .= self::XHTML_CLOSE; 1637938ca48SAndreas Gohr 1647938ca48SAndreas Gohr // if no data has been output, remove empty wrapper again 16567b5dd6eSAnna Dabrowska if($mode == 'xhtml' && !$hasdata) { 1667938ca48SAndreas Gohr $R->doc = substr($R->doc, 0, -1 * strlen(self::XHTML_OPEN . self::XHTML_CLOSE)); 1677938ca48SAndreas Gohr } 1685a1eab78SAndreas Gohr 169257dd7f8SAndreas Gohr return true; 170257dd7f8SAndreas Gohr } 171257dd7f8SAndreas Gohr} 172257dd7f8SAndreas Gohr 173257dd7f8SAndreas Gohr// vim:ts=4:sw=4:et: 174