aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--MainApp.php31
-rw-r--r--MainController.php72
-rw-r--r--MainDb.php17
-rw-r--r--MainView.php14
4 files changed, 88 insertions, 46 deletions
diff --git a/MainApp.php b/MainApp.php
index 3ad37a0..e87250d 100644
--- a/MainApp.php
+++ b/MainApp.php
@@ -26,6 +26,7 @@ class MainApp
ob_start();
// dispatch correct controller
+ $controller = self::classname($controller);
$c = new $controller;
// assume no method name was given, try $param, then default to defaultAction
@@ -55,17 +56,18 @@ class MainApp
////
// requests should always be in the form: controller/action/parameters.type
// Strip type info since it's not needed at this point
- static function url($index = null)
+ static function url($part = false)
{
$url = parse_url($_SERVER['REQUEST_URI']);
- if ($index == 'query') {
+ if ($part == 'query') {
return $url['query'];
}
$req = ltrim($url['path'], '/');
$array = explode('/', preg_replace('/\.\w+$/', '', $req));
- return (is_numeric($index)) ? $array[$index] : $array;
+
+ return (is_int($part)) ? $array[$part] : $array;
}
@@ -250,6 +252,23 @@ class MainApp
}
}
+ ////
+ // convert a "public" name to a class name
+ static function classname($name)
+ {
+ $name = str_replace('_', ' ', $name);
+ $name = ucwords($name);
+ $class_name = str_replace(' ', '', $name);
+ return $class_name;
+ }
+
+ ////
+ // convert a class name to a "public" name
+ static function tablename($name)
+ {
+
+ }
+
////
// output a JavaScript fragment
static function js($string)
@@ -275,13 +294,15 @@ class MainApp
////
// for autoloading the app's classes
- function __autoload($class_name)
+ function __autoload($name)
{
- $class_name = ucfirst($class_name);
+ $class_name = MainApp::classname($name);
include_once "{$class_name}.php";
+
if (!class_exists($class_name)) {
exit(MainApp::error("{$class_name}: no such class"));
}
+
}
?> \ No newline at end of file
diff --git a/MainController.php b/MainController.php
index f77f1bb..e09fe3e 100644
--- a/MainController.php
+++ b/MainController.php
@@ -18,6 +18,7 @@ class MainController
public $data;
private $table;
+ private $primary_key;
////
// create controller object
@@ -76,6 +77,7 @@ class MainController
foreach ($schema as $s) {
$this->data[$s['Field']] = $s['Default'];
}
+ return $this;
}
if ($_SESSION['config']['db.driver'] == 'sqlite') {
@@ -85,6 +87,7 @@ class MainController
$this->data[$s['name']] = '';
}
}
+ return $this;
}
////
@@ -129,10 +132,6 @@ class MainController
$values = 1;
}
-// $schema = App::conf('tables');
-// $this->schema = $schema[$this->table];
-
- // Ugly hack until PHP 5.3
$i_sort = static::OrderBy;
$i_fk = static::ForeignKey;
$i_mtm = static::ManyToMany;
@@ -196,23 +195,26 @@ class MainController
{
$order_by = '';
- if ($child::OrderBy) {
- $order_by = sprintf('ORDER BY `%s`.%s', $child, $child::OrderBy);
+ if ($ob = $child::OrderBy) {
+ $order_by = sprintf('ORDER BY `%s`.%s', $child, $ob);
}
+ $table = ($child::TableName) ? $child::TableName : $child;
+
// determine nature of relationship
- $one_to_many = explode(',', eval("return $child::ForeignKey;"));
- $many_to_many = explode(',', eval("return $child::ManyToMany;"));
- $sql = "SELECT * FROM `$child` WHERE `{$this->table}_id` = ? $order_by";
+ $one_to_many = explode(',', $child::ForeignKey);
+ $many_to_many = explode(',', $child::ManyToMany);
+
+ $sql = "SELECT * FROM `$table` WHERE `{$this->table}_id` = ? $order_by";
if (in_array($this->table, $many_to_many)) // m/n
{
- $sql = "SELECT `{$child}`.*, `{$child}_{$this->table}`.*,
- `{$child}_{$this->table}`.id AS {$child}_{$this->table}_id,
- `{$child}`.*
- FROM `{$child}_{$this->table}`, `{$this->table}`, `$child`
- WHERE `{$child}_{$this->table}`.`{$this->table}_id` = `$this->table`.id AND
- `{$child}_{$this->table}`.`{$child}_id` = `$child`.id AND
+ $sql = "SELECT `{$table}`.*, `{$table}_{$this->table}`.*,
+ `{$table}_{$this->table}`.id AS {$table}_{$this->table}_id,
+ `{$table}`.*
+ FROM `{$table}_{$this->table}`, `{$this->table}`, `$table`
+ WHERE `{$table}_{$this->table}`.`{$this->table}_id` = `$this->table`.id AND
+ `{$table}_{$this->table}`.`{$table}_id` = `$table`.id AND
`{$this->table}`.id = ?";
} else if (@in_array($table, $ref_schema['belongsTo'])) { // 1/m
$sql = "SELECT * FROM `$ref` WHERE `$ref`.`{$table}_id` = ?";
@@ -233,7 +235,7 @@ class MainController
private function find_parent($row, $i)
{
$select = '*';
- $fk = explode(',', eval("return {$this->class}::ForeignKey;"));
+ $fk = explode(',', static::ForeignKey);
// No parents defined
if (empty($fk[0])) {
@@ -244,16 +246,8 @@ class MainController
{
$fkey = 'id';
$lkey = "{$parent}_id";
- /*
- if ($this->schema['foreignKey'][$parent])
- {
- list($lkey, $fkey) = explode("|", $this->schema['foreignKey'][$parent]);
- }
- */
+
@$parent_id = $row[$lkey];
-
-// $ref_schema = App::conf('tables');
-// $ref_schema = $ref_schema[$parent];
@$ref_schema = $fk[''];
if ($ref_schema['select'])
@@ -330,7 +324,7 @@ class MainController
// update this Thing
// We keep this in the Controller since it might know
// more about the topmost class
- protected function update($data, $where = null)
+ protected function update($data, $where = NULL)
{
if (!is_array($data)) {
return MainApp::error('Cannot update without parameters');
@@ -350,7 +344,7 @@ class MainController
foreach ($data as $k => $v) {
$query .= "`$k` = :$k, ";
- $values[":{$k}"] = $v;
+ $values[':'.$k] = $v;
}
$query = rtrim($query, ', ');
@@ -362,9 +356,9 @@ class MainController
////
// render a view
- public function render($view = null, $data = null)
+ public function render($view = NULL, $data = NULL)
{
- // Default to the same view as the method
+ // default to the same view as the method
if (!$view) {
$bt = debug_backtrace();
$view = $bt[1]['function'];
@@ -379,19 +373,17 @@ class MainController
}
$type = MainApp::type();
+ @list($c, $p, $m) = MainApp::url();
+
+ if (empty($c)) {
+ $c = strtolower($this->class);
+ }
- $controller = strtolower($this->class);
- $template = "../system/views/default.{$type}";
- $file = "../system/views/{$controller}/{$view}.{$type}";
+ $template = '../system/views/default.'.$type;
+ $file = "../system/views/{$c}/{$view}.{$type}";
if (!is_file($file)) {
- return MainApp::error("{$controller}/{$view}.{$type}: no such view");
- }
-
- if ($data) {
- foreach ($data as $k => $v) {
- $$k = $v;
- }
+ return MainApp::error("{$c}/{$view}.{$type}: no such view");
}
// Capture view
@@ -430,7 +422,7 @@ class MainController
////
// insert or update
- public function upsert($data, $where = null)
+ public function upsert($data, $where = NULL)
{
if(!$this->get($where)) {
$out = $this->insert($data);
diff --git a/MainDb.php b/MainDb.php
index f26527d..6a18f9e 100644
--- a/MainDb.php
+++ b/MainDb.php
@@ -103,7 +103,12 @@ class MainDb
return MainApp::error($error);
}
- // select statements need the query results
+ // DELETE statements should report number of rows
+ if (preg_match('/^DELETE/i', $sql)) {
+ return $result->rowCount();
+ }
+
+ // SELECT statements need the query results
if (preg_match('/^SELECT/i', $sql)) {
return $stmt;
}
@@ -147,6 +152,16 @@ class MainDb
$stmt = self::query($sql, $args) or exit(MainApp::error('Error executing query '.$sql));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
+
+ ////
+ // count something
+ public static function total($table)
+ {
+ $sql = 'SELECT COUNT(*) AS the_count FROM `%s`';
+ $res = self::fetch(sprintf($sql, $table));
+ $res = current($res);
+ return $res['the_count'];
+ }
}
diff --git a/MainView.php b/MainView.php
index 2e5ad0c..2c3be45 100644
--- a/MainView.php
+++ b/MainView.php
@@ -32,6 +32,20 @@ class MainView
}
+ // $this->mainView->form('/some/save')->
+ function action($action)
+ {
+ $port = ($_SERVER['SERVER_PORT'] > 80) ? ':'.$_SERVER['SERVER_PORT'] : '';
+ $base = str_replace('index.php', '', $_SERVER['PHP_SELF']);
+ return 'action="'.$base.$action.$port.'"';
+ }
+
+ function form($action)
+ {
+ $port = ($_SERVER['SERVER_PORT'] > 80) ? ':'.$_SERVER['SERVER_PORT'] : '';
+ $out = '<form action="'.$action.$port.'" accept-charset="utf-8"';
+ }
+
function input($params)
{
return $this->tag('input', $params);