introduced new view system (also introduced new api version 0.2)

This commit is contained in:
Steffen Vogel 2010-06-09 19:34:29 +02:00
parent eabac78a6a
commit 984306304b
6 changed files with 87 additions and 42 deletions

View file

@ -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() {
}
}
?>

View file

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

View file

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

View file

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

View file

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

View file

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