2010-06-06 16:05:46 +02:00
|
|
|
<?php
|
2010-07-21 12:44:01 +02:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2010, The volkszaehler.org project
|
2010-07-22 10:32:51 +02:00
|
|
|
* @package default
|
2010-07-21 12:44:01 +02:00
|
|
|
* @license http://www.opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
|
*
|
|
|
|
* This file is part of volkzaehler.org
|
2010-06-06 16:05:46 +02:00
|
|
|
*
|
2010-07-21 12:44:01 +02:00
|
|
|
* volkzaehler.org is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2010-06-06 16:05:46 +02:00
|
|
|
*
|
2010-07-21 12:44:01 +02:00
|
|
|
* volkzaehler.org is distributed in the hope that it will be useful,
|
2010-06-06 16:05:46 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-07-21 12:44:01 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2010-06-06 16:05:46 +02:00
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2010-07-21 12:44:01 +02:00
|
|
|
* along with volkszaehler.org. If not, see <http://www.gnu.org/licenses/>.
|
2010-06-06 16:05:46 +02:00
|
|
|
*/
|
|
|
|
|
2010-07-21 12:44:01 +02:00
|
|
|
namespace Volkszaehler\View\CSV;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-07-21 12:44:01 +02:00
|
|
|
use Volkszaehler\View\HTTP;
|
|
|
|
use Volkszaehler\View;
|
2010-07-20 19:10:45 +02:00
|
|
|
use Volkszaehler\Util;
|
|
|
|
|
2010-07-22 10:32:51 +02:00
|
|
|
/**
|
2010-07-21 12:44:01 +02:00
|
|
|
* CSV view
|
|
|
|
*
|
|
|
|
* also used for data
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <info@steffenvogel.de>
|
2010-07-22 10:32:51 +02:00
|
|
|
* @package default
|
2010-07-21 12:44:01 +02:00
|
|
|
*/
|
2010-07-22 16:10:08 +02:00
|
|
|
abstract class CSVView extends View\View {
|
2010-07-20 11:28:22 +02:00
|
|
|
protected $csv = array();
|
|
|
|
protected $header = array();
|
|
|
|
protected $footer = array();
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
protected $delimiter = ';';
|
|
|
|
protected $enclosure = '"';
|
2010-07-20 00:31:38 +02:00
|
|
|
|
2010-07-21 12:44:01 +02:00
|
|
|
/**
|
2010-07-20 00:31:38 +02:00
|
|
|
* constructor
|
|
|
|
*/
|
2010-07-21 12:44:01 +02:00
|
|
|
public function __construct(HTTP\Request $request, 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
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 11:28:22 +02:00
|
|
|
echo PHP_EOL;
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 11:28:22 +02:00
|
|
|
foreach ($this->csv as $array) {
|
2010-07-20 22:43:08 +02:00
|
|
|
$array = array_map(array($this, 'escape'), $array);
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
echo implode($this->delimiter, $array) . PHP_EOL;
|
2010-07-20 11:28:22 +02:00
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 11:28:22 +02:00
|
|
|
echo PHP_EOL;
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 11:28:22 +02:00
|
|
|
foreach ($this->footer as $line) {
|
|
|
|
echo $line . PHP_EOL;
|
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 12:06:04 +02:00
|
|
|
parent::render();
|
2010-07-20 00:31:38 +02:00
|
|
|
}
|
2010-07-21 12:44:01 +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;
|
|
|
|
}
|
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
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-21 12:44:01 +02:00
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
foreach ($debug->getMessages() as $message) {
|
|
|
|
$this->footer[] = 'message: ' . $message['message']; // TODO add more information
|
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-07-20 22:43:08 +02:00
|
|
|
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
|
|
|
}
|
2010-07-21 12:44:01 +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
|
|
|
}
|
|
|
|
|
|
|
|
?>
|