Various Modifications

Move everything into App Directory
Add a SQLite Library
This commit is contained in:
Brian Buller 2012-09-07 12:19:33 -05:00
parent 52b3f04ade
commit a371f1b0ff
11 changed files with 162 additions and 39 deletions

View File

@ -1,7 +1,7 @@
<?php
class Welcome_controller extends Controller {
public function __construct() { }
public function __construct() {}
public function index() {
$this->load_views('welcome');

View File

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

View File

@ -0,0 +1,96 @@
<?php
class Anvil_SQLite {
private $handle;
private $db_file_name;
private $config_db_name = "/db/my_db.sqlite";
private $sel_cols = array();
private $where_arr = array();
private $result;
function __construct($no_open=FALSE) {
if($no_open===FALSE) {
$this->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);
}
}

52
app/views/welcome.php Normal file
View File

@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the Anvil Framework</title>
<style type="text/css">
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
color: #333;
}
.welcome_div {
position:relative;
margin:15px 0;
padding:39px 19px 14px;
background-color:#FFF;
border:1px solid #DDD;
-webkit-border-radius: 4px;
-moz-border-radius: 4px;
border-radius: 4px;
}
.welcome_div::after {
content: "Anvil_PHP";
position: absolute;
top: -1px;
left: -1px;
padding:3px 7px;
font-size: 12px;
font-weight: bold;
background-color: whiteSmoke;
border: 1px solid #DDD;
color: #9DA0A4;
-webkit-border-radius: 4px 0 4px 0;
-moz-border-radius: 4px 0 4px 0;
border-radius: 4px 0 4px 0;
}
</style>
</head>
<body style="text-align:left; padding-left:50px;">
<div class="welcome_div">
Welcome to the Anvil Framework!
<ul>
<li>Controllers go in the app/controllers directory</li>
<li>Models go in the app/models directory</li>
<li>Views go in the app/views directory</li>
<li>Libraries/Add-ons go in the app/libraries directory</li>
</ul>
<p>If you place the Application Root anywhere other than the default, then you need to change the "APP_ROOT" constant in index.php</p>
<p>Next, define your main controller in the config.php file</p>
<p>That is the controller that will run if no other controller is specified.</p>
</div>
</body>
</html>

View File

@ -1,18 +1,21 @@
<?php
require_once('config.php');
define("PHP_ROOT","");
define("APP_ROOT","app");
require_once(APP_ROOT.'/config.php');
// Load up the base classes
require_once('core/Controller.php');
require_once('core/Model.php');
require_once(APP_ROOT.'/core/Controller.php');
require_once(APP_ROOT.'/core/Model.php');
// We need the uri library for things to work
require_once('core/uri_library.php');
require_once(APP_ROOT.'/core/uri_library.php');
// Load up the globals
foreach($global_libraries as $alib) {
require_once('libraries/'.$alib.'_library.php');
require_once(APP_ROOT.'/libraries/'.$alib.'_library.php');
}
foreach($global_models as $amod) {
require_once('models/'.$amod.'_model.php');
require_once(APP_ROOT.'/models/'.$amod.'_model.php');
}
// Buffer all output for speed!
ob_start();
@ -21,7 +24,7 @@ $uri = new Uri_library($_SERVER['REQUEST_URI']);
$uri_array = $uri->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?

View File

@ -1,20 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<title>Welcome to the Anvil Framework</title>
</head>
<body style="text-align:left; padding-left:50px;">
Welcome to the Anvil Framework!
Everything is pretty self explanatory.
<ul>
<li>Controllers go in the controllers directory</li>
<li>Models go in the models directory</li>
<li>Views go in the views directory</li>
<li>Libraries/Add-ons go in the libraries directory</li>
</ul>
Define your main controller in the config.php file.<br />
That is the controller that will run if no other controller is specified.<br />
<br />
For more help, view the help
</body>
</html>