Updated a few things

This commit is contained in:
Brian Buller 2015-10-22 11:02:50 -05:00
parent eedba502d4
commit 48a9a1790c
5 changed files with 343 additions and 83 deletions

View File

@ -34,6 +34,7 @@ class Anvil {
? $this->config->item('default_controller') : array_shift($uri_array);
$start_token = $this->config->item('starting_token');
while($start_token-- > 0) { array_shift($uri_array); }
$this->request->uri_array = $uri_array;
$cc_name = '';
if(!file_exists(APP_ROOT.'/controllers/'.$class_name.'_controller.php')) {
$class_name = $this->config->item('default_controller');

View File

@ -27,6 +27,21 @@ class Controller {
}
}
public function load_helpers($helper=NULL) {
$this->load_helper($helper);
}
public function load_helper($helper=NULL) {
// All helpers end with '_helper'
if(is_array($helper)) {
foreach($helper as $k=>$m) {
$helper[$k]=$m."_helper";
}
} else {
$helper.="_helper";
}
$this->_load_files($helper, "helpers");
}
public function load_models($model=NULL) {
$this->load_model($model);
}

View File

@ -11,6 +11,7 @@ class Request {
public $server;
public $uri_array;
public $original_uri_array;
private $_anvil;
@ -19,6 +20,7 @@ class Request {
public function __construct($a) {
$this->_anvil = $a;
$this->createFromGlobals();
$this->clearFlashdata();
}
public function createFromGlobals() {
@ -32,6 +34,7 @@ class Request {
$this->files = $_FILES;
$this->server = $_SERVER;
$this->uri_array = $this->uriToArray();
$this->original_uri_array = $this->uri_array;
}
/** Is this an HTTPS request? */
@ -71,7 +74,11 @@ class Request {
}
public function process_data($index=NULL, $xss_clean=FALSE) {
$request_vars = (array)$this->json();
if(count($_FILES) > 0) {
$request_vars = $_POST;
} else {
$request_vars = (array)$this->json();
}
if($index==NULL && !empty($request_vars)) {
$post = array();
foreach(array_keys($request_vars) as $key) {
@ -115,6 +122,23 @@ class Request {
$this->setCookie($key, '', time()-3600*24*365);
}
public function setFlashdata($key, $val) {
return $this->setCookie('flashdata_'.$key, $val);
}
public function getFlashdata($key) {
if(!isset($this->cookie['flashdata_'.$key])) { return false; }
return $this->cookie['flashdata_'.$key];
}
public function clearFlashdata() {
foreach($this->cookie as $k => $v) {
if(strpos($k, 'flashdata_') !== FALSE) {
$this->clearCookie($k);
}
}
}
/**
* URI Parsing Functions
*/

View File

@ -7,6 +7,16 @@ class Response {
$this->_anvil = $a;
}
public function badrequest($txt="Bad Request") {
header('HTTP/1.0 400 Bad Request');
echo $txt;
}
public function notfound($txt="Page Not Found") {
header('HTTP/1.0 404 Not Found');
echo $txt;
}
public function redirect($url) {
header('Location: '.$url);
}

View File

@ -1,96 +1,306 @@
<?php
class Anvil_SQLite {
private $handle;
private $db_file_name;
private $config_db_name = "/db/my_db.sqlite";
private $handle;
private $db_file_name;
private $config_db_name = '/db/my_db.sqlite';
private $sel_cols = array();
private $where_arr = array();
private $sel_cols = array();
private $where_arr = array();
private $or_where_arr = array();
private $like_arr = array();
private $or_like_arr = array();
private $order_qry = '';
private $limit = -1;
private $offset = 0;
private $result;
private $result;
public $last_query='';
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 __construct($no_open=FALSE) {
if($no_open===FALSE) {
$this->open_db();
}
}
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 open_db() {
$this->db_file_name = APP_ROOT.$this->config_db_name;
$this->handle = new SQLite3($this->db_file_name);
}
function select($colnames) {
$colnames=(is_array($colnames)?$colnames:array($colnames));
$this->sel_cols = $colnames;
}
function lastId($table) {
$r = $this->handle->query('SELECT MAX(id) FROM '.$table);
$ret = $this->fetch_array($r);
$ret = array_shift($ret);
$ret = $ret['MAX(id)'];
if(empty($ret)) { return 0; }
return $ret;
}
function where(array $where_arr) {
$this->where_arr = $where_arr;
}
function query($query, $escape=TRUE) {
$do_query = ($escape===TRUE)?$this->handle->escapeString($query):$query;
$this->result = $this->handle->query($do_query);
return $this;//$this->result;
}
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 select($colnames) {
$colnames=(is_array($colnames)?$colnames:array($colnames));
$this->sel_cols = $colnames;
return $this;
}
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 like(array $like_arr) {
$this->like_arr = $like_arr;
return $this;
}
function insert($tablename, array $val_arr) {
$ins_q = "INSERT INTO ".$tablename;
$num_cols = count($val_arr);
if($num_cols <= 0) { return false; }
function likeOr(array $like_arr) {
$this->or_like_arr = $like_arr;
return $this;
}
$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);
}
function where(array $where_arr) {
$this->where_arr = $where_arr;
return $this;
}
function whereOr(array $where_arr) {
$this->or_where_arr = $where_arr;
return $this;
}
function order($colname, $ord='DESC') {
$this->order_qry = ' ORDER BY '.$colname.' '.$ord;
return $this;
}
function limit($num, $offset=0) {
$this->limit = $num;
$this->offset = $offset;
}
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 = '';
$bind_arr = array();
$num_where_arr = count($this->where_arr);
if($num_where_arr > 0) {
$where_q = 'WHERE ';
foreach($this->where_arr as $a_col => $a_where) {
if(is_array($a_where)) {
$num_where_subarr = count($a_where);
foreach($a_where as $a) {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a;
if(--$num_where_subarr > 0) {
$where_q.='AND ';
}
}
} else {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a_where;
if(--$num_where_arr > 0) {
$where_q.='AND ';
}
}
}
}
$num_where_arr = count($this->or_where_arr);
if($num_where_arr > 0) {
if(empty($where_q)) {
$where_q = 'WHERE ';
} else {
$where_q = 'OR ';
}
foreach($this->where_arr as $a_col => $a_where) {
if(is_array($a_where)) {
$num_where_subarr = count($a_where);
foreach($a_where as $a) {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a;
if(--$num_where_subarr > 0) {
$where_q.='OR ';
}
}
} else {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a_where;
if(--$num_where_arr > 0) {
$where_q.='OR ';
}
}
}
}
$num_where_arr = count($this->like_arr);
if($num_where_arr > 0) {
if(empty($where_q)) {
$where_q = 'WHERE ';
} else {
$where_q = 'AND ';
}
foreach($this->like_arr as $a_col => $a_like) {
if(is_array($a_like)) {
$num_like_subarr = count($a_like);
foreach($a_like as $a) {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a.'%';
if(--$num_like_subarr > 0) {
$where_q.='AND ';
}
}
} else {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a_like.'%';
if(--$num_like_arr > 0) {
$where_q.='AND ';
}
}
}
}
$num_where_arr = count($this->or_like_arr);
if($num_where_arr > 0) {
if(empty($where_q)) {
$where_q = 'WHERE ';
} else {
$where_q = 'OR ';
}
foreach($this->or_like_arr as $a_col => $a_like) {
if(is_array($a_like)) {
$num_like_subarr = count($a_like);
foreach($a_like as $a) {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a.'%';
if(--$num_like_subarr > 0) {
$where_q.='OR ';
}
}
} else {
$where_q.=$a_col.' LIKE ? ';
$bind_arr[]='%'.$a_like.'%';
if(--$num_like_arr > 0) {
$where_q.='OR ';
}
}
}
}
$st = $this->handle->prepare($select_q.$from_q.$where_q);
$t_cnt = 1;
foreach($bind_arr as $val) {
$st->bindParam($t_cnt, $val);
$t_cnt++;
}
$order_q = $this->order_qry;
$limit_q = '';
if($this->limit > -1) {
$limit_q = ' LIMIT '.$this->limit.', '.$this->offset;
$this->limit = -1;
$this->offset = 0;
}
$this->where_arr = array();
$this->order_qry = '';
$this->last_query = $select_q.$from_q.$where_q.$order_q.$limit_q;
$this->result = $st->execute();
return $this;
}
function fetch_array($res=NULL) {
$res=(isset($res)?$res:$this->result);
$i = 0;
$ret_arr = array();
while($resx = $res->fetchArray(SQLITE3_ASSOC)) {
$ret_arr[] = $resx;
}
return $ret_arr;
}
function insert($tablename, array $val_arr) {
$ins_q = 'INSERT INTO '.$tablename;
$ins_col1 = '(';
$ins_col2 = ' VALUES (';
$num_cols = count($val_arr);
if($num_cols <= 0) { return false; }
foreach($val_arr as $col_n => $val) {
$ins_col1 .= $col_n;
$ins_col2 .= ':'.$col_n;
if(--$num_cols > 0) {
$ins_col1 .= ", ";
$ins_col2 .= ", ";
}
}
$ins_col1 .= ")";
$ins_col2 .= ")";
$st = $this->handle->prepare($ins_q.$ins_col1.$ins_col2);
foreach($val_arr as $col_n => $val) {
$st->bindValue(':'.$col_n, $val);
}
$this->last_query = $ins_q.$ins_col1.$ins_col2;
$this->result = $st->execute();
return $this;
}
function update($tablename, array $val_arr) {
$upd_q = 'UPDATE '.$tablename;
// Build the 'SET' part of the query
$set_q = ' SET ';
$num_set_arr = count($val_arr);
$bind_set_arr = array();
if($num_set_arr > 0) {
foreach($val_arr as $a_col => $a_set) {
if(is_array($a_set) && isset($a_set['val'])) {
if(isset($a_set['query']) && $a_set['query'] == 'raw') {
$set_q.=$a_col.' = '.$a_set['val'];
} else {
$set_q.=$a_col.' = ?';
$bind_set_arr[]=$a_set['val'];
}
if(--$num_set_arr > 0) {
$set_q.='AND ';
}
} else {
$set_q.=$a_col.' = ?';
$bind_set_arr[]=$a_set;
}
}
}
// Build the 'WHERE' part of the query
$where_q = '';
$num_where_arr = count($this->where_arr);
$bind_arr = array();
if($num_where_arr > 0) {
$where_q = ' WHERE ';
foreach($this->where_arr as $a_col => $a_where) {
$where_q.=$a_col.' = ? ';
$bind_arr[]=$a_where;
if(--$num_where_arr > 0) {
$where_q.='AND ';
}
}
}
$st = $this->handle->prepare($upd_q.$set_q.$where_q);
$t_cnt = 1;
foreach($bind_set_arr as $val) {
$st->bindParam($t_cnt++, $val);
}
foreach($bind_arr as $val) {
$st->bindParam($t_cnt++, $val);
}
$this->last_query = $upd_q.$set_q.$where_q;
$this->result = $st->execute();
return $this;
}
}