Browse Source

Export par véhicules

feature/export_comptable
Fab 4 years ago
parent
commit
b656e44d4d
1 changed files with 58 additions and 10 deletions
  1. +58
    -10
      ShopBundle/Services/CsvGenerator.php

+ 58
- 10
ShopBundle/Services/CsvGenerator.php View File

@@ -16,31 +16,79 @@ use Lc\ShopBundle\Context\UserPointSaleInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Contracts\Translation\TranslatorInterface;

class CsvGenerator
{
protected $em ;
protected $parameterBag ;
protected $merchantUtils ;
protected $session;
protected $translator;
protected $configManager;

protected $arrayToExport;
protected $columns;
protected $titleDocument;


public function __construct()
{
$this->arrayToExport = array();
$this->titleDocument = 'csv_file';
}

public function setTitle($title, $displayHeader = false){

$this->titleDocument = $this->formatTitle($title);

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 cell($column, $value){
$this->arrayToExport[] = array_merge($this->columns, array($column =>$value));
}

public function setColumns($columns){
$this->columns = $columns;
public function row($values = null, $row = false){

if(!$row){
if($values)$this->arrayToExport[] = array_merge($this->columns, $values);
else $this->arrayToExport[] = array();
}else{
if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values);
else $this->arrayToExport[$row] = array();
}

}

public function dump(){
dump($this->arrayToExport);
}

public function getReponse(){
$response = new StreamedResponse(function () {
$handle = fopen('php://output', 'r+');

foreach ($this->arrayToExport as $line) {
fputcsv($handle, $line, ';', ' ');
}
fclose($handle);
});

$response->headers->set('Content-Type', 'application/force-download');
$response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
return $response;
}
private function formatTitle($str) {
$str = str_replace("-", ' ', $str);
$str = preg_replace('/\s+/', '_',$str);;
//$str = str_replace(" ", '_', $str);
$str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);

public function setCell($row, $column, $value){
$this->arrayToExport[$row][$column] = $value;
return $str;
}



Loading…
Cancel
Save