1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\PanelTraits; |
4
|
|
|
|
5
|
|
|
trait Read |
6
|
|
|
{ |
7
|
|
|
/* |
8
|
|
|
|-------------------------------------------------------------------------- |
9
|
|
|
| READ |
10
|
|
|
|-------------------------------------------------------------------------- |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Find and retrieve an entry in the database or fail. |
15
|
|
|
* |
16
|
|
|
* @param [int] The id of the row in the db to fetch. |
17
|
|
|
* |
18
|
|
|
* @return [Eloquent Collection] The row in the db. |
|
|
|
|
19
|
|
|
*/ |
20
|
|
|
public function getEntry($id) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
if (! $this->entry) { |
23
|
|
|
$this->entry = $this->model->findOrFail($id); |
|
|
|
|
24
|
|
|
$this->entry = $this->entry->withFakes(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
return $this->entry; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Make the query JOIN all relationships used in the columns, too, |
32
|
|
|
* so there will be less database queries overall. |
33
|
|
|
*/ |
34
|
|
|
public function autoEagerLoadRelationshipColumns() |
35
|
|
|
{ |
36
|
|
|
$relationships = $this->getColumnsRelationships(); |
|
|
|
|
37
|
|
|
|
38
|
|
|
if (count($relationships)) { |
39
|
|
|
$this->with($relationships); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get all entries from the database. |
45
|
|
|
* |
46
|
|
|
* @return [Collection of your model] |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public function getEntries() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this->autoEagerLoadRelationshipColumns(); |
51
|
|
|
|
52
|
|
|
$entries = $this->query->get(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
// add the fake columns for each entry |
55
|
|
|
foreach ($entries as $key => $entry) { |
56
|
|
|
$entry->addFakes($this->getFakeColumnsAsArray()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $entries; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get all entries from the database with conditions. |
64
|
|
|
* |
65
|
|
|
* @param string $length the number of records requested |
|
|
|
|
66
|
|
|
* @param string $skip how many to skip |
|
|
|
|
67
|
|
|
* @param string $orderBy the column to order by |
|
|
|
|
68
|
|
|
* @param string $orderDirection how to order asc/desc |
69
|
|
|
* @param string $filter what string to filter the name by |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return [Collection of your model] |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
public function getEntriesWithConditions( |
|
|
|
|
74
|
|
|
$length = null, |
75
|
|
|
$skip = 0, |
76
|
|
|
$orderBy = null, |
77
|
|
|
$orderDirection = 'asc', |
78
|
|
|
$filter = null |
79
|
|
|
) { |
80
|
|
|
$modifiers = 0; |
81
|
|
|
|
82
|
|
|
if ($filter !== null) { |
83
|
|
|
$modifiers = 1; |
84
|
|
|
$entries = $this->query->where(function ($query) use ($filter) { |
85
|
|
|
foreach ($this->columns as $column) { |
|
|
|
|
86
|
|
|
if ($this->getColumnQuery($column) !== null) { |
87
|
|
|
$query->orWhere( |
88
|
|
|
$this->getColumnQuery($column), |
89
|
|
|
'like', |
90
|
|
|
'%'.$filter.'%' |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
}); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($length !== null) { |
98
|
|
|
$modifiers = 1; |
99
|
|
|
$entries = $this->query->skip($skip)->take($length); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($orderBy !== null) { |
103
|
|
|
$modifiers = 1; |
104
|
|
|
$entries = $this->query->orderBy($orderBy, $orderDirection); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($modifiers == 0) { |
108
|
|
|
$entries = $this->query->get(); |
109
|
|
|
} else { |
110
|
|
|
$entries = $entries->get(); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// add the fake columns for each entry |
114
|
|
|
foreach ($entries as $key => $entry) { |
115
|
|
|
$entry->addFakes($this->getFakeColumnsAsArray()); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $entries; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Receives a filter and tries to get all the columns to be filtered by that filter *Work in progress*. |
123
|
|
|
* |
124
|
|
|
* @param $column |
125
|
|
|
* @return null|string |
126
|
|
|
*/ |
127
|
|
|
private function getColumnQuery($column) |
128
|
|
|
{ |
129
|
|
|
if (isset($column['type']) && $column['type'] == 'model_function') { |
130
|
|
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (is_array($column)) { |
134
|
|
|
return $column['name']; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return Model::resolveConnection()->getQueryGrammar()->wrap($column); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get the fields for the create or update forms. |
142
|
|
|
* |
143
|
|
|
* @param [form] create / update / both - defaults to 'both' |
144
|
|
|
* @param [integer] the ID of the entity to be edited in the Update form |
145
|
|
|
* |
146
|
|
|
* @return [array] all the fields that need to be shown and their information |
|
|
|
|
147
|
|
|
*/ |
148
|
|
|
public function getFields($form, $id = false) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
switch (strtolower($form)) { |
151
|
|
|
case 'create': |
152
|
|
|
return $this->getCreateFields(); |
|
|
|
|
153
|
|
|
break; |
|
|
|
|
154
|
|
|
|
155
|
|
|
case 'update': |
156
|
|
|
return $this->getUpdateFields($id); |
|
|
|
|
157
|
|
|
break; |
|
|
|
|
158
|
|
|
|
159
|
|
|
default: |
160
|
|
|
return $this->getCreateFields(); |
|
|
|
|
161
|
|
|
break; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Check if the create/update form has upload fields. |
167
|
|
|
* Upload fields are the ones that have "upload" => true defined on them. |
168
|
|
|
* @param [form] create / update / both - defaults to 'both' |
169
|
|
|
* @param [id] id of the entity - defaults to false |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
|
|
public function hasUploadFields($form, $id = false) |
173
|
|
|
{ |
174
|
|
|
$fields = $this->getFields($form, $id); |
175
|
|
|
$upload_fields = array_where($fields, function ($value, $key) { |
|
|
|
|
176
|
|
|
return isset($value['upload']) && $value['upload'] == true; |
177
|
|
|
}); |
178
|
|
|
|
179
|
|
|
return count($upload_fields) ? true : false; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Enable the DETAILS ROW functionality:. |
184
|
|
|
* |
185
|
|
|
* In the table view, show a plus sign next to each entry. |
186
|
|
|
* When clicking that plus sign, an AJAX call will bring whatever content you want from the EntityCrudController::showDetailsRow($id) and show it to the user. |
|
|
|
|
187
|
|
|
*/ |
188
|
|
|
public function enableDetailsRow() |
189
|
|
|
{ |
190
|
|
|
$this->details_row = true; |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Disable the DETAILS ROW functionality:. |
195
|
|
|
*/ |
196
|
|
|
public function disableDetailsRow() |
197
|
|
|
{ |
198
|
|
|
$this->details_row = false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Set the number of rows that should be show on the table page (list view). |
203
|
|
|
*/ |
204
|
|
|
public function setDefaultPageLength($value) |
205
|
|
|
{ |
206
|
|
|
$this->default_page_length = $value; |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the number of rows that should be show on the table page (list view). |
211
|
|
|
*/ |
212
|
|
|
public function getDefaultPageLength() |
|
|
|
|
213
|
|
|
{ |
214
|
|
|
// return the custom value for this crud panel, if set using setPageLength() |
215
|
|
|
if ($this->default_page_length) { |
216
|
|
|
return $this->default_page_length; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// otherwise return the default value in the config file |
220
|
|
|
if (config('backpack.crud.default_page_length')) { |
221
|
|
|
return config('backpack.crud.default_page_length'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return 25; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/* |
228
|
|
|
|-------------------------------------------------------------------------- |
229
|
|
|
| AJAX TABLE |
230
|
|
|
|-------------------------------------------------------------------------- |
231
|
|
|
*/ |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Tell the list view to use AJAX for loading multiple rows. |
235
|
|
|
*/ |
236
|
|
|
public function enableAjaxTable() |
237
|
|
|
{ |
238
|
|
|
$this->ajax_table = true; |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Check if ajax is enabled for the table view. |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
|
|
public function ajaxTable() |
246
|
|
|
{ |
247
|
|
|
return $this->ajax_table; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Get the HTML of the cells in a table row, for a certain DB entry. |
252
|
|
|
* @param Entity $entry A db entry of the current entity; |
253
|
|
|
* @return array Array of HTML cell contents. |
254
|
|
|
*/ |
255
|
|
|
public function getRowViews($entry) |
256
|
|
|
{ |
257
|
|
|
$response = []; |
258
|
|
|
foreach ($this->columns as $key => $column) { |
259
|
|
|
$response[] = $this->getCellView($column, $entry); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $response; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get the HTML of a cell, using the column types. |
267
|
|
|
* @param array $column |
268
|
|
|
* @param Entity $entry A db entry of the current entity; |
269
|
|
|
* @return HTML |
270
|
|
|
*/ |
271
|
|
|
public function getCellView($column, $entry) |
272
|
|
|
{ |
273
|
|
|
if (! isset($column['type'])) { |
274
|
|
|
return \View::make('crud::columns.text')->with('crud', $this)->with('column', $column)->with('entry', |
275
|
|
|
$entry)->render(); |
276
|
|
|
} else { |
277
|
|
|
if (view()->exists('vendor.backpack.crud.columns.'.$column['type'])) { |
278
|
|
|
return \View::make('vendor.backpack.crud.columns.'.$column['type'])->with('crud', |
279
|
|
|
$this)->with('column', $column)->with('entry', $entry)->render(); |
280
|
|
|
} else { |
281
|
|
|
if (view()->exists('crud::columns.'.$column['type'])) { |
282
|
|
|
return \View::make('crud::columns.'.$column['type'])->with('crud', $this)->with('column', |
283
|
|
|
$column)->with('entry', $entry)->render(); |
284
|
|
|
} else { |
285
|
|
|
return \View::make('crud::columns.text')->with('crud', $this)->with('column', |
286
|
|
|
$column)->with('entry', $entry)->render(); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/* |
293
|
|
|
|-------------------------------------------------------------------------- |
294
|
|
|
| EXPORT BUTTONS |
295
|
|
|
|-------------------------------------------------------------------------- |
296
|
|
|
*/ |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Tell the list view to show the DataTables export buttons. |
300
|
|
|
*/ |
301
|
|
|
public function enableExportButtons() |
302
|
|
|
{ |
303
|
|
|
$this->export_buttons = true; |
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Check if export buttons are enabled for the table view. |
308
|
|
|
* @return bool |
309
|
|
|
*/ |
310
|
|
|
public function exportButtons() |
311
|
|
|
{ |
312
|
|
|
return $this->export_buttons; |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
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.