diff --git a/content/backend.php b/content/backend.php index ef1423d..6c4f69b 100644 --- a/content/backend.php +++ b/content/backend.php @@ -20,17 +20,15 @@ */ /* - * Entrypoint for all backend actions + * Bootstrap entrypoint, just calls Frontcontroller::run() */ -// initialize environment (error handling, configuration, loading classes) -include '../init.php'; +include '../init.php'; // initialize environment (error handling, configuration, class autoloading) try { $fc = new FrontController(); // spawn frontcontroller - $fc->handleRequest(); // call controllers - $fc->sendResponse(); // send headers & output -} catch (Exception $e) { // handle all uncatched exceptions + $fc->run(); // execute controller and sends output +} catch (Exception $e) { // catch all exceptions echo $e; } ?> \ No newline at end of file diff --git a/init.php b/init.php index e5c68f4..2c85457 100644 --- a/init.php +++ b/init.php @@ -19,6 +19,8 @@ * http://www.gnu.org/copyleft/gpl.html */ +define('VZ_VERSION', '0.1'); + /* * class autoloading */ diff --git a/lib/model/channel.php b/lib/model/channel.php index ddf998a..136c8b6 100644 --- a/lib/model/channel.php +++ b/lib/model/channel.php @@ -126,7 +126,7 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { $package['value'] += $reading['value']; $package['count']++; } - + $packages[] = $package; $reading = $result->next(); } @@ -190,4 +190,14 @@ abstract class Channel extends DatabaseObject implements ChannelInterface { $sql .= static::buildFilterCondition($filters, $conjunction); return $sql; } + + public function toJson() { + return array( + 'id' => (int) $this->id, + 'ucid' => $this->ucid, + 'resolution' => (int) $this->resolution, + 'description' => $this->description, + 'type' => $this->type, + 'costs' => $this->cost); + } } \ No newline at end of file diff --git a/lib/model/channel/sensor.php b/lib/model/channel/sensor.php index c330c8d..05a6398 100644 --- a/lib/model/channel/sensor.php +++ b/lib/model/channel/sensor.php @@ -21,8 +21,8 @@ abstract class Sensor extends Channel { - public function getData($from = NULL, $to = NULL, $values = NULL) { - $data = parent::getData($from, $to, $values); + public function getData($from = NULL, $to = NULL, $groupBy = NULL) { + $data = parent::getData($from, $to, $groupBy); array_walk($data, function(&$reading) { $reading['value'] /= $reading['count']; // calculate average (ungroup the sql sum() function) diff --git a/lib/model/databaseobject.php b/lib/model/databaseobject.php index a95bd15..a9233d9 100644 --- a/lib/model/databaseobject.php +++ b/lib/model/databaseobject.php @@ -23,7 +23,7 @@ abstract class DatabaseObject { protected $dbh; // database handle for all queries in DBObject subclasses private $dirty; // do we need to update the database? - private $values = array(); + private $data = array(); static private $instances = array(); // singletons of objects @@ -32,20 +32,20 @@ abstract class DatabaseObject { */ final public function __construct($object) { $this->dbh = Database::getConnection(); - $this->values = $object; + $this->data = $object; } public function __get($key) { - if (!isset($this->values[$key]) && $this->id) { + if (!isset($this->data[$key]) && $this->id) { $this->load(); } - return $this->values[$key]; + return $this->data[$key]; } public function __set($key, $value) { // TODO untested if ($key != 'id') { - $this->values[$key] = $value; + $this->data[$key] = $value; $this->dirty = true; } } @@ -60,7 +60,7 @@ abstract class DatabaseObject { } final public function __isset($key) { - return isset($this->values[$key]); + return isset($this->data[$key]); } static protected function factory($object) { @@ -72,7 +72,7 @@ abstract class DatabaseObject { */ public function save() { if ($this->id) { // just update - foreach ($this->values as $column => $value) { + foreach ($this->data as $column => $value) { if ($column != 'id') { $columns[] = $column . ' = ' . $this->dbh->escape($value); } @@ -82,7 +82,7 @@ abstract class DatabaseObject { $this->dbh->execute($sql); } else { // insert new row - $sql = 'INSERT INTO ' . static::table . ' (' . implode(', ', array_keys($this->values)) . ') VALUES (' . implode(', ', array_map(array($this->dbh, 'escape'), $this->values)) . ')'; + $sql = 'INSERT INTO ' . static::table . ' (' . implode(', ', array_keys($this->data)) . ') VALUES (' . implode(', ', array_map(array($this->dbh, 'escape'), $this->data)) . ')'; $this->dbh->execute($sql); $this->id = $this->dbh->lastInsertId(); } @@ -90,17 +90,17 @@ abstract class DatabaseObject { } /* - * loads all columns from the database and caches them in $this->values + * loads all columns from the database and caches them in $this->data */ private function load() { $result = $this->dbh->query('SELECT * FROM ' . static::table . ' WHERE id = ' . (int) $this->id, 1)->current(); if ($result == false) { - unset($this->values['id']); + unset($this->data['id']); return false; } else { - $this->values = $result; + $this->data = $result; $this->loaded = true; return true; } @@ -112,7 +112,7 @@ abstract class DatabaseObject { */ public function delete() { $this->dbh->execute('DELETE FROM ' . static::table . ' WHERE id = ' . (int) $this->id); // delete from database - unset($this->values['id']); + unset($this->data['id']); } /* diff --git a/lib/view/http/httphandle.php b/lib/view/http/httphandle.php index e0de001..55e15f7 100644 --- a/lib/view/http/httphandle.php +++ b/lib/view/http/httphandle.php @@ -23,47 +23,48 @@ abstract class HttpHandle { public $code; public $headers = array(); - - protected static $codes = array(100 => 'Continue', - 101 => 'Switching Protocols', - 200 => 'OK', - 201 => 'Created', - 202 => 'Accepted', - 203 => 'Non-Authoritative Information', - 204 => 'No Content', - 205 => 'Reset Content', - 206 => 'Partial Content', - 300 => 'Multiple Choicesv', - 301 => 'Moved Permanently', - 302 => 'Found', - 303 => 'See Other', - 304 => 'Not Modified', - 305 => 'Use Proxy', - 307 => 'Temporary Redirect', - 400 => 'Bad Request', - 401 => 'Unauthorized', - 402 => 'Payment Required', - 403 => 'Forbidden', - 404 => 'Not Found', - 405 => 'Method Not Allowed', - 406 => 'Not Acceptable', - 407 => 'Proxy Authentication Required', - 408 => 'Request Timeout', - 409 => 'Conflict', - 410 => 'Gone', - 411 => 'Length Required', - 412 => 'Precondition Failed', - 413 => 'Request Entity Too Large', - 414 => 'Request-URI Too Long', - 415 => 'Unsupported Media Type', - 416 => 'Requested Range Not Satisfiable', - 417 => 'Expectation Failed', - 500 => 'Internal Server Error', - 501 => 'Not Implemented', - 502 => 'Bad Gateway', - 503 => 'Service Unavailable', - 504 => 'Gateway Timeout', - 505 => 'HTTP Version Not Supported'); + protected static $codes = array( + 100 => 'Continue', + 101 => 'Switching Protocols', + 200 => 'OK', // success + 201 => 'Created', + 202 => 'Accepted', + 203 => 'Non-Authoritative Information', + 204 => 'No Content', + 205 => 'Reset Content', + 206 => 'Partial Content', + 300 => 'Multiple Choicesv', + 301 => 'Moved Permanently', // redirection + 302 => 'Found', + 303 => 'See Other', + 304 => 'Not Modified', + 305 => 'Use Proxy', + 307 => 'Temporary Redirect', + 400 => 'Bad Request', // client error + 401 => 'Unauthorized', + 402 => 'Payment Required', + 403 => 'Forbidden', + 404 => 'Not Found', + 405 => 'Method Not Allowed', + 406 => 'Not Acceptable', + 407 => 'Proxy Authentication Required', + 408 => 'Request Timeout', + 409 => 'Conflict', + 410 => 'Gone', + 411 => 'Length Required', + 412 => 'Precondition Failed', + 413 => 'Request Entity Too Large', + 414 => 'Request-URI Too Long', + 415 => 'Unsupported Media Type', + 416 => 'Requested Range Not Satisfiable', + 417 => 'Expectation Failed', + 500 => 'Internal Server Error', // server error + 501 => 'Not Implemented', + 502 => 'Bad Gateway', + 503 => 'Service Unavailable', + 504 => 'Gateway Timeout', + 505 => 'HTTP Version Not Supported' + ); } ?> \ No newline at end of file