No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

CsvGenerator.php 5.0KB

hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. protected $delimiter ;
  28. public function __construct()
  29. {
  30. $this->arrayToExport = array();
  31. $this->titleDocument = 'csv_file';
  32. $this->convertEncoding = false ;
  33. $this->fromEncoding = 'UTF-8' ;
  34. $this->toEncoding = 'ISO-8859-1' ;
  35. $this->delimiter = ';' ;
  36. }
  37. public function enableConvertEncoding($toEncoding, $fromEncoding = null)
  38. {
  39. $this->convertEncoding = true ;
  40. $this->toEncoding = $toEncoding ;
  41. if(!is_null($fromEncoding)) {
  42. $this->fromEncoding = $fromEncoding ;
  43. }
  44. }
  45. public function encode($value)
  46. {
  47. if($this->convertEncoding) {
  48. return mb_convert_encoding(
  49. $value,
  50. $this->toEncoding,
  51. $this->fromEncoding) ;
  52. }
  53. return $value ;
  54. }
  55. public function encodeArray($array)
  56. {
  57. if($array && is_array($array)) {
  58. return array_map(function($value) {
  59. return $this->encode($value) ;
  60. }, $array) ;
  61. }
  62. return $array ;
  63. }
  64. public function setTitle($title, $displayHeader = false){
  65. $this->titleDocument = $this->formatTitle($title);
  66. if($displayHeader) {
  67. array_unshift($this->arrayToExport, array(''));
  68. array_unshift($this->arrayToExport, array($title));
  69. }
  70. }
  71. public function setColumns($columns, $displayLegend = true) {
  72. $this->columns = array_fill_keys(array_keys($columns), null);
  73. if($displayLegend) $this->row($columns);
  74. }
  75. public function cell($column, $value){
  76. $this->arrayToExport[] = array_merge(
  77. $this->columns,
  78. array($column => $this->encode($value))
  79. );
  80. }
  81. public function row($values = null, $row = false){
  82. $values = $this->encodeArray($values) ;
  83. if(!$row){
  84. if($values)$this->arrayToExport[] = array_merge($this->columns, $values);
  85. else $this->arrayToExport[] = array();
  86. }else{
  87. if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values);
  88. else $this->arrayToExport[$row] = array();
  89. }
  90. }
  91. public function emptyRow()
  92. {
  93. $this->row([]) ;
  94. }
  95. public function dump(){
  96. dump($this->arrayToExport);
  97. }
  98. public function createCsv($path)
  99. {
  100. $handle = fopen($path, 'w+');
  101. foreach ($this->arrayToExport as $line) {
  102. fputcsv($handle, $line, $this->getDelimiter(), "\"");
  103. }
  104. fclose($handle);
  105. }
  106. public function getReponse(){
  107. $response = new StreamedResponse(function () {
  108. $this->createCsv('php://output');
  109. });
  110. $response->headers->set('Content-Encoding', $this->toEncoding);
  111. $response->headers->set('Content-Type', 'application/force-download');
  112. $response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
  113. return $response;
  114. }
  115. private function formatTitle($str) {
  116. $str = str_replace("-", ' ', $str);
  117. $str = preg_replace('/\s+/', '_',$str);;
  118. //$str = str_replace(" ", '_', $str);
  119. $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  120. return $str;
  121. }
  122. public function getDelimiter()
  123. {
  124. return $this->delimiter ;
  125. }
  126. }