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
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Add conditions to the CRUD query for a particular search term. |
17
|
|
|
* |
18
|
|
|
* @param [string] $searchTerm Whatever string the user types in the search bar. |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
public function applySearchTerm($searchTerm) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
return $this->query->where(function ($query) use ($searchTerm) { |
|
|
|
|
23
|
|
|
foreach ($this->getColumns() as $column) { |
|
|
|
|
24
|
|
|
if (! isset($column['type'])) { |
25
|
|
|
abort(400, 'Missing column type when trying to apply search term.'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$this->applySearchLogicForColumn($query, $column, $searchTerm); |
29
|
|
|
} |
30
|
|
|
}); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Apply the search logic for each CRUD column. |
35
|
|
|
*/ |
36
|
|
|
public function applySearchLogicForColumn($query, $column, $searchTerm) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
// if there's a particular search logic defined, apply that one |
39
|
|
|
if (isset($column['searchLogic'])) { |
40
|
|
|
$searchLogic = $column['searchLogic']; |
41
|
|
|
|
42
|
|
|
if (is_callable($searchLogic)) { |
43
|
|
|
return $searchLogic($query, $column, $searchTerm); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($searchLogic == false) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// sensible fallback search logic, if none was explicitly given |
52
|
|
|
if ($column['tableColumn']) { |
53
|
|
|
switch ($column['type']) { |
54
|
|
|
case 'email': |
55
|
|
|
case 'date': |
56
|
|
|
case 'datetime': |
57
|
|
|
case 'text': |
58
|
|
|
$query->orWhere($column['name'], 'like', '%'.$searchTerm.'%'); |
59
|
|
|
break; |
60
|
|
|
|
61
|
|
|
case 'select': |
62
|
|
|
case 'select_multiple': |
63
|
|
|
$query->orWhereHas($column['entity'], function ($q) use ($column, $searchTerm) { |
64
|
|
|
$q->where($column['attribute'], 'like', '%'.$searchTerm.'%'); |
65
|
|
|
}); |
66
|
|
|
break; |
67
|
|
|
|
68
|
|
|
default: |
69
|
|
|
return; |
70
|
|
|
break; |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tell the list view to use AJAX for loading multiple rows. |
77
|
|
|
* |
78
|
|
|
* @deprecated 3.3.0 All tables are AjaxTables starting with 3.3.0. |
79
|
|
|
*/ |
80
|
|
|
public function enableAjaxTable() |
81
|
|
|
{ |
82
|
|
|
$this->ajax_table = true; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Check if ajax is enabled for the table view. |
87
|
|
|
* |
88
|
|
|
* @deprecated 3.3.0 Since all tables use ajax, this will soon be removed. |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function ajaxTable() |
92
|
|
|
{ |
93
|
|
|
return $this->ajax_table; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the HTML of the cells in a table row, for a certain DB entry. |
98
|
|
|
* @param Entity $entry A db entry of the current entity; |
99
|
|
|
* @return array Array of HTML cell contents. |
100
|
|
|
*/ |
101
|
|
|
public function getRowViews($entry) |
102
|
|
|
{ |
103
|
|
|
$row_items = []; |
104
|
|
|
foreach ($this->columns as $key => $column) { |
|
|
|
|
105
|
|
|
$row_items[] = $this->getCellView($column, $entry); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// add the buttons as the last column |
109
|
|
|
if ($this->buttons->where('stack', 'line')->count()) { |
|
|
|
|
110
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
111
|
|
|
->with('crud', $this) |
112
|
|
|
->with('entry', $entry) |
113
|
|
|
->render(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// add the details_row buttons as the first column |
117
|
|
|
if ($this->details_row) { |
|
|
|
|
118
|
|
|
array_unshift($row_items, \View::make('crud::columns.details_row_button') |
119
|
|
|
->with('crud', $this) |
120
|
|
|
->with('entry', $entry) |
121
|
|
|
->render()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $row_items; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the HTML of a cell, using the column types. |
129
|
|
|
* @param array $column |
130
|
|
|
* @param Entity $entry A db entry of the current entity; |
131
|
|
|
* @return HTML |
132
|
|
|
*/ |
133
|
|
|
public function getCellView($column, $entry) |
134
|
|
|
{ |
135
|
|
|
// if column type not set, show as text |
136
|
|
|
if (! isset($column['type'])) { |
137
|
|
|
return \View::make('crud::columns.text') |
138
|
|
|
->with('crud', $this) |
139
|
|
|
->with('column', $column) |
140
|
|
|
->with('entry', $entry) |
141
|
|
|
->render(); |
142
|
|
|
} else { |
143
|
|
|
// if the column has been overwritten show that one |
144
|
|
|
if (view()->exists('vendor.backpack.crud.columns.'.$column['type'])) { |
145
|
|
|
return \View::make('vendor.backpack.crud.columns.'.$column['type']) |
146
|
|
|
->with('crud', $this) |
147
|
|
|
->with('column', $column) |
148
|
|
|
->with('entry', $entry) |
149
|
|
|
->render(); |
150
|
|
|
} else { |
151
|
|
|
// show the column from the package |
152
|
|
|
if (view()->exists('crud::columns.'.$column['type'])) { |
|
|
|
|
153
|
|
|
return \View::make('crud::columns.'.$column['type']) |
154
|
|
|
->with('crud', $this) |
155
|
|
|
->with('column', $column) |
156
|
|
|
->with('entry', $entry) |
157
|
|
|
->render(); |
158
|
|
|
} else { |
159
|
|
|
return \View::make('crud::columns.text') |
160
|
|
|
->with('crud', $this) |
161
|
|
|
->with('column', $column) |
162
|
|
|
->with('entry', $entry) |
163
|
|
|
->render(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Created the array to be fed to the data table. |
171
|
|
|
* |
172
|
|
|
* @param $entries Eloquent results. |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public function getEntriesAsJsonForDatatables($entries, $totalRows, $filteredRows) |
176
|
|
|
{ |
177
|
|
|
$rows = []; |
178
|
|
|
|
179
|
|
|
foreach ($entries as $row) { |
180
|
|
|
$rows[] = $this->getRowViews($row); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return [ |
184
|
|
|
'draw' => (isset($this->request['draw']) ? (int) $this->request['draw'] : 0), |
|
|
|
|
185
|
|
|
'recordsTotal' => $totalRows, |
186
|
|
|
'recordsFiltered' => $filteredRows, |
187
|
|
|
'data' => $rows, |
188
|
|
|
]; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
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.