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.

100 lines
3.3KB

  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. public function __construct()
  25. {
  26. $this->arrayToExport = array();
  27. $this->titleDocument = 'csv_file';
  28. }
  29. public function setTitle($title, $displayHeader = false){
  30. $this->titleDocument = $this->formatTitle($title);
  31. if($displayHeader){
  32. array_unshift($this->arrayToExport, array(''));
  33. array_unshift($this->arrayToExport, array($title));
  34. }
  35. }
  36. public function setColumns($columns, $displayLegend = true){
  37. $this->columns =array_fill_keys(array_keys($columns), null);;
  38. if($displayLegend)$this->arrayToExport[] = $columns;
  39. }
  40. public function cell($column, $value){
  41. $this->arrayToExport[] = array_merge($this->columns, array($column =>$value));
  42. }
  43. public function row($values = null, $row = false){
  44. if(!$row){
  45. if($values)$this->arrayToExport[] = array_merge($this->columns, $values);
  46. else $this->arrayToExport[] = array();
  47. }else{
  48. if($values)$this->arrayToExport[$row] = array_merge($this->columns, $values);
  49. else $this->arrayToExport[$row] = array();
  50. }
  51. }
  52. public function dump(){
  53. dump($this->arrayToExport);
  54. }
  55. public function getReponse(){
  56. $response = new StreamedResponse(function () {
  57. $handle = fopen('php://output', 'r+');
  58. foreach ($this->arrayToExport as $line) {
  59. fputcsv($handle, $line, ';', ' ');
  60. }
  61. fclose($handle);
  62. });
  63. $response->headers->set('Content-Type', 'application/force-download');
  64. $response->headers->set('Content-Disposition', 'attachment; filename="'.$this->titleDocument.'.csv"');
  65. return $response;
  66. }
  67. private function formatTitle($str) {
  68. $str = str_replace("-", ' ', $str);
  69. $str = preg_replace('/\s+/', '_',$str);;
  70. //$str = str_replace(" ", '_', $str);
  71. $str = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  72. return $str;
  73. }
  74. }