1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214:
<?php
class icms_ipf_export_Renderer {
public $data;
public $format;
public $filename;
public $filepath;
public $options;
public function __construct($data, $filename=false, $filepath=false, $format='csv', $options=array('separator'=>';')) {
$this->data = $data;
$this->format = $format;
$this->filename = $filename;
$this->filepath = $filepath;
$this->options = $options;
}
public function arrayToCsvString($dataArray, $separator, $trim = 'both', $removeEmptyLines = TRUE) {
if (!is_array($dataArray) || empty ($dataArray))
return '';
switch ($trim) {
case 'none' :
$trimFunction = FALSE;
break;
case 'left' :
$trimFunction = 'ltrim';
break;
case 'right' :
$trimFunction = 'rtrim';
break;
default :
$trimFunction = 'trim';
break;
}
$ret = array();
foreach ($dataArray as $key=>$field) {
$ret[$key] = $this->valToCsvHelper($field, $separator, $trimFunction);
}
return implode($separator, $ret);
}
public function valToCsvHelper($val, $separator, $trimFunction) {
if ($trimFunction)
$val = $trimFunction ($val);
$needQuote = FALSE;
do {
if (strpos($val, '"') !== FALSE) {
$val = str_replace('"', '""', $val);
$needQuote = TRUE;
break;
}
if (strpos($val, $separator) !== FALSE) {
$needQuote = TRUE;
break;
}
if ((strpos($val, "\n") !== FALSE) || (strpos($val, "\r") !== FALSE)) {
$needQuote = TRUE;
break;
}
} while (FALSE);
if ($needQuote) {
$val = '"' . $val . '"';
}
return $val;
}
public function execute() {
$exportFileData = '';
switch ($this->format) {
case 'csv':
$separator = isset($this->options['separator']) ? $this->options['separator'] : ';';
$firstRow = implode($separator, $this->data['columnsHeaders']);
$exportFileData .= $firstRow . "\r\n";
foreach ($this->data['rows'] as $cols) {
$exportFileData .= $this->arrayToCsvString($cols, $separator) . "\r\n";
}
break;
default:
break;
}
$this->saveExportFile($exportFileData);
}
public function saveExportFile($content) {
switch ($this->format) {
case 'csv':
$this->saveCsv($content);
break;
default:
break;
}
}
public function saveCsv($content) {
if (!$this->filepath) {
$this->filepath = ICMS_UPLOAD_PATH . '/';
}
if (!$this->filename) {
$this->filename .= time();
$this->filename .= '.csv';
}
$fullFileName = $this->filepath . $this->filename;
if (!$handle = fopen($fullFileName, 'a+')) {
trigger_error('Unable to open ' . $fullFileName, E_USER_WARNING);
} elseif (fwrite($handle, $content) === FALSE) {
trigger_error('Unable to write in ' . $fullFileName, E_USER_WARNING);
} else {
$mimeType = 'text/csv';
$file = strrev($this->filename);
$temp_name = strtolower(strrev(substr($file,0,strpos($file,"--"))));
if ($temp_name == '') {
$file_name = $this->filename;
} else {
$file_name = $temp_name;
}
$fullFileName = $this->filepath . stripslashes(trim($this->filename));
if (ini_get('zlib.output_compression')) {
ini_set('zlib.output_compression', 'Off');
}
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Transfer-Encoding: binary");
if (isset($mimeType)) {
header("Content-Type: " . $mimeType);
}
header("Content-Disposition: attachment; filename=" . $file_name);
if (isset($mimeType) && strstr($mimeType, "text/")) {
$fp = fopen($fullFileName, "r");
} else {
$fp = fopen($fullFileName, "rb");
}
fpassthru($fp);
exit();
}
fclose($handle);
}
}