. */ namespace Volkszaehler\View; use Volkszaehler\View\HTTP; use Volkszaehler\Util; use Volkszaehler\Interpreter; /** * CSV view * * @author Steffen Vogel * @package default * @todo rework */ class CSV extends View { const DELIMITER = ';'; const ENCLOSURE = '"'; protected $csv = array(); /** * constructor */ public function __construct(HTTP\Request $request, HTTP\Response $response) { parent::__construct($request, $response); echo '# source: volkszaehler.org' . PHP_EOL; echo '# version: ' . VZ_VERSION . PHP_EOL; $this->response->setHeader('Content-type', 'text/csv'); } /** * Add object to output * * @param mixed $data */ public function add($data) { if ($data instanceof Interpreter\Interpreter || $data instanceof Interpreter\AggregatorInterpreter) { $this->addData($data); } elseif ($data instanceof \Exception) { $this->addException($data); } elseif ($data instanceof Util\Debug) { $this->addDebug($data); } elseif (isset($data)) { // ignores NULL data throw new \Exception('Can\'t show ' . get_class($data)); } } /** * Add debugging information include queries and messages to output queue * * @param Util\Debug $debug */ protected function addDebug(Util\Debug $debug) { echo '# time: ' . $debug->getExecutionTime() . PHP_EOL; echo '# database: ' . Util\Configuration::read('db.driver') . PHP_EOL; foreach ($debug->getMessages() as $message) { echo '# message: ' . $message['message'] . PHP_EOL; // TODO add more information } foreach ($debug->getQueries() as $query) { echo '# query: ' . $query['sql'] . PHP_EOL; if (isset($query['parameters'])) { echo "# \tparameters: " . implode(', ', $query['parameters']) . PHP_EOL; } } } /** * Add exception to output queue * * @param \Exception $exception * @param boolean $debug */ protected function addException(\Exception $exception) { echo get_class($exception) . '# [' . $exception->getCode() . ']' . ':' . $exception->getMessage() . PHP_EOL; if (Util\Debug::isActivated()) { echo "#\tfile: " . $exception->getFile() . PHP_EOL; echo "#\tline: " . $exception->getLine() . PHP_EOL; } } /** * Add data to output queue * * @param Interpreter\InterpreterInterface $interpreter */ protected function addData(Interpreter\Interpreter $interpreter) { $this->response->setHeader( 'Content-Disposition', 'attachment; ' . 'filename="' . strtolower($interpreter->getEntity()->getProperty('title')) . '.csv" ' . 'creation-date="' . date(DATE_RFC2822, $interpreter->getTo()/1000). '"' ); $tuples = $interpreter->processData( function($tuple) { return array( $tuple[0], View::formatNumber($tuple[1]), $tuple[2] ); } ); $min = $interpreter->getMin(); $max = $interpreter->getMax(); $average = $interpreter->getAverage(); $consumption = $interpreter->getConsumption(); $from = $interpreter->getFrom(); $to = $interpreter->getTo(); echo '# uuid: ' . $interpreter->getEntity()->getUuid() . PHP_EOL; if (isset($from)) echo '# from: ' . $from . PHP_EOL; if (isset($to)) echo '# to: ' . $to . PHP_EOL; if (isset($min)) echo '# min: ' . $min[0] . ' => ' . $min[1] . PHP_EOL; if (isset($max)) echo '# max: ' . $max[0] . ' => ' . $max[1] . PHP_EOL; if (isset($average)) echo '# average: ' . View::formatNumber($average) . PHP_EOL; if (isset($consumption)) echo '# consumption: ' . View::formatNumber($consumption) . PHP_EOL; echo '# rows: ' . $interpreter->getRowCount() . PHP_EOL; foreach ($tuples as $tuple) { echo implode(CSV::DELIMITER, $tuple) . PHP_EOL; } } /** * Process, encode and print output to stdout */ protected function render() { } /** * Escape data according to CSV format * * @param $value to be escaped * @return string escaped data */ protected function escape($value) { if (is_string($value)) { return self::ENCLOSURE . $value . self::ENCLOSURE; } elseif (is_numeric($value)) { return $value; } else { return (string) $value; } } } ?>