From 9e44e1385be0262aabe50b634fdb6dff0be810a0 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 20 Jul 2010 11:58:39 +0200 Subject: [PATCH] updated views --- backend/lib/View/Http/Response.php | 2 +- backend/lib/View/View.php | 2 +- backend/lib/View/Xml/Channel.php | 61 +++++++++++++++ backend/lib/View/Xml/Group.php | 42 ++++++++++ backend/lib/View/Xml/Xml.php | 120 +++++++---------------------- 5 files changed, 134 insertions(+), 93 deletions(-) create mode 100644 backend/lib/View/Xml/Channel.php create mode 100644 backend/lib/View/Xml/Group.php diff --git a/backend/lib/View/Http/Response.php b/backend/lib/View/Http/Response.php index 8104e11..b467ee0 100644 --- a/backend/lib/View/Http/Response.php +++ b/backend/lib/View/Http/Response.php @@ -86,7 +86,7 @@ class Response { public function send() { // change returncode - header('HTTP/1.1 ' . $this->code . ' ' . self::getCodeDescription($this->code)); // TODO untested + header('HTTP/1.1 ' . $this->code . ' ' . self::getCodeDescription($this->code)); // send headers foreach ($this->headers as $name => $value) { diff --git a/backend/lib/View/View.php b/backend/lib/View/View.php index 7081b47..ee0f685 100644 --- a/backend/lib/View/View.php +++ b/backend/lib/View/View.php @@ -56,7 +56,7 @@ abstract class View implements ViewInterface { $view = new JpGraph($request, $response, $format); } else { - if ($controller == 'data' && $format == 'json') { + if ($controller == 'data' && ($format == 'json' || $format == 'xml')) { $controller = 'channel'; } $viewClassName = 'Volkszaehler\View\\' . ucfirst($format) . '\\' . ucfirst($controller); diff --git a/backend/lib/View/Xml/Channel.php b/backend/lib/View/Xml/Channel.php new file mode 100644 index 0000000..07ca5f3 --- /dev/null +++ b/backend/lib/View/Xml/Channel.php @@ -0,0 +1,61 @@ + + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License (either version 2 or + * version 3) as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +namespace Volkszaehler\View\Xml; + +class Channel extends Xml { + + public function __construct(\Volkszaehler\View\Http\Request $request, \Volkszaehler\View\Http\Response $response) { + parent::__construct($request, $response); + + $this->xml = $this->xmlDoc->createElement('channels'); + } + + public function add(\Volkszaehler\Model\Channel $obj, $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 (!is_null($data) && is_array($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); + } +} \ No newline at end of file diff --git a/backend/lib/View/Xml/Group.php b/backend/lib/View/Xml/Group.php new file mode 100644 index 0000000..6eb5ead --- /dev/null +++ b/backend/lib/View/Xml/Group.php @@ -0,0 +1,42 @@ + + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License (either version 2 or + * version 3) as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +namespace Volkszaehler\View\Xml; + +class Group extends Xml { + + public function __construct(\Volkszaehler\View\Http\Request $request, \Volkszaehler\View\Http\Response $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('id', (int) $obj->id); + $xmlGroup->appendChild($this->xmlDoc->createElement('uuid', $obj->uuid)); + $xmlGroup->appendChild($this->xmlDoc->createElement('description', $obj->description)); + + // TODO include sub groups? + + $this->xml->appendChild($xmlGroup); + } +} \ No newline at end of file diff --git a/backend/lib/View/Xml/Xml.php b/backend/lib/View/Xml/Xml.php index 0c879ab..e19e505 100644 --- a/backend/lib/View/Xml/Xml.php +++ b/backend/lib/View/Xml/Xml.php @@ -19,110 +19,35 @@ * http://www.gnu.org/copyleft/gpl.html */ -namespace Volkszaehler\View; - +namespace Volkszaehler\View\Xml; // TODO outdated -class Xml extends View { - private $xmlDoc; - private $xml; - private $xmlChannels; - private $xmlUsers; - private $xmlGroups; +class Xml extends \Volkszaehler\View\View { + protected $xmlDoc; + protected $xml; - public function __construct() { - parent::__construct(); + public function __construct(\Volkszaehler\View\Http\Request $request, \Volkszaehler\View\Http\Response $response) { + parent::__construct($request, $response); - $config = Registry::get('config'); + $this->xmlDoc = new \DOMDocument('1.0', 'UTF-8'); - $this->xmlDoc = new DOMDocument('1.0', 'UTF-8'); + $this->xmlRoot = $this->xmlDoc->createElement('volkszaehler'); + $this->xmlRoot->setAttribute('version', \Volkszaehler\VERSION); - $this->xml = $this->xmlDoc->createElement('volkszaehler'); - $this->xml->setAttribute('version', VERSION); - $this->xmlChannels = $this->xmlDoc->createElement('channels'); - $this->xmlUsers = $this->xmlDoc->createElement('users'); - $this->xmlGroups = $this->xmlDoc->createElement('groups'); + $this->xmlRoot->appendChild($this->xmlDoc->createElement('source', 'volkszaehler.org')); - $this->xml->appendChild($this->xmlDoc->createElement('source', 'volkszaehler.org')); - $this->xml->appendChild($this->xmlDoc->createElement('storage', $config['db']['backend'])); - $this->xml->appendChild($this->xmlDoc->createElement('controller', $request->getParameter('controller'))); - $this->xml->appendChild($this->xmlDoc->createElement('action', $request->getParameter('action'))); - - $this->response->setHeader('Content-type', 'text/xml'); - } - - public function addChannel(\Volkszaehler\Model\Channel $obj, $data = NULL) { - $xmlChannel = $this->xmlDoc->createElement('channel'); - $xmlChannel->setAttribute('id', (int) $obj->id); - - $xmlChannel->appendChild($this->xmlDoc->createElement('uuid', $obj->uuid)); - $xmlChannel->appendChild($this->xmlDoc->createElement('type', $obj->type)); - $xmlChannel->appendChild($this->xmlDoc->createElement('unit', $obj::unit)); - $xmlChannel->appendChild($this->xmlDoc->createElement('description', $obj->description)); - $xmlChannel->appendChild($this->xmlDoc->createElement('resolution', (int) $obj->resolution)); - $xmlChannel->appendChild($this->xmlDoc->createElement('cost', (float) $obj->cost)); - - if (!is_null($data) && is_array($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->xmlChannels->appendChild($xmlChannel); - } - - public function addUser(\Volkszaehler\Model\User $obj) { - $xmlUser = $this->xmlDoc->createElement('user'); - $xmlUser->setAttribute('id', (int) $obj->id); - $xmlUser->appendChild($this->xmlDoc->createElement('uuid', $obj->uuid)); - - $this->xmlUsers->appendChild($xmlUser); - } - - public function addGroup(\Volkszaehler\Model\Group $obj) { - $xmlGroup = $this->xmlDoc->createElement('group'); - $xmlGroup->setAttribute('id', (int) $obj->id); - $xmlGroup->appendChild($this->xmlDoc->createElement('uuid', $obj->uuid)); - $xmlGroup->appendChild($this->xmlDoc->createElement('description', $obj->description)); - - // TODO include sub groups? - - $this->xmlGroups->appendChild($xmlGroup); + $this->response->setHeader('Content-type', 'application/xml; charset=UTF-8'); } public function render() { - $this->xml->appendChild($this->xmlDoc->createElement('time', $this->getTime())); + parent::render(); - // channels - if ($this->xmlChannels->hasChildNodes()) { - $this->xml->appendChild($this->xmlChannels); - } - - // users - if ($this->xmlUsers->hasChildNodes()) { - $this->xml->appendChild($this->xmlUsers); - } - - // groups - if ($this->xmlGroups->hasChildNodes()) { - $this->xml->appendChild($this->xmlGroups); - } - - $this->xmlDoc->appendChild($this->xml); + $this->xmlDoc->appendChild($this->xmlRoot); + $this->xmlRoot->appendChild($this->xml); echo $this->xmlDoc->saveXML(); } - protected function addException(\Exception $exception) { + public function addException(\Exception $exception) { $xmlException = $this->xmlDoc->createElement('exception'); $xmlException->setAttribute('code', $exception->getCode()); $xmlException->appendChild($this->xmlDoc->createElement('message', $exception->getMessage())); @@ -130,7 +55,20 @@ class Xml extends View { $xmlException->appendChild($this->xmlDoc->createElement('file', $exception->getFile())); $xmlException->appendChild($this->fromTrace($exception->getTrace())); - $this->xml->appendChild($xmlException); + $this->xmlRoot->appendChild($xmlException); + } + + public function addDebug() { + $config = \Volkszaehler\Util\Registry::get('config'); + + $xmlDebug = $this->xmlDoc->createElement('debug'); + + $xmlDebug->appendChild($this->xmlDoc->createElement('time', $this->getTime())); + $xmlDebug->appendChild($this->xmlDoc->createElement('database', $config['db']['driver'])); + + // TODO add queries + + $this->xmlRoot->appendChild($xmlDebug); } private function fromTrace($traces) {