1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Models\Traits; |
4
|
|
|
|
5
|
|
|
use Request; |
6
|
|
|
|
7
|
|
|
trait ModelQuerying |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Query. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $query; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Allows filtering of the default query, for instance:. |
18
|
|
|
* |
19
|
|
|
* $queryFilter = [ |
20
|
|
|
* 'whereNotNull' => ['parent_id'], |
21
|
|
|
* 'where' => ['name', 'John'], |
22
|
|
|
* ] |
23
|
|
|
* |
24
|
|
|
* Would result in an Eloquent query with the following scope: |
25
|
|
|
* Model::whereNotNull('parent_id')->where('name', 'John')->get(); |
26
|
|
|
* |
27
|
|
|
* Note: This queryFilter is not used for custom filters and |
28
|
|
|
* can also be overridden by setQueryFilter(); |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $queryFilter = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Any array of Filters. |
36
|
|
|
* |
37
|
|
|
* Filter should be setup in the same fashion as a default |
38
|
|
|
* $queryFilter is setup, however, inside an associative |
39
|
|
|
* array. |
40
|
|
|
* |
41
|
|
|
* The associative array is your filter 'action' and is |
42
|
|
|
* used as the filter label (and converted to a URL safe |
43
|
|
|
* query parameter). |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $filters = []; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* The number of models to return for pagination. |
51
|
|
|
* |
52
|
|
|
* @var int |
53
|
|
|
*/ |
54
|
|
|
protected $perPage = 15; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Order By - Column/Attribute to OrderBy. |
58
|
|
|
* |
59
|
|
|
* Primary Key of Model by default |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $orderBy; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sort By - Either Desc or Asc. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
protected $sortBy; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Finds an existing Model entry and sets it to the current model. |
74
|
|
|
* |
75
|
|
|
* @param int $modelitemId |
76
|
|
|
* |
77
|
|
|
* @return |
78
|
|
|
*/ |
79
|
|
|
public function find($modelitemId) |
80
|
|
|
{ |
81
|
|
|
$this->model = $this->model->findOrFail($modelitemId); |
82
|
|
|
|
83
|
|
|
return $this->model; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns Model Items, either all() or paginated(). |
88
|
|
|
* |
89
|
|
|
* Filtered by any defined query filters ($queryFilter) |
90
|
|
|
* Ordered by Managed Model orderBy and sortBy methods |
91
|
|
|
* |
92
|
|
|
* @return |
93
|
|
|
*/ |
94
|
|
|
public function items($count = false) |
95
|
|
|
{ |
96
|
|
|
$this->query = $this->model->newQuery(); |
97
|
|
|
|
98
|
|
|
return $this->query($count); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Returns All Model Items, either all() or paginated(). |
103
|
|
|
* |
104
|
|
|
* Filtered by any defined query filters ($queryFilter) |
105
|
|
|
* Ordered by Managed Model orderBy and sortBy methods |
106
|
|
|
* |
107
|
|
|
* @return |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function allItems($count = false) |
110
|
|
|
{ |
111
|
|
|
if (!$this->hasSoftDeleting()) { |
112
|
|
|
throw new \Exception('Model does not have Soft Deleting'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->query = $this->model->newQuery()->withTrashed(); |
116
|
|
|
|
117
|
|
|
return $this->query($count); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns Model Items, either all() or paginated(). |
122
|
|
|
* |
123
|
|
|
* Filtered by any defined query filters ($queryFilter) |
124
|
|
|
* Ordered by Managed Model orderBy and sortBy methods |
125
|
|
|
* |
126
|
|
|
* @return |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function onlyTrashedItems($count = false) |
129
|
|
|
{ |
130
|
|
|
if (!$this->hasSoftDeleting()) { |
131
|
|
|
throw new \Exception('Model does not have Soft Deleting'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$this->query = $this->model->newQuery()->onlyTrashed(); |
135
|
|
|
|
136
|
|
|
return $this->query($count); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Performs the Model Query. |
141
|
|
|
* |
142
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
143
|
|
|
*/ |
144
|
|
|
private function query($count) |
145
|
|
|
{ |
146
|
|
|
$this->applyQueryFilters(); |
147
|
|
|
|
148
|
|
|
if ($this->orderBy()) { |
149
|
|
|
$this->query = $this->query->orderBy( |
150
|
|
|
$this->orderBy(), |
151
|
|
|
$this->sortBy() |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ($count) { |
156
|
|
|
return $this->query->count(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($this->perPage > 0) { |
160
|
|
|
return $this->query->paginate($this->perPage); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this->query->get(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return Totals of All, With Trashed and Only Trashed. |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
public function totals() |
172
|
|
|
{ |
173
|
|
|
if ($this->hasSoftDeleting()) { |
174
|
|
|
return [ |
175
|
|
|
'all' => $this->items($count = true), |
176
|
|
|
'with_trashed' => $this->allItems($count = true), |
177
|
|
|
'only_trashed' => $this->onlyTrashedItems($count = true), |
178
|
|
|
]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return ['all' => $this->items($count = true)]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return Managed Model OrderBy. |
186
|
|
|
* |
187
|
|
|
* Primary key is default. |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function orderBy() |
192
|
|
|
{ |
193
|
|
|
if (Request::input('order')) { |
194
|
|
|
return Request::input('order'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
if ($this->orderBy) { |
198
|
|
|
return $this->orderBy; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this->model->getKeyName(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Return Managed Model SortBy (Asc or Desc). |
206
|
|
|
* |
207
|
|
|
* Descending is default. |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
*/ |
211
|
|
|
public function sortBy() |
212
|
|
|
{ |
213
|
|
|
if (Request::input('sort')) { |
214
|
|
|
return Request::input('sort'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
if ($this->sortBy == 'desc') { |
218
|
|
|
return 'desc'; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return 'asc'; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the number of models to return per page. |
226
|
|
|
* |
227
|
|
|
* @return int |
228
|
|
|
*/ |
229
|
|
|
public function getPerPage() |
230
|
|
|
{ |
231
|
|
|
return $this->perPage; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Set the number of models to return per page. |
236
|
|
|
* |
237
|
|
|
* @param int $perPage |
238
|
|
|
*/ |
239
|
|
|
public function setPerPage($perPage) |
240
|
|
|
{ |
241
|
|
|
$this->perPage = $perPage; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Apply the Query Filters. |
246
|
|
|
* |
247
|
|
|
* @return |
248
|
|
|
*/ |
249
|
|
|
private function applyQueryFilters() |
250
|
|
|
{ |
251
|
|
|
if (is_array($this->queryFilter())) { |
252
|
|
|
$this->createQueryFilter(); |
253
|
|
|
} else { |
254
|
|
|
$this->queryFilter(); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$this->queryFilterRequest(); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Apply the Query Filters specific to this Request. |
262
|
|
|
* |
263
|
|
|
* @return |
264
|
|
|
*/ |
265
|
|
|
private function queryFilterRequest() |
266
|
|
|
{ |
267
|
|
|
if (!$safeFilter = Request::get('filter')) { |
268
|
|
|
return false; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
if (!isset($this->safeFilters()[$safeFilter])) { |
272
|
|
|
return false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->query = $this->filters()[$this->safeFilters()[$safeFilter]]; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Create the Query Filter from Array. |
280
|
|
|
* |
281
|
|
|
* @return |
282
|
|
|
*/ |
283
|
|
|
private function createQueryFilter() |
284
|
|
|
{ |
285
|
|
|
if (count($this->queryFilter()) > 0) { |
286
|
|
|
foreach ($this->queryFilter() as $filter => $parameters) { |
287
|
|
|
if (!is_array($parameters)) { |
288
|
|
|
$parameters = [$parameters]; |
289
|
|
|
} |
290
|
|
|
$this->query = call_user_func_array([$this->query, $filter], $parameters); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Access the Query Filter. |
297
|
|
|
* |
298
|
|
|
* @return |
299
|
|
|
*/ |
300
|
|
|
public function queryFilter() |
301
|
|
|
{ |
302
|
|
|
return $this->queryFilter; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Access the Query Filter Options. |
307
|
|
|
* |
308
|
|
|
* @return |
309
|
|
|
*/ |
310
|
|
|
public function filters() |
311
|
|
|
{ |
312
|
|
|
return $this->filters; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Associative array of safe filter names to |
317
|
|
|
* their corresponding normal counterpart. |
318
|
|
|
* |
319
|
|
|
* @return |
320
|
|
|
*/ |
321
|
|
|
public function safeFilters() |
322
|
|
|
{ |
323
|
|
|
$filters = []; |
324
|
|
|
|
325
|
|
|
foreach ($this->filters() as $filterName => $query) { |
326
|
|
|
$filters[str_slug($filterName)] = $filterName; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $filters; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Set the Query Filter. |
334
|
|
|
* |
335
|
|
|
* @param array $filter |
336
|
|
|
*/ |
337
|
|
|
public function setQueryFilter($filter = []) |
338
|
|
|
{ |
339
|
|
|
$this->queryFilter = $filter; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: