Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Amit Sarker
Added: Mar 7, 2018 11:06 AM
Modified: Oct 14, 2018 7:19 AM
Views: 19
Tags: no tags
Ex_Model New
  1. <?php
  2.  
  3. class Ex_Model extends CI_Model {
  4.  
  5.     protected $table_name = '';
  6.     protected $primary_key = 'id';
  7.     protected $primary_filter = 'intval';
  8.     public $order_by = '';
  9.     public $order = '';
  10.     public $rules = array();
  11.     public $limit = 0;
  12.     public $offset = 0;
  13.     public $where_column = 'id';
  14.     public $search_column_name;
  15.  
  16.     public function __construct() {
  17.         parent::__construct();
  18.     }
  19.  
  20.     public function data_from_post($dataFields = array()) {
  21.         $dataArray = array();
  22.         foreach ($dataFields as $field) {
  23.             $dataArray[$field] = trim($this->input->post($field));
  24.         }
  25.         return $dataArray;
  26.     }
  27.  
  28.     public function get($id = null) {
  29.         if ($id != null) {
  30.             $this->db->where($this->primary_key, intval($id));
  31.             $method = 'row';
  32.             return $this->db->get($this->table_name)->$method();
  33.         } else {
  34.             $method = 'result';
  35.             return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->$method();
  36.         }
  37.     }
  38.  
  39.     public function get_all() {
  40.         return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->result();
  41.     }
  42.  
  43.     public function get_by_id($id) {
  44.         $this->db->where($this->primary_key, intval($id));
  45.         $method = 'row';
  46.         return $this->db->get($this->table_name)->$method();
  47.     }
  48.  
  49.     public function insert_data($data) {
  50.         $result = (!empty($data)) ? $this->db->insert($this->table_name, $data) : FALSE;
  51.         return $result ? intval($this->db->insert_id()) : 0;
  52.     }
  53.  
  54.     public function currently_insert_id() {
  55.         return $this->db->insert_id();
  56.     }
  57.  
  58.     public function update_data($data) {
  59.         if (!empty($data)) {
  60.             $this->db->set($data);
  61.             $this->db->where($this->where_column, $data[$this->where_column]);
  62.             return $this->db->update($this->table_name);
  63.         } else {
  64.             return FALSE;
  65.         }
  66.     }
  67.  
  68.     public function get_where($where = array(), $single = FALSE) {
  69.         if (!empty($where)) {
  70.             $this->db->where($where);
  71.             $method = ($single) ? 'row' : 'result';
  72.             return $this->db->limit($this->limit, $this->offset)->get($this->table_name)->$method();
  73.         } else {
  74.             return null;
  75.         }
  76.     }
  77.  
  78.     public function delete($id) {
  79.         $filter = $this->primary_filter;
  80.         $id = $filter($id);
  81.         if (!$id) {
  82.             return FALSE;
  83.         }
  84.         $this->db->where($this->primary_key, $id);
  85.         $this->db->limit(1);
  86.         return $this->db->delete($this->table_name);
  87.     }
  88.  
  89.     public function delete_where($column_name, $value) {
  90.  
  91.         $this->db->where($column_name, $value);
  92.         return $this->db->delete($this->table_name);
  93.     }
  94.  
  95.     public function delete_all($where = array()) {
  96.  
  97.         if (!empty($where)) {
  98.             $this->db->where($where);
  99.         }
  100.         return $this->db->delete($this->table_name);
  101.     }
  102.  
  103.     public function set_deleted($id = 0) {
  104.         $this->db->set('is_deleted', 1);
  105.         $this->db->where($this->primary_key, $id);
  106.         return $this->db->update($this->table_name);
  107.     }
  108.  
  109.     public function is_column_value_exist($column_name = '', $value = '') {
  110.         if ((!empty($column_name) && (!empty($value)))) {
  111.             $this->db->where($column_name, $value);
  112.             $total_count = $this->db->get($this->table_name)->num_rows();
  113.             return ($total_count > 0) ? TRUE : FALSE;
  114.         } else {
  115.             return TRUE;
  116.         }
  117.     }
  118.  
  119.     public function is_column_value_exist_with_another_row($column_name = '', $value = '', $id = 0) {
  120.         if ((!empty($column_name) && (!empty($value)) && ($id > 0))) {
  121.             $this->db->where(array($column_name => $value, 'id!=' => $id));
  122.             $total_count = $this->db->get($this->table_name)->num_rows();
  123.             return ($total_count > 0) ? TRUE : FALSE;
  124.         } else {
  125.             return TRUE;
  126.         }
  127.     }
  128.  
  129.     public function form_validation_custom_with_callable_method($name = '', $label = '', $callback_function = '', $message = '') {
  130.         $ci = &get_instance();
  131.         $ci->form_validation->set_rules(
  132.                 $name, $label, array(
  133.             array($name . '_callable', array($this, $callback_function))
  134.                 )
  135.         );
  136.         $ci->form_validation->set_message($name . '_callable', $message);
  137.     }
  138.  
  139.     public function get_tables_fields() {
  140.         return $this->db->list_fields($this->table_name);
  141.     }
  142.  
  143.     public function insert_batch($data = null) {
  144.  
  145.         if (!empty($data)) {
  146.             return $this->db->insert_batch($this->table_name, $data);
  147.         } else {
  148.             return FALSE;
  149.         }
  150.     }
  151.  
  152.     public function get_table_name() {
  153.         return $this->table_name;
  154.     }
  155.  
  156.     public function update($data = array(), $conditions = array()) {
  157.         $this->db->set($data);
  158.         $this->db->where($conditions);
  159.         return $this->db->update($this->table_name);
  160.     }
  161.  
  162.     public function update_batch($data = null, $index = null) {
  163.         $index = ($index == null) ? 'id' : $index;
  164.         if (!empty($data)) {
  165.             $result = $this->db->update_batch($this->table_name, $data, $index);
  166.             return !empty($result) ? (($result > 0) ? TRUE : FALSE) : FALSE;
  167.         } else {
  168.             return FALSE;
  169.         }
  170.     }
  171.  
  172.     public function upload_images($file, $upload_path) {
  173.         $path = '';
  174.         $response_data = array();
  175. //        $new_name = $file['file']['name'];
  176.         $config['file_name'] = date("Y_m_d_H_i_s");
  177.         $config['upload_path'] = './' . $upload_path;
  178.         $config['allowed_types'] = 'gif|jpg|png|jpeg';
  179.         $config['max_size'] = 250;
  180.         $this->load->library('upload', $config);
  181.         $is_upload = $this->upload->do_upload('file');
  182.         $errors = '';
  183.         if (!$is_upload) {
  184.             $errors = $this->upload->display_errors();
  185.         } else {
  186.             $data = array('upload_data' => $this->upload->data());
  187.             $path = str_replace('.', '', $upload_path) . $data['upload_data']['file_name'];
  188.         }
  189.         $response_data = array(
  190.             'is_uploaded' => $is_upload,
  191.             'path' => $path,
  192.             'error' => $errors,
  193.         );
  194.         return $response_data;
  195.     }
  196.  
  197.     public function delete_uploaded_image($image_path) {
  198.         if (!empty($image_path)) {
  199.             $image_path = './' . $image_path;
  200.             unlink(($image_path));
  201.         }
  202.     }
  203.  
  204.     public function get_uploaded_image_path($id = 0, $delete_image_value = 0, $image_url = '', $image_upload_path = '') {
  205.         $image_path = '';
  206.         if (!empty($_FILES['file'])) {
  207.             $image_upload_result = $this->upload_images($_FILES['file'], $image_upload_path);
  208.             $is_uploaded = (!empty($image_upload_result)) ? array_key_exists('is_uploaded', $image_upload_result) ? $image_upload_result['is_uploaded'] : FALSE : FALSE;
  209.             if ($is_uploaded) {
  210.                 if (!empty($image_url) && file_exists($image_url)) {
  211.                     $this->delete_uploaded_image($image_url);
  212.                 }
  213.                 $image_path = (!empty($image_upload_result)) ? array_key_exists('path', $image_upload_result) ? $image_upload_result['path'] : '' : '';
  214.             } else {
  215.                 $image_path = $image_url;
  216.             }
  217.         }
  218.         if (intval($delete_image_value) == 1) {
  219.             if (file_exists($image_url)) {
  220.                 $this->delete_uploaded_image($image_url);
  221.             }
  222.             $image_path = '';
  223.         }
  224.         return $image_path;
  225.     }
  226.  
  227.     public function get_maximum_number($column_name) {
  228.         $this->db->select_max($column_name);
  229.         return $this->db->get($this->table_name)->row();
  230.     }
  231.  
  232.     public function get_last_query() {
  233.         return $string = $this->db->last_query();
  234.     }
  235.  
  236.     public function email_send($data = array()) {
  237.         $is_sent = FALSE;
  238.         $data['attach'] = array_key_exists('attach', $data) ? $data['attach'] : '';
  239.         $this->load->library('email');
  240.         $this->email->clear(TRUE);
  241.         $this->email->initialize(smtp_config());
  242.         $this->email->from($data['from_email'], $data['from_title']);
  243.         $this->email->to($data['to_email_array']);
  244.         $this->email->subject($data['subject']);
  245. //        $this->email->set_mailtype("html");
  246.         $this->email->message($data['body']);
  247.         $this->email->attach($data['attach']);
  248.         try {
  249.             $is_sent = $this->email->send();
  250.         } catch (Exception $e) {
  251.             $is_sent = FALSE;
  252.         }
  253.         return $is_sent;
  254.     }
  255.  
  256.     public function delete_uploaded_file($path) {
  257.         if (!empty($path)) {
  258.             $path = (file_exists($path)) ? './' . $path : '';
  259.             unlink(($path));
  260.         }
  261.     }
  262.  
  263.     public function get_all_db_tables() {
  264.         return $tables = $this->db->list_tables();
  265.     }
  266.  
  267.     public function drop_all_table_from_db($data = NULL) {
  268.         $is_drop = FALSE;
  269.         $this->load->dbforge();
  270.         $tables = ($data == NULL) ? $this->db->list_tables() : (is_array($data) ? $data : (array) $data);
  271.         if (!empty($tables)) {
  272.             foreach ($tables as $table) {
  273.                 $this->dbforge->drop_table($table, TRUE); //Produces: DROP TABLE IF EXISTS table_name
  274.             }
  275.             $is_drop = TRUE;
  276.         }
  277.         return $is_drop;
  278.     }
  279.  
  280.     public function get_db_clear($data = NULL, $truncate = TRUE) {
  281.         $is_delete = FALSE;
  282.         $tables = ($data == NULL) ? $this->db->list_tables() : (is_array($data) ? $data : (array) $data);
  283.         if (!empty($tables)) {
  284.             foreach ($tables as $table) {
  285.                 $is_delete = (boolval($truncate)) ? $this->db->truncate($table) : $this->db->empty_table($table);
  286.             }
  287.         }
  288.         return $is_delete;
  289.     }
  290.  
  291. }
  292.