123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- /**
- *
- * PHP Pro Bid $Id$ p2gZqULhePiAENUWfbou8Nmzpc53bryqOCvD6CLcEUE=
- *
- * @link http://www.phpprobid.com
- * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
- * @license http://www.phpprobid.com/license Commercial License
- *
- * @version 7.4
- */
- /**
- * generate and download csv file class
- */
- namespace Ppb\Http;
- use Cube\Controller\Response\AbstractResponse;
- class GenerateCSV extends AbstractResponse
- {
- /**
- *
- * name of the file that will be generated
- *
- * @var string
- */
- protected $_fileName;
- /**
- *
- * first row of the csv file (column descriptions)
- *
- * @var array
- */
- protected $_heading = array();
- /**
- *
- * csv data
- *
- * @var array
- */
- protected $_data = array();
- /**
- *
- * class constructor
- *
- * @param string $fileName
- */
- public function __construct($fileName = null)
- {
- if ($fileName !== null) {
- $this->setFileName($fileName);
- }
- }
- /**
- *
- * set file name
- *
- * @param string $fileName
- *
- * @return $this
- */
- public function setFileName($fileName)
- {
- $this->_fileName = $fileName;
- return $this;
- }
- /**
- *
- * get file name
- *
- * @return string
- */
- public function getFileName()
- {
- return $this->_fileName;
- }
- /**
- *
- * get heading array
- *
- * @return array
- */
- public function getHeading()
- {
- return $this->_heading;
- }
- /**
- *
- * set heading array
- *
- * @param array $heading
- *
- * @return $this
- */
- public function setHeading($heading)
- {
- $this->_heading = $heading;
- return $this;
- }
- /**
- *
- * get data
- *
- * @return array
- */
- public function getData()
- {
- return $this->_data;
- }
- /**
- *
- * set data
- *
- * @param array $data
- *
- * @return $this
- */
- public function setData($data)
- {
- $this->_data = $data;
- return $this;
- }
- /**
- *
- * generate csv file from array and send it to download
- *
- * @return void
- */
- public function send()
- {
- if (empty($this->_fileName)) {
- throw new \InvalidArgumentException("A file name must be set.");
- }
- $this->setHeader('Content-Type: text/csv')
- ->addHeader('Content-Disposition: attachment; filename="' . $this->_fileName . '"')
- ->addHeader('Pragma: no-cache')
- ->addHeader('Expires: 0');
- $output = fopen("php://output", "w");
- if (!empty($this->_heading)) {
- fputcsv($output, $this->_heading);
- }
- foreach ($this->_data as $row) {
- fputcsv($output, $row);
- }
- fclose($output);
- parent::send();
- exit(0);
- }
- }
|