CsvGenerator.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Lc\ShopBundle\Services;
  3. use Cocur\Slugify\Slugify;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigManager;
  6. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  7. use Lc\ShopBundle\Context\PageInterface;
  8. use Lc\ShopBundle\Context\PointSaleInterface;
  9. use Lc\ShopBundle\Context\ReminderInterface;
  10. use Lc\ShopBundle\Context\TaxRateInterface;
  11. use Lc\ShopBundle\Context\UnitInterface;
  12. use Lc\ShopBundle\Context\UserInterface;
  13. use Lc\ShopBundle\Context\UserPointSaleInterface;
  14. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  15. use Symfony\Component\HttpFoundation\ParameterBag;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\HttpFoundation\StreamedResponse;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class CsvGenerator
  20. {
  21. protected $arrayToExport;
  22. protected $columns;
  23. protected $titleDocument;
  24. protected $convertEncoding ;
  25. protected $fromEncoding ;
  26. protected $toEncoding ;
  27. public function __construct()
  28. {
  29. $this->arrayToExport = array();
  30. $this->titleDocument = 'csv_file';
  31. $this->convertEncoding = false ;
  32. $this->fromEncoding = 'UTF-8' ;
  33. $this->toEncoding = 'ISO-8859-1' ;
  34. }
  35. public function enableConvertEncoding($toEncoding, $fromEncoding = null)
  36. {
  37. $this->convertEncoding = true ;
  38. $this->toEncoding = $toEncoding ;
  39. if(!is_null($fromEncoding)) {
  40. $this->fromEncoding = $fromEncoding ;
  41. }
  42. }
  43. public function encode($value)
  44. {
  45. if($this->convertEncoding) {
  46. return mb_convert_encoding($value, $this->toEncoding, $this->fromEncoding) ;
  47. }
  48. return $value ;
  49. }
  50. public function encodeArray($array)
  51. {
  52. if($array && is_array($array)) {
  53. return array_map(function($value) {
  54. return $this->encode($value) ;
  55. }, $array) ;
  56. }
  57. return $array ;
  58. }
  59. public function setTitle($title, $displayHeader = false){
  60. $this->titleDocument = $this->formatTitle($title);
  61. if($displayHeader) {
  62. array_unshift($this->arrayToExport, array(''));
  63. array_unshift($this->arrayToExport, array($title));
  64. }
  65. }
  66. public function setColumns($columns, $displayLegend = true) {
  67. $this->columns = array_fill_keys(array_keys($columns), null);
  68. if($displayLegend) $this->row($columns);
  69. }
  70. public function cell($column, $value){
  71. $this->arrayToExport[] = array_merge(
  72. $this->columns,
  73. array($column => $this->encode($value))
  74. );
  75. }
  76. public function row($values = null, $row = false){
  77. $values = $this->encodeArray($values) ;
  78. if(!$row){
  79. if($values)$this->arrayToExport[] = array_merge($this->columns, $values);
  80. else $this->arrayToExport[] = array();
  81. }else{
  82. if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values);
  83. else $this->arrayToExport[$row] = array();
  84. }
  85. }
  86. public function emptyRow()
  87. {
  88. $this->row([]) ;
  89. }
  90. public function dump(){
  91. dump($this->arrayToExport);
  92. }
  93. public function createCsv($path)
  94. {
  95. $handle = fopen($path, 'w+');
  96. foreach ($this->arrayToExport as $line) {
  97. fputcsv($handle, $line, ';', ' ');
  98. }
  99. fclose($handle);
  100. }
  101. public function getReponse(){
  102. $response = new StreamedResponse(function () {
  103. $this->createCsv('php://output');
  104. });
  105. $response->headers->set('Content-Type', 'application/force-download');
  106. $response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
  107. return $response;
  108. }
  109. private function formatTitle($str) {
  110. $str = str_replace("-", ' ', $str);
  111. $str = preg_replace('/\s+/', '_',$str);;
  112. //$str = str_replace(" ", '_', $str);
  113. $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  114. return $str;
  115. }
  116. }