. */ namespace Volkszaehler\View\CSV; use Volkszaehler\View\HTTP; use Volkszaehler\View; use Volkszaehler\Util; /** * CSV view * * also used for data * * @author Steffen Vogel * @package default */ abstract class CSVView extends View\View { protected $csv = array(); protected $header = array(); protected $footer = array(); protected $delimiter = ';'; protected $enclosure = '"'; /** * constructor */ public function __construct(HTTP\Request $request, HTTP\Response $response) { parent::__construct($request, $response); $this->header[] = 'source: volkszaehler.org'; $this->header[] = 'version: ' . \Volkszaehler\VERSION; $this->response->setHeader('Content-type', 'text/csv'); $this->response->setHeader('Content-Disposition', 'attachment; filename="data.csv"'); } public function render() { foreach ($this->header as $line) { echo $line . PHP_EOL; } echo PHP_EOL; foreach ($this->csv as $array) { $array = array_map(array($this, 'escape'), $array); echo implode($this->delimiter, $array) . PHP_EOL; } echo PHP_EOL; foreach ($this->footer as $line) { echo $line . PHP_EOL; } parent::render(); } protected function escape($value) { if (is_string($value)) { return $this->enclosure . $value . $this->enclosure; } elseif (is_numeric($value)) { return $value; } else { return (string) $value; } } public function addDebug(Util\Debug $debug) { $this->footer[] = 'time: ' . $debug->getExecutionTime(); $this->footer[] = 'database: ' . Util\Configuration::read('db.driver'); foreach ($debug->getMessages() as $message) { $this->footer[] = 'message: ' . $message['message']; // TODO add more information } foreach ($debug->getQueries() as $query) { $this->footer[] = 'query: ' . $query['sql']; $this->footer[] = ' parameters: ' . implode(', ', $query['parameters']); } } public function addException(\Exception $exception) { echo $exception; } } ?>