Skip to content

Commit bb0ea19

Browse files
committed
Utilize built-in CodeIgniter query builder functions for filtering and ordering
- Refactored filtering and ordering logic to leverage CodeIgniter's built-in query builder functions - Simplify getting column name by a resolver
1 parent f60394f commit bb0ea19

File tree

2 files changed

+56
-75
lines changed

2 files changed

+56
-75
lines changed

src/Config.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ class Config
1515
'countAllResults' => 'count_all_results',
1616
'orderBy' => 'order_by',
1717
'where' => 'where',
18+
'like' => 'like',
19+
'orLike' => 'or_like',
1820
'limit' => 'limit',
1921
'get' => 'get',
2022
'QBSelect' => 'qb_select',
2123
'getFieldNames' => 'list_fields',
2224
'getResult' => 'result',
2325
'getResultArray' => 'result_array',
26+
'groupStart' => 'group_start',
27+
'groupEnd' => 'group_end'
2428
]
2529
];
2630

src/DataTables.php

Lines changed: 52 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,34 @@ protected function setReturnedFieldNames()
171171
}
172172
}
173173

174+
/**
175+
* Resolves the column name
176+
*
177+
* @param int|string $column The column index or name, depending on `asObject`
178+
* @return string|null The resolved column name or NULL if out of bounds
179+
*/
180+
protected function resolveColumnName($column)
181+
{
182+
if ( ! $this->asObject) {
183+
$fieldIndex = $this->sequenceNumber ? $column - 1 : $column;
184+
185+
// Skip sequence number and extra column
186+
if (
187+
($this->sequenceNumber && $column == 0) OR
188+
$fieldIndex > count($this->returnedFieldNames) - 1
189+
) return NULL;
190+
191+
$column = $this->returnedFieldNames[$fieldIndex];
192+
}
193+
194+
// Checking if it using a column alias
195+
$column = isset($this->columnAliases[$column])
196+
? $this->columnAliases[$column]
197+
: $column;
198+
199+
return $column;
200+
}
201+
174202
/**
175203
* Add sequence number to the output
176204
* @param string $key Used when returning object output as the key
@@ -188,84 +216,48 @@ public function addSequenceNumber($key = 'sequenceNumber')
188216
protected function filter()
189217
{
190218
$globalSearch = [];
191-
$columnSearch = [];
192-
193-
$fieldNamesLength = count($this->returnedFieldNames);
194219

195220
// Global column filtering
196221
if ($this->request->get('search') && ($keyword = $this->request->get('search')['value']) != '') {
197222
foreach ($this->request->get('columns', []) as $request_column) {
198223
if (filter_var($request_column['searchable'], FILTER_VALIDATE_BOOLEAN)) {
199224
$column = $request_column['data'];
200-
201-
if ( ! $this->asObject) {
202-
// Skip sequence number
203-
if ($this->sequenceNumber && $column == 0) continue;
204-
205-
$fieldIndex = $this->sequenceNumber ? $column - 1 : $column;
206-
207-
// Skip extra column
208-
if ($fieldIndex > $fieldNamesLength - 1) break;
209-
210-
$column = $this->returnedFieldNames[$fieldIndex];
211-
}
212-
213-
// Checking if it using a column alias
214-
$column = isset($this->columnAliases[$column])
215-
? $this->columnAliases[$column]
216-
: $column;
225+
$column = $this->resolveColumnName($column);
217226

218-
$globalSearch[] = sprintf("`%s` LIKE '%%%s%%'", $column, $keyword);
227+
if (empty($column)) continue;
228+
229+
$globalSearch[] = $column;
219230
}
220231
}
221232
}
222233

234+
// Apply global search criteria
235+
if (!empty($globalSearch)) {
236+
$this->queryBuilder->{$this->config->get('groupStart')}();
237+
238+
foreach ($globalSearch as $column) {
239+
$this->queryBuilder->{$this->config->get('orLike')}($column, $keyword);
240+
}
241+
242+
$this->queryBuilder->{$this->config->get('groupEnd')}();
243+
}
244+
223245
// Individual column filtering
224246
foreach ($this->request->get('columns', []) as $request_column) {
225247
if (
226248
filter_var($request_column['searchable'], FILTER_VALIDATE_BOOLEAN) &&
227249
($keyword = $request_column['search']['value']) != ''
228250
) {
229251
$column = $request_column['data'];
252+
$column = $this->resolveColumnName($column);
253+
254+
if (empty($column)) continue;
230255

231-
if ( ! $this->asObject) {
232-
// Skip sequence number
233-
if ($this->sequenceNumber && $column == 0) continue;
234-
235-
$fieldIndex = $this->sequenceNumber ? $column - 1 : $column;
236-
237-
// Skip extra column
238-
if ($fieldIndex > $fieldNamesLength - 1) break;
239-
240-
$column = $this->returnedFieldNames[$fieldIndex];
241-
}
242-
243-
// Checking if it using a column alias
244-
$column = isset($this->columnAliases[$column])
245-
? $this->columnAliases[$column]
246-
: $column;
247-
248-
$columnSearch[] = sprintf("`%s` LIKE '%%%s%%'", $column, $keyword);
256+
// Apply column-specific search criteria
257+
$this->queryBuilder->{$this->config->get('like')}($column, $keyword);
249258
}
250259
}
251260

252-
// Merge global search & column search
253-
$w_filter = '';
254-
255-
if ( ! empty($globalSearch)) {
256-
$w_filter = '(' . implode(' OR ', $globalSearch) . ')';
257-
}
258-
259-
if ( ! empty($columnSearch)) {
260-
$w_filter = $w_filter === '' ?
261-
implode(' AND ', $columnSearch) :
262-
$w_filter . ' AND ' . implode(' AND ', $columnSearch);
263-
}
264-
265-
if ($w_filter !== '') {
266-
$this->queryBuilder->{$this->config->get('where')}($w_filter);
267-
}
268-
269261
$this->recordsFiltered = $this->queryBuilder->{$this->config->get('countAllResults')}('', FALSE);
270262
}
271263

@@ -275,35 +267,20 @@ protected function filter()
275267
protected function order()
276268
{
277269
if ($this->request->get('order') && count($this->request->get('order'))) {
278-
$orders = [];
279-
$fieldNamesLength = count($this->returnedFieldNames);
280-
281270
foreach ($this->request->get('order') as $order) {
282271
$column_idx = $order['column'];
283272
$request_column = $this->request->get('columns')[$column_idx];
284273

285274
if (filter_var($request_column['orderable'], FILTER_VALIDATE_BOOLEAN)) {
286275
$column = $request_column['data'];
276+
$column = $this->resolveColumnName($column);
287277

288-
if ( ! $this->asObject) {
289-
// Skip sequence number
290-
if ($this->sequenceNumber && $column == 0) continue;
291-
292-
$fieldIndex = $this->sequenceNumber ? $column - 1 : $column;
293-
294-
// Skip extra column
295-
if ($fieldIndex > $fieldNamesLength - 1) break;
296-
297-
$column = $this->returnedFieldNames[$fieldIndex];
298-
}
278+
if (empty($column)) continue;
299279

300-
$orders[] = sprintf('`%s` %s', $column, strtoupper($order['dir']));
280+
// Apply order
281+
$this->queryBuilder->{$this->config->get('orderBy')}($column, $order['dir']);
301282
}
302283
}
303-
304-
if (!empty($orders)) {
305-
$this->queryBuilder->{$this->config->get('orderBy')}(implode(', ', $orders));
306-
}
307284
}
308285
}
309286

0 commit comments

Comments
 (0)