Advertisement
gundambison

datatable02-model

Aug 25th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.62 KB | None | 0 0
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. //===================
  6. // ver : 2018-05-07
  7. //===================
  8.  
  9. class Wilayah_m extends CI_Model {
  10.  
  11.     public $tables;
  12.     public $db_main, $model_name, $model_short_name;
  13.  
  14.     public function __CONSTRUCT() {
  15.         parent::__construct();
  16.         $this->db_main = $this->load->database('default', TRUE);
  17. //load db only in model.. not controller.
  18.         $this->tables = array(
  19.             'main' => 'sda_wilayah',
  20.             'city' => 'kabupaten' //ignore this
  21.         );
  22.         $this->model_name = 'wilayah_m';
  23.         $this->model_short_name = 'wilayah';
  24.     }
  25.  
  26.     function _datatables($params = FALSE) {
  27.         $get = $params;
  28.         $result = array(
  29.             'raw' => array(),
  30.             'data' => array(),
  31.             'sql' => array(),
  32.             'time' => array(),
  33.             'params' => $params,
  34.             'head' => array(),
  35.         );
  36.         if ($params === FALSE) {
  37.             return $result;
  38.         }
  39.  
  40.         //====start
  41.         $start = isset($get['start']) ? $get['start'] : 0;
  42.         $limit = isset($get['length']) ? $get['length'] : 10;
  43.  
  44.         $result['draw'] = isset($get['draw']) ? $get['draw'] : rand(10000, 99999);
  45.         $result['recordsTotal'] = $result['recordsFiltered'] = 0; //default
  46.         $result['time']['start'] = microtime(); //wajib ada.. buat debug proses mana yg lambat
  47.  
  48.         $filters = array();
  49.         $result['recordsTotal'] = $result['recordsFiltered'] = $this->_counts($filters);
  50.         $result['sql'][]=$this->db_main->last_query();
  51.         /*
  52.          * Konfigurasi sort by  
  53.          * You can put the config above.. but for easy learning.. I put in here
  54.          */
  55.         $result['time']['sort 1'] = microtime();
  56.         $column = "id"; //default
  57.         if (isset($get['order'][0])) {
  58.             $pos_column = $get['order'][0]['column'];
  59.             $filters['order_dir'] = $get['order'][0]['dir'];
  60.             /*
  61.              * I'm not using case... in order to readable for some
  62.              * programer..
  63.              */
  64.             if ($pos_column == 0) {
  65.                 $column = 'c1.num';
  66.             }
  67.             if ($pos_column == 1) {
  68.                 $column = 'c1.id';
  69.             }
  70.             if ($pos_column == 2) {
  71.                 $column = 'c1.nama';
  72.             }
  73.  
  74.             //for advance use.. like using 2 ordering column.. We need to
  75.             //change this
  76.             $filters['order_by'] = array(
  77.                 $column,
  78.                 $filters['order_dir']
  79.             );
  80.         }
  81.         $result['time']['sort 2'] = microtime();
  82.         //===========SEARCh
  83.         $SEARCH = isset($get['search']['value']) ? $get['search']['value'] : FALSE;
  84.         $filters['datatable_search'] = $SEARCH;
  85.         $raw_data = $this->_gets($filters, $limit, $start, FALSE, FALSE);
  86.         $result['sql'][]=$this->db_main->last_query();
  87.         //update total
  88.         $result['recordsFiltered'] = $this->_counts($filters);
  89.        
  90.         $result['sql'][]=$this->db_main->last_query();
  91.         $result['raw'] = $raw_data;
  92.         //return $result;
  93.         /* ===== Parse Data ===== */
  94.         $result['time']['parse begin'] = microtime();
  95.         $n = 0;
  96.         $result['head'] = array(
  97.             'update',
  98.             'code',
  99.             'nama',
  100.             'parent',
  101.             'action'
  102.         );
  103.         $data = array();
  104.         $no = $start;
  105.         foreach ($raw_data as $id => $row) {
  106.             $no++;
  107.             $res = array(
  108.                 $no,
  109.                 $row['id'],
  110.                 $row['nama'],
  111.                 '-'
  112.             );
  113.             $data[] = $res;
  114.         }
  115.         $result['raw'][] = array($filters, $raw_data);
  116.  
  117.         $result['time']['parse end'] = microtime();
  118.         $result['data'] = $data;
  119.         $result['time']['end'] = microtime();
  120.         /*
  121.          * draw
  122.          * order
  123.          *
  124.          */
  125.         return $result;
  126.     }
  127.  
  128.     public function _counts($filter = array()) {
  129.         $count = $this->_gets($filter, 1, 0, TRUE);
  130.         return $count;
  131.     }
  132.  
  133.     public function _gets($filter = array(), $limit = 10, $start = 0, $count = FALSE, $debug = FALSE) {
  134.         $times[] = microtime(true);
  135.         $head_table = 'main';
  136.         $table = $this->tables[$head_table];
  137.         //=========
  138.         $this->db_main->from("$table c1");
  139.  
  140.         $times[] = microtime(true);
  141.  
  142. //====DATATABLES=====================
  143.         if (isset($filter['datatable_search'])) {
  144.             if (strlen($filter['datatable_search']) > 2) {
  145. //----Manual-----------
  146.                 /*
  147.                  * code, name  
  148.                  */
  149.                 $this->db_main->group_start()
  150.                         ->or_like("c1.id", strtoupper($filter['datatable_search']))
  151.                         ->or_like("c1.nama", strtoupper($filter['datatable_search']))
  152.                         ->group_end();
  153.             } else {
  154.                 unset($filter['datatable_search']);
  155.             }
  156.         }
  157.         //------------------Show all include delete--------------
  158.         //==============COUNT=============
  159.         if ($count) {
  160.             $this->db_main->select("count(*) c");
  161.             $data = $this->db_main->get()->row_array();
  162.             return $data['c'];
  163.         }
  164.  
  165.         //==================SELECT==========    
  166.         if (isset($filter['selects'])) {
  167.             $this->db_main->select($filter['selects']);
  168.             $select = TRUE;
  169.         }
  170.         //================order by=== NEED 2 FIX
  171.         if (!isset($filter['order_by'])) {
  172.             $fields = "c1.num";
  173.             $this->db_main->order_by($fields, 'desc');
  174.         } elseif (is_array($filter['order_by'])) {
  175.             $this->db_main->order_by($filter['order_by'][0], $filter['order_by'][1]);
  176.         } elseif (isset($filter['order_by']) && isset($filter['order_dir'])) {
  177.             $this->db_main->order_by($filter['order_by'], $filter['order_dir']);
  178.         }
  179.  
  180.         $this->db_main->limit($limit, $start);
  181.  
  182.         if (!isset($select)) {
  183.             //write default list
  184.             $this->db_main->select("c1.id, c1.num,c1.nama");
  185.         }
  186.  
  187.         $times[] = microtime(true);
  188.         $data = $this->db_main->get()->result_array();
  189.         $times[] = microtime(true);
  190.  
  191.         $sql = $this->db_main->last_query();
  192.         $result_debug = array(
  193.             'data' => $data,
  194.             'sql' => $sql,
  195.             'filter' => $filter,
  196.             'times' => $times
  197.         );
  198.  
  199.         if ($debug) {
  200.             return $result_debug;
  201.         } else {
  202.             return $data;
  203.         }
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement