1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
use Validator; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
|
8
|
|
|
trait Search |
9
|
|
|
{ |
10
|
|
|
/* |
11
|
|
|
|-------------------------------------------------------------------------- |
12
|
|
|
| SEARCH |
13
|
|
|
|-------------------------------------------------------------------------- |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
public $ajax_table = true; |
17
|
|
|
public $responsive_table; |
18
|
|
|
public $persistent_table; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Add conditions to the CRUD query for a particular search term. |
22
|
|
|
* |
23
|
|
|
* @param string $searchTerm Whatever string the user types in the search bar. |
24
|
|
|
* |
25
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
26
|
|
|
*/ |
27
|
|
|
public function applySearchTerm($searchTerm) |
28
|
|
|
{ |
29
|
|
|
return $this->query->where(function ($query) use ($searchTerm) { |
|
|
|
|
30
|
|
|
foreach ($this->getColumns() as $column) { |
|
|
|
|
31
|
|
|
if (! isset($column['type'])) { |
32
|
|
|
abort(400, 'Missing column type when trying to apply search term.'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->applySearchLogicForColumn($query, $column, $searchTerm); |
36
|
|
|
} |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Apply the search logic for each CRUD column. |
42
|
|
|
*/ |
43
|
|
|
public function applySearchLogicForColumn($query, $column, $searchTerm) |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$columnType = $column['type']; |
46
|
|
|
|
47
|
|
|
// if there's a particular search logic defined, apply that one |
48
|
|
|
if (isset($column['searchLogic'])) { |
49
|
|
|
$searchLogic = $column['searchLogic']; |
50
|
|
|
|
51
|
|
|
// if a closure was passed, execute it |
52
|
|
|
if (is_callable($searchLogic)) { |
53
|
|
|
return $searchLogic($query, $column, $searchTerm); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// if a string was passed, search like it was that column type |
57
|
|
|
if (is_string($searchLogic)) { |
58
|
|
|
$columnType = $searchLogic; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// if false was passed, don't search this column |
62
|
|
|
if ($searchLogic == false) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// sensible fallback search logic, if none was explicitly given |
68
|
|
|
if ($column['tableColumn']) { |
69
|
|
|
switch ($columnType) { |
70
|
|
|
case 'email': |
71
|
|
|
case 'text': |
72
|
|
|
case 'textarea': |
73
|
|
|
$query->orWhere($column['name'], 'like', '%'.$searchTerm.'%'); |
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
case 'date': |
77
|
|
|
case 'datetime': |
78
|
|
|
$validator = Validator::make(['value' => $searchTerm], ['value' => 'date']); |
79
|
|
|
|
80
|
|
|
if ($validator->fails()) { |
81
|
|
|
break; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$query->orWhereDate($column['name'], Carbon::parse($searchTerm)); |
85
|
|
|
break; |
86
|
|
|
|
87
|
|
|
case 'select': |
88
|
|
|
case 'select_multiple': |
89
|
|
|
$query->orWhereHas($column['entity'], function ($q) use ($column, $searchTerm) { |
90
|
|
|
$q->where($column['attribute'], 'like', '%'.$searchTerm.'%'); |
91
|
|
|
}); |
92
|
|
|
break; |
93
|
|
|
|
94
|
|
|
default: |
95
|
|
|
return; |
96
|
|
|
break; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Tell the list view to use AJAX for loading multiple rows. |
103
|
|
|
* |
104
|
|
|
* @deprecated 3.3.0 All tables are AjaxTables starting with 3.3.0. |
105
|
|
|
*/ |
106
|
1 |
|
public function enableAjaxTable() |
107
|
|
|
{ |
108
|
1 |
|
$this->ajax_table = true; |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Check if ajax is enabled for the table view. |
113
|
|
|
* |
114
|
|
|
* @deprecated 3.3.0 Since all tables use ajax, this will soon be removed. |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
2 |
|
public function ajaxTable() |
118
|
|
|
{ |
119
|
2 |
|
return $this->ajax_table; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// ------------------------- |
123
|
|
|
// Responsive Table |
124
|
|
|
// ------------------------- |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Tell the list view to NOT show a reponsive DataTable. |
128
|
|
|
* |
129
|
|
|
* @param bool $value |
130
|
|
|
*/ |
131
|
|
|
public function setResponsiveTable($value = true) |
132
|
|
|
{ |
133
|
|
|
$this->responsive_table = $value; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Check if responsiveness is enabled for the table view. |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function getResponsiveTable() |
142
|
|
|
{ |
143
|
|
|
if ($this->responsive_table !== null) { |
144
|
|
|
return $this->responsive_table; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return config('backpack.crud.responsive_table'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Remember to show a responsive table. |
152
|
|
|
*/ |
153
|
|
|
public function enableResponsiveTable() |
154
|
|
|
{ |
155
|
|
|
$this->setResponsiveTable(true); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Remember to show a table with horizontal scrolling. |
160
|
|
|
*/ |
161
|
|
|
public function disableResponsiveTable() |
162
|
|
|
{ |
163
|
|
|
$this->setResponsiveTable(false); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
// ------------------------- |
167
|
|
|
// Persistent Table |
168
|
|
|
// ------------------------- |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Tell the list view to NOT store datatable information in local storage. |
172
|
|
|
* |
173
|
|
|
* @param bool $value |
174
|
|
|
*/ |
175
|
|
|
public function setPersistentTable($value = true) |
176
|
|
|
{ |
177
|
|
|
$this->persistent_table = $value; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Check if saved state is enabled for the table view. |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function getPersistentTable() |
186
|
|
|
{ |
187
|
|
|
if ($this->persistent_table !== null) { |
188
|
|
|
return $this->persistent_table; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return config('backpack.crud.persistent_table'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Remember to show a persistent table. |
196
|
|
|
*/ |
197
|
|
|
public function enablePersistentTable() |
198
|
|
|
{ |
199
|
|
|
$this->setPersistentTable(true); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Remember to show a table that doesn't store URLs and pagination in local storage. |
204
|
|
|
*/ |
205
|
|
|
public function disablePersistentTable() |
206
|
|
|
{ |
207
|
|
|
$this->setPersistentTable(false); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Get the HTML of the cells in a table row, for a certain DB entry. |
212
|
|
|
* |
213
|
|
|
* @param \Illuminate\Database\Eloquent\Model $entry A db entry of the current entity; |
214
|
|
|
* @param bool|int $rowNumber The number shown to the user as row number (index); |
215
|
|
|
* |
216
|
|
|
* @return array Array of HTML cell contents. |
217
|
|
|
*/ |
218
|
|
|
public function getRowViews($entry, $rowNumber = false) |
219
|
|
|
{ |
220
|
|
|
$row_items = []; |
221
|
|
|
|
222
|
|
|
foreach ($this->columns as $key => $column) { |
|
|
|
|
223
|
|
|
$row_items[] = $this->getCellView($column, $entry, $rowNumber); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// add the buttons as the last column |
227
|
|
|
if ($this->buttons->where('stack', 'line')->count()) { |
|
|
|
|
228
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
229
|
|
|
->with('crud', $this) |
230
|
|
|
->with('entry', $entry) |
231
|
|
|
->with('row_number', $rowNumber) |
232
|
|
|
->render(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// add the details_row button to the first column |
236
|
|
|
if ($this->details_row) { |
|
|
|
|
237
|
|
|
$details_row_button = \View::make('crud::columns.details_row_button') |
238
|
|
|
->with('crud', $this) |
239
|
|
|
->with('entry', $entry) |
240
|
|
|
->with('row_number', $rowNumber) |
241
|
|
|
->render(); |
242
|
|
|
$row_items[0] = $details_row_button.$row_items[0]; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $row_items; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the HTML of a cell, using the column types. |
250
|
|
|
* |
251
|
|
|
* @param array $column |
252
|
|
|
* @param \Illuminate\Database\Eloquent\Model $entry A db entry of the current entity; |
253
|
|
|
* @param bool|int $rowNumber The number shown to the user as row number (index); |
254
|
|
|
* |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function getCellView($column, $entry, $rowNumber = false) |
258
|
|
|
{ |
259
|
|
|
return $this->renderCellView($this->getCellViewName($column), $column, $entry, $rowNumber); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Get the name of the view to load for the cell. |
264
|
|
|
* |
265
|
|
|
* @param array $column |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
|
|
private function getCellViewName($column) |
270
|
|
|
{ |
271
|
|
|
// return custom column if view_namespace attribute is set |
272
|
|
|
if (isset($column['view_namespace']) && isset($column['type'])) { |
273
|
|
|
return $column['view_namespace'].'.'.$column['type']; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
if (isset($column['type'])) { |
277
|
|
|
// if the column has been overwritten return that one |
278
|
|
|
if (view()->exists('vendor.backpack.crud.columns.'.$column['type'])) { |
|
|
|
|
279
|
|
|
return 'vendor.backpack.crud.columns.'.$column['type']; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
// return the column from the package |
283
|
|
|
return 'crud::columns.'.$column['type']; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
// fallback to text column |
287
|
|
|
return 'crud::columns.text'; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Render the given view. |
292
|
|
|
* |
293
|
|
|
* @param string $view |
294
|
|
|
* @param array $column |
295
|
|
|
* @param object $entry |
296
|
|
|
* @param bool|int $rowNumber The number shown to the user as row number (index) |
297
|
|
|
* |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
|
|
private function renderCellView($view, $column, $entry, $rowNumber = false) |
301
|
|
|
{ |
302
|
|
|
if (! view()->exists($view)) { |
|
|
|
|
303
|
|
|
$view = 'crud::columns.text'; // fallback to text column |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return \View::make($view) |
307
|
|
|
->with('crud', $this) |
308
|
|
|
->with('column', $column) |
309
|
|
|
->with('entry', $entry) |
310
|
|
|
->with('rowNumber', $rowNumber) |
311
|
|
|
->render(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Created the array to be fed to the data table. |
316
|
|
|
* |
317
|
|
|
* @param array $entries Eloquent results. |
318
|
|
|
* @param int $totalRows |
319
|
|
|
* @param int $filteredRows |
320
|
|
|
* @param bool|int $startIndex |
321
|
|
|
* |
322
|
|
|
* @return array |
|
|
|
|
323
|
|
|
*/ |
324
|
|
|
public function getEntriesAsJsonForDatatables($entries, $totalRows, $filteredRows, $startIndex = false) |
325
|
|
|
{ |
326
|
|
|
$rows = []; |
327
|
|
|
|
328
|
|
|
foreach ($entries as $row) { |
329
|
|
|
$rows[] = $this->getRowViews($row, $startIndex === false ? false : ++$startIndex); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return [ |
333
|
|
|
'draw' => (isset($this->request['draw']) ? (int) $this->request['draw'] : 0), |
|
|
|
|
334
|
|
|
'recordsTotal' => $totalRows, |
335
|
|
|
'recordsFiltered' => $filteredRows, |
336
|
|
|
'data' => $rows, |
337
|
|
|
]; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: