You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.6KB

  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. return array_map(function($value) {
  53. return $this->encode($value) ;
  54. }, $array) ;
  55. }
  56. public function setTitle($title, $displayHeader = false){
  57. $this->titleDocument = $this->formatTitle($title);
  58. if($displayHeader) {
  59. array_unshift($this->arrayToExport, array(''));
  60. array_unshift($this->arrayToExport, array($title));
  61. }
  62. }
  63. public function setColumns($columns, $displayLegend = true) {
  64. $this->columns = array_fill_keys(array_keys($columns), null);
  65. if($displayLegend) $this->row($columns);
  66. }
  67. public function cell($column, $value){
  68. $this->arrayToExport[] = array_merge(
  69. $this->columns,
  70. array($column => $this->encode($value))
  71. );
  72. }
  73. public function row($values = null, $row = false){
  74. $values = $this->encodeArray($values) ;
  75. if(!$row){
  76. if($values)$this->arrayToExport[] = array_merge($this->columns, $values);
  77. else $this->arrayToExport[] = array();
  78. }else{
  79. if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values);
  80. else $this->arrayToExport[$row] = array();
  81. }
  82. }
  83. public function emptyRow()
  84. {
  85. $this->row([]) ;
  86. }
  87. public function dump(){
  88. dump($this->arrayToExport);
  89. }
  90. public function createCsv($path)
  91. {
  92. $handle = fopen($path, 'w+');
  93. foreach ($this->arrayToExport as $line) {
  94. fputcsv($handle, $line, ';', ' ');
  95. }
  96. fclose($handle);
  97. }
  98. public function getReponse(){
  99. $response = new StreamedResponse(function () {
  100. $this->createCsv('php://output');
  101. });
  102. $response->headers->set('Content-Type', 'application/force-download');
  103. $response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
  104. return $response;
  105. }
  106. private function formatTitle($str) {
  107. $str = str_replace("-", ' ', $str);
  108. $str = preg_replace('/\s+/', '_',$str);;
  109. //$str = str_replace(" ", '_', $str);
  110. $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  111. return $str;
  112. }
  113. }