-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatatable.php
More file actions
188 lines (170 loc) · 4.56 KB
/
Datatable.php
File metadata and controls
188 lines (170 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/*
* @author Luis Eduardo da Silva Santos <https://github.com/luisdev1>
* @copyright Copyright (c) 2018, Luis Eduardo da Silva Santos. (https://github.com/luisdev1)
* @license http://opensource.org/licenses/MIT MIT License
*/
class Datatable
{
/*
* Tabela do banco de dados
* @var string
*/
protected $table_name = '';
/*
* Colunas a serem ordenadas
* @var array
*/
protected $order_columns = array();
/*
* Colunas a serem selecionadas
* @var array
*/
protected $select_columns = array();
/*
* Coluna a ser utilizada na pesquisa
* @var mixed
*/
protected $search_columns = '';
/*
* Condição para a consulta
* @var mixed
*/
protected $where = array();
public function __construct($config = array()){
if(count($config) > 0){
$this->initialize($config);
}
}
/**
* Retorna a instancia do CI
*
* @return mixed
*/
public function __get($var) {
return get_instance()->$var;
}
/**
* Inicializa a biblioteca carregando os arquivos de configuração ou
* um array() passada no carregamento da classe.
*
* @param $config array()
* @return void
*/
public function initialize($config = array())
{
foreach ($config as $key => $val) {
if (isset($this->$key)) {
$method = 'set_' . $key;
if (method_exists($this, $method)) {
$this->$method($val);
} else {
$this->$key = $val;
}
}
}
return $this;
}
/**
*
*
* @return void
*/
public function make_query()
{
$search = $this->input->post('search', TRUE);
$order = $this->input->post('order', TRUE);
$order_column = array($this->order_columns);
$this->db->select($this->select_columns);
$this->db->from($this->table_name);
if(is_array($this->where) && count($this->where) > 0){
$this->db->where($this->where);
}
if(isset($search['value'])){
if(is_array($this->search_columns) && count($this->search_columns) > 0){
$this->db->like($this->search_columns[0], $search['value']);
if(is_array($this->where) && count($this->where) > 0){
$this->db->where($this->where);
}
for($i = 1; $i < count($this->search_columns); $i++){
$this->db->or_like($this->search_columns[$i], $search['value']);
if(is_array($this->where) && count($this->where) > 0){
$this->db->where($this->where);
}
}
} else {
$this->db->like($this->search_columns, $search['value']);
if(is_array($this->where) && count($this->where) > 0){
$this->db->where($this->where);
}
}
}
if(isset($order[0]['column'])){
$this->db->order_by($order_column[$order[0]['column']], $order[0]['dir']);
} else {
$this->db->order_by($this->table_name.'.id', 'DESC');
}
}
/**
*
*
* @return mixed
*/
public function make_table()
{
$this->make_query();
if($this->input->post('length') > -1){
$this->db->limit($this->input->post('length'), $this->input->post('start'));
}
$query = $this->db->get();
return $query->result_array();
}
/**
*
*
* @return mixed
*/
public function get_filtered_data()
{
$this->make_query();
$query = $this->db->get();
return $query->num_rows();
}
/**
*
*
* @return int
*/
public function get_all_data()
{
$this->db->select('*');
$this->db->from($this->table_name);
if(is_array($this->where) && count($this->where) > 0){
$this->db->where($this->where);
}
return $this->db->count_all_results();
}
/**
* Retorna a tabela construída a partir da $data passada
*
* @param mixed $data
* @return json
*/
public function get_table($data = array())
{
if(count($data) == 0){
$data = array();
$result = array_values($this->make_table());
foreach ($result as $row) {
$data[] = array_values($row);
}
}
$output = array(
'draw' => intval($this->input->post('draw')),
'recordsTotal' => $this->datatable->get_all_data(),
'recordsFiltered' => $this->datatable->get_filtered_data(),
'data' => $data
);
return json_encode($output, JSON_UNESCAPED_UNICODE);
}
}