Advertisement
Guest User

Db_engine.php

a guest
May 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.03 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  * Description of Db_engine
  11.  *
  12.  * @author ian
  13.  */
  14. class Db_engine {
  15.     //put your code here
  16.    
  17.     protected $db;
  18.     private $sql;
  19.     private $bind_marker;
  20.  
  21.     public function __construct() {
  22.         $ci =& get_instance();
  23.         $this->db = $ci->db;
  24.         $this->bind_marker = $this->db->bind_marker;
  25.     }
  26.    
  27.     public function generate_datatables_input($filters = array(), $column_search = array(), $column_order = array(), $order = array(), $pagination = false){
  28.      
  29.       if(isset($filters["search"]["value"]) && !empty($filters["search"]["value"])){
  30.         // remove null for global search
  31.         $tmp = $column_search;
  32.         $column_search = array();
  33.         foreach ($tmp as $i => $v) {
  34.           if($v == null || empty($v)) continue;
  35.           $column_search[] = $v;
  36.         }
  37.        
  38.         $i=0;        
  39.         foreach ($column_search as $item) {          
  40.           if($i == 0){
  41.             $this->db->group_start();
  42.             $this->db->like($item, $filters["search"]["value"]);
  43.           } else{
  44.             $this->db->or_like($item, $filters["search"]["value"]);
  45.           }
  46.  
  47.           if(count($column_search) - 1 == $i){
  48.             $this->db->group_end();
  49.           }
  50.           $i++;
  51.         }                  
  52.       }
  53.  
  54.       $i=0;
  55.       foreach ($column_search as $index => $item) {          
  56.         if(isset($filters['columns'][$index])){
  57.           if(!empty($filters["columns"][$index]['search']['value'])){
  58.             $value = $filters["columns"][$index]['search']['value'];  
  59.             $this->db->like($item, trim($value));
  60.           }
  61.         }
  62.  
  63.         $i++;
  64.       }
  65.  
  66.       if(isset($filters["order"])){          
  67.         $this->db->order_by($column_order[$filters["order"][0]["column"]], $filters["order"][0]["dir"]);
  68.       } else if(isset ($order)){
  69.         $this->db->order_by(key($order), $order[key($order)]);
  70.       }
  71.  
  72.       if($pagination){
  73.         $start  = isset($filters["start"]) ? (int) $filters["start"] : 0;
  74.         $length = isset($filters["length"]) ? (int) $filters["length"] : -1;
  75.        
  76.         if($length != -1) $this->db->limit($length, $start);        
  77.       }
  78.     }
  79.    
  80.     public function table_get_rows_count($table_name = ""){
  81.       if(empty($table_name)) return 0;
  82.      
  83.       $this->db->select("count(1) as count");
  84.       $this->db->from($table_name);
  85.       $res = $this->db->get()->result_array();
  86.       $res = $res[0];
  87.       return $res["count"];
  88.     }
  89.    
  90.     final protected function query($sql, $bind=false, $return_object = true){
  91.         $this->sql = $sql;        
  92.         if(is_array($bind) and count($bind)>0){
  93.             $bind = $this->process_bind($bind);
  94.         }
  95.         $query = $this->db->query($this->sql, $bind, $return_object);
  96.         return $query->result_array();
  97.     }
  98.  
  99.     private function process_bind($bind){
  100.         $bindOrder = null;
  101.         $bindList = null;
  102.  
  103.         $pattern = "/[^']:[A-Za-z0-9_]+[^']/";
  104.         $preg = preg_match_all($pattern, $this->sql, $matches, PREG_OFFSET_CAPTURE);
  105.         if($preg !== 0 and $preg !== false){
  106.             foreach($matches[0] as $key=>$val){
  107.                 $bindOrder[$key] = trim($val[0]);
  108.             }
  109.             foreach($bindOrder as $field){
  110.                 $this->sql = str_replace($field, $this->bind_marker, $this->sql);
  111.                 $bindList[] = $bind[$field];
  112.             }
  113.         }else{
  114.             $bindList = $bind;
  115.         }
  116.  
  117.         return $bindList;
  118.     }
  119.    
  120.     function get_id_by_hash_id($hash_id = "", $table_name = ""){
  121.         if(empty($hash_id)) return 0;        
  122.         if(trim($hash_id) == "0") return 0;
  123.         if(empty($table_name)) return 0;
  124.        
  125.         $this->db->select("id");
  126.         $this->db->from($table_name);
  127.         $this->db->where("md5(id)", $hash_id);
  128.         $res = $this->db->get()->result_array();
  129.        
  130.         if(count($res) == 0) return 0;
  131.         $res = $res[0];
  132.        
  133.         return (int)$res['id'];
  134.     }
  135.    
  136.     function get_parent_id_by_parent_hash_id($parent_hash_id = "", $table_name){
  137.         if(empty($parent_hash_id)) return 0;        
  138.         if(trim($parent_hash_id) == "0") return 0;
  139.         if(empty($table_name)) return 0;                
  140.        
  141.         $this->db->select("parent_id");
  142.         $this->db->from($table_name);
  143.         $this->db->where("md5(parent_id)", $parent_hash_id);                
  144.        
  145.         $res = $this->db->get()->result_array();
  146.                
  147.        
  148.         if(count($res) == 0) return 0;
  149.         $res = $res[0];
  150.        
  151.         return (int)$res['parent_id'];
  152.     }
  153.  
  154.     function get_app_setting_list(){
  155.         $this->db->select("*");
  156.         $this->db->from("app_settings");
  157.         $res = $this->db->get()->result_array();
  158.        
  159.         if(count($res) == 0) return $res;
  160.         return $res[0];
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement