Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- //===================
- // ver : 2018-05-07
- //===================
- class Wilayah_m extends CI_Model {
- public $tables;
- public $db_main, $model_name, $model_short_name;
- public function __CONSTRUCT() {
- parent::__construct();
- $this->db_main = $this->load->database('default', TRUE);
- //load db only in model.. not controller.
- $this->tables = array(
- 'main' => 'sda_wilayah',
- 'city' => 'kabupaten' //ignore this
- );
- $this->model_name = 'wilayah_m';
- $this->model_short_name = 'wilayah';
- }
- function _datatables($params = FALSE) {
- $get = $params;
- $result = array(
- 'raw' => array(),
- 'data' => array(),
- 'sql' => array(),
- 'time' => array(),
- 'params' => $params,
- 'head' => array(),
- );
- if ($params === FALSE) {
- return $result;
- }
- //====start
- $start = isset($get['start']) ? $get['start'] : 0;
- $limit = isset($get['length']) ? $get['length'] : 10;
- $result['draw'] = isset($get['draw']) ? $get['draw'] : rand(10000, 99999);
- $result['recordsTotal'] = $result['recordsFiltered'] = 0; //default
- $result['time']['start'] = microtime(); //wajib ada.. buat debug proses mana yg lambat
- $filters = array();
- $result['recordsTotal'] = $result['recordsFiltered'] = $this->_counts($filters);
- $result['sql'][]=$this->db_main->last_query();
- /*
- * Konfigurasi sort by
- * You can put the config above.. but for easy learning.. I put in here
- */
- $result['time']['sort 1'] = microtime();
- $column = "id"; //default
- if (isset($get['order'][0])) {
- $pos_column = $get['order'][0]['column'];
- $filters['order_dir'] = $get['order'][0]['dir'];
- /*
- * I'm not using case... in order to readable for some
- * programer..
- */
- if ($pos_column == 0) {
- $column = 'c1.num';
- }
- if ($pos_column == 1) {
- $column = 'c1.id';
- }
- if ($pos_column == 2) {
- $column = 'c1.nama';
- }
- //for advance use.. like using 2 ordering column.. We need to
- //change this
- $filters['order_by'] = array(
- $column,
- $filters['order_dir']
- );
- }
- $result['time']['sort 2'] = microtime();
- //===========SEARCh
- $SEARCH = isset($get['search']['value']) ? $get['search']['value'] : FALSE;
- $filters['datatable_search'] = $SEARCH;
- $raw_data = $this->_gets($filters, $limit, $start, FALSE, FALSE);
- $result['sql'][]=$this->db_main->last_query();
- //update total
- $result['recordsFiltered'] = $this->_counts($filters);
- $result['sql'][]=$this->db_main->last_query();
- $result['raw'] = $raw_data;
- //return $result;
- /* ===== Parse Data ===== */
- $result['time']['parse begin'] = microtime();
- $n = 0;
- $result['head'] = array(
- 'update',
- 'code',
- 'nama',
- 'parent',
- 'action'
- );
- $data = array();
- $no = $start;
- foreach ($raw_data as $id => $row) {
- $no++;
- $res = array(
- $no,
- $row['id'],
- $row['nama'],
- '-'
- );
- $data[] = $res;
- }
- $result['raw'][] = array($filters, $raw_data);
- $result['time']['parse end'] = microtime();
- $result['data'] = $data;
- $result['time']['end'] = microtime();
- /*
- * draw
- * order
- *
- */
- return $result;
- }
- public function _counts($filter = array()) {
- $count = $this->_gets($filter, 1, 0, TRUE);
- return $count;
- }
- public function _gets($filter = array(), $limit = 10, $start = 0, $count = FALSE, $debug = FALSE) {
- $times[] = microtime(true);
- $head_table = 'main';
- $table = $this->tables[$head_table];
- //=========
- $this->db_main->from("$table c1");
- $times[] = microtime(true);
- //====DATATABLES=====================
- if (isset($filter['datatable_search'])) {
- if (strlen($filter['datatable_search']) > 2) {
- //----Manual-----------
- /*
- * code, name
- */
- $this->db_main->group_start()
- ->or_like("c1.id", strtoupper($filter['datatable_search']))
- ->or_like("c1.nama", strtoupper($filter['datatable_search']))
- ->group_end();
- } else {
- unset($filter['datatable_search']);
- }
- }
- //------------------Show all include delete--------------
- //==============COUNT=============
- if ($count) {
- $this->db_main->select("count(*) c");
- $data = $this->db_main->get()->row_array();
- return $data['c'];
- }
- //==================SELECT==========
- if (isset($filter['selects'])) {
- $this->db_main->select($filter['selects']);
- $select = TRUE;
- }
- //================order by=== NEED 2 FIX
- if (!isset($filter['order_by'])) {
- $fields = "c1.num";
- $this->db_main->order_by($fields, 'desc');
- } elseif (is_array($filter['order_by'])) {
- $this->db_main->order_by($filter['order_by'][0], $filter['order_by'][1]);
- } elseif (isset($filter['order_by']) && isset($filter['order_dir'])) {
- $this->db_main->order_by($filter['order_by'], $filter['order_dir']);
- }
- $this->db_main->limit($limit, $start);
- if (!isset($select)) {
- //write default list
- $this->db_main->select("c1.id, c1.num,c1.nama");
- }
- $times[] = microtime(true);
- $data = $this->db_main->get()->result_array();
- $times[] = microtime(true);
- $sql = $this->db_main->last_query();
- $result_debug = array(
- 'data' => $data,
- 'sql' => $sql,
- 'filter' => $filter,
- 'times' => $times
- );
- if ($debug) {
- return $result_debug;
- } else {
- return $data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement