diff --git a/backend/lib/Controller/ChannelController.php b/backend/lib/Controller/ChannelController.php
index d719c17..0336e30 100644
--- a/backend/lib/Controller/ChannelController.php
+++ b/backend/lib/Controller/ChannelController.php
@@ -33,19 +33,23 @@ use \Volkszaehler\Model;
*/
class ChannelController extends Controller {
- // TODO authentification/indentification
+ /**
+ * get channels by filter
+ *
+ * @todo authentification/indentification
+ */
public function get() {
$dql = 'SELECT c FROM Volkszaehler\Model\Channel c';
- if ($this->view->request->getParameter('uuid')) {
+ if ($uuid = $this->view->request->getParameter('uuid')) {
// TODO add conditions
}
- if ($this->view->request->getParameter('ugid')) {
+ if ($ugid = $this->view->request->getParameter('ugid')) {
// TODO add conditions
}
- if ($this->view->request->getParameter('indicator')) {
+ if ($indicator = $this->view->request->getParameter('indicator')) {
// TODO add conditions
}
@@ -53,7 +57,7 @@ class ChannelController extends Controller {
$channels = $q->getResult();
foreach ($channels as $channel) {
- $this->view->add($channel);
+ $this->view->addChannel($channel);
}
}
@@ -63,7 +67,7 @@ class ChannelController extends Controller {
* @todo validate input and throw exceptions
*/
public function add() {
- $channel = new Model\Channel\Meter($this->view->request->getParameter('indicator'));
+ $channel = new Model\Channel($this->view->request->getParameter('indicator'));
$channel->setName($this->view->request->getParameter('name'));
$channel->setDescription($this->view->request->getParameter('description'));
@@ -74,7 +78,7 @@ class ChannelController extends Controller {
$this->em->persist($channel);
$this->em->flush();
- $this->view->add($channel);
+ $this->view->addChannel($channel);
}
/**
@@ -84,7 +88,7 @@ class ChannelController extends Controller {
*/
public function delete() {
$ucid = $this->view->request->getParameter('ucid');
- $channel = $this->em->getRepository('Volkszaehler\Model\Channel\Channel')->findOneBy(array('uuid' => $ucid));
+ $channel = $this->em->getRepository('Volkszaehler\Model\Channel')->findOneBy(array('uuid' => $ucid));
$this->em->remove($channel);
$this->em->flush();
diff --git a/backend/lib/Controller/Controller.php b/backend/lib/Controller/Controller.php
index 2a51f89..f660cc6 100644
--- a/backend/lib/Controller/Controller.php
+++ b/backend/lib/Controller/Controller.php
@@ -48,7 +48,7 @@ abstract class Controller {
*/
public function run($action) {
if (!method_exists($this, $action)) {
- throw new \InvalidArgumentException('\'' . $action . '\' is not a valid controller action');
+ throw new \Exception('\'' . $action . '\' is not a valid controller action');
}
$this->$action();
diff --git a/backend/lib/Controller/DataController.php b/backend/lib/Controller/DataController.php
index 4f9a30a..94dbfcf 100644
--- a/backend/lib/Controller/DataController.php
+++ b/backend/lib/Controller/DataController.php
@@ -50,7 +50,7 @@ class DataController extends Controller {
foreach ($channels as $channel) {
$interpreter = $channel->getInterpreter($this->em);
- $this->view->add($channel, $interpreter->getValues($from, $to, $groupBy));
+ $this->view->addChannel($channel, $interpreter->getValues($from, $to, $groupBy));
}
}
diff --git a/backend/lib/Controller/GroupController.php b/backend/lib/Controller/GroupController.php
index 1340b11..366976c 100644
--- a/backend/lib/Controller/GroupController.php
+++ b/backend/lib/Controller/GroupController.php
@@ -29,29 +29,53 @@ namespace Volkszaehler\Controller;
* @author Steffen Vogel (info@steffenvogel.de)
* @package default
*/
+use Volkszaehler\Model;
+
class GroupController extends Controller {
/**
- *
+ * get groups by filter
*/
public function get() {
- // TODO get groups from entitymanager according to API specs
+ $dql = 'SELECT g FROM Volkszaehler\Model\Group g';
+
+ $recursion = $this->view->request->getParameter('recursion');
+
+ if ($uuid = $this->view->request->getParameter('uuid')) {
+ // TODO add conditions
+ }
+
+ if ($ugid = $this->view->request->getParameter('ugid')) {
+ // TODO add conditions
+ }
+
+ $q = $this->em->createQuery($dql);
+ $groups = $q->getResult();
foreach ($groups as $group) {
- $this->view->addGroup($group);
+ $this->view->addGroup($group, $recursion);
}
}
/**
- *
+ * add new group as child of a parent group
*/
public function add() {
- $group = new Group();
+ $ugid = $this->view->request->getParameter('ugid');
+ $parent = $this->em->getRepository('Volkszaehler\Model\Group')->findOneBy(array('uuid' => $ugid));
- $group->name = $this->view->request->getParameter('name');
- $group->description = $this->view->request->getParameter('description');
+ if ($parent === FALSE) {
+ throw new \Exception('every group needs a parent');
+ }
+
+ $group = new Model\Group();
+
+ $group->setName($this->view->request->getParameter('name'));
+ $group->setDescription($this->view->request->getParameter('description'));
$this->em->persist($group);
+ $parent->addGroup($group);
+
$this->em->flush();
$this->view->add($group);
@@ -61,7 +85,8 @@ class GroupController extends Controller {
* @todo authentification/indentification
*/
public function delete() {
- $group = Group::getByUuid($this->view->request->getParameter('ugid'));
+ $ugid = $this->view->request->getParameter('ugid');
+ $group = $this->em->getRepository('Volkszaehler\Model\Group')->findOneBy(array('uuid' => $ugid));
$this->em->remove($group);
$this->em->flush();
@@ -74,7 +99,7 @@ class GroupController extends Controller {
*/
public function edit() {
-}
+ }
}
?>
\ No newline at end of file
diff --git a/backend/lib/Dispatcher.php b/backend/lib/Dispatcher.php
index b86bbc3..3e4649e 100644
--- a/backend/lib/Dispatcher.php
+++ b/backend/lib/Dispatcher.php
@@ -75,33 +75,38 @@ class Dispatcher {
$request = new HTTP\Request();
$response = new HTTP\Response();
- $format = ($request->getParameter('format')) ? $request->getParameter('format') : 'json'; // default view
- $controller = $request->getParameter('controller');
+ if (!($format = $request->getParameter('format'))) {
+ $format = 'json'; // default view
+ }
+
+ if (!($controller = $request->getParameter('controller'))) {
+ throw new \Exception('no controller specified');
+ }
// initialize entity manager
$this->em = Dispatcher::createEntityManager();
- // staring debugging
- if (($request->getParameter('debug') && $request->getParameter('debug') > 0) || Util\Configuration::read('debug')) {
+ // starting debugging
+ if (($debug = $request->getParameter('debug')) && (int) $debug > 0) {
$this->debug = new Util\Debug($request->getParameter('debug'));
$this->em->getConnection()->getConfiguration()->setSQLLogger($this->debug);
}
+ // TODO debug controll via configuration file
// initialize view
switch ($format) {
case 'png':
case 'jpeg':
case 'gif':
- $this->view = new View\JpGraphView($request, $response, $format);
+ $this->view = new View\JpGraph($request, $response, $format);
break;
case 'json':
case 'xml':
- $controller = 'channel';
case 'csv':
- $viewClassName = 'Volkszaehler\View\\' . strtoupper($format) . '\\' . strtoupper($format) . ucfirst($controller) . 'View';
+ $viewClassName = 'Volkszaehler\View\\' . strtoupper($format);
if (!(Util\ClassLoader::classExists($viewClassName)) || !is_subclass_of($viewClassName, '\Volkszaehler\View\View')) {
- throw new \InvalidArgumentException('\'' . $viewClassName . '\' is not a valid View');
+ throw new \Exception('\'' . $viewClassName . '\' is not a valid View');
}
$this->view = new $viewClassName($request, $response);
@@ -114,9 +119,9 @@ class Dispatcher {
}
// initialize controller
- $controllerClassName = 'Volkszaehler\Controller\\' . ucfirst(strtolower($request->getParameter('controller'))) . 'Controller';
+ $controllerClassName = 'Volkszaehler\Controller\\' . ucfirst($request->getParameter('controller')) . 'Controller';
if (!(Util\ClassLoader::classExists($controllerClassName)) || !is_subclass_of($controllerClassName, '\Volkszaehler\Controller\Controller')) {
- throw new \InvalidArgumentException('\'' . $controllerClassName . '\' is not a valid controller');
+ throw new \Exception('\'' . $controllerClassName . '\' is not a valid controller');
}
$this->controller = new $controllerClassName($this->view, $this->em);
}
@@ -141,7 +146,7 @@ class Dispatcher {
$this->view->addDebug($this->debug);
}
- $this->view->render(); // render view & send http response
+ $this->view->sendResponse(); // render view & send http response
}
/**
diff --git a/backend/lib/View/CSV/CSVView.php b/backend/lib/View/CSV.php
similarity index 63%
rename from backend/lib/View/CSV/CSVView.php
rename to backend/lib/View/CSV.php
index 71a8884..81fa677 100644
--- a/backend/lib/View/CSV/CSVView.php
+++ b/backend/lib/View/CSV.php
@@ -21,10 +21,9 @@
* along with volkszaehler.org. If not, see .
*/
-namespace Volkszaehler\View\CSV;
+namespace Volkszaehler\View;
use Volkszaehler\View\HTTP;
-use Volkszaehler\View;
use Volkszaehler\Util;
/**
@@ -35,11 +34,7 @@ use Volkszaehler\Util;
* @author Steffen Vogel
* @package default
*/
-abstract class CSVView extends View\View {
- protected $csv = array();
- protected $header = array();
- protected $footer = array();
-
+class CSV extends View {
protected $delimiter = ';';
protected $enclosure = '"';
@@ -49,33 +44,57 @@ abstract class CSVView extends View\View {
public function __construct(HTTP\Request $request, HTTP\Response $response) {
parent::__construct($request, $response);
- $this->header[] = 'source: volkszaehler.org';
- $this->header[] = 'version: ' . \Volkszaehler\VERSION;
+ echo 'source: volkszaehler.org' . PHP_EOL;
+ echo 'version: ' . \Volkszaehler\VERSION . PHP_EOL;
$this->response->setHeader('Content-type', 'text/csv');
$this->response->setHeader('Content-Disposition', 'attachment; filename="data.csv"');
}
- public function render() {
- foreach ($this->header as $line) {
- echo $line . PHP_EOL;
+ public function addChannel(Model\Channel $channel, array $data = NULL) {
+ $this->csv = array_merge($this->csv, $data);
+ }
+
+ public function addGroup(Model\Group $group) {
+
+ }
+
+ public function addDebug(Util\Debug $debug) {
+
+ }
+
+ protected function renderResponse() {
+
+ }
+
+ protected function addException(\Exception $e) {
+
+ }
+
+ public function renderResponse() {
+ // channel data
+ foreach ($this->channels as $channel) {
+ foreach ($channel[1] as $reading) {
+ $array = array_map(array($this, 'escape'), );
+
+ echo implode($this->delimiter, $array) . PHP_EOL;
+ }
}
echo PHP_EOL;
- foreach ($this->csv as $array) {
- $array = array_map(array($this, 'escape'), $array);
+ // debug
+ echo 'time: ' . $debug->getExecutionTime() . PHP_EOL;
+ echo 'database: ' . Util\Configuration::read('db.driver') . PHP_EOL;
- echo implode($this->delimiter, $array) . PHP_EOL;
+ foreach ($debug->getMessages() as $message) {
+ echo 'message: ' . $message['message'] . PHP_EOL; // TODO add more information
}
- echo PHP_EOL;
-
- foreach ($this->footer as $line) {
- echo $line . PHP_EOL;
+ foreach ($debug->getQueries() as $query) {
+ echo 'query: ' . $query['sql'] . PHP_EOL;
+ echo ' parameters: ' . implode(', ', $query['parameters']) . PHP_EOL;
}
-
- parent::render();
}
protected function escape($value) {
@@ -89,24 +108,6 @@ abstract class CSVView extends View\View {
return (string) $value;
}
}
-
- public function addDebug(Util\Debug $debug) {
- $this->footer[] = 'time: ' . $debug->getExecutionTime();
- $this->footer[] = 'database: ' . Util\Configuration::read('db.driver');
-
- foreach ($debug->getMessages() as $message) {
- $this->footer[] = 'message: ' . $message['message']; // TODO add more information
- }
-
- foreach ($debug->getQueries() as $query) {
- $this->footer[] = 'query: ' . $query['sql'];
- $this->footer[] = ' parameters: ' . implode(', ', $query['parameters']);
- }
- }
-
- public function addException(\Exception $exception) {
- echo $exception;
- }
}
?>
\ No newline at end of file
diff --git a/backend/lib/View/CSV/CSVDataView.php b/backend/lib/View/CSV/CSVDataView.php
deleted file mode 100644
index 8d57859..0000000
--- a/backend/lib/View/CSV/CSVDataView.php
+++ /dev/null
@@ -1,38 +0,0 @@
-.
- */
-
-namespace Volkszaehler\View\CSV;
-
-/**
- * CSV data view
- *
- * @author Steffen Vogel
- * @package default
- */
-class CSVDataView extends CSVView {
- public function add($obj, $data) {
- $this->csv = array_merge($this->csv, $data);
- }
-}
-
-?>
diff --git a/backend/lib/View/JSON/JSONView.php b/backend/lib/View/JSON.php
similarity index 63%
rename from backend/lib/View/JSON/JSONView.php
rename to backend/lib/View/JSON.php
index a6953f4..6a98b36 100644
--- a/backend/lib/View/JSON/JSONView.php
+++ b/backend/lib/View/JSON.php
@@ -21,12 +21,11 @@
* along with volkszaehler.org. If not, see .
*/
-namespace Volkszaehler\View\JSON;
+namespace Volkszaehler\View;
use Volkszaehler\View\HTTP;
-
-use Volkszaehler\View;
use Volkszaehler\Util;
+use Volkszaehler\Model;
/**
* JSON view
@@ -36,9 +35,11 @@ use Volkszaehler\Util;
* @package default
* @author Steffen Vogel
*/
-abstract class JSONView extends View\View {
+class JSON extends View {
protected $json = array();
+ protected $padding = FALSE;
+
/**
* constructor
*/
@@ -51,16 +52,89 @@ abstract class JSONView extends View\View {
$this->response->setHeader('Content-type', 'application/json');
}
- public function render() {
+ public function setPadding($padding) { $this->padding = $padding; }
+
+ public function addChannel(Model\Channel $channel, array &$data = NULL) {
+ $jsonChannel['uuid'] = (string) $channel->getUuid();
+ $jsonChannel['indicator'] = $channel->getIndicator();
+ $jsonChannel['unit'] = $channel->getUnit();
+ $jsonChannel['name'] = $channel->getName();
+ $jsonChannel['description'] = $channel->getDescription();
+ $jsonChannel['resolution'] = (int) $channel->getResolution();
+ $jsonChannel['cost'] = (float) $channel->getCost();
+
+ if (isset($data)) {
+ $jsonChannel['data'] = array();
+
+ foreach ($data as $reading) {
+ $jsonChannel['data'][] = array($reading['timestamp'], $reading['value'], $reading['count']);
+ }
+ }
+
+ $this->json['channels'][] = $jsonChannel;
+ }
+
+ public function addGroup(Model\Group $group, $recursive = FALSE) {
+ $this->json['groups'][] = $this->toJson($group, $recursive);
+ }
+
+ 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()
+ )
+ );
+ }
+
+ protected function addException(\Exception $exception) {
+ $this->json['exception'] = array(
+ 'type' => get_class($exception),
+ 'message' => $exception->getMessage(),
+ 'code' => $exception->getCode(),
+ 'file' => $exception->getFile(),
+ 'line' => $exception->getLine(),
+ 'trace' => $exception->getTrace()
+ );
+ }
+
+ protected function toJson(Model\Group $group, $recursive = FALSE) {
+ $jsonGroup = array();
+
+ $jsonGroup['uuid'] = (string) $group->getUuid();
+ $jsonGroup['name'] = $group->getName();
+ $jsonGroup['description'] = $group->getDescription();
+ $jsonGroup['channels'] = array();
+
+ foreach ($group->getChannels() as $channel) {
+ $jsonGroup['channels'][] = (string) $channel->getUuid();
+ }
+
+ if ($recursive) {
+ $jsonGroup['children'] = array();
+
+ foreach ($group->getSubGroups() as $subGroup) {
+ $jsonGroup['children'][] = $this->toJson($subGroup); // recursion
+ }
+ }
+
+ return $jsonGroup;
+ }
+
+ public function renderResponse() {
$json = json_encode($this->json);
if (Util\Debug::isActivated()) {
$json = self::format($json);
}
- echo $json;
+ if ($this->padding) {
+ $json = $this->padding . '(' . $json . ')';
+ }
- parent::render();
+ echo $json;
}
protected static function format($json) {
@@ -112,28 +186,6 @@ abstract class JSONView extends View\View {
return $formatted;
}
-
- 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()
- )
- );
- }
-
- public function addException(\Exception $exception) {
- $this->json['exception'] = array(
- 'type' => get_class($exception),
- 'message' => $exception->getMessage(),
- 'code' => $exception->getCode(),
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'trace' => $exception->getTrace()
- );
- }
}
?>
\ No newline at end of file
diff --git a/backend/lib/View/JSON/JSONChannelView.php b/backend/lib/View/JSON/JSONChannelView.php
deleted file mode 100644
index cdc5dd8..0000000
--- a/backend/lib/View/JSON/JSONChannelView.php
+++ /dev/null
@@ -1,60 +0,0 @@
-.
- */
-
-namespace Volkszaehler\View\JSON;
-
-/**
- * JSON channel view
- *
- * also used for data
- *
- * @author Steffen Vogel
- * @package default
- */
-class JSONChannelView extends JSONView {
-
- public function add(\Volkszaehler\Model\Channel $obj, array $data = NULL) {
- $channel['uuid'] = (string) $obj->getUuid();
- $channel['indicator'] = $obj->getIndicator();
- $channel['unit'] = $obj->getUnit();
- $channel['name'] = $obj->getName();
- $channel['description'] = $obj->getDescription();
-
- // TODO adapt to new indicator style
- if (is_subclass_of($obj, '\Volkszaehler\Model\Channel\Meter')) {
- $channel['resolution'] = (int) $obj->getResolution();
- $channel['cost'] = (float) $obj->getCost();
- }
-
- if (isset($data)) {
- $channel['data'] = array();
- foreach ($data as $reading) {
- $channel['data'][] = array($reading['timestamp'], $reading['value'], $reading['count']);
- }
- }
-
- $this->json['channels'][] = $channel;
- }
-}
-
-?>
\ No newline at end of file
diff --git a/backend/lib/View/JSON/JSONGroupView.php b/backend/lib/View/JSON/JSONGroupView.php
deleted file mode 100644
index ef204fd..0000000
--- a/backend/lib/View/JSON/JSONGroupView.php
+++ /dev/null
@@ -1,52 +0,0 @@
-.
- */
-
-namespace Volkszaehler\View\JSON;
-
-/**
- * JSON group view
- *
- * @author Steffen Vogel
- * @package default
- */
-class JSONGroupView extends JSONView {
-
- public function add(\Volkszaehler\Model\Group $obj, $recursive = FALSE) {
- $group['uuid'] = (string) $obj->getUuid();
- $group['name'] = $obj->getName();
- $group['description'] = $obj->getDescription();
-
- if ($recursive) { // TODO add nested groups in json view
- $children = $obj->getChildren();
-
- foreach ($children as $child) {
- $this->addGroup($child, $recursive);
- }
- }
-
- $this->json['groups'][] = $group;
- }
-}
-
-?>
-
diff --git a/backend/lib/View/JpGraphView.php b/backend/lib/View/JpGraph.php
similarity index 82%
rename from backend/lib/View/JpGraphView.php
rename to backend/lib/View/JpGraph.php
index bc376f8..5127ab7 100644
--- a/backend/lib/View/JpGraphView.php
+++ b/backend/lib/View/JpGraph.php
@@ -23,6 +23,9 @@
namespace Volkszaehler\View;
+use Volkszaehler\Model;
+use Volkszaehler\Util;
+
require_once \Volkszaehler\BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph.php';
require_once \Volkszaehler\BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph_scatter.php';
require_once \Volkszaehler\BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph_date.php';
@@ -37,7 +40,7 @@ require_once \Volkszaehler\BACKEND_DIR . '/lib/vendor/JpGraph/jpgraph_date.php';
* @link http://jpgraph.net/
* @todo add caching
*/
-class JpGraphView extends View {
+class JpGraph extends View {
/**
* indicator => ynaxis[n] mapping
*/
@@ -90,7 +93,7 @@ class JpGraphView extends View {
* @param $obj
* @param $data
*/
- public function add(\Volkszaehler\Model\Channel $obj, array $data) {
+ public function addChannel(Model\Channel $channel, array $data = NULL){
$count = count($this->channels);
$xData = $yData = array();
foreach ($data as $reading) {
@@ -101,7 +104,7 @@ class JpGraphView extends View {
// Create the scatter plot
$plot = new \ScatterPlot($yData, $xData);
- $plot->setLegend($obj->getName() . ': ' . $obj->getDescription() . ' [' . $obj->getUnit() . ']');
+ $plot->setLegend($channel->getName() . ': ' . $channel->getDescription() . ' [' . $channel->getUnit() . ']');
$plot->SetLinkPoints(TRUE, self::$colors[$count]);
$plot->mark->SetColor(self::$colors[$count]);
@@ -109,7 +112,7 @@ class JpGraphView extends View {
$plot->mark->SetType(MARK_DIAMOND);
$plot->mark->SetWidth(1);
- $axis = $this->getAxisIndex($obj);
+ $axis = $this->getAxisIndex($channel);
if ($axis >= 0) {
$this->graph->AddY($axis, $plot);
}
@@ -117,7 +120,31 @@ class JpGraphView extends View {
$this->graph->Add($plot);
}
- $this->channels[] = $obj;
+ $this->channels[] = $channel;
+ }
+
+ /**
+ * adds all channel of group as new plots to the graph
+ *
+ * @param Model\Group $group
+ */
+ public function addGroup(Model\Group $group) {
+ foreach ($group->getChannels() as $child) {
+ $this->addChannel($child);
+ }
+ }
+
+ public function addDebug(Util\Debug $debug) {
+ throw new \Exception(get_class($this) . ' cant show debugging information');
+ }
+
+ /**
+ * shows exception
+ * @todo avoid graph plotting and set content-type to text/plain
+ * @param \Exception $exception
+ */
+ protected function addException(\Exception $exception) {
+ echo $exception;
}
/**
@@ -157,13 +184,11 @@ class JpGraphView extends View {
*
* headers has been set automatically
*/
- public function render() {
+ protected function renderResponse() {
$this->graph->SetMargin(75, (count($this->axes) - 1) * 65 + 10, 20, 90);
// Display the graph
$this->graph->Stroke();
-
- parent::render();
}
}
diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php
index dc3c86c..f45e6c2 100644
--- a/backend/lib/View/View.php
+++ b/backend/lib/View/View.php
@@ -23,9 +23,22 @@
namespace Volkszaehler\View;
+use Volkszaehler\Model;
use Volkszaehler\View\HTTP;
use Volkszaehler\Util;
+/**
+ * Interface for all View classes
+ *
+ * @author Steffen Vogel
+ * @package default
+ */
+interface ViewInterface {
+ public function addChannel(Model\Channel $channel, array $data = NULL);
+ function addGroup(Model\Group $group);
+ function addDebug(Util\Debug $debug);
+}
+
/**
* superclass for all view classes
*
@@ -33,7 +46,7 @@ use Volkszaehler\Util;
* @author Steffen Vogel
*
*/
-abstract class View {
+abstract class View implements ViewInterface {
public $request;
protected $response;
@@ -56,26 +69,21 @@ abstract class View {
final public function exceptionHandler(\Exception $exception) {
$this->addException($exception);
- //$this->status = STATUS_EXCEPTION; // TODO add status reporting to API
-
$code = ($exception->getCode() == 0 && HTTP\Response::getCodeDescription($exception->getCode())) ? 400 : $exception->getCode();
$this->response->setCode($code);
- $this->render();
+ $this->renderResponse();
die();
}
- public function render() {
+ public function sendResponse() {
+ $this->renderResponse();
+
$this->response->send();
}
- public function addException(\Exception $e) {
- echo $e;
- }
-
- public function addDebug(Util\Debug $debug) {
-
- }
+ protected abstract function renderResponse();
+ protected abstract function addException(\Exception $exception);
}
?>
diff --git a/backend/lib/View/XML/XMLView.php b/backend/lib/View/XML.php
similarity index 63%
rename from backend/lib/View/XML/XMLView.php
rename to backend/lib/View/XML.php
index 815ab46..641e0b3 100644
--- a/backend/lib/View/XML/XMLView.php
+++ b/backend/lib/View/XML.php
@@ -21,10 +21,9 @@
* along with volkszaehler.org. If not, see .
*/
-namespace Volkszaehler\View\XML;
+namespace Volkszaehler\View;
use Volkszaehler\View\HTTP;
-use Volkszaehler\View;
use Volkszaehler\Util;
/**
@@ -33,10 +32,10 @@ use Volkszaehler\Util;
* @author Steffen Vogel
* @package default
*/
-abstract class XMLView extends View\View {
+class XML extends View {
protected $xmlDoc;
- public function __construct(HTTP\Request $request, HTTP\Response $response) {
+ public function __construct(HTTP\Request $request, HTTP\Response $response) {
parent::__construct($request, $response);
$this->xmlDoc = new \DOMDocument('1.0', 'UTF-8');
@@ -49,22 +48,45 @@ abstract class XMLView extends View\View {
$this->response->setHeader('Content-type', 'application/xml; charset=UTF-8');
}
- public function render() {
- $this->xmlDoc->appendChild($this->xmlRoot);
- echo $this->xmlDoc->saveXML();
+ public function addChannel(Model\Channel $channel, array $data = NULL) {
+ $xmlChannel = $this->xmlDoc->createElement('channel');
+ $xmlChannel->setAttribute('uuid', $obj->getUuid());
- parent::render();
+ $xmlChannel->appendChild($this->xmlDoc->createElement('indicator', $obj->getIndicator()));
+ $xmlChannel->appendChild($this->xmlDoc->createElement('unit', $obj->getUnit()));
+ $xmlChannel->appendChild($this->xmlDoc->createElement('name', $obj->getName()));
+ $xmlChannel->appendChild($this->xmlDoc->createElement('description', $obj->getDescription()));
+ $xmlChannel->appendChild($this->xmlDoc->createElement('resolution', (int) $obj->getResolution()));
+ $xmlChannel->appendChild($this->xmlDoc->createElement('cost', (float) $obj->getCost()));
+
+ if (isset($data)) {
+ $xmlData = $this->xmlDoc->createElement('data');
+
+ foreach ($data as $reading) {
+ $xmlReading = $this->xmlDoc->createElement('reading');
+
+ $xmlReading->setAttribute('timestamp', $reading['timestamp']); // hardcoded data fields for performance optimization
+ $xmlReading->setAttribute('value', $reading['value']);
+ $xmlReading->setAttribute('count', $reading['count']);
+
+ $xmlData->appendChild($xmlReading);
+ }
+
+ $xmlChannel->appendChild($xmlData);
+ }
+
+ $this->xml->appendChild($xmlChannel);
}
- public function addException(\Exception $exception) {
- $xmlException = $this->xmlDoc->createElement('exception');
- $xmlException->setAttribute('code', $exception->getCode());
- $xmlException->appendChild($this->xmlDoc->createElement('message', $exception->getMessage()));
- $xmlException->appendChild($this->xmlDoc->createElement('line', $exception->getLine()));
- $xmlException->appendChild($this->xmlDoc->createElement('file', $exception->getFile()));
- $xmlException->appendChild($this->fromTrace($exception->getTrace()));
+ public function addGroup(Model\Group $group) {
+ $xmlGroup = $this->xmlDoc->createElement('group');
+ $xmlGroup->setAttribute('uuid', $obj->getUuid());
+ $xmlGroup->appendChild($this->xmlDoc->createElement('name', $obj->getName()));
+ $xmlGroup->appendChild($this->xmlDoc->createElement('description', $obj->getDescription()));
- $this->xmlRoot->appendChild($xmlException);
+ // TODO include sub groups?
+
+ $this->xml->appendChild($xmlGroup);
}
public function addDebug(Util\Debug $debug) {
@@ -79,6 +101,21 @@ abstract class XMLView extends View\View {
$this->xmlRoot->appendChild($xmlDebug);
}
+ protected function addException(\Exception $e) {
+ $xmlException = $this->xmlDoc->createElement('exception');
+ $xmlException->setAttribute('code', $exception->getCode());
+ $xmlException->appendChild($this->xmlDoc->createElement('message', $exception->getMessage()));
+ $xmlException->appendChild($this->xmlDoc->createElement('line', $exception->getLine()));
+ $xmlException->appendChild($this->xmlDoc->createElement('file', $exception->getFile()));
+ $xmlException->appendChild($this->fromTrace($exception->getTrace()));
+
+ $this->xmlRoot->appendChild($xmlException);
+ }
+
+ protected function renderResponse() {
+
+ }
+
private function fromTrace($traces) {
$xmlTraces = $this->xmlDoc->createElement('backtrace');
diff --git a/backend/lib/View/XML/XMLChannelView.php b/backend/lib/View/XML/XMLChannelView.php
deleted file mode 100644
index 44b03d9..0000000
--- a/backend/lib/View/XML/XMLChannelView.php
+++ /dev/null
@@ -1,81 +0,0 @@
-.
- */
-
-namespace Volkszaehler\View\XML;
-
-use Volkszaehler\View\HTTP;
-
-/**
- * XML channel view
- *
- * also used for data
- *
- * @author Steffen Vogel
- * @package default
- */
-class XMLChannelView extends XMLView {
-
- public function __construct(HTTP\Request $request, HTTP\Response $response) {
- parent::__construct($request, $response);
-
- $this->xml = $this->xmlDoc->createElement('channels');
- }
-
- public function add(\Volkszaehler\Model\Channel $obj, array $data = NULL) {
- $xmlChannel = $this->xmlDoc->createElement('channel');
- $xmlChannel->setAttribute('uuid', $obj->getUuid());
-
- $xmlChannel->appendChild($this->xmlDoc->createElement('indicator', $obj->getIndicator()));
- $xmlChannel->appendChild($this->xmlDoc->createElement('unit', $obj->getUnit()));
- $xmlChannel->appendChild($this->xmlDoc->createElement('name', $obj->getName()));
- $xmlChannel->appendChild($this->xmlDoc->createElement('description', $obj->getDescription()));
- $xmlChannel->appendChild($this->xmlDoc->createElement('resolution', (int) $obj->getResolution()));
- $xmlChannel->appendChild($this->xmlDoc->createElement('cost', (float) $obj->getCost()));
-
- if (isset($data)) {
- $xmlData = $this->xmlDoc->createElement('data');
-
- foreach ($data as $reading) {
- $xmlReading = $this->xmlDoc->createElement('reading');
-
- $xmlReading->setAttribute('timestamp', $reading['timestamp']); // hardcoded data fields for performance optimization
- $xmlReading->setAttribute('value', $reading['value']);
- $xmlReading->setAttribute('count', $reading['count']);
-
- $xmlData->appendChild($xmlReading);
- }
-
- $xmlChannel->appendChild($xmlData);
- }
-
- $this->xml->appendChild($xmlChannel);
- }
-
- public function render() {
- $this->xmlRoot->appendChild($this->xml);
-
- parent::render();
- }
-}
-
-?>
diff --git a/backend/lib/View/XML/XMLGroupView.php b/backend/lib/View/XML/XMLGroupView.php
deleted file mode 100644
index 8f63807..0000000
--- a/backend/lib/View/XML/XMLGroupView.php
+++ /dev/null
@@ -1,61 +0,0 @@
-.
- */
-
-namespace Volkszaehler\View\XML;
-
-use Volkszaehler\View\HTTP;
-
-/**
- * XML group view
- *
- * @author Steffen Vogel
- * @package default
- */
-class XMLGroupView extends XMLView {
- protected $xml;
-
- public function __construct(HTTP\Request $request, HTTP\Request $response) {
- parent::__construct($request, $response);
-
- $this->xml = $this->xmlDoc->createElement('groups');
- }
-
- public function add(\Volkszaehler\Model\Group $obj) {
- $xmlGroup = $this->xmlDoc->createElement('group');
- $xmlGroup->setAttribute('uuid', $obj->getUuid());
- $xmlGroup->appendChild($this->xmlDoc->createElement('name', $obj->getName()));
- $xmlGroup->appendChild($this->xmlDoc->createElement('description', $obj->getDescription()));
-
- // TODO include sub groups?
-
- $this->xml->appendChild($xmlGroup);
- }
-
- public function render() {
- $this->xmlRoot->appendChild($this->xml);
-
- parent::render();
- }
-}
-
-?>