From 984306304bf109e5e57ee01b87374bcfd211558a Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 9 Jun 2010 19:34:29 +0200 Subject: [PATCH] introduced new view system (also introduced new api version 0.2) --- backend/lib/controller/channelcontroller.php | 8 +-- backend/lib/controller/datacontroller.php | 17 +----- backend/lib/controller/groupcontroller.php | 13 ++++ backend/lib/controller/usercontroller.php | 22 +++++-- backend/lib/view/jsonview.php | 64 +++++++++++++++----- backend/lib/view/view.php | 5 ++ 6 files changed, 87 insertions(+), 42 deletions(-) diff --git a/backend/lib/controller/channelcontroller.php b/backend/lib/controller/channelcontroller.php index fc4ece2..988992b 100644 --- a/backend/lib/controller/channelcontroller.php +++ b/backend/lib/controller/channelcontroller.php @@ -25,7 +25,7 @@ class ChannelController extends Controller { $channels = $user->getChannels(); foreach ($channels as $channel) { - $this->view->data['channels'][] = $this->view->getChannel($channel); + $this->view->addChannel($channel); } } @@ -42,17 +42,13 @@ class ChannelController extends Controller { } $channel->save(); - $this->view->data['channel'] = $this->view->getChannel($channel); + $this->view->addChannel($channel); } public function delete() { // TODO untested $channel = Channel::getByUcid($this->view->request->get['ucid']); $channel->delete(); } - - public function edit() { - - } } ?> \ No newline at end of file diff --git a/backend/lib/controller/datacontroller.php b/backend/lib/controller/datacontroller.php index 7bb583a..1af6dda 100644 --- a/backend/lib/controller/datacontroller.php +++ b/backend/lib/controller/datacontroller.php @@ -22,9 +22,7 @@ class DataController extends Controller { public function add() { $ucid = $this->view->request->get['ucid']; - $channel = Channel::getByUcid($ucid); - $channel->addData($this->view->request->get); // array(timestamp, value, count) } @@ -36,21 +34,8 @@ class DataController extends Controller { $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; // TODO nescessary? - - $jsonChannels = array(); foreach ($channels as $channel) { - $jsonChannel = $this->view->getChannel($channel); - - $jsonChannel['data'] = array(); - - foreach ($channel->getPulses($from, $to, $groupBy) as $reading) { - $jsonChannel['data'][] = array($reading['timestamp'], $reading['value'], $reading['count']); - } - - $data['channels'][] = $jsonChannel; + $this->view->addChannel($channel, $channel->getPulses($from, $to, $groupBy)); } - $this->view->data += $data; } } \ No newline at end of file diff --git a/backend/lib/controller/groupcontroller.php b/backend/lib/controller/groupcontroller.php index b5d9b07..6f426fc 100644 --- a/backend/lib/controller/groupcontroller.php +++ b/backend/lib/controller/groupcontroller.php @@ -21,15 +21,28 @@ class GroupController extends Controller { public function add() { + $group = new Group(); + $group->description = $this->view->request->get['description']; + $group->save(); + // TODO add ugid + + $this->view->addGroup($group); } public function delete() { + $group = Group::getByFilter(array('ugid' => $this->view->request->get['ugid'])); + $group->delete(); } public function get() { + $user = User::getByFilter(array('uuid' => $this->view->request->get['uuid'])); + $groups = Group::getByFilter(array('user' => $user)); + foreach ($groups as $group) { + $this->view->addGroup($group); + } } } diff --git a/backend/lib/controller/usercontroller.php b/backend/lib/controller/usercontroller.php index e27426a..1f37851 100644 --- a/backend/lib/controller/usercontroller.php +++ b/backend/lib/controller/usercontroller.php @@ -21,30 +21,40 @@ class UserController extends Controller { public function add() { - + $user = new User(); + $user->password = $this->view->request->get['password']; + $user->save(); + $this->view->addUser($user); } public function delete() { - + $user = User::getByFilter(array('uuid' => $this->view->request->get['uuid'])); + $user->delete(); } public function edit() { - + // TODO implement UserController::edit(); } public function login() { + $user = User::getByFilter(array('uuid' => $this->view->request->get['uuid'])); + if ($user->checkPasswort($this->view->request->get['password'])) { + $_SESSION['user_id'] = $user->id; // TODO add session handling + } } public function logout() { - + if ($_SESSION['user_id']) { + unset($_SESSION['user_id']); + } } public function subscribe() { - + // TODO implement UserController::subscribe(); } public function unsubscribe() { - + // TODO implement UserController::unsubscribe(); } } \ No newline at end of file diff --git a/backend/lib/view/jsonview.php b/backend/lib/view/jsonview.php index 5f5e5a3..233bc7c 100644 --- a/backend/lib/view/jsonview.php +++ b/backend/lib/view/jsonview.php @@ -21,10 +21,10 @@ class JsonView extends View { public $data = array(); - + public function __construct(HttpRequest $request, HttpResponse $response) { parent::__construct($request, $response); - + $config = Registry::get('config'); $this->data['source'] = 'volkszaehler.org'; @@ -32,34 +32,70 @@ class JsonView extends View { $this->data['storage'] = $config['db']['backend']; $this->data['controller'] = $request->get['controller']; $this->data['action'] = $request->get['action']; - + $this->response->setHeader('Content-type', 'application/json'); } - + public function render() { $this->data['time'] = round(microtime(true) - $this->created, 4); echo json_encode($this->data); } - + public function exceptionHandler(Exception $exception) { $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 - return array('id' => (int) $channel->id, - 'ucid' => $channel->ucid, - 'resolution' => (int) $channel->resolution, - 'description' => $channel->description, - 'type' => $channel->type, - 'costs' => $channel->cost); + + public function addChannel(Channel $obj, $data = NULL) { + $channel['id'] = (int) $obj->id; + $channel['ucid'] = $obj->ucid; + $channel['resolution'] = (int) $obj->resolution; + $channel['description'] = $obj->description; + $channel['type'] = $obj->type; + $channel['costs'] = $obj->cost; + + // TODO check for optional data in second param + if (!is_null($data) && is_array($data)) { + $channel['data'][] = array(); + foreach ($data as $reading) { + $channel['data'][] = array($reading['timestamp'], $reading['value'], $reading['count']); + } + } + + $this->data['channels'][] = $channel; + } + + public function addUser(User $obj) { + $user['id'] = (int) $obj->id; + $user['uuid'] = $obj->uuid; + + $this->data['users'][] = $user; + } + + public function addGroup(Group $obj) { + $group['id'] = (int) $obj->id; + $group['ugid'] = $obj->ugid; + $group['description'] = $obj->description; + + // TODO include sub groups? + + $this->data['groups'][] = $group; + } + + public function add($obj) { + if (is_array($obj)) { + array_merge($this->data, $obj); // TODO check array_merge beavior with duplicate keys + } + else { + $this->data[] = $obj; + } } } diff --git a/backend/lib/view/view.php b/backend/lib/view/view.php index 65a80a5..c96f24d 100644 --- a/backend/lib/view/view.php +++ b/backend/lib/view/view.php @@ -24,6 +24,11 @@ interface ViewInterface { public function render(); public function exceptionHandler(Exception $exception); public function errorHandler($errno, $errstr, $errfile, $errline); + + public function add($obj); + public function addChannel(Channel $obj); + public function addUser(User $obj); + public function addGroup(Group $obj); } abstract class View implements ViewInterface {