Created
April 9, 2012 18:42
-
-
Save jfensign/2345369 to your computer and use it in GitHub Desktop.
Revisions
-
jfensign revised this gist
Apr 9, 2012 . 1 changed file with 20 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,11 @@ <?php if(! defined("BASEPATH")) exit("No direct script access allowed"); abstract class MY_Model extends CI_Model { private $file_dest; private $table; private $fields; private $field_values; public function __construct() { parent::__construct(); @@ -28,7 +27,7 @@ public function get_field_meta($table_name) { } //reads data from table when a condition is met public function read_values_where($table_name, $condition=NULL, $field=NULL) { $this->table = strtolower($table_name); $field_name = (!isset($field) ? "id" : $field); @@ -46,13 +45,15 @@ public function read_values_where($table_name, $condition = NULL, $field = NULL) } public function create_update_logic($table_name,$operation,$record=NULL,$field=NULL) { $this->table = $table_name; $write = fopen("insert_data.txt", "a"); //path to image upload directory $this->file_dest = realpath(str_replace('system', 'assets/img', BASEPATH)); $path = $this->file_dest.'/'.strtolower($this ->uri ->segment(2)); //loop through table's fields and retrieve metadata foreach($this->get_field_meta($this->table) as $field) { @@ -66,7 +67,9 @@ public function create_update_logic($table_name, $operation, $record = NULL, $fi for($k = 1; $k <= count($_FILES); $k++) { $file = $path.'/'.$_FILES['userfile_'.$k]['name']; if(move_uploaded_file($_FILES['userfile_'.$k]['tmp_name'], $file)) { $upload_val = "'".base_url()."assets/img/".$this ->uri ->segment(2) $upload_val .= '/'.$_FILES['userfile_'.$k]['name']."'"; $this->fields[] = "`image_".$k."`"; @@ -84,16 +87,18 @@ public function create_update_logic($table_name, $operation, $record = NULL, $fi } else { $this->fields[] = $field->name; $postVal = $this->input->post($field->name); if(is_numeric($postVal)) { if(strpos("date", $field->name)) { $this->field_values[] = date('Y-m-d H:i:s', strtotime($postVal)); } else { $this->field_values[] = mysql_real_escape_string($postVal); } } else { $this->field_values[] = "'".mysql_real_escape_string($postVal)."'"; } } } @@ -148,4 +153,4 @@ public function destroy($table ,$field_id, $row_id) { } } ?> -
jfensign created this gist
Apr 9, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,151 @@ <?php if(! defined("BASEPATH")) exit("No direct script access allowed"); abstract class MY_Model extends CI_Model { private $file_dest, $table, $fields, $field_values; public function __construct() { parent::__construct(); $this->load->database(); $this->load->helper(array('date', 'html')); $this->load->library(array('ion_auth')); $this->table = (string) ''; $this->fields = array(); $this->field_values = ''; $this->file_dest = ''; } //Retrieves table's field meta public function get_field_meta($table_name) { $this->table = $table_name; return $this->db->field_data($this->table); } //reads data from table when a condition is met public function read_values_where($table_name, $condition = NULL, $field = NULL) { $this->table = strtolower($table_name); $field_name = (!isset($field) ? "id" : $field); $sql = "SELECT * FROM `$table_name` "; if(isset($condition)) { $sql .= "WHERE $field = '$condition'"; } $sql .= ";"; $query = $this->db->query($sql); return $query->num_rows() > 0 ? $query->result_array() : FALSE; } public function create_update_logic($table_name, $operation, $record = NULL, $field = NULL) { $this->table = $table_name; $write = fopen("insert_data.txt", "a"); //path to image upload directory $this->file_dest = realpath(str_replace('system', 'assets/img', BASEPATH)); $path = $this->file_dest.'/'.strtolower($this->uri->segment(2)); //loop through table's fields and retrieve metadata foreach($this->get_field_meta($this->table) as $field) { //id is pk, auto-increment if($field->name != "id") { if(substr($field->name, 0, -2) == "image") { //if upload destination does not exist if(!is_dir($path)) { @mkdir($path, 0777); } for($k = 1; $k <= count($_FILES); $k++) { $file = $path.'/'.$_FILES['userfile_'.$k]['name']; if(move_uploaded_file($_FILES['userfile_'.$k]['tmp_name'], $file)) { $upload_val = "'".base_url()."assets/img/".$this->uri->segment(2); $upload_val .= '/'.$_FILES['userfile_'.$k]['name']."'"; $this->fields[] = "`image_".$k."`"; $this->field_values[] = $upload_val; } else { ob_start(); $errors = $_FILES['userfile_'.$k]['errors']; $out = print_r($errors); fwrite($write, $out); ob_end_clean(); } } } else { $this->fields[] = $field->name; if(is_numeric($this->input->post($field->name))) { if($field->name == "insert_data") { $this->field_values[] = date('Y-m-d H:i:s', strtotime($this->input->post($field->name))); } else { $this->field_values[] = mysql_real_escape_string($this->input->post($field->name)); } } else { $this->field_values[] = "'".mysql_real_escape_string($this->input->post($field->name))."'"; } } } } if($operation == "add") { $sql = "INSERT INTO `$this->table` "; $sql .= "(".implode(', ', $this->fields).") "; $sql .= "VALUES (".implode(', ', $this->field_values).")"; fwrite($write, $sql); } if($operation == "edit" ) { $field_count = count($this->fields); $sql = "UPDATE $this->table "; $sql .= "SET "; for($i=0; $i <= $field_count - 1; $i++) { $sql .= $this->fields[$i]." = ".$this->field_values[$i]; $i != $field_count - 1 ? $sql .= ", " : $sql .= " "; } if(!is_numeric($this->uri->segment(3)))) { throw new Exception("Improperly formatted URL"); } else { $sql .= "WHERE id = '$record;"; } } return $this->db->query($sql); } public function destroy($table ,$field_id, $row_id) { $row_id = str_replace('_', ' ', $row_id); foreach(MY_Model::get_field_meta($table) as $field) { if($field->name == "image") { $sql = "SELECT image from $table;"; $query = $this->db->query($sql); if($query->num_rows() > 0) { foreach($query->result() as $q) { $data[] = $q; } unlink($data); } } } $sql = "DELETE FROM $table "; $sql .= "WHERE $field_id = '$row_id';"; return $this->db->query($sql); } } ?>