|
6 | 6 | class DataTable
|
7 | 7 | {
|
8 | 8 |
|
| 9 | + /** |
| 10 | + * DataTableQuery object. |
| 11 | + * |
| 12 | + * @var \Hermawan\CodeIgniter4-DataTable\DataTableQuery |
| 13 | + */ |
| 14 | + private $query; |
| 15 | + |
| 16 | + |
| 17 | + /** |
| 18 | + * DataTablColumns object. |
| 19 | + * |
| 20 | + * @var \Hermawan\CodeIgniter4-DataTable\DataTableColumns |
| 21 | + */ |
| 22 | + private $columnDefs; |
| 23 | + |
| 24 | + |
| 25 | + /** |
| 26 | + * Builder from CodeIgniter Query Builder |
| 27 | + * @param Builder $builder |
| 28 | + */ |
| 29 | + public function __construct($builder) |
| 30 | + { |
| 31 | + $this->query = new DataTableQuery($builder); |
| 32 | + $this->columnDefs = new DataTableColumnDefs($builder); |
| 33 | + |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
9 | 37 | /**
|
10 | 38 | * Make a DataTable instance from builder.
|
11 | 39 | *
|
12 | 40 | * Builder from CodeIgniter Query Builder
|
13 | 41 | * @param Builder $builder
|
14 |
| - * @return DataTableServerSide |
15 | 42 | */
|
16 | 43 | public static function of($builder)
|
17 | 44 | {
|
18 |
| - return new DataTableServerSide($builder); |
| 45 | + return new self($builder); |
19 | 46 | }
|
20 | 47 |
|
| 48 | + /** |
| 49 | + * postQuery |
| 50 | + * @param Closure $postQuery |
| 51 | + */ |
| 52 | + public function postQuery($postQuery) |
| 53 | + { |
| 54 | + $this->query->postQuery($postQuery); |
| 55 | + return $this; |
| 56 | + } |
21 | 57 |
|
22 | 58 | /**
|
23 |
| - * Get DataTable Request |
24 |
| - * |
25 |
| - * @param String $requestName |
26 |
| - * @return String|Array |
| 59 | + * custom Filter |
| 60 | + * @param Closure function |
27 | 61 | */
|
| 62 | + public function filter($filterFunction) |
| 63 | + { |
| 64 | + $this->query->filter($filterFunction); |
| 65 | + return $this; |
| 66 | + } |
28 | 67 |
|
29 | 68 |
|
30 |
| - public static function request($requestName = NULL) |
| 69 | + /** |
| 70 | + * Add numbering to first column |
| 71 | + * @param String $column |
| 72 | + */ |
| 73 | + public function addNumbering($column = NULL) |
| 74 | + { |
| 75 | + $this->columnDefs->addNumbering($column); |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Add extra column |
| 82 | + * |
| 83 | + * @param String $column |
| 84 | + * @param Closure $callback |
| 85 | + * @param String|int $position |
| 86 | + */ |
| 87 | + public function add($column, $callback, $position = 'last') |
31 | 88 | {
|
32 |
| - $request = Services::request(); |
33 |
| - if($requestName !== NULL) |
34 |
| - return $request->getGetPost($requestName); |
| 89 | + $this->columnDefs->add($column, $callback, $position); |
| 90 | + return $this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Edit column |
| 95 | + * |
| 96 | + * @param String $column |
| 97 | + * @param Closure $callback |
| 98 | + */ |
| 99 | + public function edit($column, $callback) |
| 100 | + { |
| 101 | + $this->columnDefs->edit($column, $callback); |
| 102 | + return $this; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Format column |
| 107 | + * |
| 108 | + * @param String $column |
| 109 | + * @param Closure $callback |
| 110 | + */ |
| 111 | + public function format($column, $callback) |
| 112 | + { |
| 113 | + $this->columnDefs->format($column, $callback); |
| 114 | + return $this; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Hide column |
| 119 | + * |
| 120 | + * @param String $column |
| 121 | + */ |
| 122 | + public function hide($column) |
| 123 | + { |
| 124 | + $this->columnDefs->remove($column); |
| 125 | + return $this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Set Searchable columns |
| 130 | + * @param String|Array |
| 131 | + */ |
| 132 | + public function setSearchableColumns($columns) |
| 133 | + { |
| 134 | + $this->columnDefs->setSearchable($columns); |
| 135 | + return $this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Add Searchable columns |
| 140 | + * @param String|Array |
| 141 | + */ |
| 142 | + public function addSearchableColumns($columns) |
| 143 | + { |
| 144 | + $this->columnDefs->addSearchable($columns); |
| 145 | + return $this; |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + /** |
| 151 | + * Return JSON output |
| 152 | + * |
| 153 | + * @param bool $returnAsObject |
| 154 | + * @return JSON |
| 155 | + */ |
| 156 | + public function toJson($returnAsObject = NULL) |
| 157 | + { |
| 158 | + |
| 159 | + if(! Request::get('draw')) |
| 160 | + { |
| 161 | + return self::throwError('no datatable request detected'); |
| 162 | + } |
| 163 | + |
| 164 | + if($returnAsObject !== NULL) |
| 165 | + $this->columnDefs->returnAsObject($returnAsObject); |
| 166 | + |
| 167 | + $this->query->setColumnDefs($this->columnDefs); |
35 | 168 |
|
36 |
| - return (Object) (($request->getMethod() == 'get') ? $request->getGet() : $request->getPost()); |
| 169 | + $response = Services::response(); |
37 | 170 |
|
| 171 | + return $response->setJSON([ |
| 172 | + 'draw' => Request::get('draw'), |
| 173 | + 'recordsTotal' => $this->query->countAll(), |
| 174 | + 'recordsFiltered' => $this->query->countFiltered(), |
| 175 | + 'data' => $this->query->getDataResult(), |
| 176 | + |
| 177 | + ]); |
38 | 178 | }
|
| 179 | + |
| 180 | + |
| 181 | + /** |
| 182 | + * Throw Error |
| 183 | + * |
| 184 | + * @param String $message |
| 185 | + * @return Error |
| 186 | + */ |
| 187 | + public static function throwError($message) |
| 188 | + { |
| 189 | + $response = Services::response(); |
| 190 | + return $response->setJSON([ |
| 191 | + 'error' => $message, |
| 192 | + |
| 193 | + ]); |
| 194 | + } |
| 195 | + |
| 196 | + |
39 | 197 |
|
40 | 198 | } // End of DataTables Library Class.
|
0 commit comments