renamed controller and view function calls

This commit is contained in:
Steffen Vogel 2010-06-07 02:14:15 +02:00
parent 71e8d9f9a1
commit 29cc78bc1b
6 changed files with 73 additions and 62 deletions

View file

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

View file

@ -19,6 +19,8 @@
* http://www.gnu.org/copyleft/gpl.html
*/
define('VZ_VERSION', '0.1');
/*
* class autoloading
*/

View file

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

View file

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

View file

@ -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']);
}
/*

View file

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