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-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-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-07-25 23:15:49 +02:00
|
|
|
public function addChannel(Model\Channel $channel, array $data = NULL) {
|
2010-09-04 01:28:29 +02:00
|
|
|
$jsonChannel = self::convertEntity($channel);
|
2010-09-19 20:47:59 +02:00
|
|
|
$jsonChannel['type'] = $channel->getType();
|
2010-07-24 13:03:34 +02:00
|
|
|
|
|
|
|
if (isset($data)) {
|
2010-07-29 00:04:33 +02:00
|
|
|
$jsonChannel['data'] = self::convertData($data);
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 20:47:59 +02:00
|
|
|
$this->json['channel'] = $jsonChannel;
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
2010-08-28 03:27:14 +02:00
|
|
|
public function addAggregator(Model\Aggregator $aggregator, $recursive = FALSE) {
|
2010-09-19 20:47:59 +02:00
|
|
|
$this->json['group'] = self::convertAggregator($aggregator, $recursive);
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addDebug(Util\Debug $debug) {
|
|
|
|
$this->json['debug'] = array(
|
|
|
|
'time' => $debug->getExecutionTime(),
|
|
|
|
'messages' => $debug->getMessages(),
|
|
|
|
'database' => array(
|
|
|
|
'driver' => Util\Configuration::read('db.driver'),
|
|
|
|
'queries' => $debug->getQueries()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-04 01:28:29 +02:00
|
|
|
protected static function convertEntity(Model\Entity $entity) {
|
|
|
|
$jsonEntity = array();
|
|
|
|
$jsonEntity['uuid'] = (string) $entity->getUuid();
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function convertAggregator(Model\Aggregator $aggregator, $recursive = FALSE) {
|
|
|
|
$jsonAggregator = self::convertEntity($aggregator);
|
2010-07-24 13:03:34 +02:00
|
|
|
|
2010-08-28 03:27:14 +02:00
|
|
|
foreach ($aggregator->getChannels() as $channel) {
|
|
|
|
$jsonAggregator['channels'][] = (string) $channel->getUuid();
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($recursive) {
|
2010-09-19 20:47:59 +02:00
|
|
|
$jsonAggregator['groups'] = array();
|
2010-07-24 13:03:34 +02:00
|
|
|
|
2010-08-28 03:27:14 +02:00
|
|
|
foreach ($aggregator->getChildren() as $subAggregator) {
|
2010-09-19 20:47:59 +02:00
|
|
|
$jsonAggregator['groups'][] = $this->toJson($subAggregator, $recursive); // recursion
|
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-07-29 00:04:33 +02:00
|
|
|
protected static function convertData($data) {
|
2010-09-19 20:47:59 +02:00
|
|
|
array_walk($data, function(&$reading) {
|
|
|
|
$reading[1] = round($reading[1], View::PRECISSION);
|
|
|
|
});
|
2010-07-29 00:04:33 +02:00
|
|
|
|
2010-09-19 20:47:59 +02:00
|
|
|
return $data;
|
2010-07-29 00:04:33 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 20:47:59 +02:00
|
|
|
public function render() {
|
2010-08-24 15:33:55 +02:00
|
|
|
$json = $this->json->encode((Util\Debug::isActivated()) ? JSON_PRETTY : 0);
|
2010-07-20 21:27:53 +02:00
|
|
|
|
2010-07-24 13:03:34 +02:00
|
|
|
if ($this->padding) {
|
2010-07-28 00:42:28 +02:00
|
|
|
$json = 'if (self.' . $this->padding . ') { ' . $this->padding . '(' . $json . '); }';
|
2010-07-24 13:03:34 +02:00
|
|
|
}
|
2010-07-21 12:44:01 +02:00
|
|
|
|
2010-09-19 20:47:59 +02:00
|
|
|
$this->response->setHeader('Content-type', 'application/json');
|
2010-07-24 13:03:34 +02:00
|
|
|
echo $json;
|
2010-07-19 17:33:26 +02:00
|
|
|
}
|
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
|
|
|
?>
|