GenerateCSV.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ p2gZqULhePiAENUWfbou8Nmzpc53bryqOCvD6CLcEUE=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.4
  11. */
  12. /**
  13. * generate and download csv file class
  14. */
  15. namespace Ppb\Http;
  16. use Cube\Controller\Response\AbstractResponse;
  17. class GenerateCSV extends AbstractResponse
  18. {
  19. /**
  20. *
  21. * name of the file that will be generated
  22. *
  23. * @var string
  24. */
  25. protected $_fileName;
  26. /**
  27. *
  28. * first row of the csv file (column descriptions)
  29. *
  30. * @var array
  31. */
  32. protected $_heading = array();
  33. /**
  34. *
  35. * csv data
  36. *
  37. * @var array
  38. */
  39. protected $_data = array();
  40. /**
  41. *
  42. * class constructor
  43. *
  44. * @param string $fileName
  45. */
  46. public function __construct($fileName = null)
  47. {
  48. if ($fileName !== null) {
  49. $this->setFileName($fileName);
  50. }
  51. }
  52. /**
  53. *
  54. * set file name
  55. *
  56. * @param string $fileName
  57. *
  58. * @return $this
  59. */
  60. public function setFileName($fileName)
  61. {
  62. $this->_fileName = $fileName;
  63. return $this;
  64. }
  65. /**
  66. *
  67. * get file name
  68. *
  69. * @return string
  70. */
  71. public function getFileName()
  72. {
  73. return $this->_fileName;
  74. }
  75. /**
  76. *
  77. * get heading array
  78. *
  79. * @return array
  80. */
  81. public function getHeading()
  82. {
  83. return $this->_heading;
  84. }
  85. /**
  86. *
  87. * set heading array
  88. *
  89. * @param array $heading
  90. *
  91. * @return $this
  92. */
  93. public function setHeading($heading)
  94. {
  95. $this->_heading = $heading;
  96. return $this;
  97. }
  98. /**
  99. *
  100. * get data
  101. *
  102. * @return array
  103. */
  104. public function getData()
  105. {
  106. return $this->_data;
  107. }
  108. /**
  109. *
  110. * set data
  111. *
  112. * @param array $data
  113. *
  114. * @return $this
  115. */
  116. public function setData($data)
  117. {
  118. $this->_data = $data;
  119. return $this;
  120. }
  121. /**
  122. *
  123. * generate csv file from array and send it to download
  124. *
  125. * @return void
  126. */
  127. public function send()
  128. {
  129. if (empty($this->_fileName)) {
  130. throw new \InvalidArgumentException("A file name must be set.");
  131. }
  132. $this->setHeader('Content-Type: text/csv')
  133. ->addHeader('Content-Disposition: attachment; filename="' . $this->_fileName . '"')
  134. ->addHeader('Pragma: no-cache')
  135. ->addHeader('Expires: 0');
  136. $output = fopen("php://output", "w");
  137. if (!empty($this->_heading)) {
  138. fputcsv($output, $this->_heading);
  139. }
  140. foreach ($this->_data as $row) {
  141. fputcsv($output, $row);
  142. }
  143. fclose($output);
  144. parent::send();
  145. exit(0);
  146. }
  147. }