Example #1
0
	/**
	 * Send a CSV header and setup all necessary options to the View object to provide a download.
	 *
	 * All the data headers will be rendered automatically, (with the exception of the final 'controls' column).
	 *
	 * @param \View  $view  Page view to manipulate
	 * @param string $title Title of the file, (will get converted to a valid URL)
	 */
	public function sendCSVHeader(\View $view, $title = 'csv export'){
		$filename = \Core\str_to_url($title) . '-' . date('Y-m-d') . '.csv';

		$view->mode = \View::MODE_NOOUTPUT;
		$view->contenttype = 'text/csv';
		$view->addHeader('Content-Disposition', 'attachment; filename=' . $filename);

		// Set the limits and everything as necessary.
		$this->setLimit(99999);
		if(!$this->_applied){
			$this->getFilters()->applyToFactory($this->getModelFactory());
			$this->_results = $this->getModelFactory()->get();
			$this->_applied = true;
		}

		// Build the CSV header to send, (the first record).
		$header = [];
		foreach($this->_columns as $c){
			/** @var Column $c */
			$header[] = $c->title;
		}

		// Send the headers and start the output.
		$view->render();
		$this->sendCSVRecord($header);
	}