1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Search |
6
|
|
|
{ |
7
|
|
|
/* |
8
|
|
|
|-------------------------------------------------------------------------- |
9
|
|
|
| SEARCH |
10
|
|
|
|-------------------------------------------------------------------------- |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
public $ajax_table = true; |
14
|
|
|
public $responsive_table; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Add conditions to the CRUD query for a particular search term. |
18
|
|
|
* |
19
|
|
|
* @param [string] $searchTerm Whatever string the user types in the search bar. |
|
|
|
|
20
|
|
|
*/ |
21
|
|
|
public function applySearchTerm($searchTerm) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
return $this->query->where(function ($query) use ($searchTerm) { |
|
|
|
|
24
|
|
|
foreach ($this->getColumns() as $column) { |
|
|
|
|
25
|
|
|
if (! isset($column['type'])) { |
26
|
|
|
abort(400, 'Missing column type when trying to apply search term.'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$this->applySearchLogicForColumn($query, $column, $searchTerm); |
30
|
|
|
} |
31
|
|
|
}); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Apply the search logic for each CRUD column. |
36
|
|
|
*/ |
37
|
|
|
public function applySearchLogicForColumn($query, $column, $searchTerm) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$columnType = $column['type']; |
40
|
|
|
|
41
|
|
|
// if there's a particular search logic defined, apply that one |
42
|
|
|
if (isset($column['searchLogic'])) { |
43
|
|
|
$searchLogic = $column['searchLogic']; |
44
|
|
|
|
45
|
|
|
// if a closure was passed, execute it |
46
|
|
|
if (is_callable($searchLogic)) { |
47
|
|
|
return $searchLogic($query, $column, $searchTerm); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// if a string was passed, search like it was that column type |
51
|
|
|
if (is_string($searchLogic)) { |
52
|
|
|
$columnType = $searchLogic; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// if false was passed, don't search this column |
56
|
|
|
if ($searchLogic == false) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// sensible fallback search logic, if none was explicitly given |
62
|
|
|
if ($column['tableColumn']) { |
63
|
|
|
switch ($columnType) { |
64
|
|
|
case 'email': |
65
|
|
|
case 'date': |
66
|
|
|
case 'datetime': |
67
|
|
|
case 'text': |
68
|
|
|
$query->orWhere($column['name'], 'like', '%'.$searchTerm.'%'); |
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case 'select': |
72
|
|
|
case 'select_multiple': |
73
|
|
|
$query->orWhereHas($column['entity'], function ($q) use ($column, $searchTerm) { |
74
|
|
|
$q->where($column['attribute'], 'like', '%'.$searchTerm.'%'); |
75
|
|
|
}); |
76
|
|
|
break; |
77
|
|
|
|
78
|
|
|
default: |
79
|
|
|
return; |
80
|
1 |
|
break; |
|
|
|
|
81
|
|
|
} |
82
|
1 |
|
} |
83
|
1 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Tell the list view to use AJAX for loading multiple rows. |
87
|
|
|
* |
88
|
|
|
* @deprecated 3.3.0 All tables are AjaxTables starting with 3.3.0. |
89
|
|
|
*/ |
90
|
|
|
public function enableAjaxTable() |
91
|
2 |
|
{ |
92
|
|
|
$this->ajax_table = true; |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if ajax is enabled for the table view. |
97
|
|
|
* |
98
|
|
|
* @deprecated 3.3.0 Since all tables use ajax, this will soon be removed. |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function ajaxTable() |
102
|
|
|
{ |
103
|
|
|
return $this->ajax_table; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Tell the list view to NOT show a reponsive DataTable. |
108
|
|
|
* @param bool $value |
109
|
|
|
*/ |
110
|
|
|
public function setResponsiveTable($value = true) |
111
|
|
|
{ |
112
|
|
|
$this->responsive_table = $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check if responsiveness is enabled for the table view. |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
|
|
public function getResponsiveTable() |
121
|
|
|
{ |
122
|
|
|
if ($this->responsive_table !== null) { |
123
|
|
|
return $this->responsive_table; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return config('backpack.crud.responsive_table'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Remember to show a responsive table. |
131
|
|
|
*/ |
132
|
|
|
public function enableResponsiveTable() |
133
|
|
|
{ |
134
|
|
|
$this->setResponsiveTable(true); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remember to show a table with horizontal scrolling. |
139
|
|
|
*/ |
140
|
|
|
public function disableResponsiveTable() |
141
|
|
|
{ |
142
|
|
|
$this->setResponsiveTable(false); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get the HTML of the cells in a table row, for a certain DB entry. |
147
|
|
|
* @param Entity $entry A db entry of the current entity; |
148
|
|
|
* @param int The number shown to the user as row number (index); |
149
|
|
|
* @return array Array of HTML cell contents. |
150
|
|
|
*/ |
151
|
|
|
public function getRowViews($entry, $rowNumber = false) |
152
|
|
|
{ |
153
|
|
|
$row_items = []; |
154
|
|
|
|
155
|
|
|
foreach ($this->columns as $key => $column) { |
|
|
|
|
156
|
|
|
$row_items[] = $this->getCellView($column, $entry, $rowNumber); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// add the buttons as the last column |
160
|
|
|
if ($this->buttons->where('stack', 'line')->count()) { |
|
|
|
|
161
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
162
|
|
|
->with('crud', $this) |
163
|
|
|
->with('entry', $entry) |
164
|
|
|
->with('row_number', $rowNumber) |
165
|
|
|
->render(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// add the details_row button to the first column |
169
|
|
|
if ($this->details_row) { |
|
|
|
|
170
|
|
|
$details_row_button = \View::make('crud::columns.details_row_button') |
171
|
|
|
->with('crud', $this) |
172
|
|
|
->with('entry', $entry) |
173
|
|
|
->with('row_number', $rowNumber) |
174
|
|
|
->render(); |
175
|
|
|
$row_items[0] = $details_row_button.$row_items[0]; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $row_items; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the HTML of a cell, using the column types. |
183
|
|
|
* @param array $column |
184
|
|
|
* @param Entity $entry A db entry of the current entity; |
185
|
|
|
* @param int The number shown to the user as row number (index); |
186
|
|
|
* @return HTML |
187
|
|
|
*/ |
188
|
|
|
public function getCellView($column, $entry, $rowNumber = false) |
189
|
|
|
{ |
190
|
|
|
return $this->renderCellView($this->getCellViewName($column), $column, $entry, $rowNumber); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the name of the view to load for the cell. |
195
|
|
|
* @param $column |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
private function getCellViewName($column) |
199
|
|
|
{ |
200
|
|
|
// return custom column if view_namespace attribute is set |
201
|
|
|
if (isset($column['view_namespace']) && isset($column['type'])) { |
202
|
|
|
return $column['view_namespace'].'.'.$column['type']; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
if (isset($column['type'])) { |
206
|
|
|
// if the column has been overwritten return that one |
207
|
|
|
if (view()->exists('vendor.backpack.crud.columns.'.$column['type'])) { |
|
|
|
|
208
|
|
|
return 'vendor.backpack.crud.columns.'.$column['type']; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// return the column from the package |
212
|
|
|
return 'crud::columns.'.$column['type']; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
// fallback to text column |
216
|
|
|
return 'crud::columns.text'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Render the given view. |
221
|
|
|
* @param $view |
222
|
|
|
* @param $column |
223
|
|
|
* @param $entry |
224
|
|
|
* @param int The number shown to the user as row number (index); |
225
|
|
|
* @return mixed |
226
|
|
|
*/ |
227
|
|
|
private function renderCellView($view, $column, $entry, $rowNumber = false) |
228
|
|
|
{ |
229
|
|
|
if (! view()->exists($view)) { |
|
|
|
|
230
|
|
|
$view = 'crud::columns.text'; // fallback to text column |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return \View::make($view) |
234
|
|
|
->with('crud', $this) |
235
|
|
|
->with('column', $column) |
236
|
|
|
->with('entry', $entry) |
237
|
|
|
->with('rowNumber', $rowNumber) |
238
|
|
|
->render(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Created the array to be fed to the data table. |
243
|
|
|
* |
244
|
|
|
* @param $entries Eloquent results. |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
public function getEntriesAsJsonForDatatables($entries, $totalRows, $filteredRows, $startIndex = false) |
248
|
|
|
{ |
249
|
|
|
$rows = []; |
250
|
|
|
|
251
|
|
|
foreach ($entries as $row) { |
252
|
|
|
$rows[] = $this->getRowViews($row, $startIndex === false ? false : ++$startIndex); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return [ |
256
|
|
|
'draw' => (isset($this->request['draw']) ? (int) $this->request['draw'] : 0), |
|
|
|
|
257
|
|
|
'recordsTotal' => $totalRows, |
258
|
|
|
'recordsFiltered' => $filteredRows, |
259
|
|
|
'data' => $rows, |
260
|
|
|
]; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.