updated views

This commit is contained in:
Steffen Vogel 2010-07-20 11:58:39 +02:00
parent 925faf8bc7
commit 9e44e1385b
5 changed files with 134 additions and 93 deletions

View file

@ -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) {

View file

@ -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);

View file

@ -0,0 +1,61 @@
<?php
/*
* Copyright (c) 2010 by Justin Otherguy <justin@justinotherguy.org>
*
* 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);
}
}

View file

@ -0,0 +1,42 @@
<?php
/*
* Copyright (c) 2010 by Justin Otherguy <justin@justinotherguy.org>
*
* 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);
}
}

View file

@ -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) {