From d20ba8b589cf20fed46232831c4f1a8c1f9ad85b Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Tue, 8 Jun 2010 15:39:31 +0200 Subject: [PATCH] introduced DataController reorganized Views --- backend/lib/controller/channelcontroller.php | 62 ++++----------- backend/lib/controller/datacontroller.php | 58 ++++++++++++++ backend/lib/controller/frontcontroller.php | 3 +- backend/lib/controller/groupcontroller.php | 36 +++++++++ backend/lib/controller/usercontroller.php | 8 ++ backend/lib/view/jsonview.php | 43 ++++------ backend/lib/view/view.php | 7 +- backend/lib/view/xmlview.php | 82 +++++++++++++++++--- 8 files changed, 213 insertions(+), 86 deletions(-) create mode 100644 backend/lib/controller/datacontroller.php create mode 100644 backend/lib/controller/groupcontroller.php diff --git a/backend/lib/controller/channelcontroller.php b/backend/lib/controller/channelcontroller.php index d42e56f..fc4ece2 100644 --- a/backend/lib/controller/channelcontroller.php +++ b/backend/lib/controller/channelcontroller.php @@ -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() { + } } diff --git a/backend/lib/controller/datacontroller.php b/backend/lib/controller/datacontroller.php new file mode 100644 index 0000000..345f955 --- /dev/null +++ b/backend/lib/controller/datacontroller.php @@ -0,0 +1,58 @@ + + * + * 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; + } +} \ No newline at end of file diff --git a/backend/lib/controller/frontcontroller.php b/backend/lib/controller/frontcontroller.php index d5770fc..6557272 100644 --- a/backend/lib/controller/frontcontroller.php +++ b/backend/lib/controller/frontcontroller.php @@ -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 } } diff --git a/backend/lib/controller/groupcontroller.php b/backend/lib/controller/groupcontroller.php new file mode 100644 index 0000000..b5d9b07 --- /dev/null +++ b/backend/lib/controller/groupcontroller.php @@ -0,0 +1,36 @@ + + * + * 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() { + + } +} + +?> \ No newline at end of file diff --git a/backend/lib/controller/usercontroller.php b/backend/lib/controller/usercontroller.php index 419c2fd..e27426a 100644 --- a/backend/lib/controller/usercontroller.php +++ b/backend/lib/controller/usercontroller.php @@ -39,4 +39,12 @@ class UserController extends Controller { public function logout() { } + + public function subscribe() { + + } + + public function unsubscribe() { + + } } \ No newline at end of file diff --git a/backend/lib/view/jsonview.php b/backend/lib/view/jsonview.php index 423fdc7..5f5e5a3 100644 --- a/backend/lib/view/jsonview.php +++ b/backend/lib/view/jsonview.php @@ -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 diff --git a/backend/lib/view/view.php b/backend/lib/view/view.php index 4798bfc..65a80a5 100644 --- a/backend/lib/view/view.php +++ b/backend/lib/view/view.php @@ -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 } } \ No newline at end of file diff --git a/backend/lib/view/xmlview.php b/backend/lib/view/xmlview.php index 899d314..5792d1d 100644 --- a/backend/lib/view/xmlview.php +++ b/backend/lib/view/xmlview.php @@ -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; } }