|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Backpack\CRUD; |
|
4
|
|
|
|
|
5
|
|
|
use Backpack\CRUD\PanelTraits\Read; |
|
6
|
|
|
use Backpack\CRUD\PanelTraits\Tabs; |
|
7
|
|
|
use Backpack\CRUD\PanelTraits\Query; |
|
8
|
|
|
use Backpack\CRUD\PanelTraits\Views; |
|
9
|
|
|
use Backpack\CRUD\PanelTraits\Access; |
|
10
|
|
|
use Backpack\CRUD\PanelTraits\Create; |
|
11
|
|
|
use Backpack\CRUD\PanelTraits\Delete; |
|
12
|
|
|
use Backpack\CRUD\PanelTraits\Errors; |
|
13
|
|
|
use Backpack\CRUD\PanelTraits\Fields; |
|
14
|
|
|
use Backpack\CRUD\PanelTraits\Search; |
|
15
|
|
|
use Backpack\CRUD\PanelTraits\Update; |
|
16
|
|
|
use Backpack\CRUD\PanelTraits\AutoSet; |
|
17
|
|
|
use Backpack\CRUD\PanelTraits\Buttons; |
|
18
|
|
|
use Backpack\CRUD\PanelTraits\Columns; |
|
19
|
|
|
use Backpack\CRUD\PanelTraits\Filters; |
|
20
|
|
|
use Backpack\CRUD\PanelTraits\Reorder; |
|
21
|
|
|
use Backpack\CRUD\PanelTraits\AutoFocus; |
|
22
|
|
|
use Backpack\CRUD\PanelTraits\FakeFields; |
|
23
|
|
|
use Backpack\CRUD\PanelTraits\FakeColumns; |
|
24
|
|
|
use Backpack\CRUD\PanelTraits\ViewsAndRestoresRevisions; |
|
25
|
|
|
|
|
26
|
|
|
class CrudPanel |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
use Create, Read, Search, Update, Delete, Errors, Reorder, Access, Columns, Fields, Query, Buttons, AutoSet, FakeFields, FakeColumns, ViewsAndRestoresRevisions, AutoFocus, Filters, Tabs, Views; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
// -------------- |
|
31
|
|
|
// CRUD variables |
|
32
|
|
|
// -------------- |
|
33
|
|
|
// These variables are passed to the CRUD views, inside the $crud variable. |
|
34
|
|
|
// All variables are public, so they can be modified from your EntityCrudController. |
|
35
|
|
|
// All functions and methods are also public, so they can be used in your EntityCrudController to modify these variables. |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
// TODO: translate $entity_name and $entity_name_plural by default, with english fallback |
|
38
|
|
|
|
|
39
|
|
|
public $model = "\App\Models\Entity"; // what's the namespace for your entity's model |
|
40
|
|
|
public $route; // what route have you defined for your entity? used for links. |
|
41
|
|
|
public $entity_name = 'entry'; // what name will show up on the buttons, in singural (ex: Add entity) |
|
42
|
|
|
public $entity_name_plural = 'entries'; // what name will show up on the buttons, in plural (ex: Delete 5 entities) |
|
43
|
|
|
public $request; |
|
44
|
|
|
|
|
45
|
|
|
public $access = ['list', 'create', 'update', 'delete'/* 'revisions', reorder', 'show', 'details_row' */]; |
|
46
|
|
|
|
|
47
|
|
|
public $reorder = false; |
|
48
|
|
|
public $reorder_label = false; |
|
49
|
|
|
public $reorder_max_level = 3; |
|
50
|
|
|
|
|
51
|
|
|
public $details_row = false; |
|
52
|
|
|
public $export_buttons = false; |
|
53
|
|
|
|
|
54
|
|
|
public $columns = []; // Define the columns for the table view as an array; |
|
55
|
|
|
public $create_fields = []; // Define the fields for the "Add new entry" view as an array; |
|
56
|
|
|
public $update_fields = []; // Define the fields for the "Edit entry" view as an array; |
|
57
|
|
|
|
|
58
|
|
|
public $query; |
|
59
|
|
|
public $entry; |
|
60
|
|
|
public $buttons; |
|
61
|
|
|
public $db_column_types = []; |
|
62
|
|
|
public $default_page_length = false; |
|
63
|
|
|
|
|
64
|
|
|
// TONE FIELDS - TODO: find out what he did with them, replicate or delete |
|
65
|
|
|
public $sort = []; |
|
66
|
|
|
|
|
67
|
|
|
// The following methods are used in CrudController or your EntityCrudController to manipulate the variables above. |
|
68
|
|
|
|
|
69
|
159 |
|
public function __construct() |
|
70
|
|
|
{ |
|
71
|
159 |
|
$this->setErrorDefaults(); |
|
72
|
159 |
|
$this->initButtons(); |
|
73
|
159 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
// ------------------------------------------------------ |
|
76
|
|
|
// BASICS - model, route, entity_name, entity_name_plural |
|
77
|
|
|
// ------------------------------------------------------ |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* This function binds the CRUD to its corresponding Model (which extends Eloquent). |
|
81
|
|
|
* All Create-Read-Update-Delete operations are done using that Eloquent Collection. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $model_namespace Full model namespace. Ex: App\Models\Article] |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \Exception in case the model does not exist |
|
86
|
|
|
*/ |
|
87
|
159 |
|
public function setModel($model_namespace) |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
159 |
|
if (! class_exists($model_namespace)) { |
|
90
|
1 |
|
throw new \Exception('This model does not exist.', 404); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
159 |
|
$this->model = new $model_namespace(); |
|
|
|
|
|
|
94
|
159 |
|
$this->query = $this->model->select('*'); |
|
95
|
159 |
|
$this->entry = null; |
|
96
|
159 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the corresponding Eloquent Model for the CrudController, as defined with the setModel() function;. |
|
100
|
|
|
* |
|
101
|
|
|
* @return [Eloquent Collection] |
|
|
|
|
|
|
102
|
|
|
*/ |
|
103
|
159 |
|
public function getModel() |
|
104
|
|
|
{ |
|
105
|
159 |
|
return $this->model; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the database connection, as specified in the .env file or overwritten by the property on the model. |
|
110
|
|
|
*/ |
|
111
|
3 |
|
private function getSchema() |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
3 |
|
return \Schema::setConnection($this->getModel()->getConnection()); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set the route for this CRUD. |
|
118
|
|
|
* Ex: admin/article. |
|
119
|
|
|
* |
|
120
|
|
|
* @param [string] Route name. |
|
121
|
|
|
* @param [array] Parameters. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setRoute($route) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->route = $route; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the route for this CRUD using the route name. |
|
130
|
|
|
* Ex: admin.article. |
|
131
|
|
|
* |
|
132
|
|
|
* @param [string] Route name. |
|
133
|
|
|
* @param [array] Parameters. |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function setRouteName($route, $parameters = []) |
|
136
|
|
|
{ |
|
137
|
1 |
|
$complete_route = $route.'.index'; |
|
138
|
|
|
|
|
139
|
1 |
|
if (! \Route::has($complete_route)) { |
|
140
|
1 |
|
throw new \Exception('There are no routes for this route name.', 404); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$this->route = route($complete_route, $parameters); |
|
144
|
|
|
$this->initButtons(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get the current CrudController route. |
|
149
|
|
|
* |
|
150
|
|
|
* Can be defined in the CrudController with: |
|
151
|
|
|
* - $this->crud->setRoute(config('backpack.base.route_prefix').'/article') |
|
152
|
|
|
* - $this->crud->setRouteName(config('backpack.base.route_prefix').'.article') |
|
153
|
|
|
* - $this->crud->route = config('backpack.base.route_prefix')."/article" |
|
154
|
|
|
* |
|
155
|
|
|
* @return [string] |
|
|
|
|
|
|
156
|
|
|
*/ |
|
157
|
|
|
public function getRoute() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->route; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set the entity name in singular and plural. |
|
164
|
|
|
* Used all over the CRUD interface (header, add button, reorder button, breadcrumbs). |
|
165
|
|
|
* |
|
166
|
|
|
* @param [string] Entity name, in singular. Ex: article |
|
167
|
|
|
* @param [string] Entity name, in plural. Ex: articles |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setEntityNameStrings($singular, $plural) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->entity_name = $singular; |
|
172
|
|
|
$this->entity_name_plural = $plural; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// ---------------------------------- |
|
176
|
|
|
// Miscellaneous functions or methods |
|
177
|
|
|
// ---------------------------------- |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Return the first element in an array that has the given 'type' attribute. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $type |
|
183
|
|
|
* @param array $array |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getFirstOfItsTypeInArray($type, $array) |
|
188
|
|
|
{ |
|
189
|
2 |
|
return array_first($array, function ($item) use ($type) { |
|
190
|
2 |
|
return $item['type'] == $type; |
|
191
|
2 |
|
}); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
// ------------ |
|
195
|
|
|
// TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE |
|
196
|
|
|
// ------------ |
|
197
|
|
|
// |
|
198
|
|
|
// TODO: |
|
199
|
|
|
// - figure out if they are really needed |
|
200
|
|
|
// - comments inside the function to explain how they work |
|
201
|
|
|
// - write docblock for them |
|
202
|
|
|
// - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION) |
|
203
|
|
|
|
|
204
|
|
|
public function sync($type, $fields, $attributes) |
|
205
|
|
|
{ |
|
206
|
|
|
if (! empty($this->{$type})) { |
|
207
|
|
|
$this->{$type} = array_map(function ($field) use ($fields, $attributes) { |
|
208
|
|
|
if (in_array($field['name'], (array) $fields)) { |
|
209
|
|
|
$field = array_merge($field, $attributes); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $field; |
|
213
|
|
|
}, $this->{$type}); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @deprecated No longer used by internal code and not recommended. |
|
219
|
|
|
*/ |
|
220
|
|
|
public function setSort($items, $order) |
|
221
|
|
|
{ |
|
222
|
|
|
$this->sort[$items] = $order; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @deprecated No longer used by internal code and not recommended. |
|
227
|
|
|
*/ |
|
228
|
|
|
public function sort($items) |
|
|
|
|
|
|
229
|
|
|
{ |
|
230
|
|
|
if (array_key_exists($items, $this->sort)) { |
|
231
|
|
|
$elements = []; |
|
232
|
|
|
|
|
233
|
|
|
foreach ($this->sort[$items] as $item) { |
|
234
|
|
|
if (is_numeric($key = array_search($item, array_column($this->{$items}, 'name')))) { |
|
235
|
|
|
$elements[] = $this->{$items}[$key]; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
return $this->{$items} = array_merge($elements, array_filter($this->{$items}, function ($item) use ($items) { |
|
|
|
|
|
|
240
|
|
|
return ! in_array($item['name'], $this->sort[$items]); |
|
241
|
|
|
})); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $this->{$items}; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Get the Eloquent Model name from the given relation definition string. |
|
249
|
|
|
* |
|
250
|
|
|
* @example For a given string 'company' and a relation between App/Models/User and App/Models/Company, defined by a |
|
251
|
|
|
* company() method on the user model, the 'App/Models/Company' string will be returned. |
|
252
|
|
|
* |
|
253
|
|
|
* @example For a given string 'company.address' and a relation between App/Models/User, App/Models/Company and |
|
254
|
|
|
* App/Models/Address defined by a company() method on the user model and an address() method on the |
|
255
|
|
|
* company model, the 'App/Models/Address' string will be returned. |
|
256
|
|
|
* |
|
257
|
|
|
* @param $relationString String Relation string. A dot notation can be used to chain multiple relations. |
|
258
|
|
|
* |
|
259
|
|
|
* @return string relation model name |
|
260
|
|
|
*/ |
|
261
|
|
|
private function getRelationModel($relationString) |
|
262
|
|
|
{ |
|
263
|
9 |
|
$result = array_reduce(explode('.', $relationString), function ($obj, $method) { |
|
264
|
9 |
|
return $obj->$method()->getRelated(); |
|
265
|
9 |
|
}, $this->model); |
|
266
|
|
|
|
|
267
|
9 |
|
return get_class($result); |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.