1*c64c1748SAndreas Gohr<?php 2*c64c1748SAndreas Gohr 3*c64c1748SAndreas Gohr/** 4*c64c1748SAndreas Gohr * DokuWiki Plugin dbquery (Syntax Component) 5*c64c1748SAndreas Gohr * 6*c64c1748SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7*c64c1748SAndreas Gohr * @author Andreas Gohr <dokuwiki@cosmocode.de> 8*c64c1748SAndreas Gohr */ 9*c64c1748SAndreas Gohrclass syntax_plugin_dbquery_query extends DokuWiki_Syntax_Plugin 10*c64c1748SAndreas Gohr{ 11*c64c1748SAndreas Gohr /** @inheritDoc */ 12*c64c1748SAndreas Gohr public function getType() 13*c64c1748SAndreas Gohr { 14*c64c1748SAndreas Gohr return 'substition'; 15*c64c1748SAndreas Gohr } 16*c64c1748SAndreas Gohr 17*c64c1748SAndreas Gohr /** @inheritDoc */ 18*c64c1748SAndreas Gohr public function getPType() 19*c64c1748SAndreas Gohr { 20*c64c1748SAndreas Gohr return 'block'; 21*c64c1748SAndreas Gohr } 22*c64c1748SAndreas Gohr 23*c64c1748SAndreas Gohr /** @inheritDoc */ 24*c64c1748SAndreas Gohr public function getSort() 25*c64c1748SAndreas Gohr { 26*c64c1748SAndreas Gohr return 135; 27*c64c1748SAndreas Gohr } 28*c64c1748SAndreas Gohr 29*c64c1748SAndreas Gohr /** @inheritDoc */ 30*c64c1748SAndreas Gohr public function connectTo($mode) 31*c64c1748SAndreas Gohr { 32*c64c1748SAndreas Gohr $this->Lexer->addSpecialPattern('{{QUERY:\w+}}', $mode, 'plugin_dbquery_query'); 33*c64c1748SAndreas Gohr } 34*c64c1748SAndreas Gohr 35*c64c1748SAndreas Gohr /** @inheritDoc */ 36*c64c1748SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) 37*c64c1748SAndreas Gohr { 38*c64c1748SAndreas Gohr return ['name' => substr($match, 8, -2)]; 39*c64c1748SAndreas Gohr } 40*c64c1748SAndreas Gohr 41*c64c1748SAndreas Gohr /** @inheritDoc */ 42*c64c1748SAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) 43*c64c1748SAndreas Gohr { 44*c64c1748SAndreas Gohr if ($mode !== 'xhtml') { 45*c64c1748SAndreas Gohr return false; 46*c64c1748SAndreas Gohr } 47*c64c1748SAndreas Gohr 48*c64c1748SAndreas Gohr /** @var helper_plugin_dbquery $hlp */ 49*c64c1748SAndreas Gohr $hlp = plugin_load('helper', 'dbquery'); 50*c64c1748SAndreas Gohr try { 51*c64c1748SAndreas Gohr $qdata = $hlp->loadDataFromPage($data['name']); 52*c64c1748SAndreas Gohr $result = $hlp->executeQuery($qdata['codeblocks']['_']); 53*c64c1748SAndreas Gohr } catch (\Exception $e) { 54*c64c1748SAndreas Gohr msg(hsc($e->getMessage()), -1); 55*c64c1748SAndreas Gohr return true; 56*c64c1748SAndreas Gohr } 57*c64c1748SAndreas Gohr 58*c64c1748SAndreas Gohr if (count($result) === 1 && isset($result[0]['status']) && isset($qdata['codeblocks'][$result[0]['status']])) { 59*c64c1748SAndreas Gohr $this->renderStatus($result, $qdata['codeblocks'][$result[0]['status']], $renderer); 60*c64c1748SAndreas Gohr } else { 61*c64c1748SAndreas Gohr if ($qdata['macros']['transpose']) { 62*c64c1748SAndreas Gohr $this->renderTransposedResultTable($result, $renderer); 63*c64c1748SAndreas Gohr } else { 64*c64c1748SAndreas Gohr $this->renderResultTable($result, $renderer); 65*c64c1748SAndreas Gohr } 66*c64c1748SAndreas Gohr } 67*c64c1748SAndreas Gohr 68*c64c1748SAndreas Gohr return true; 69*c64c1748SAndreas Gohr } 70*c64c1748SAndreas Gohr 71*c64c1748SAndreas Gohr /** 72*c64c1748SAndreas Gohr * Render given result via the given status HTML 73*c64c1748SAndreas Gohr * 74*c64c1748SAndreas Gohr * @param string[][] $result 75*c64c1748SAndreas Gohr * @param string $html 76*c64c1748SAndreas Gohr * @param Doku_Renderer $R 77*c64c1748SAndreas Gohr */ 78*c64c1748SAndreas Gohr public function renderStatus($result, $html, Doku_Renderer $R) 79*c64c1748SAndreas Gohr { 80*c64c1748SAndreas Gohr $value = isset($result[0]['result']) ? $result[0]['result'] : ''; 81*c64c1748SAndreas Gohr $html = str_replace(':result', hsc($value), $html); 82*c64c1748SAndreas Gohr $R->doc .= $html; 83*c64c1748SAndreas Gohr } 84*c64c1748SAndreas Gohr 85*c64c1748SAndreas Gohr /** 86*c64c1748SAndreas Gohr * Render the given result as a table 87*c64c1748SAndreas Gohr * 88*c64c1748SAndreas Gohr * @param string[][] $result 89*c64c1748SAndreas Gohr * @param Doku_Renderer $R 90*c64c1748SAndreas Gohr */ 91*c64c1748SAndreas Gohr public function renderResultTable($result, Doku_Renderer $R) 92*c64c1748SAndreas Gohr { 93*c64c1748SAndreas Gohr global $lang; 94*c64c1748SAndreas Gohr 95*c64c1748SAndreas Gohr if (!count($result)) { 96*c64c1748SAndreas Gohr $R->cdata($lang['nothingfound']); 97*c64c1748SAndreas Gohr return; 98*c64c1748SAndreas Gohr } 99*c64c1748SAndreas Gohr 100*c64c1748SAndreas Gohr $R->table_open(); 101*c64c1748SAndreas Gohr $R->tablerow_open(); 102*c64c1748SAndreas Gohr foreach (array_keys($result[0]) as $header) { 103*c64c1748SAndreas Gohr $R->tableheader_open(); 104*c64c1748SAndreas Gohr $R->cdata($header); 105*c64c1748SAndreas Gohr $R->tableheader_close(); 106*c64c1748SAndreas Gohr } 107*c64c1748SAndreas Gohr $R->tablerow_close(); 108*c64c1748SAndreas Gohr 109*c64c1748SAndreas Gohr foreach ($result as $row) { 110*c64c1748SAndreas Gohr $R->tablerow_open(); 111*c64c1748SAndreas Gohr foreach ($row as $cell) { 112*c64c1748SAndreas Gohr $R->tablecell_open(); 113*c64c1748SAndreas Gohr $R->cdata($cell); 114*c64c1748SAndreas Gohr $R->tablecell_close(); 115*c64c1748SAndreas Gohr } 116*c64c1748SAndreas Gohr $R->tablerow_close(); 117*c64c1748SAndreas Gohr } 118*c64c1748SAndreas Gohr $R->table_close(); 119*c64c1748SAndreas Gohr } 120*c64c1748SAndreas Gohr 121*c64c1748SAndreas Gohr /** 122*c64c1748SAndreas Gohr * Render the given result as a table, but turned 90 degrees 123*c64c1748SAndreas Gohr * 124*c64c1748SAndreas Gohr * @param string[][] $result 125*c64c1748SAndreas Gohr * @param Doku_Renderer $R 126*c64c1748SAndreas Gohr */ 127*c64c1748SAndreas Gohr public function renderTransposedResultTable($result, Doku_Renderer $R) 128*c64c1748SAndreas Gohr { 129*c64c1748SAndreas Gohr global $lang; 130*c64c1748SAndreas Gohr 131*c64c1748SAndreas Gohr if (!count($result)) { 132*c64c1748SAndreas Gohr $R->cdata($lang['nothingfound']); 133*c64c1748SAndreas Gohr return; 134*c64c1748SAndreas Gohr } 135*c64c1748SAndreas Gohr 136*c64c1748SAndreas Gohr $width = count($result[0]); 137*c64c1748SAndreas Gohr $height = count($result); 138*c64c1748SAndreas Gohr 139*c64c1748SAndreas Gohr $R->table_open(); 140*c64c1748SAndreas Gohr for ($x = 0; $x < $width; $x++) { 141*c64c1748SAndreas Gohr $R->tablerow_open(); 142*c64c1748SAndreas Gohr $R->tableheader_open(); 143*c64c1748SAndreas Gohr $R->cdata(array_keys($result[0])[$x]); 144*c64c1748SAndreas Gohr $R->tableheader_close(); 145*c64c1748SAndreas Gohr 146*c64c1748SAndreas Gohr for ($y = 0; $y < $height; $y++) { 147*c64c1748SAndreas Gohr $R->tablecell_open(); 148*c64c1748SAndreas Gohr $R->cdata(array_values($result[$y])[$x]); 149*c64c1748SAndreas Gohr $R->tablecell_close(); 150*c64c1748SAndreas Gohr } 151*c64c1748SAndreas Gohr $R->tablerow_close(); 152*c64c1748SAndreas Gohr } 153*c64c1748SAndreas Gohr $R->table_close(); 154*c64c1748SAndreas Gohr } 155*c64c1748SAndreas Gohr 156*c64c1748SAndreas Gohr} 157*c64c1748SAndreas Gohr 158