|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
-
- namespace Lc\ShopBundle\Services;
-
- use Cocur\Slugify\Slugify;
- use Doctrine\ORM\EntityManagerInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigManager;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\PageInterface;
- use Lc\ShopBundle\Context\PointSaleInterface;
- use Lc\ShopBundle\Context\ReminderInterface;
- use Lc\ShopBundle\Context\TaxRateInterface;
- use Lc\ShopBundle\Context\UnitInterface;
- use Lc\ShopBundle\Context\UserInterface;
- 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 $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 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);
-
- return $str;
- }
-
-
-
-
-
-
- }
|