2010-07-18 17:12:00 +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
|
2010-07-22 16:21:26 +02:00
|
|
|
*/
|
|
|
|
/*
|
2010-07-21 12:44:01 +02:00
|
|
|
* This file is part of volkzaehler.org
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* volkzaehler.org is distributed in the hope that it will be useful,
|
2010-07-18 17:12:00 +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-07-18 17:12:00 +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-07-18 17:12:00 +02:00
|
|
|
*/
|
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
namespace Volkszaehler\View;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-10-01 11:23:31 +02:00
|
|
|
use Doctrine\ORM\Query\AST\Functions;
|
2010-09-22 02:27:43 +02:00
|
|
|
use Volkszaehler\Interpreter;
|
2010-07-21 12:44:01 +02:00
|
|
|
use Volkszaehler\View\HTTP;
|
2010-07-18 17:12:00 +02:00
|
|
|
use Volkszaehler\Util;
|
2010-07-24 13:03:34 +02:00
|
|
|
use Volkszaehler\Model;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-07-22 10:32:51 +02:00
|
|
|
/**
|
|
|
|
* JSON view
|
|
|
|
*
|
|
|
|
* @package default
|
|
|
|
* @author Steffen Vogel <info@steffenvogel.de>
|
|
|
|
*/
|
2010-07-24 13:03:34 +02:00
|
|
|
class JSON extends View {
|
2010-09-05 22:15:41 +02:00
|
|
|
/**
|
|
|
|
* @var array holds the JSON data in an array
|
|
|
|
*/
|
2010-08-24 15:33:55 +02:00
|
|
|
protected $json;
|
2010-07-18 17:12:00 +02:00
|
|
|
|
2010-09-05 22:15:41 +02:00
|
|
|
/**
|
|
|
|
* @var string padding function name or NULL if disabled
|
|
|
|
*/
|
2010-07-24 13:03:34 +02:00
|
|
|
protected $padding = FALSE;
|
|
|
|
|
2010-07-21 12:44:01 +02:00
|
|
|
/**
|
2010-07-18 17:12:00 +02:00
|
|
|
* constructor
|
|
|
|
*/
|
2010-09-19 20:47:59 +02:00
|
|
|
public function __construct(HTTP\Request $request, HTTP\Response $response) {
|
2010-07-18 17:12:00 +02:00
|
|
|
parent::__construct($request, $response);
|
|
|
|
|
2010-08-24 15:33:55 +02:00
|
|
|
$this->json = new Util\JSON();
|
2010-07-18 17:12:00 +02:00
|
|
|
$this->json['source'] = 'volkszaehler.org';
|
2010-08-24 15:33:55 +02:00
|
|
|
$this->json['version'] = VZ_VERSION;
|
2010-09-25 00:42:07 +02:00
|
|
|
$this->json['component'] = 'backend';
|
2010-07-19 17:33:26 +02:00
|
|
|
|
2010-09-19 20:47:59 +02:00
|
|
|
$this->setPadding($request->getParameter('padding'));
|
2010-07-18 17:12:00 +02:00
|
|
|
}
|
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
/**
|
|
|
|
* Process, encode and print output
|
|
|
|
*/
|
|
|
|
protected function render() {
|
|
|
|
$json = $this->json->encode((Util\Debug::isActivated()) ? JSON_PRETTY : 0);
|
2010-07-24 13:03:34 +02:00
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
if ($this->padding) {
|
2010-10-01 11:23:31 +02:00
|
|
|
$json = 'if (' . $this->padding . ') { ' . $this->padding . '(' . $json . '); }';
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
$this->response->setHeader('Content-type', 'application/json');
|
|
|
|
echo $json;
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
/**
|
|
|
|
* Add channel to output queue
|
|
|
|
*
|
|
|
|
* @param Model\Channel $channel
|
|
|
|
*/
|
2010-10-01 11:23:31 +02:00
|
|
|
protected function addEntity(Model\Entity $entity) {
|
|
|
|
if ($entity instanceof Model\Aggregator) {
|
|
|
|
$this->json['entity'] = self::convertAggregator($entity);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->json['entity'] = self::convertEntity($entity);
|
|
|
|
}
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
/**
|
|
|
|
* Add debugging information include queries and messages to output queue
|
|
|
|
*
|
|
|
|
* @param Util\Debug $debug
|
|
|
|
*/
|
|
|
|
protected function addDebug(Util\Debug $debug) {
|
2010-09-25 00:42:07 +02:00
|
|
|
$queries = $debug->getQueries();
|
|
|
|
$messages = $debug->getMessages();
|
|
|
|
|
|
|
|
$jsonDebug['time'] = $debug->getExecutionTime();
|
|
|
|
|
|
|
|
if (count($messages) > 0) {
|
|
|
|
$jsonDebug['messages'] = $messages;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($queries) > 0) {
|
|
|
|
$jsonDebug['database'] = array(
|
2010-07-24 13:03:34 +02:00
|
|
|
'driver' => Util\Configuration::read('db.driver'),
|
2010-09-25 00:42:07 +02:00
|
|
|
'queries' => $queries
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->json['debug'] = $jsonDebug;
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
/**
|
|
|
|
* Add exception to output queue
|
|
|
|
*
|
|
|
|
* @param \Exception $exception
|
|
|
|
* @param boolean $debug
|
|
|
|
*/
|
2010-09-04 01:28:29 +02:00
|
|
|
protected function addException(\Exception $exception, $debug = FALSE) {
|
|
|
|
$exceptionInfo = array(
|
2010-07-24 13:03:34 +02:00
|
|
|
'type' => get_class($exception),
|
|
|
|
'message' => $exception->getMessage(),
|
2010-09-04 01:28:29 +02:00
|
|
|
'code' => $exception->getCode()
|
2010-07-24 13:03:34 +02:00
|
|
|
);
|
2010-09-04 01:28:29 +02:00
|
|
|
|
|
|
|
if ($debug) {
|
|
|
|
$debugInfo = array('file' => $exception->getFile(),
|
|
|
|
'line' => $exception->getLine(),
|
|
|
|
'trace' => $exception->getTrace()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->json['exception'] = array_merge($exceptionInfo, $debugInfo);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->json['exception'] = $exceptionInfo;
|
|
|
|
}
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
/**
|
|
|
|
* Add data to output queue
|
|
|
|
*
|
|
|
|
* @param Interpreter\InterpreterInterface $interpreter
|
|
|
|
*/
|
|
|
|
protected function addData(Interpreter\InterpreterInterface $interpreter) {
|
2010-09-28 15:46:30 +02:00
|
|
|
$data = $interpreter->getValues($this->request->getParameter('tuples'), $this->request->getParameter('group'));
|
|
|
|
|
|
|
|
$this->json['data'][] = array(
|
|
|
|
'uuid' => $interpreter->getUuid(),
|
|
|
|
'count' => count($data),
|
|
|
|
'min' => $interpreter->getMin(),
|
|
|
|
'max' => $interpreter->getMax(),
|
|
|
|
'average' => $interpreter->getAverage(),
|
|
|
|
'tuples' => $data
|
|
|
|
);
|
2010-09-25 00:42:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function addArray($data) {
|
|
|
|
foreach ($data as $index => $value) {
|
|
|
|
$this->json[$index] = $value;
|
|
|
|
}
|
2010-09-22 02:27:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts entity to array for json_encode()
|
|
|
|
*
|
|
|
|
* @param Model\Entity $entity
|
|
|
|
* @return array
|
|
|
|
*/
|
2010-09-04 01:28:29 +02:00
|
|
|
protected static function convertEntity(Model\Entity $entity) {
|
|
|
|
$jsonEntity = array();
|
|
|
|
$jsonEntity['uuid'] = (string) $entity->getUuid();
|
2010-09-22 02:27:43 +02:00
|
|
|
$jsonEntity['type'] = $entity->getType();
|
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
|
2010-09-19 20:47:59 +02:00
|
|
|
foreach ($entity->getProperties() as $key => $value) {
|
|
|
|
$jsonEntity[$key] = $value;
|
2010-09-04 01:28:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $jsonEntity;
|
|
|
|
}
|
|
|
|
|
2010-09-22 02:27:43 +02:00
|
|
|
/**
|
|
|
|
* Converts aggregator to array for json_encode
|
|
|
|
*
|
|
|
|
* @param Model\Aggregator $aggregator
|
|
|
|
* @param boolean $recursive
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected static function convertAggregator(Model\Aggregator $aggregator) {
|
2010-09-04 01:28:29 +02:00
|
|
|
$jsonAggregator = self::convertEntity($aggregator);
|
2010-07-24 13:03:34 +02:00
|
|
|
|
2010-09-22 20:54:51 +02:00
|
|
|
foreach ($aggregator->getChildren() as $entity) {
|
|
|
|
if ($entity instanceof Model\Channel) {
|
2010-10-01 11:23:31 +02:00
|
|
|
$jsonAggregator['children'][] = self::convertEntity($entity);
|
2010-09-22 20:54:51 +02:00
|
|
|
}
|
|
|
|
elseif ($entity instanceof Model\Aggregator) {
|
2010-10-01 11:23:31 +02:00
|
|
|
$jsonAggregator['children'][] = self::convertAggregator($entity);
|
2010-09-22 20:54:51 +02:00
|
|
|
}
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-08-28 03:27:14 +02:00
|
|
|
return $jsonAggregator;
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-25 00:42:07 +02:00
|
|
|
public function add($data) {
|
|
|
|
if ($data instanceof Util\JSON || is_array($data)) {
|
|
|
|
$this->addArray($data);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
parent::add($data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-04 01:28:29 +02:00
|
|
|
/*
|
|
|
|
* Setter & getter
|
|
|
|
*/
|
|
|
|
public function setPadding($padding) { $this->padding = $padding; }
|
2010-07-18 17:12:00 +02:00
|
|
|
}
|
|
|
|
|
2010-09-18 01:48:59 +02:00
|
|
|
?>
|