diff options
-rw-r--r-- | MainApp.php | 28 | ||||
-rw-r--r-- | MainController.php | 57 | ||||
-rw-r--r-- | MainDb.php | 40 | ||||
-rw-r--r-- | MainView.php | 19 | ||||
-rw-r--r-- | config.default.ini | 3 |
5 files changed, 81 insertions, 66 deletions
diff --git a/MainApp.php b/MainApp.php index 45c0165..997b3ba 100644 --- a/MainApp.php +++ b/MainApp.php @@ -6,10 +6,12 @@ class MainApp { //// - // Fire up the application + // fire up the application static public function init() { - @list($controller, $param, $action) = App::url(); + $url = self::url(); + + @list($controller, $param, $action) = $url; // no action given, read default one if (empty($param)) { @@ -27,7 +29,7 @@ class MainApp // dispatch correct controller $c = new $controller; - // Assume no method name was given, try $param, then default to defaultAction + // assume no method name was given, try $param, then default to defaultAction // controller/param/action if (method_exists($c, $action)) { return $c->$action($c); @@ -44,32 +46,35 @@ class MainApp return $c->$action($c); } - App::error("{$controller}_{$action}: no such method"); + self::error("{$controller}_{$action}: no such method"); + // release the output buffer ob_end_flush(); } //// - // Requests should always be in the form: controller/action/parameters.type + // 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) { $url = parse_url($_SERVER['REQUEST_URI']); - + if ($index == 'query') { return $url['query']; } + $req = ltrim($url['path'], '/'); $array = explode('/', preg_replace('/\.\w+$/', '', $req)); return (is_numeric($index)) ? $array[$index] : $array; + } //// // return parameter part of URL static function param() { - $url = App::url(); + $url = self::url(); return $url[1]; } @@ -122,6 +127,7 @@ class MainApp static function error($msg) { $err = array('result' => 'error', 'msg' => $msg); + trigger_error($msg, E_USER_ERROR); // And log it locally self::log($msg); } @@ -184,7 +190,7 @@ class MainApp public function delete($table, $where) { if (empty($where)) { - exit(App::error('Delete without parameters')); + exit(self::error('Delete without parameters')); } list($key, $value) = each($where); @@ -290,13 +296,15 @@ class MainApp } } - + + //// + // for autoloading the app's classes function __autoload($class_name) { $class_name = ucfirst($class_name); include_once "{$class_name}.php"; if (!class_exists($class_name)) { - exit(App::error("{$class_name}: no such class")); + exit(MainApp::error("{$class_name}: no such class")); } } diff --git a/MainController.php b/MainController.php index ed8d5e6..84b56ce 100644 --- a/MainController.php +++ b/MainController.php @@ -6,6 +6,7 @@ class MainController { public $view; // Where to store the data to be rendered public $pageTitle = ''; // Title of the rendered page + public $defaultAction = ''; // Method to run when none specified const OrderBy = ''; @@ -15,15 +16,17 @@ class MainController const ForeignKey = ''; const TableSelect = ''; + //// + // create controller object function __construct($id = null) { // child classes should always have the same name as their tables $this->class = get_class($this); $this->table = eval("return {$this->class}::TableName;"); - $this->view = new MainView(); + $this->mainView = new MainView(); - // Table name not defined, default to class name + // table name not defined, default to class name if (!$this->table) { $this->table = strtolower($this->class); } @@ -53,12 +56,11 @@ class MainController public function db() { - return Db::getInstance(); + return MainDb::getInstance(); } - /** - * The New Find - */ + //// + // the New Find public function find($where = null, $sort = false, $limit = false) { $select = '*'; $q = ''; @@ -92,7 +94,7 @@ class MainController $values = array($where); } - if ($where == null) { + if ($where == NULL) { $q = 'WHERE ?'; $values = array(1); } @@ -128,8 +130,8 @@ class MainController $sql .= " LIMIT $limit"; } - $result = Db::fetch($sql, $values); - + $result = MainDb::fetch($sql, $values); + if (empty($result)) { $this->data = false; return; @@ -143,14 +145,13 @@ class MainController // $this->find_children($row, $i); } - return $this; + return $this->data; } - /** - * Find all child rows for this row - * @return void - */ + //// + // find all child rows for this row + // @return void private function find_children($row, $i) { $id = $row['id']; // ID of the parent @@ -164,7 +165,7 @@ class MainController { $sql = "SELECT * FROM `$child` WHERE `{$this->table}_id` = ?"; - $ref_schema = App::conf('tables'); + $ref_schema = MainApp::conf('tables'); $ref_schema = $ref_schema[$child]; if (@in_array($this->table, $ref_schema['belongsToMany'])) // m/n @@ -181,7 +182,7 @@ class MainController $sql = "SELECT * FROM `$ref` WHERE `$ref`.`{$table}_id` = ?"; } - $stmt = DB::query($sql, array($id)); + $stmt = MainDb::query($sql, array($id)); $this->data[$i][$child] = $stmt->fetchAll(PDO::FETCH_ASSOC); } @@ -226,7 +227,7 @@ class MainController $sql = "SELECT $select FROM `{$parent}` WHERE `{$fkey}` = ?"; - $stmt = DB::query($sql, array($parent_id)); + $stmt = MainDb::query($sql, array($parent_id)); $this->data[$i][$parent] = $stmt->fetchAll(PDO::FETCH_ASSOC); } @@ -245,7 +246,7 @@ class MainController public function insert($data) { if (empty($data)) { - return App::error('Cannot insert emptiness'); + return MainApp::error('Cannot insert emptiness'); } $insert = ''; @@ -261,7 +262,7 @@ class MainController $val = implode(', ', array_keys($values)); $sql = "INSERT INTO `{$this->table}` ({$insert}) VALUES ({$val})"; - return DB::query($sql, $values); + return MainDb::query($sql, $values); } @@ -271,7 +272,7 @@ class MainController protected function delete($where, $limit = '') { if (empty($where)) { - return App::error('Cannot delete without arguments'); + return MainApp::error('Cannot delete without arguments'); } list($key, $value) = each($where); @@ -283,7 +284,7 @@ class MainController $data = array(":{$key}" => $value); $sql = "DELETE FROM `{$this->table}` WHERE `{$key}` = :{$key} $limit"; - return Db::query($sql, $data); + return MainDb::query($sql, $data); } @@ -295,7 +296,7 @@ class MainController protected function update($data, $where = null) { if (!is_array($data)) { - return App::error('Cannot update without parameters'); + return MainApp::error('Cannot update without parameters'); } if (empty($where)) { @@ -317,7 +318,7 @@ class MainController $query = rtrim($query, ", "); $sql = "UPDATE `{$this->table}` SET $query WHERE `$col` = :$col"; - return Db::query($sql, $values); + return MainDb::query($sql, $values); } @@ -340,14 +341,14 @@ class MainController $data = $this->view; } - $type = App::type(); + $type = MainApp::type(); // @very temporary hack? - $tpl = (App::url(0) == "admin") ? "admin" : "default"; + $tpl = (MainApp::url(0) == "admin") ? "admin" : "default"; $template = "../system/views/{$tpl}.{$type}"; $file = "../system/views/{$this->table}/{$view}.{$type}"; if (!is_file($file)) { - return App::error("{$this->table}_{$view}_{$type}: no such view"); + return MainApp::error("{$this->table}_{$view}_{$type}: no such view"); } if ($data) { @@ -368,7 +369,7 @@ class MainController $tpl_contents = ob_get_contents(); ob_end_clean(); - $title = ($this->pageTitle) ? $this->pageTitle : App::conf("defaults.title"); + $title = ($this->pageTitle) ? $this->pageTitle : MainApp::conf("defaults.title"); $tpl_contents = preg_replace( '/<title>.*?<\/title>/', "<title>{$title}</title>", $tpl_contents ); @@ -386,7 +387,7 @@ class MainController $sql = rtrim($sql, ","); $sql = "SELECT * FROM `{$this->table}` WHERE MATCH($sql) AGAINST('{$match}')"; - return App::db()->query($sql); + return MainApp::db()->query($sql); } @@ -12,12 +12,11 @@ class MainDb } - /** - * Open persistent connection to database - */ + //// + // open persistent connection to database public static function getInstance() { - $c = App::conf(); + $c = MainApp::conf(); if (!self::$instance) { @@ -27,10 +26,11 @@ class MainDb $c['db.username'], $c['db.password'], array(PDO::ATTR_PERSISTENT => true) ); - self::$instance->query("SET NAMES utf8"); + // always use UTF-8? + self::$instance->query('SET NAMES utf8'); } catch (PDOException $e) { - exit(App::error($e->getMessage())); + exit(MainApp::error($e->getMessage())); } } @@ -39,25 +39,23 @@ class MainDb } - /** - * Deny cloning - */ + //// + // deny cloning public function __clone() { - trigger_error("Cloning not work is", E_USER_ERROR); + trigger_error('Cloning disabled', E_USER_ERROR); } - /** - * Execute an SQL query - * @return mixed - */ + //// + // execute an SQL query + // @return mixed public static function query($sql, $data = null) { if (!$data) { $data = array(); } - // Might be just a string + // might be just a string if (!is_array($data)) { $data = array($data); } @@ -71,13 +69,13 @@ class MainDb if (!$result) { list($ec, $dec, $emsg) = $pdo->errorInfo(); $error = $emsg ."\n" . print_r(debug_backtrace(), true); - return App::error($error); + return MainApp::error($error); } } catch (PDOException $e) { $error = $e->getMessage() . $sql; $error .= "\n" . print_r(debug_backtrace(), true); - return App::error($error); + return MainApp::error($error); } // Select statements need the query results @@ -101,13 +99,11 @@ class MainDb } - /** - * - */ + //// + // fetch something from DB public static function fetch($sql, $data = null) { - $stmt = self::query($sql, $data) - or exit(App::error("Error executing query $sql")); + $stmt = self::query($sql, $data) or exit(MainApp::error("Error executing query $sql")); return $stmt->fetchAll(PDO::FETCH_ASSOC); } diff --git a/MainView.php b/MainView.php index d964b6c..038ccf7 100644 --- a/MainView.php +++ b/MainView.php @@ -5,6 +5,15 @@ // @author Filipp Lepalaan <filipp@mac.com> class MainView { + //// + // include something within the project tree + function snippet($path) + { + $base = dirname(__FILE__).'/../..'; + $base = realpath($base); + include $base.'/'.$path; + } + /** * Create HTML <select> options from array * @param array array @@ -39,16 +48,16 @@ class MainView break; } - if (is_array ($args)) + if (is_array($args)) { - while (list ($k, $v) = each ($args)) { - if (!empty ($k)) $str_args .= ' ' . $k . '="' . $v . '"'; + while (list($k, $v) = each($args)) { + if (!empty($k)) $str_args .= ' ' . $k . '="' . $v . '"'; } } - if (is_array ($content)) + if (is_array($content)) { - foreach ($content as $k => $v) + foreach($content as $k => $v) { // } diff --git a/config.default.ini b/config.default.ini index 68c97ce..dca0c40 100644 --- a/config.default.ini +++ b/config.default.ini @@ -2,7 +2,7 @@ db.name = servo db.driver = mysql db.host = localhost -db.username = servo +db.username = db.password = app.timezone = Europe/Helsinki @@ -10,3 +10,4 @@ app.error_log = error.log defaults.controller = session defaults.action = check +defaults.title = MyWebApp
\ No newline at end of file |