Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

133 lines
4.1KB

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