1<?php 2 3/** 4 * CSV export of tabular data 5 * 6 * @link https://tools.ietf.org/html/rfc4180 7 * @link http://csvlint.io/ 8 */ 9class renderer_plugin_struct_csv extends Doku_Renderer { 10 11 protected $first = false; 12 13 /** 14 * Determine if out put is wanted right now 15 * 16 * @return bool 17 */ 18 function _doOutput() { 19 global $INPUT; 20 21 if( 22 !isset($this->info['struct_table_hash']) or 23 $this->info['struct_table_hash'] != $INPUT->str('hash') 24 ) { 25 return false; 26 } 27 28 if(!empty($this->info['struct_table_meta'])) { 29 return false; 30 } 31 32 return true; 33 } 34 35 /** 36 * Our own format 37 * 38 * @return string 39 */ 40 function getFormat() { 41 return 'struct_csv'; 42 } 43 44 /** 45 * Set proper headers 46 */ 47 function document_start() { 48 global $ID; 49 $filename = noNS($ID) . '.csv'; 50 $headers = array( 51 'Content-Type' => 'text/csv', 52 'Content-Disposition' => 'attachment; filename="' . $filename . '";' 53 ); 54 p_set_metadata($ID, array('format' => array('struct' => $headers))); 55 // don't cache 56 $this->nocache(); 57 } 58 59 /** 60 * Opening a table row prevents the separator for the first following cell 61 */ 62 function tablerow_open() { 63 if(!$this->_doOutput()) return; 64 $this->first = true; 65 } 66 67 /** 68 * Output the delimiter (unless it's the first cell of this row) and the text wrapper 69 * 70 * @param int $colspan ignored 71 * @param null $align ignored 72 * @param int $rowspan ignored 73 */ 74 function tablecell_open($colspan = 1, $align = null, $rowspan = 1) { 75 if(!$this->_doOutput()) return; 76 if(!$this->first) { 77 $this->doc .= ","; 78 } 79 $this->first = false; 80 81 $this->doc .= '"'; 82 } 83 84 /** 85 * Close the text wrapper 86 */ 87 function tablecell_close() { 88 if(!$this->_doOutput()) return; 89 $this->doc .= '"'; 90 } 91 92 /** 93 * Alias for tablecell_open 94 * 95 * @param int $colspan ignored 96 * @param null $align ignored 97 * @param int $rowspan ignored 98 */ 99 function tableheader_open($colspan = 1, $align = null, $rowspan = 1) { 100 $this->tablecell_open($colspan, $align, $rowspan); 101 } 102 103 /** 104 * Alias for tablecell_close 105 */ 106 function tableheader_close() { 107 $this->tablecell_close(); 108 } 109 110 /** 111 * Add CRLF newline at the end of one line 112 */ 113 function tablerow_close() { 114 if(!$this->_doOutput()) return; 115 $this->doc .= "\r\n"; 116 } 117 118 /** 119 * Outputs cell content 120 * 121 * @param string $text 122 */ 123 function cdata($text) { 124 if(!$this->_doOutput()) return; 125 if($text === '') return; 126 127 $this->doc .= str_replace('"', '""', $text); 128 } 129 130 /** 131 * Uses cdata to output the title 132 * 133 * @param string $link 134 * @param null $title 135 */ 136 function internallink($link, $title = null) { 137 $this->cdata($title); 138 } 139 140 /** 141 * Uses cdata to output the email address 142 * 143 * @param string $address 144 * @param null $name 145 */ 146 function emaillink($address, $name = null) { 147 $this->cdata($address); 148 } 149} 150