|
|
@@ -26,34 +26,72 @@ class CsvGenerator |
|
|
|
protected $columns; |
|
|
|
protected $titleDocument; |
|
|
|
|
|
|
|
protected $convertEncoding ; |
|
|
|
protected $fromEncoding ; |
|
|
|
protected $toEncoding ; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct() |
|
|
|
{ |
|
|
|
$this->arrayToExport = array(); |
|
|
|
$this->titleDocument = 'csv_file'; |
|
|
|
$this->convertEncoding = false ; |
|
|
|
$this->fromEncoding = 'UTF-8' ; |
|
|
|
$this->toEncoding = 'ISO-8859-1' ; |
|
|
|
} |
|
|
|
|
|
|
|
public function enableConvertEncoding($toEncoding, $fromEncoding = null) |
|
|
|
{ |
|
|
|
$this->convertEncoding = true ; |
|
|
|
$this->toEncoding = $toEncoding ; |
|
|
|
|
|
|
|
if(!is_null($fromEncoding)) { |
|
|
|
$this->fromEncoding = $fromEncoding ; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function encode($value) |
|
|
|
{ |
|
|
|
if($this->convertEncoding) { |
|
|
|
return mb_convert_encoding($value, $this->toEncoding, $this->fromEncoding) ; |
|
|
|
} |
|
|
|
|
|
|
|
return $value ; |
|
|
|
} |
|
|
|
|
|
|
|
public function encodeArray($array) |
|
|
|
{ |
|
|
|
return array_map(function($value) { |
|
|
|
return $this->encode($value) ; |
|
|
|
}, $array) ; |
|
|
|
} |
|
|
|
|
|
|
|
public function setTitle($title, $displayHeader = false){ |
|
|
|
|
|
|
|
$this->titleDocument = $this->formatTitle($title); |
|
|
|
|
|
|
|
if($displayHeader){ |
|
|
|
if($displayHeader) { |
|
|
|
array_unshift($this->arrayToExport, array('')); |
|
|
|
array_unshift($this->arrayToExport, array($title)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function setColumns($columns, $displayLegend = true){ |
|
|
|
$this->columns =array_fill_keys(array_keys($columns), null);; |
|
|
|
if($displayLegend)$this->arrayToExport[] = $columns; |
|
|
|
public function setColumns($columns, $displayLegend = true) { |
|
|
|
$this->columns = array_fill_keys(array_keys($columns), null); |
|
|
|
if($displayLegend) $this->row($columns); |
|
|
|
} |
|
|
|
|
|
|
|
public function cell($column, $value){ |
|
|
|
$this->arrayToExport[] = array_merge($this->columns, array($column =>$value)); |
|
|
|
$this->arrayToExport[] = array_merge( |
|
|
|
$this->columns, |
|
|
|
array($column => $this->encode($value)) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
public function row($values = null, $row = false){ |
|
|
|
|
|
|
|
$values = $this->encodeArray($values) ; |
|
|
|
|
|
|
|
if(!$row){ |
|
|
|
if($values)$this->arrayToExport[] = array_merge($this->columns, $values); |
|
|
|
else $this->arrayToExport[] = array(); |
|
|
@@ -61,21 +99,30 @@ class CsvGenerator |
|
|
|
if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values); |
|
|
|
else $this->arrayToExport[$row] = array(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function emptyRow() |
|
|
|
{ |
|
|
|
$this->row([]) ; |
|
|
|
} |
|
|
|
|
|
|
|
public function dump(){ |
|
|
|
dump($this->arrayToExport); |
|
|
|
} |
|
|
|
|
|
|
|
public function createCsv($path) |
|
|
|
{ |
|
|
|
$handle = fopen($path, 'w+'); |
|
|
|
|
|
|
|
foreach ($this->arrayToExport as $line) { |
|
|
|
fputcsv($handle, $line, ';', ' '); |
|
|
|
} |
|
|
|
fclose($handle); |
|
|
|
} |
|
|
|
|
|
|
|
public function getReponse(){ |
|
|
|
$response = new StreamedResponse(function () { |
|
|
|
$handle = fopen('php://output', 'r+'); |
|
|
|
|
|
|
|
foreach ($this->arrayToExport as $line) { |
|
|
|
fputcsv($handle, $line, ';', ' '); |
|
|
|
} |
|
|
|
fclose($handle); |
|
|
|
$this->createCsv('php://output'); |
|
|
|
}); |
|
|
|
|
|
|
|
$response->headers->set('Content-Type', 'application/force-download'); |