From 677d2f066d66a360ebfb367d4521926ad14d5480 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Sun, 27 Jun 2010 11:40:53 +0300 Subject: some fixes --- MainApp.php | 51 +++++++++++++----------- MainController.php | 114 +++++++++++++++++++++++++---------------------------- MainDb.php | 16 +++++--- MainView.php | 16 +++++--- 4 files changed, 102 insertions(+), 95 deletions(-) diff --git a/MainApp.php b/MainApp.php index 997b3ba..95b3efa 100644 --- a/MainApp.php +++ b/MainApp.php @@ -12,17 +12,17 @@ class MainApp $url = self::url(); @list($controller, $param, $action) = $url; + + // no controller given, read default one + if (!$controller) { + $controller = self::conf('defaults.controller'); + } // no action given, read default one - if (empty($param)) { + if (strlen($param) < 1) { $action = self::conf('defaults.action'); } - // no controller given, read default one - if (!$controller) { - $controller = self::conf('defaults.controller'); - } - // fire up the output buffer ob_start(); @@ -32,21 +32,21 @@ class MainApp // assume no method name was given, try $param, then default to defaultAction // controller/param/action if (method_exists($c, $action)) { - return $c->$action($c); + return $c->$action($_POST); } // controller/action if (method_exists($c, $param)) { - return $c->$param($c); + return $c->$param($_POST); } // controller/param if (method_exists($c, $c->defaultAction)) { $action = $c->defaultAction; - return $c->$action($c); + return $c->$action($_POST); } - self::error("{$controller}_{$action}: no such method"); + self::error("{$controller}/{$action}: no such method"); // release the output buffer ob_end_flush(); @@ -75,6 +75,10 @@ class MainApp static function param() { $url = self::url(); + // no parameter given + if (count($url) < 3) { + return NULL; + } return $url[1]; } @@ -107,7 +111,7 @@ class MainApp $tokens = explode('/', $_SERVER['REQUEST_URI']); $last = array_pop($tokens); $type = ltrim(strrchr($last, '.'), '.'); - + $contentTypes = array('html', 'rss', 'xml', 'tpl', 'pdf', 'jpg'); if (in_array($type, $contentTypes)) { @@ -144,21 +148,22 @@ class MainApp // log an error to our own logging system static function log($msg) { - if (is_array($msg)) { - $msg = print_r($msg, true); - } - $file = self::conf('app.error_log'); - - if (!is_file($file)) { - file_put_contents($file, $msg); + + if (!file_exists($file)) { + exit('Log file does not exist'); } - $msg = trim($msg); $fh = fopen($file, 'a+'); - fwrite($fh, trim($msg) . "\n"); - fclose($fh); + foreach (func_get_args() as $arg) + { + if (is_array($arg)) { + $arg = print_r($arg, true); + } + fwrite($fh, date('r') . "\t" . trim($arg) . "\n"); + } + fclose($fh); } //// @@ -269,7 +274,7 @@ class MainApp } //// - // Prompt for HTTP authentication + // prompt for HTTP authentication // @param string [$callback] Function that makes the actual authentication // @param string [$realm] Realm name // @return mixed false if cancelled or output of $function @@ -286,7 +291,7 @@ class MainApp } //// - // Output a JavaScript fragment + // output a JavaScript fragment public function js($string) { header('Content-Type: text/javascript'); diff --git a/MainController.php b/MainController.php index 84b56ce..c4d937d 100644 --- a/MainController.php +++ b/MainController.php @@ -14,7 +14,7 @@ class MainController const TableName = ''; const ManyToMany = ''; const ForeignKey = ''; - const TableSelect = ''; + const TableSelect = ''; // extra fields to select //// // create controller object @@ -23,16 +23,19 @@ class MainController // child classes should always have the same name as their tables $this->class = get_class($this); $this->table = eval("return {$this->class}::TableName;"); - $this->mainView = new MainView(); // table name not defined, default to class name if (!$this->table) { $this->table = strtolower($this->class); } - - $this->result = null; + // populate indices + $schema = MainDb::fetch("DESCRIBE `{$this->table}`"); + foreach ($schema as $s) { + $this->data[$s['Field']] = $s['Default']; + } + if ($id) { return $this->get($id); } @@ -50,22 +53,23 @@ class MainController } $this->find($where); + if (!is_array($this->data)) { + return false; // found nothing + } + return current($this->data); } - public function db() - { - return MainDb::getInstance(); - } - //// // the New Find public function find($where = null, $sort = false, $limit = false) { - $select = '*'; $q = ''; + $select = '*'; + $q = ''; + $this->data = array(); - // Allow custom queries + // allow custom queries if (is_array($where)) { foreach ($where as $k => $v) @@ -116,7 +120,7 @@ class MainController } if (!$sort && $i_sort) { - $sort = "ORDER BY `{$this->table}`.{$i_sort}"; + $sort = "ORDER BY {$i_sort}"; } if ($i_select) { @@ -142,7 +146,7 @@ class MainController $row = $result[$i]; $this->data[$i] = $row; $this->find_parent($row, $i); -// $this->find_children($row, $i); + $this->find_children($row, $i); } return $this->data; @@ -155,7 +159,7 @@ class MainController private function find_children($row, $i) { $id = $row['id']; // ID of the parent - $fk = explode(",", eval("return $this->class::HasMany;")); + $fk = explode(',', eval("return $this->class::HasMany;")); if (empty($fk[0])) { return false; @@ -163,12 +167,12 @@ class MainController foreach ($fk as $child) { + // determine nature of relationship $sql = "SELECT * FROM `$child` WHERE `{$this->table}_id` = ?"; - - $ref_schema = MainApp::conf('tables'); - $ref_schema = $ref_schema[$child]; - - if (@in_array($this->table, $ref_schema['belongsToMany'])) // m/n + $one_to_many = explode(',', eval("return $child::ForeignKey;")); + $many_to_many = explode(',', eval("return $child::ManyToMany;")); + + 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, @@ -176,8 +180,7 @@ class MainController 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 - `{$this->table}`.id = ? - ORDER BY `{$child}`.{$ref_schema['orderBy']}"; + `{$this->table}`.id = ?"; } else if (@in_array ($table, $ref_schema['belongsTo'])) { // 1/m $sql = "SELECT * FROM `$ref` WHERE `$ref`.`{$table}_id` = ?"; } @@ -188,13 +191,12 @@ class MainController } } - /** - * Find all rows for this row - */ + //// + // find all parent rows for this row private function find_parent($row, $i) { - $select = "*"; - $fk = explode(",", eval("return {$this->class}::ForeignKey;")); + $select = '*'; + $fk = explode(',', eval("return {$this->class}::ForeignKey;")); // No parents defined if (empty($fk[0])) { @@ -211,11 +213,11 @@ class MainController list($lkey, $fkey) = explode("|", $this->schema['foreignKey'][$parent]); } */ - $parent_id = $row[$lkey]; + @$parent_id = $row[$lkey]; // $ref_schema = App::conf('tables'); // $ref_schema = $ref_schema[$parent]; - $ref_schema = $fk['']; + @$ref_schema = $fk['']; if ($ref_schema['select']) { @@ -234,15 +236,8 @@ class MainController } - private function find_parents() - { - - } - - /** - * Insert this thing in the DB and return inserted - * Thing - */ + //// + // insert this thing in the DB and return inserted Thing public function insert($data) { if (empty($data)) { @@ -266,9 +261,8 @@ class MainController } - /** - * Delete This Thing - */ + //// + // delete This Thing protected function delete($where, $limit = '') { if (empty($where)) { @@ -288,11 +282,10 @@ class MainController } - /** - * Update this Thing - * We keep this in the Controller since it might know - * more about the topmost class - */ + //// + // update this Thing + // We keep this in the Controller since it might know + // more about the topmost class protected function update($data, $where = null) { if (!is_array($data)) { @@ -303,7 +296,8 @@ class MainController $where = array('id' => 'id'); } - $query = ""; $values = array(); + $query = ''; + $values = array(); list($col, $val) = each($where); if (!isset($data[$col])) { @@ -315,17 +309,16 @@ class MainController $values[":{$k}"] = $v; } - $query = rtrim($query, ", "); + $query = rtrim($query, ', '); $sql = "UPDATE `{$this->table}` SET $query WHERE `$col` = :$col"; return MainDb::query($sql, $values); } - /** - * Render a view - */ - public function render($data = null, $view = null) + //// + // render a view + public function render($view = null, $data = null) { // Default to the same view as the method if (!$view) { @@ -342,13 +335,13 @@ class MainController } $type = MainApp::type(); - // @very temporary hack? - $tpl = (MainApp::url(0) == "admin") ? "admin" : "default"; - $template = "../system/views/{$tpl}.{$type}"; - $file = "../system/views/{$this->table}/{$view}.{$type}"; + + $controller = strtolower($this->class); + $template = "../system/views/default.{$type}"; + $file = "../system/views/{$controller}/{$view}.{$type}"; if (!is_file($file)) { - return MainApp::error("{$this->table}_{$view}_{$type}: no such view"); + return MainApp::error("{$controller}/{$view}.{$type}: no such view"); } if ($data) { @@ -369,7 +362,7 @@ class MainController $tpl_contents = ob_get_contents(); ob_end_clean(); - $title = ($this->pageTitle) ? $this->pageTitle : MainApp::conf("defaults.title"); + $title = ($this->pageTitle) ? $this->pageTitle : MainApp::conf('defaults.title'); $tpl_contents = preg_replace( '/.*?<\/title>/', "<title>{$title}", $tpl_contents ); @@ -391,9 +384,8 @@ class MainController } - /** - * Insert or update - */ + //// + // insert or update public function upsert($data, $where = null) { if(!$this->get($where)) { @@ -401,7 +393,7 @@ class MainController } else { $out = $this->update($data, $where); } -// App::log($out); + return $out; } diff --git a/MainDb.php b/MainDb.php index 89006ed..e672651 100644 --- a/MainDb.php +++ b/MainDb.php @@ -54,7 +54,7 @@ class MainDb if (!$data) { $data = array(); } - + // might be just a string if (!is_array($data)) { $data = array($data); @@ -78,13 +78,18 @@ class MainDb return MainApp::error($error); } - // Select statements need the query results + // select statements need the query results if (preg_match('/^SELECT/i', $sql)) { return $stmt; } - if (empty($data['id'])) { - $data['id'] = $pdo->lastInsertId(); + // describe statements need the query results + if (preg_match('/^DESCRIBE/i', $sql)) { + return $stmt; + } + + if (empty($data[':id'])) { + $data[':id'] = $pdo->lastInsertId(); } $out = array(); @@ -103,7 +108,8 @@ class MainDb // fetch something from DB public static function fetch($sql, $data = null) { - $stmt = self::query($sql, $data) or exit(MainApp::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 038ccf7..2e5ad0c 100644 --- a/MainView.php +++ b/MainView.php @@ -14,12 +14,11 @@ class MainView include $base.'/'.$path; } - /** - * Create HTML options from array + // @param array array + // @param mixed select option with this value + // @return string function select($array, $current = null) { $out = ''; @@ -33,6 +32,11 @@ class MainView } + function input($params) + { + return $this->tag('input', $params); + } + function tag($name, $args = '', $content = '', $selected = '') { $str_args = ''; -- cgit v1.2.3