<?php
class Ex_Model extends CI_Model {
protected $table_name = '';
protected $primary_key = 'id';
protected $primary_filter = 'intval';
public $order_by = '';
public $order = '';
public $limit = 0;
public $offset = 0;
public $where_column = 'id';
public $search_column_name;
public function __construct() {
parent::__construct();
}
public function data_from_post
($dataFields = array()) {
foreach ($dataFields as $field) {
$dataArray[$field] = trim($this->input->post($field));
}
return $dataArray;
}
public function get($id = null) {
if ($id != null) {
$this->db->where($this->primary_key, intval($id));
$method = 'row';
return $this->db->get($this->table_name)->$method();
} else {
$method = 'result';
return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->$method();
}
}
public function get_all() {
return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->result();
}
public function get_by_id($id) {
$this->db->where($this->primary_key, intval($id));
$method = 'row';
return $this->db->get($this->table_name)->$method();
}
public function insert_data($data) {
$result = (!empty($data)) ?
$this->db->insert($this->table_name, $data) : FALSE;
return $result ?
intval($this->db->insert_id()) : 0;
}
public function currently_insert_id() {
return $this->db->insert_id();
}
public function update_data($data) {
$this->db->set($data);
$this->db->where($this->where_column, $data[$this->where_column]);
return $this->db->update($this->table_name);
} else {
return FALSE;
}
}
public function get_where
($where = array(), $single = FALSE) {
$this->db->where($where);
$method = ($single) ? 'row' : 'result';
return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->$method();
} else {
return null;
}
}
public function delete($id) {
$filter = $this->primary_filter;
$id = $filter($id);
if (!$id) {
return FALSE;
}
$this->db->where($this->primary_key, $id);
$this->db->limit(1);
return $this->db->delete($this->table_name);
}
public function delete_where($column_name, $value) {
$this->db->where($column_name, $value);
return $this->db->delete($this->table_name);
}
public function delete_all
($where = array()) {
$this->db->where($where);
}
return $this->db->delete($this->table_name);
}
public function set_deleted($id = 0) {
$this->db->set('is_deleted', 1);
$this->db->where($this->primary_key, $id);
return $this->db->update($this->table_name);
}
public function is_column_value_exist($column_name = '', $value = '') {
$this->db->where($column_name, $value);
$total_count = $this->db->get($this->table_name)->num_rows();
return ($total_count > 0) ? TRUE : FALSE;
} else {
return TRUE;
}
}
public function is_column_value_exist_with_another_row($column_name = '', $value = '', $id = 0) {
if ((!empty($column_name) && (!empty($value)) && ($id > 0))) {
$this->db->where(array($column_name => $value, 'id!=' => $id));
$total_count = $this->db->get($this->table_name)->num_rows();
return ($total_count > 0) ? TRUE : FALSE;
} else {
return TRUE;
}
}
public function form_validation_custom_with_callable_method($name = '', $label = '', $callback_function = '', $message = '') {
$ci = &get_instance();
$ci->form_validation->set_rules(
array($name . '_callable', array($this, $callback_function))
)
);
$ci->form_validation->set_message($name . '_callable', $message);
}
public function get_tables_fields() {
return $this->db->list_fields($this->table_name);
}
public function insert_batch($data = null) {
return $this->db->insert_batch($this->table_name, $data);
} else {
return FALSE;
}
}
public function get_table_name() {
return $this->table_name;
}
public function update
($data = array(), $conditions = array()) {
$this->db->set($data);
$this->db->where($conditions);
return $this->db->update($this->table_name);
}
public function update_batch($data = null, $index = null) {
$index = ($index == null) ? 'id' : $index;
$result = $this->db->update_batch($this->table_name, $data, $index);
return !empty($result) ?
(($result > 0) ?
TRUE : FALSE) : FALSE;
} else {
return FALSE;
}
}
public function upload_images($file, $upload_path) {
$path = '';
$response_data = array();
// $new_name = $file['file']['name'];
$config['file_name'] = date("Y_m_d_H_i_s");
$config['upload_path'] = './' . $upload_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 250;
$this->load->library('upload', $config);
$is_upload = $this->upload->do_upload('file');
$errors = '';
if (!$is_upload) {
$errors = $this->upload->display_errors();
} else {
$data = array('upload_data' => $this->upload->data());
$path = str_replace('.', '', $upload_path) . $data['upload_data']['file_name'];
}
'is_uploaded' => $is_upload,
'path' => $path,
'error' => $errors,
);
return $response_data;
}
public function delete_uploaded_image($image_path) {
if (!empty($image_path)) {
$image_path = './' . $image_path;
}
}
public function get_uploaded_image_path($id = 0, $delete_image_value = 0, $image_url = '', $image_upload_path = '') {
$image_path = '';
if (!empty($_FILES['file'])) {
$image_upload_result = $this->upload_images($_FILES['file'], $image_upload_path);
$is_uploaded = (!empty($image_upload_result)) ?
array_key_exists('is_uploaded', $image_upload_result) ?
$image_upload_result['is_uploaded'] : FALSE : FALSE;
if ($is_uploaded) {
$this->delete_uploaded_image($image_url);
}
$image_path = (!empty($image_upload_result)) ?
array_key_exists('path', $image_upload_result) ?
$image_upload_result['path'] : '' : '';
} else {
$image_path = $image_url;
}
}
if (intval($delete_image_value) == 1) {
$this->delete_uploaded_image($image_url);
}
$image_path = '';
}
return $image_path;
}
public function get_maximum_number($column_name) {
$this->db->select_max($column_name);
return $this->db->get($this->table_name)->row();
}
public function get_last_query() {
return $string = $this->db->last_query();
}
public function email_send
($data = array()) {
$is_sent = FALSE;
$this->load->library('email');
$this->email->clear(TRUE);
$this->email->initialize(smtp_config());
$this->email->from($data['from_email'], $data['from_title']);
$this->email->to($data['to_email_array']);
$this->email->subject($data['subject']);
// $this->email->set_mailtype("html");
$this->email->message($data['body']);
$this->email->attach($data['attach']);
try {
$is_sent = $this->email->send();
} catch (Exception $e) {
$is_sent = FALSE;
}
return $is_sent;
}
public function delete_uploaded_file($path) {
}
}
public function get_all_db_tables() {
return $tables = $this->db->list_tables();
}
public function drop_all_table_from_db($data = NULL) {
$is_drop = FALSE;
$this->load->dbforge();
$tables = ($data == NULL) ?
$this->db->list_tables() : (is_array($data) ?
$data : (array) $data);
foreach ($tables as $table) {
$this->dbforge->drop_table($table, TRUE); //Produces: DROP TABLE IF EXISTS table_name
}
$is_drop = TRUE;
}
return $is_drop;
}
public function get_db_clear($data = NULL, $truncate = TRUE) {
$is_delete = FALSE;
$tables = ($data == NULL) ?
$this->db->list_tables() : (is_array($data) ?
$data : (array) $data);
foreach ($tables as $table) {
$is_delete = (boolval($truncate)) ? $this->db->truncate($table) : $this->db->empty_table($table);
}
}
return $is_delete;
}
}