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.

158 líneas
5.0KB

  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-Type', 'application/force-download');
  111. $response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
  112. return $response;
  113. }
  114. private function formatTitle($str) {
  115. $str = str_replace("-", ' ', $str);
  116. $str = preg_replace('/\s+/', '_',$str);;
  117. //$str = str_replace(" ", '_', $str);
  118. $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  119. return $str;
  120. }
  121. public function getDelimiter()
  122. {
  123. return $this->delimiter ;
  124. }
  125. }