2010-06-06 16:05:46 +02:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 by Justin Otherguy <justin@justinotherguy.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License (either version 2 or
|
|
|
|
* version 3) as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
* For more information on the GPL, please go to:
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*/
|
|
|
|
|
2010-07-20 00:31:38 +02:00
|
|
|
namespace Volkszaehler\View\Csv;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-07-20 19:10:45 +02:00
|
|
|
use Volkszaehler\Util;
|
|
|
|
|
2010-07-20 12:06:04 +02:00
|
|
|
abstract class Csv extends \Volkszaehler\View\View {
|
2010-07-20 11:28:22 +02:00
|
|
|
protected $csv = array();
|
|
|
|
protected $header = array();
|
|
|
|
protected $footer = array();
|
2010-07-20 22:43:08 +02:00
|
|
|
|
|
|
|
protected $delimiter = ';';
|
|
|
|
protected $enclosure = '"';
|
2010-07-20 00:31:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* constructor
|
|
|
|
*/
|
2010-07-20 11:28:22 +02:00
|
|
|
public function __construct(\Volkszaehler\View\Http\Request $request, \Volkszaehler\View\Http\Response $response) {
|
2010-07-20 00:31:38 +02:00
|
|
|
parent::__construct($request, $response);
|
|
|
|
|
2010-07-20 11:28:22 +02:00
|
|
|
$this->header[] = 'source: volkszaehler.org';
|
|
|
|
$this->header[] = 'version: ' . \Volkszaehler\VERSION;
|
2010-07-20 00:31:38 +02:00
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
$this->response->setHeader('Content-type', 'text/csv');
|
|
|
|
$this->response->setHeader('Content-Disposition', 'attachment; filename="data.csv"');
|
2010-07-20 00:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function render() {
|
2010-07-20 11:28:22 +02:00
|
|
|
foreach ($this->header as $line) {
|
|
|
|
echo $line . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo PHP_EOL;
|
|
|
|
|
|
|
|
foreach ($this->csv as $array) {
|
2010-07-20 22:43:08 +02:00
|
|
|
$array = array_map(array($this, 'escape'), $array);
|
|
|
|
|
|
|
|
echo implode($this->delimiter, $array) . PHP_EOL;
|
2010-07-20 11:28:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
echo PHP_EOL;
|
|
|
|
|
|
|
|
foreach ($this->footer as $line) {
|
|
|
|
echo $line . PHP_EOL;
|
|
|
|
}
|
2010-07-20 12:06:04 +02:00
|
|
|
|
|
|
|
parent::render();
|
2010-07-20 00:31:38 +02:00
|
|
|
}
|
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
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();
|
2010-07-20 19:10:45 +02:00
|
|
|
$this->footer[] = 'database: ' . Util\Configuration::read('db.driver');
|
2010-07-20 11:28:22 +02:00
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
foreach ($debug->getMessages() as $message) {
|
|
|
|
$this->footer[] = 'message: ' . $message['message']; // TODO add more information
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($debug->getQueries() as $query) {
|
2010-07-20 11:28:22 +02:00
|
|
|
$this->footer[] = 'query: ' . $query['sql'];
|
2010-07-20 19:10:45 +02:00
|
|
|
$this->footer[] = ' parameters: ' . implode(', ', $query['parameters']);
|
2010-07-20 11:28:22 +02:00
|
|
|
}
|
2010-07-20 00:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addException(\Exception $exception) {
|
2010-07-20 11:28:22 +02:00
|
|
|
echo $exception;
|
2010-07-18 17:12:00 +02:00
|
|
|
}
|
2010-06-06 16:05:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|