|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Project codeigniter-framework |
| 4 | + * Created by PhpStorm |
| 5 | + * User: 713uk13m <dev@nguyenanhung.com> |
| 6 | + * Copyright: 713uk13m <dev@nguyenanhung.com> |
| 7 | + * Date: 10/03/2023 |
| 8 | + * Time: 15:01 |
| 9 | + */ |
| 10 | +defined('BASEPATH') or exit('No direct script access allowed'); |
| 11 | + |
| 12 | +/** |
| 13 | + * Class CI_Data_tables |
| 14 | + * |
| 15 | + * @author 713uk13m <dev@nguyenanhung.com> |
| 16 | + * @copyright 713uk13m <dev@nguyenanhung.com> |
| 17 | + */ |
| 18 | +class CI_Data_tables |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Database table |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private $table; |
| 26 | + |
| 27 | + /** |
| 28 | + * Primary key |
| 29 | + * |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + private $primary_key; |
| 33 | + |
| 34 | + /** |
| 35 | + * Columns to fetch |
| 36 | + * |
| 37 | + * @var array |
| 38 | + */ |
| 39 | + private $columns; |
| 40 | + |
| 41 | + /** |
| 42 | + * Where clause |
| 43 | + * |
| 44 | + * @var mixed |
| 45 | + */ |
| 46 | + private $where; |
| 47 | + |
| 48 | + /** |
| 49 | + * CI Singleton |
| 50 | + * |
| 51 | + * @var object |
| 52 | + */ |
| 53 | + private $CI; |
| 54 | + |
| 55 | + /** |
| 56 | + * GET request |
| 57 | + * |
| 58 | + * @var array |
| 59 | + */ |
| 60 | + private $request; |
| 61 | + |
| 62 | + // -------------------------------------------------------------------- |
| 63 | + |
| 64 | + /** |
| 65 | + * Constructor |
| 66 | + * |
| 67 | + * @param array $params Initialization parameters |
| 68 | + */ |
| 69 | + public function __construct($params) |
| 70 | + { |
| 71 | + $this->table = (array_key_exists('table', $params) === true && is_string($params['table']) === true) ? $params['table'] : ''; |
| 72 | + |
| 73 | + $this->primary_key = (array_key_exists('primary_key', $params) === true && is_string($params['primary_key']) === true) ? $params['primary_key'] : ''; |
| 74 | + |
| 75 | + $this->columns = (array_key_exists('columns', $params) === true && is_array($params['columns']) === true) ? $params['columns'] : []; |
| 76 | + |
| 77 | + $this->where = (array_key_exists('where', $params) === true && (is_array($params['where']) === true || is_string($params['where']) === true)) ? $params['where'] : []; |
| 78 | + |
| 79 | + $this->CI =& get_instance(); |
| 80 | + |
| 81 | + $this->request = $this->CI->input->get(); |
| 82 | + |
| 83 | + $this->validate_table(); |
| 84 | + |
| 85 | + $this->validate_primary_key(); |
| 86 | + |
| 87 | + $this->validate_columns(); |
| 88 | + |
| 89 | + $this->validate_request(); |
| 90 | + } |
| 91 | + |
| 92 | + // -------------------------------------------------------------------- |
| 93 | + |
| 94 | + /** |
| 95 | + * Validate database table |
| 96 | + */ |
| 97 | + private function validate_table() |
| 98 | + { |
| 99 | + if ($this->CI->db->table_exists($this->table) === false) { |
| 100 | + $this->response(array('error' => 'Table doesn\'t exist.')); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // -------------------------------------------------------------------- |
| 105 | + |
| 106 | + /** |
| 107 | + * Validate primary key |
| 108 | + */ |
| 109 | + private function validate_primary_key() |
| 110 | + { |
| 111 | + if ($this->CI->db->field_exists($this->primary_key, $this->table) === false) { |
| 112 | + $this->response(array('error' => 'Invalid primary key.')); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // -------------------------------------------------------------------- |
| 117 | + |
| 118 | + /** |
| 119 | + * validate columns to fetch |
| 120 | + */ |
| 121 | + private function validate_columns() |
| 122 | + { |
| 123 | + foreach ($this->columns as $column) { |
| 124 | + if (is_string($column) === false || $this->CI->db->field_exists($column, $this->table) === false) { |
| 125 | + $this->response(array('error' => 'Invalid column.')); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // -------------------------------------------------------------------- |
| 131 | + |
| 132 | + /** |
| 133 | + * validate GET request |
| 134 | + */ |
| 135 | + private function validate_request() |
| 136 | + { |
| 137 | + if (count($this->request['columns']) !== count($this->columns)) { |
| 138 | + $this->response(array('error' => 'Column count mismatch.')); |
| 139 | + } |
| 140 | + |
| 141 | + foreach ($this->request['columns'] as $column) { |
| 142 | + if (isset($this->columns[$column['data']]) === false) { |
| 143 | + $this->response(array('error' => 'Missing column.')); |
| 144 | + } |
| 145 | + |
| 146 | + $this->request['columns'][$column['data']]['name'] = $this->columns[$column['data']]; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // -------------------------------------------------------------------- |
| 151 | + |
| 152 | + /** |
| 153 | + * Generates the ORDER BY portion of the query |
| 154 | + */ |
| 155 | + private function order() |
| 156 | + { |
| 157 | + foreach ($this->request['order'] as $order) { |
| 158 | + $column = $this->request['columns'][$order['column']]; |
| 159 | + |
| 160 | + if ($column['orderable'] === 'true') { |
| 161 | + $this->CI->db->order_by($column['name'], strtoupper($order['dir'])); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // -------------------------------------------------------------------- |
| 167 | + |
| 168 | + /** |
| 169 | + * Generates the LIKE portion of the query |
| 170 | + */ |
| 171 | + private function search() |
| 172 | + { |
| 173 | + $global_search_value = $this->request['search']['value']; |
| 174 | + $likes = array(); |
| 175 | + |
| 176 | + foreach ($this->request['columns'] as $column) { |
| 177 | + if ($column['searchable'] === 'true') { |
| 178 | + if (empty($global_search_value) === false) { |
| 179 | + $likes[] = array( |
| 180 | + 'field' => $column['name'], |
| 181 | + 'match' => $global_search_value |
| 182 | + ); |
| 183 | + } |
| 184 | + |
| 185 | + if (empty($column['search']['value']) === false) { |
| 186 | + $likes[] = array( |
| 187 | + 'field' => $column['name'], |
| 188 | + 'match' => $column['search']['value'] |
| 189 | + ); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + foreach ($likes as $index => $like) { |
| 195 | + if ($index === 0) { |
| 196 | + $this->CI->db->like($like['field'], $like['match']); |
| 197 | + } else { |
| 198 | + $this->CI->db->or_like($like['field'], $like['match']); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + // -------------------------------------------------------------------- |
| 204 | + |
| 205 | + /** |
| 206 | + * Generates the WHERE portion of the query |
| 207 | + */ |
| 208 | + private function where() |
| 209 | + { |
| 210 | + $this->CI->db->where($this->where); |
| 211 | + } |
| 212 | + |
| 213 | + // -------------------------------------------------------------------- |
| 214 | + |
| 215 | + /** |
| 216 | + * Send response to DataTables |
| 217 | + * |
| 218 | + * @param array $data |
| 219 | + */ |
| 220 | + private function response($data) |
| 221 | + { |
| 222 | + $this->CI->output->set_content_type('application/json'); |
| 223 | + $this->CI->output->set_output(json_encode($data)); |
| 224 | + $this->CI->output->_display(); |
| 225 | + |
| 226 | + exit; |
| 227 | + } |
| 228 | + |
| 229 | + // -------------------------------------------------------------------- |
| 230 | + |
| 231 | + /** |
| 232 | + * Calculate total number of records |
| 233 | + * |
| 234 | + * @return int |
| 235 | + */ |
| 236 | + private function records_total() |
| 237 | + { |
| 238 | + $this->CI->db->reset_query(); |
| 239 | + |
| 240 | + $this->where(); |
| 241 | + |
| 242 | + $this->CI->db->from($this->table); |
| 243 | + |
| 244 | + return $this->CI->db->count_all_results(); |
| 245 | + } |
| 246 | + |
| 247 | + // -------------------------------------------------------------------- |
| 248 | + |
| 249 | + /** |
| 250 | + * Calculate filtered records |
| 251 | + * |
| 252 | + * @return int |
| 253 | + */ |
| 254 | + private function records_filtered() |
| 255 | + { |
| 256 | + $this->CI->db->reset_query(); |
| 257 | + |
| 258 | + $this->search(); |
| 259 | + |
| 260 | + $this->where(); |
| 261 | + |
| 262 | + $this->CI->db->from($this->table); |
| 263 | + |
| 264 | + return $this->CI->db->count_all_results(); |
| 265 | + } |
| 266 | + |
| 267 | + // -------------------------------------------------------------------- |
| 268 | + |
| 269 | + /** |
| 270 | + * Process |
| 271 | + * |
| 272 | + * @param string $row_id = 'data' |
| 273 | + * @param string $row_class = '' |
| 274 | + */ |
| 275 | + public function process($row_id = 'data', $row_class = '') |
| 276 | + { |
| 277 | + if (in_array($row_id, array('id', 'data', 'none'), true) === false) { |
| 278 | + $this->response(array('error' => 'Invalid DT_RowId.')); |
| 279 | + } |
| 280 | + |
| 281 | + if (is_string($row_class) === false) { |
| 282 | + $this->response(array('error' => 'Invalid DT_RowClass.')); |
| 283 | + } |
| 284 | + |
| 285 | + $columns = array(); |
| 286 | + |
| 287 | + $add_primary_key = true; |
| 288 | + |
| 289 | + foreach ($this->columns as $column) { |
| 290 | + $columns[] = $column; |
| 291 | + |
| 292 | + if ($column === $this->primary_key) { |
| 293 | + $add_primary_key = false; |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + if ($add_primary_key === true) { |
| 298 | + $columns[] = $this->primary_key; |
| 299 | + } |
| 300 | + |
| 301 | + $this->CI->db->select(implode(',', $columns)); |
| 302 | + |
| 303 | + $this->order(); |
| 304 | + |
| 305 | + $this->search(); |
| 306 | + |
| 307 | + $this->where(); |
| 308 | + |
| 309 | + $query = $this->CI->db->get($this->table, $this->request['length'], $this->request['start']); |
| 310 | + |
| 311 | + $data['data'] = array(); |
| 312 | + |
| 313 | + foreach ($query->result_array() as $row) { |
| 314 | + $r = array(); |
| 315 | + |
| 316 | + foreach ($this->columns as $column) { |
| 317 | + $r[] = $row[$column]; |
| 318 | + } |
| 319 | + |
| 320 | + if ($row_id === 'id') { |
| 321 | + $r['DT_RowId'] = $row[$this->primary_key]; |
| 322 | + } |
| 323 | + |
| 324 | + if ($row_id === 'data') { |
| 325 | + $r['DT_RowData'] = array( |
| 326 | + 'id' => $row[$this->primary_key] |
| 327 | + ); |
| 328 | + } |
| 329 | + |
| 330 | + if ($row_class !== '') { |
| 331 | + $r['DT_RowClass'] = $row_class; |
| 332 | + } |
| 333 | + |
| 334 | + $data['data'][] = $r; |
| 335 | + } |
| 336 | + |
| 337 | + $data['draw'] = (int) $this->request['draw']; |
| 338 | + |
| 339 | + $data['recordsTotal'] = $this->records_total(); |
| 340 | + |
| 341 | + $data['recordsFiltered'] = $this->records_filtered(); |
| 342 | + |
| 343 | + $this->response($data); |
| 344 | + } |
| 345 | +} |
0 commit comments