introduced DataController
reorganized Views
This commit is contained in:
parent
1dae152814
commit
d20ba8b589
8 changed files with 213 additions and 86 deletions
|
@ -21,55 +21,15 @@
|
|||
|
||||
class ChannelController extends Controller {
|
||||
public function get() {
|
||||
if ($this->view->request->get['data'] == 'channels' || $this->view->request->get['data'] == 'pulses') {
|
||||
$this->view->channels = array();
|
||||
|
||||
if ($this->view->request->get['data'] == 'channels') { // get all channels assigned to user
|
||||
$user = User::getByUuid($this->view->request->get['uuid']);
|
||||
$channels = $user->getChannels();
|
||||
}
|
||||
else {
|
||||
$ids = explode(',', trim($this->view->request->get['ids']));
|
||||
$channels = Channel::getByFilter(array('id' => $ids), true, false); // get all channels with id in $ids as an array
|
||||
|
||||
$from = (isset($this->view->request->get['from'])) ? (int) $this->view->request->get['from'] : NULL;
|
||||
$to = (isset($this->view->request->get['to'])) ? (int) $this->view->request->get['to'] : NULL;
|
||||
$groupBy = (isset($this->view->request->get['groupby'])) ? $this->view->request->get['groupby'] : NULL; // get all readings by default
|
||||
|
||||
$this->view->from = $from; // TODO use min max timestamps from Channel::getData()
|
||||
$this->view->to = $to;
|
||||
}
|
||||
|
||||
$jsonChannels = array();
|
||||
foreach ($channels as $channel) {
|
||||
$jsonChannel = $this->view->getChannel($channel);
|
||||
|
||||
if ($this->view->request->get['data'] == 'readings') {
|
||||
$jsonChannel['readings'] = array();
|
||||
|
||||
foreach ($channel->getPulses($from, $to, $groupBy) as $pulse) {
|
||||
$jsonChannel['readingsgi'][] = array($pulse['timestamp'], $pulse['value']);
|
||||
}
|
||||
}
|
||||
|
||||
$jsonChannels[] = $jsonChannel;
|
||||
}
|
||||
|
||||
$this->view->channels = $jsonChannels;
|
||||
$user = User::getByUuid($this->view->request->get['uuid']);
|
||||
$channels = $user->getChannels();
|
||||
|
||||
foreach ($channels as $channel) {
|
||||
$this->view->data['channels'][] = $this->view->getChannel($channel);
|
||||
}
|
||||
}
|
||||
|
||||
public function log() {
|
||||
$ucid = $this->view->request->get['ucid'];
|
||||
|
||||
$channel = Channel::getByUcid($ucid);
|
||||
|
||||
// TODO add channel if it doesn't exist (use $this->add)
|
||||
|
||||
$channel->addData($this->view->request->get);
|
||||
}
|
||||
|
||||
public function add($ucid) { // TODO rework
|
||||
public function add() {
|
||||
$channel = new Channel();
|
||||
$channel->ucid = $ucid;
|
||||
|
||||
|
@ -82,6 +42,16 @@ class ChannelController extends Controller {
|
|||
}
|
||||
|
||||
$channel->save();
|
||||
$this->view->data['channel'] = $this->view->getChannel($channel);
|
||||
}
|
||||
|
||||
public function delete() { // TODO untested
|
||||
$channel = Channel::getByUcid($this->view->request->get['ucid']);
|
||||
$channel->delete();
|
||||
}
|
||||
|
||||
public function edit() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
58
backend/lib/controller/datacontroller.php
Normal file
58
backend/lib/controller/datacontroller.php
Normal file
|
@ -0,0 +1,58 @@
|
|||
<?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
|
||||
*/
|
||||
|
||||
class DataController extends Controller {
|
||||
public function add() {
|
||||
$ucid = $this->view->request->get['ucid'];
|
||||
|
||||
$channel = Channel::getByUcid($ucid);
|
||||
|
||||
// TODO add channel if it doesn't exist (use $this->add)
|
||||
|
||||
$channel->addData($this->view->request->get);
|
||||
}
|
||||
|
||||
public function get() {
|
||||
$ids = explode(',', trim($this->view->request->get['ids']));
|
||||
$channels = Channel::getByFilter(array('id' => $ids), true, false); // get all channels with id in $ids as an array
|
||||
|
||||
$from = (isset($this->view->request->get['from'])) ? (int) $this->view->request->get['from'] : NULL;
|
||||
$to = (isset($this->view->request->get['to'])) ? (int) $this->view->request->get['to'] : NULL;
|
||||
$groupBy = (isset($this->view->request->get['groupby'])) ? $this->view->request->get['groupby'] : 400; // get all readings by default
|
||||
|
||||
$data['from'] = $from; // TODO use min max timestamps from Channel::getData()
|
||||
$data['to'] = $to;
|
||||
|
||||
$jsonChannels = array();
|
||||
foreach ($channels as $channel) {
|
||||
$jsonChannel = $this->view->getChannel($channel);
|
||||
|
||||
$jsonChannel['readings'] = array();
|
||||
|
||||
foreach ($channel->getPulses($from, $to, $groupBy) as $reading) {
|
||||
$jsonChannel['readings'][] = array($reading['timestamp'], $reading['value'], $reading['count']);
|
||||
}
|
||||
|
||||
$data['channels'][] = $jsonChannel;
|
||||
}
|
||||
$this->view->data += $data;
|
||||
}
|
||||
}
|
|
@ -44,8 +44,9 @@ final class FrontController {
|
|||
|
||||
public function run() {
|
||||
$action = $this->view->request->get['action'];
|
||||
|
||||
$this->controller->$action(); // run controllers actions (usually CRUD: http://de.wikipedia.org/wiki/CRUD)
|
||||
$this->view->render(); // send response
|
||||
$this->view->render(); // render view & send http response
|
||||
}
|
||||
}
|
||||
|
||||
|
|
36
backend/lib/controller/groupcontroller.php
Normal file
36
backend/lib/controller/groupcontroller.php
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?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
|
||||
*/
|
||||
|
||||
class GroupController extends Controller {
|
||||
public function add() {
|
||||
|
||||
}
|
||||
|
||||
public function delete() {
|
||||
|
||||
}
|
||||
|
||||
public function get() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -39,4 +39,12 @@ class UserController extends Controller {
|
|||
public function logout() {
|
||||
|
||||
}
|
||||
|
||||
public function subscribe() {
|
||||
|
||||
}
|
||||
|
||||
public function unsubscribe() {
|
||||
|
||||
}
|
||||
}
|
|
@ -20,48 +20,37 @@
|
|||
*/
|
||||
|
||||
class JsonView extends View {
|
||||
private $data = array();
|
||||
public $data = array();
|
||||
|
||||
public function __construct(HttpRequest $request, HttpResponse $response) {
|
||||
parent::__construct($request, $response);
|
||||
|
||||
$config = Registry::get('config');
|
||||
|
||||
$this->source = 'volkszaehler.org';
|
||||
$this->version = VZ_VERSION;
|
||||
$this->storage = $config['db']['backend'];
|
||||
$this->controller = $request->get['controller'];
|
||||
$this->action = $request->get['action'];
|
||||
$this->data['source'] = 'volkszaehler.org';
|
||||
$this->data['version'] = VZ_VERSION;
|
||||
$this->data['storage'] = $config['db']['backend'];
|
||||
$this->data['controller'] = $request->get['controller'];
|
||||
$this->data['action'] = $request->get['action'];
|
||||
|
||||
|
||||
$this->response->headers['Content-type'] = 'application/json';
|
||||
}
|
||||
|
||||
public function __set($key, $value) {
|
||||
$this->data[$key] = $value;
|
||||
}
|
||||
|
||||
public function __get($key) {
|
||||
return $this->data[$key];
|
||||
}
|
||||
|
||||
public function __isset($key) {
|
||||
return isset($this->data[$key]);
|
||||
$this->response->setHeader('Content-type', 'application/json');
|
||||
}
|
||||
|
||||
public function render() {
|
||||
$this->time = round(microtime(true) - $this->created, 4);
|
||||
$this->data['time'] = round(microtime(true) - $this->created, 4);
|
||||
echo json_encode($this->data);
|
||||
}
|
||||
|
||||
public function exceptionHandler(Exception $exception) {
|
||||
$this->exception = array('message' => $exception->getMessage(),
|
||||
'code' => $exception->getCode(),
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'trace' => $exception->getTrace()
|
||||
);
|
||||
$this->data['exception'] = array('message' => $exception->getMessage(),
|
||||
'code' => $exception->getCode(),
|
||||
'file' => $exception->getFile(),
|
||||
'line' => $exception->getLine(),
|
||||
'trace' => $exception->getTrace()
|
||||
);
|
||||
$this->data['status'] = 'exception';
|
||||
$this->render();
|
||||
die();
|
||||
}
|
||||
|
||||
public function getChannel(Channel $channel) { // TODO improve view interface
|
||||
|
|
|
@ -38,11 +38,14 @@ abstract class View implements ViewInterface {
|
|||
|
||||
// error & exception handling by view
|
||||
set_exception_handler(array($this, 'exceptionHandler'));
|
||||
set_error_handler(array($this, 'errorHandler'), E_ALL);
|
||||
set_error_handler(array($this, 'errorHandler'));
|
||||
}
|
||||
|
||||
final public function errorHandler($errno, $errstr, $errfile, $errline) {
|
||||
$this->exceptionHandler(new ErrorException($errstr, 0, $errno, $errfile, $errline));
|
||||
die();
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->response->send(); // send response
|
||||
}
|
||||
}
|
|
@ -20,20 +20,82 @@
|
|||
*/
|
||||
|
||||
class XmlView extends View {
|
||||
public $doc;
|
||||
|
||||
public function __construct(HttpRequest $request, HttpResponse $response) {
|
||||
parent::__construct($request, $response);
|
||||
|
||||
$config = Registry::get('config');
|
||||
|
||||
$this->doc = new DOMDocument('1.0', 'UTF-8');
|
||||
|
||||
$this->source = 'volkszaehler.org'; // TODO create XML
|
||||
$this->version = VZ_VERSION;
|
||||
$this->storage = $config['db']['backend'];
|
||||
$this->controller = $request->get['controller'];
|
||||
$this->action = $request->get['action'];
|
||||
|
||||
$this->response->headers['Content-type'] = 'application/json';
|
||||
}
|
||||
|
||||
// just moved from exception class
|
||||
// TODO implement exception handling
|
||||
public function toXml(DOMDocument $doc) {
|
||||
$xmlRecord = $doc->createElement('exception');
|
||||
$xmlRecord->setAttribute('code', $this->code);
|
||||
public function getChannel(Channel $channel) { // TODO improve view interface
|
||||
return array('id' => (int) $channel->id,
|
||||
'ucid' => $channel->ucid,
|
||||
'resolution' => (int) $channel->resolution,
|
||||
'description' => $channel->description,
|
||||
'type' => $channel->type,
|
||||
'costs' => $channel->cost);
|
||||
}
|
||||
|
||||
$xmlRecord->appendChild($doc->createElement('message', $this->message));
|
||||
$xmlRecord->appendChild($doc->createElement('line', $this->line));
|
||||
$xmlRecord->appendChild($doc->createElement('file', $this->file));
|
||||
public function render() {
|
||||
$this->time = round(microtime(true) - $this->created, 4);
|
||||
echo json_encode($this->data);
|
||||
}
|
||||
|
||||
$xmlRecord->appendChild(backtrace2xml($this->getTrace(), $doc));
|
||||
public function exceptionHandler(Exception $exception) {
|
||||
$xmlException = $this->doc->createElement('exception');
|
||||
|
||||
return $xmlRecord;
|
||||
$xmlException->setAttribute('code', $exception->getCode());
|
||||
|
||||
$xmlException->appendChild($this->doc->createElement('message', $exception->getMessage()));
|
||||
$xmlException->appendChild($this->doc->createElement('line', $exception->getLine()));
|
||||
$xmlException->appendChild($this->doc->createElement('file', $exception->getFile()));
|
||||
|
||||
$xmlException->appendChild($this->backtrace($exception->getTrace()));
|
||||
|
||||
$this->render();
|
||||
die();
|
||||
}
|
||||
|
||||
function backtrace($traces) {
|
||||
$xmlTraces = $this->doc->createElement('backtrace');
|
||||
|
||||
foreach ($traces as $step => $trace) {
|
||||
$xmlTrace = $this->doc->createElement('trace');
|
||||
$xmlTraces->appendChild($xmlTrace);
|
||||
$xmlTrace->setAttribute('step', $step);
|
||||
|
||||
foreach ($trace as $key => $value) {
|
||||
switch ($key) {
|
||||
case 'function':
|
||||
case 'line':
|
||||
case 'file':
|
||||
case 'class':
|
||||
case 'type':
|
||||
$xmlTrace->appendChild($this->doc->createElement($key, $value));
|
||||
break;
|
||||
case 'args':
|
||||
$xmlArgs = $doc->createElement($key);
|
||||
$xmlTrace->appendChild($xmlArgs);
|
||||
foreach ($value as $arg) {
|
||||
$xmlArgs->appendChild($this->doc->createElement('arg', $value));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $xmlTraces;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue