From a371f1b0ff1218ad802fe2e57fc3531319b6585f Mon Sep 17 00:00:00 2001 From: br0xen Date: Fri, 7 Sep 2012 12:19:33 -0500 Subject: [PATCH] Various Modifications Move everything into App Directory Add a SQLite Library --- config.php => app/config.php | 0 .../controllers}/welcome_controller.php | 2 +- {core => app/core}/Controller.php | 12 +-- {core => app/core}/Model.php | 0 {core => app/core}/uri_library.php | 0 {libraries => app/libraries}/.gitignore | 0 app/libraries/sqlite_library.php | 96 +++++++++++++++++++ {models => app/models}/.gitignore | 0 app/views/welcome.php | 52 ++++++++++ index.php | 19 ++-- views/welcome.php | 20 ---- 11 files changed, 162 insertions(+), 39 deletions(-) rename config.php => app/config.php (100%) rename {controllers => app/controllers}/welcome_controller.php (77%) rename {core => app/core}/Controller.php (87%) rename {core => app/core}/Model.php (100%) rename {core => app/core}/uri_library.php (100%) rename {libraries => app/libraries}/.gitignore (100%) create mode 100644 app/libraries/sqlite_library.php rename {models => app/models}/.gitignore (100%) create mode 100644 app/views/welcome.php delete mode 100644 views/welcome.php diff --git a/config.php b/app/config.php similarity index 100% rename from config.php rename to app/config.php diff --git a/controllers/welcome_controller.php b/app/controllers/welcome_controller.php similarity index 77% rename from controllers/welcome_controller.php rename to app/controllers/welcome_controller.php index 555283a..dd649f0 100644 --- a/controllers/welcome_controller.php +++ b/app/controllers/welcome_controller.php @@ -1,7 +1,7 @@ load_views('welcome'); diff --git a/core/Controller.php b/app/core/Controller.php similarity index 87% rename from core/Controller.php rename to app/core/Controller.php index 7a35d3b..9fe44b7 100644 --- a/core/Controller.php +++ b/app/core/Controller.php @@ -47,19 +47,11 @@ class Controller { if(isset($a) && isset($func)) { if(is_array($a)) { foreach($a as $aa) { - if(defined('PHP_DIR')) { - $f = PHP_DIR.$func."/".$aa.".php"; - } else { - $f = $func."/".$aa.".php"; - } + $f = APP_ROOT."/".$func."/".$aa.".php"; $this->_load_file($f, ($multi===true), $vars); } } else { - if(defined('PHP_DIR')) { - $f = PHP_DIR.$func."/".$a.".php"; - } else { - $f = $func."/".$a.".php"; - } + $f = APP_ROOT."/".$func."/".$a.".php"; $this->_load_file($f, ($multi===true), $vars); } } diff --git a/core/Model.php b/app/core/Model.php similarity index 100% rename from core/Model.php rename to app/core/Model.php diff --git a/core/uri_library.php b/app/core/uri_library.php similarity index 100% rename from core/uri_library.php rename to app/core/uri_library.php diff --git a/libraries/.gitignore b/app/libraries/.gitignore similarity index 100% rename from libraries/.gitignore rename to app/libraries/.gitignore diff --git a/app/libraries/sqlite_library.php b/app/libraries/sqlite_library.php new file mode 100644 index 0000000..a3f4cdb --- /dev/null +++ b/app/libraries/sqlite_library.php @@ -0,0 +1,96 @@ +open_db(); + } + } + + function open_db() { + $this->db_file_name = APP_ROOT.$this->config_db_name; + $this->handle = new SQLite3($this->db_file_name); + } + + function query($query, $escape=TRUE) { + $do_query = ($escape===TRUE)?$this->handle->escapeString($query):$query; + $this->result = $this->handle->query($do_query); + return $this->result; + } + + function select($colnames) { + $colnames=(is_array($colnames)?$colnames:array($colnames)); + $this->sel_cols = $colnames; + } + + function where(array $where_arr) { + $this->where_arr = $where_arr; + } + + function get($tablename) { + // Build the 'SELECT' part of the query + $select_q= "SELECT "; + $num_sel_cols = count($this->sel_cols); + if($num_sel_cols == 0) { + $select_q.="* "; + } else { + foreach($this->sel_cols as $a_col) { + $select_q.=$a_col; + if(--$num_sel_cols > 0) + $select_q.=", "; + } + } + // Build the 'FROM' part of the query + $from_q = "FROM ".$tablename." "; + // Build the 'WHERE' part of the query + $where_q = ""; + $num_where_arr = count($this->where_arr); + if($num_where_arr > 0) { + $where_q = "WHERE "; + foreach($this->where_arr as $a_col => $a_where) { + $where_q.=$a_col." = '".$a_where."' "; + if(--$num_where_arr > 0) + $where_q.="AND "; + } + } + return $this->query($select_q.$from_q.$where_q); + } + + function fetch_array($res=NULL) { + $res=(isset($res)?$res:$this->result); + $i = 0; + while($resx = $res->fetchArray(SQLITE3_ASSOC)) { + $ret_arr[] = $resx; + } + return $ret_arr; + } + + function insert($tablename, array $val_arr) { + $ins_q = "INSERT INTO ".$tablename; + $num_cols = count($val_arr); + if($num_cols <= 0) { return false; } + + $ins_col1 = "("; + $ins_col2 = " VALUES ("; + foreach($val_arr as $col_n => $val) { + $ins_col1 .= $col_n; + $ins_col2 .= "\"".$val."\""; + if(--$num_cols > 0) { + $ins_col1 .= ", "; + $ins_col2 .= ", "; + } + } + $ins_col1 .= ")"; + $ins_col2 .= ")"; + return $this->query($ins_q.$ins_col1.$ins_col2); + } +} diff --git a/models/.gitignore b/app/models/.gitignore similarity index 100% rename from models/.gitignore rename to app/models/.gitignore diff --git a/app/views/welcome.php b/app/views/welcome.php new file mode 100644 index 0000000..a913184 --- /dev/null +++ b/app/views/welcome.php @@ -0,0 +1,52 @@ + + + + Welcome to the Anvil Framework + + + +
+ Welcome to the Anvil Framework! + +

If you place the Application Root anywhere other than the default, then you need to change the "APP_ROOT" constant in index.php

+

Next, define your main controller in the config.php file

+

That is the controller that will run if no other controller is specified.

+
+ + diff --git a/index.php b/index.php index ee8470c..7fcd1a9 100644 --- a/index.php +++ b/index.php @@ -1,18 +1,21 @@ getFullArray(); while($starting_token-- > 0) { array_shift($uri_array); } // Check if $uri->getItem(0) is a controller -if(file_exists('controllers/'.$uri_array[0].'_controller.php')) { +if(file_exists(APP_ROOT.'/controllers/'.$uri_array[0].'_controller.php')) { // File exists, set the cc_name and pop the uri_array $class_name = array_shift($uri_array); $cc_name = $class_name."_controller"; @@ -30,7 +33,7 @@ if(file_exists('controllers/'.$uri_array[0].'_controller.php')) { $cc_name = $default_controller."_controller"; } // Pull in the requested Controller -require_once('controllers/'.$cc_name.'.php'); +require_once(APP_ROOT.'/controllers/'.$cc_name.'.php'); $c_class = new $cc_name; // Were we provided a method? diff --git a/views/welcome.php b/views/welcome.php deleted file mode 100644 index af7f73c..0000000 --- a/views/welcome.php +++ /dev/null @@ -1,20 +0,0 @@ - - - - Welcome to the Anvil Framework - - - Welcome to the Anvil Framework! - Everything is pretty self explanatory. - - Define your main controller in the config.php file.
- That is the controller that will run if no other controller is specified.
-
- For more help, view the help - -