1eafc109fSAndreas Gohr<?php 2eafc109fSAndreas Gohr 3eafc109fSAndreas Gohr/** 4b22abfe9SAndreas Gohr * CSV export of tabular data 5eafc109fSAndreas Gohr * 6b22abfe9SAndreas Gohr * @link https://tools.ietf.org/html/rfc4180 7b22abfe9SAndreas Gohr * @link http://csvlint.io/ 8eafc109fSAndreas Gohr */ 9eafc109fSAndreas Gohrclass renderer_plugin_struct_csv extends Doku_Renderer { 10eafc109fSAndreas Gohr 11eafc109fSAndreas Gohr protected $first = false; 12eafc109fSAndreas Gohr 13eafc109fSAndreas Gohr /** 14eafc109fSAndreas Gohr * Determine if out put is wanted right now 15eafc109fSAndreas Gohr * 16eafc109fSAndreas Gohr * @return bool 17eafc109fSAndreas Gohr */ 18eafc109fSAndreas Gohr function _doOutput() { 19eafc109fSAndreas Gohr global $INPUT; 20eafc109fSAndreas Gohr 21eafc109fSAndreas Gohr if( 22eafc109fSAndreas Gohr !isset($this->info['struct_table_hash']) or 23eafc109fSAndreas Gohr $this->info['struct_table_hash'] != $INPUT->str('hash') 24eafc109fSAndreas Gohr ) { 25eafc109fSAndreas Gohr return false; 26eafc109fSAndreas Gohr } 27eafc109fSAndreas Gohr 28eafc109fSAndreas Gohr if(!empty($this->info['struct_table_meta'])) { 29eafc109fSAndreas Gohr return false; 30eafc109fSAndreas Gohr } 31eafc109fSAndreas Gohr 32eafc109fSAndreas Gohr return true; 33eafc109fSAndreas Gohr } 34eafc109fSAndreas Gohr 35eafc109fSAndreas Gohr /** 36eafc109fSAndreas Gohr * Our own format 37eafc109fSAndreas Gohr * 38eafc109fSAndreas Gohr * @return string 39eafc109fSAndreas Gohr */ 40eafc109fSAndreas Gohr function getFormat() { 41eafc109fSAndreas Gohr return 'struct_csv'; 42eafc109fSAndreas Gohr } 43eafc109fSAndreas Gohr 44eafc109fSAndreas Gohr /** 45eafc109fSAndreas Gohr * Set proper headers 46eafc109fSAndreas Gohr */ 47eafc109fSAndreas Gohr function document_start() { 48eafc109fSAndreas Gohr global $ID; 49b22abfe9SAndreas Gohr $filename = noNS($ID) . '.csv'; 50eafc109fSAndreas Gohr $headers = array( 51b22abfe9SAndreas Gohr 'Content-Type' => 'text/csv', 52eafc109fSAndreas Gohr 'Content-Disposition' => 'attachment; filename="' . $filename . '";' 53eafc109fSAndreas Gohr ); 541ba236caSAndreas Gohr p_set_metadata($ID, array('format' => array('struct_csv' => $headers))); 55eafc109fSAndreas Gohr // don't cache 56eafc109fSAndreas Gohr $this->nocache(); 57eafc109fSAndreas Gohr } 58eafc109fSAndreas Gohr 59eafc109fSAndreas Gohr /** 60eafc109fSAndreas Gohr * Opening a table row prevents the separator for the first following cell 61eafc109fSAndreas Gohr */ 62eafc109fSAndreas Gohr function tablerow_open() { 63eafc109fSAndreas Gohr if(!$this->_doOutput()) return; 64eafc109fSAndreas Gohr $this->first = true; 65eafc109fSAndreas Gohr } 66eafc109fSAndreas Gohr 67eafc109fSAndreas Gohr /** 68b22abfe9SAndreas Gohr * Output the delimiter (unless it's the first cell of this row) and the text wrapper 69eafc109fSAndreas Gohr * 70eafc109fSAndreas Gohr * @param int $colspan ignored 71eafc109fSAndreas Gohr * @param null $align ignored 72eafc109fSAndreas Gohr * @param int $rowspan ignored 73eafc109fSAndreas Gohr */ 74eafc109fSAndreas Gohr function tablecell_open($colspan = 1, $align = null, $rowspan = 1) { 75eafc109fSAndreas Gohr if(!$this->_doOutput()) return; 76eafc109fSAndreas Gohr if(!$this->first) { 77b22abfe9SAndreas Gohr $this->doc .= ","; 78eafc109fSAndreas Gohr } 79eafc109fSAndreas Gohr $this->first = false; 80b22abfe9SAndreas Gohr 81b22abfe9SAndreas Gohr $this->doc .= '"'; 82b22abfe9SAndreas Gohr } 83b22abfe9SAndreas Gohr 84b22abfe9SAndreas Gohr /** 85b22abfe9SAndreas Gohr * Close the text wrapper 86b22abfe9SAndreas Gohr */ 87b22abfe9SAndreas Gohr function tablecell_close() { 883889beb2SAndreas Gohr if(!$this->_doOutput()) return; 89b22abfe9SAndreas Gohr $this->doc .= '"'; 90eafc109fSAndreas Gohr } 91eafc109fSAndreas Gohr 92eafc109fSAndreas Gohr /** 93eafc109fSAndreas Gohr * Alias for tablecell_open 94eafc109fSAndreas Gohr * 95eafc109fSAndreas Gohr * @param int $colspan ignored 96eafc109fSAndreas Gohr * @param null $align ignored 97eafc109fSAndreas Gohr * @param int $rowspan ignored 98eafc109fSAndreas Gohr */ 99eafc109fSAndreas Gohr function tableheader_open($colspan = 1, $align = null, $rowspan = 1) { 100eafc109fSAndreas Gohr $this->tablecell_open($colspan, $align, $rowspan); 101eafc109fSAndreas Gohr } 102eafc109fSAndreas Gohr 103eafc109fSAndreas Gohr /** 104b22abfe9SAndreas Gohr * Alias for tablecell_close 105b22abfe9SAndreas Gohr */ 106b22abfe9SAndreas Gohr function tableheader_close() { 107b22abfe9SAndreas Gohr $this->tablecell_close(); 108b22abfe9SAndreas Gohr } 109b22abfe9SAndreas Gohr 110b22abfe9SAndreas Gohr /** 111b22abfe9SAndreas Gohr * Add CRLF newline at the end of one line 112eafc109fSAndreas Gohr */ 113eafc109fSAndreas Gohr function tablerow_close() { 114eafc109fSAndreas Gohr if(!$this->_doOutput()) return; 115b22abfe9SAndreas Gohr $this->doc .= "\r\n"; 116eafc109fSAndreas Gohr } 117eafc109fSAndreas Gohr 118eafc109fSAndreas Gohr /** 119eafc109fSAndreas Gohr * Outputs cell content 120eafc109fSAndreas Gohr * 121eafc109fSAndreas Gohr * @param string $text 122eafc109fSAndreas Gohr */ 123eafc109fSAndreas Gohr function cdata($text) { 124eafc109fSAndreas Gohr if(!$this->_doOutput()) return; 125b22abfe9SAndreas Gohr if($text === '') return; 126b22abfe9SAndreas Gohr 127b22abfe9SAndreas Gohr $this->doc .= str_replace('"', '""', $text); 128eafc109fSAndreas Gohr } 129eafc109fSAndreas Gohr 130ba55430dSAndreas Gohr 131ba55430dSAndreas Gohr #region overrides using cdata for output 132ba55430dSAndreas Gohr 133eafc109fSAndreas Gohr function internallink($link, $title = null) { 134ba55430dSAndreas Gohr if(is_null($title) or is_array($title) or $title == '') { 135ba55430dSAndreas Gohr $title = $this->_simpleTitle($link); 136ba55430dSAndreas Gohr } 137eafc109fSAndreas Gohr $this->cdata($title); 138eafc109fSAndreas Gohr } 139eafc109fSAndreas Gohr 140ba55430dSAndreas Gohr function externallink($link, $title = null) { 141ba55430dSAndreas Gohr if(is_null($title) or is_array($title) or $title == '') { 142ba55430dSAndreas Gohr $title = $link; 143ba55430dSAndreas Gohr } 144ba55430dSAndreas Gohr $this->cdata($title); 145ba55430dSAndreas Gohr } 146ba55430dSAndreas Gohr 14749fe301bSAndreas Gohr function emaillink($address, $name = null) { 14849fe301bSAndreas Gohr $this->cdata($address); 14949fe301bSAndreas Gohr } 150ba55430dSAndreas Gohr 151ba55430dSAndreas Gohr function plugin($name, $args, $state = '', $match = '') { 152*470e44beSAndreas Gohr if(substr($name,0, 7) == 'struct_') { 153*470e44beSAndreas Gohr parent::plugin($name, $args, $state, $match); 154*470e44beSAndreas Gohr } else { 155ba55430dSAndreas Gohr $this->cdata($match); 156ba55430dSAndreas Gohr } 157*470e44beSAndreas Gohr } 158ba55430dSAndreas Gohr 159ba55430dSAndreas Gohr function acronym($acronym) { 160ba55430dSAndreas Gohr $this->cdata($acronym); 161ba55430dSAndreas Gohr } 162ba55430dSAndreas Gohr 163ba55430dSAndreas Gohr function code($text, $lang = null, $file = null) { 164ba55430dSAndreas Gohr $this->cdata($text); 165ba55430dSAndreas Gohr } 166ba55430dSAndreas Gohr 167ba55430dSAndreas Gohr function header($text, $level, $pos) { 168ba55430dSAndreas Gohr $this->cdata($text); 169ba55430dSAndreas Gohr } 170ba55430dSAndreas Gohr 171ba55430dSAndreas Gohr function linebreak() { 172ba55430dSAndreas Gohr $this->cdata("\r\n"); 173ba55430dSAndreas Gohr } 174ba55430dSAndreas Gohr 175ba55430dSAndreas Gohr function unformatted($text) { 176ba55430dSAndreas Gohr $this->cdata($text); 177ba55430dSAndreas Gohr } 178ba55430dSAndreas Gohr 179ba55430dSAndreas Gohr function php($text) { 180ba55430dSAndreas Gohr $this->cdata($text); 181ba55430dSAndreas Gohr } 182ba55430dSAndreas Gohr 183ba55430dSAndreas Gohr function phpblock($text) { 184ba55430dSAndreas Gohr $this->cdata($text); 185ba55430dSAndreas Gohr } 186ba55430dSAndreas Gohr 187ba55430dSAndreas Gohr function html($text) { 188ba55430dSAndreas Gohr $this->cdata($text); 189ba55430dSAndreas Gohr } 190ba55430dSAndreas Gohr 191ba55430dSAndreas Gohr function htmlblock($text) { 192ba55430dSAndreas Gohr $this->cdata($text); 193ba55430dSAndreas Gohr } 194ba55430dSAndreas Gohr 195ba55430dSAndreas Gohr function preformatted($text) { 196ba55430dSAndreas Gohr $this->cdata($text); 197ba55430dSAndreas Gohr } 198ba55430dSAndreas Gohr 199ba55430dSAndreas Gohr function file($text, $lang = null, $file = null) { 200ba55430dSAndreas Gohr $this->cdata($text); 201ba55430dSAndreas Gohr } 202ba55430dSAndreas Gohr 203ba55430dSAndreas Gohr function smiley($smiley) { 204ba55430dSAndreas Gohr $this->cdata($smiley); 205ba55430dSAndreas Gohr } 206ba55430dSAndreas Gohr 207ba55430dSAndreas Gohr function entity($entity) { 208ba55430dSAndreas Gohr $this->cdata($entity); 209ba55430dSAndreas Gohr } 210ba55430dSAndreas Gohr 211ba55430dSAndreas Gohr function multiplyentity($x, $y) { 212ba55430dSAndreas Gohr $this->cdata($x . 'x' . $y); 213ba55430dSAndreas Gohr } 214ba55430dSAndreas Gohr 215ba55430dSAndreas Gohr function locallink($hash, $name = null) { 216ba55430dSAndreas Gohr if(is_null($name) or is_array($name) or $name == '') { 217ba55430dSAndreas Gohr $name = $hash; 218ba55430dSAndreas Gohr } 219ba55430dSAndreas Gohr $this->cdata($name); 220ba55430dSAndreas Gohr } 221ba55430dSAndreas Gohr 222ba55430dSAndreas Gohr function interwikilink($link, $title = null, $wikiName, $wikiUri) { 223ba55430dSAndreas Gohr if(is_null($title) or is_array($title) or $title == '') { 224ba55430dSAndreas Gohr $title = $wikiName . '>' . $link; 225ba55430dSAndreas Gohr } 226ba55430dSAndreas Gohr $this->cdata($title); 227ba55430dSAndreas Gohr } 228ba55430dSAndreas Gohr 229ba55430dSAndreas Gohr function filelink($link, $title = null) { 230ba55430dSAndreas Gohr if(is_null($title) or is_array($title) or $title == '') { 231ba55430dSAndreas Gohr $title = $link; 232ba55430dSAndreas Gohr } 233ba55430dSAndreas Gohr $this->cdata($title); 234ba55430dSAndreas Gohr } 235ba55430dSAndreas Gohr 236ba55430dSAndreas Gohr function windowssharelink($link, $title = null) { 237ba55430dSAndreas Gohr if(is_null($title) or is_array($title) or $title == '') { 238ba55430dSAndreas Gohr $title = $link; 239ba55430dSAndreas Gohr } 240ba55430dSAndreas Gohr $this->cdata($title); 241ba55430dSAndreas Gohr } 242ba55430dSAndreas Gohr 243ba55430dSAndreas Gohr function internalmedia($src, $title = null, $align = null, $width = null, 244ba55430dSAndreas Gohr $height = null, $cache = null, $linking = null) { 245ba55430dSAndreas Gohr $this->cdata($src); 246ba55430dSAndreas Gohr } 247ba55430dSAndreas Gohr 248ba55430dSAndreas Gohr function externalmedia($src, $title = null, $align = null, $width = null, 249ba55430dSAndreas Gohr $height = null, $cache = null, $linking = null) { 250ba55430dSAndreas Gohr $this->cdata($src); 251ba55430dSAndreas Gohr } 252ba55430dSAndreas Gohr 253ba55430dSAndreas Gohr function internalmedialink($src, $title = null, $align = null, 254ba55430dSAndreas Gohr $width = null, $height = null, $cache = null) { 255ba55430dSAndreas Gohr $this->cdata($src); 256ba55430dSAndreas Gohr } 257ba55430dSAndreas Gohr 258ba55430dSAndreas Gohr function externalmedialink($src, $title = null, $align = null, 259ba55430dSAndreas Gohr $width = null, $height = null, $cache = null) { 260ba55430dSAndreas Gohr $this->cdata($src); 261ba55430dSAndreas Gohr } 262ba55430dSAndreas Gohr 263ba55430dSAndreas Gohr #endregion 264eafc109fSAndreas Gohr} 265