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