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 $this->doc .= '"'; 89 } 90 91 /** 92 * Alias for tablecell_open 93 * 94 * @param int $colspan ignored 95 * @param null $align ignored 96 * @param int $rowspan ignored 97 */ 98 function tableheader_open($colspan = 1, $align = null, $rowspan = 1) { 99 $this->tablecell_open($colspan, $align, $rowspan); 100 } 101 102 /** 103 * Alias for tablecell_close 104 */ 105 function tableheader_close() { 106 $this->tablecell_close(); 107 } 108 109 /** 110 * Add CRLF newline at the end of one line 111 */ 112 function tablerow_close() { 113 if(!$this->_doOutput()) return; 114 $this->doc .= "\r\n"; 115 } 116 117 /** 118 * Outputs cell content 119 * 120 * @param string $text 121 */ 122 function cdata($text) { 123 if(!$this->_doOutput()) return; 124 if($text === '') return; 125 126 $this->doc .= str_replace('"', '""', $text); 127 } 128 129 /** 130 * Uses cdata to output the title 131 * 132 * @param string $link 133 * @param null $title 134 */ 135 function internallink($link, $title = null) { 136 $this->cdata($title); 137 } 138 139 /** 140 * Uses cdata to output the email address 141 * 142 * @param string $address 143 * @param null $name 144 */ 145 function emaillink($address, $name = null) { 146 $this->cdata($address); 147 } 148} 149