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
|
|
|
* Set the route for this CRUD. |
110
|
|
|
* Ex: admin/article. |
111
|
|
|
* |
112
|
|
|
* @param [string] Route name. |
113
|
|
|
* @param [array] Parameters. |
114
|
|
|
*/ |
115
|
|
|
public function setRoute($route) |
116
|
|
|
{ |
117
|
|
|
$this->route = $route; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the route for this CRUD using the route name. |
122
|
|
|
* Ex: admin.article. |
123
|
|
|
* |
124
|
|
|
* @param [string] Route name. |
125
|
|
|
* @param [array] Parameters. |
126
|
|
|
*/ |
127
|
1 |
|
public function setRouteName($route, $parameters = []) |
128
|
|
|
{ |
129
|
1 |
|
$complete_route = $route.'.index'; |
130
|
|
|
|
131
|
1 |
|
if (! \Route::has($complete_route)) { |
132
|
1 |
|
throw new \Exception('There are no routes for this route name.', 404); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$this->route = route($complete_route, $parameters); |
136
|
|
|
$this->initButtons(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the current CrudController route. |
141
|
|
|
* |
142
|
|
|
* Can be defined in the CrudController with: |
143
|
|
|
* - $this->crud->setRoute(config('backpack.base.route_prefix').'/article') |
144
|
|
|
* - $this->crud->setRouteName(config('backpack.base.route_prefix').'.article') |
145
|
|
|
* - $this->crud->route = config('backpack.base.route_prefix')."/article" |
146
|
|
|
* |
147
|
|
|
* @return [string] |
|
|
|
|
148
|
|
|
*/ |
149
|
|
|
public function getRoute() |
150
|
|
|
{ |
151
|
|
|
return $this->route; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Set the entity name in singular and plural. |
156
|
|
|
* Used all over the CRUD interface (header, add button, reorder button, breadcrumbs). |
157
|
|
|
* |
158
|
|
|
* @param [string] Entity name, in singular. Ex: article |
159
|
|
|
* @param [string] Entity name, in plural. Ex: articles |
160
|
|
|
*/ |
161
|
|
|
public function setEntityNameStrings($singular, $plural) |
162
|
|
|
{ |
163
|
|
|
$this->entity_name = $singular; |
164
|
|
|
$this->entity_name_plural = $plural; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// ---------------------------------- |
168
|
|
|
// Miscellaneous functions or methods |
169
|
|
|
// ---------------------------------- |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return the first element in an array that has the given 'type' attribute. |
173
|
|
|
* |
174
|
|
|
* @param string $type |
175
|
|
|
* @param array $array |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function getFirstOfItsTypeInArray($type, $array) |
180
|
|
|
{ |
181
|
2 |
|
return array_first($array, function ($item) use ($type) { |
182
|
2 |
|
return $item['type'] == $type; |
183
|
2 |
|
}); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// ------------ |
187
|
|
|
// TONE FUNCTIONS - UNDOCUMENTED, UNTESTED, SOME MAY BE USED IN THIS FILE |
188
|
|
|
// ------------ |
189
|
|
|
// |
190
|
|
|
// TODO: |
191
|
|
|
// - figure out if they are really needed |
192
|
|
|
// - comments inside the function to explain how they work |
193
|
|
|
// - write docblock for them |
194
|
|
|
// - place in the correct section above (CREATE, READ, UPDATE, DELETE, ACCESS, MANIPULATION) |
195
|
|
|
|
196
|
|
|
public function sync($type, $fields, $attributes) |
197
|
|
|
{ |
198
|
|
|
if (! empty($this->{$type})) { |
199
|
|
|
$this->{$type} = array_map(function ($field) use ($fields, $attributes) { |
200
|
|
|
if (in_array($field['name'], (array) $fields)) { |
201
|
|
|
$field = array_merge($field, $attributes); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $field; |
205
|
|
|
}, $this->{$type}); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @deprecated No longer used by internal code and not recommended. |
211
|
|
|
*/ |
212
|
|
|
public function setSort($items, $order) |
213
|
|
|
{ |
214
|
|
|
$this->sort[$items] = $order; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @deprecated No longer used by internal code and not recommended. |
219
|
|
|
*/ |
220
|
|
|
public function sort($items) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
if (array_key_exists($items, $this->sort)) { |
223
|
|
|
$elements = []; |
224
|
|
|
|
225
|
|
|
foreach ($this->sort[$items] as $item) { |
226
|
|
|
if (is_numeric($key = array_search($item, array_column($this->{$items}, 'name')))) { |
227
|
|
|
$elements[] = $this->{$items}[$key]; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $this->{$items} = array_merge($elements, array_filter($this->{$items}, function ($item) use ($items) { |
|
|
|
|
232
|
|
|
return ! in_array($item['name'], $this->sort[$items]); |
233
|
|
|
})); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $this->{$items}; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Get the Eloquent Model name from the given relation definition string. |
241
|
|
|
* |
242
|
|
|
* @example For a given string 'company' and a relation between App/Models/User and App/Models/Company, defined by a |
243
|
|
|
* company() method on the user model, the 'App/Models/Company' string will be returned. |
244
|
|
|
* |
245
|
|
|
* @example For a given string 'company.address' and a relation between App/Models/User, App/Models/Company and |
246
|
|
|
* App/Models/Address defined by a company() method on the user model and an address() method on the |
247
|
|
|
* company model, the 'App/Models/Address' string will be returned. |
248
|
|
|
* |
249
|
|
|
* @param $relationString String Relation string. A dot notation can be used to chain multiple relations. |
250
|
|
|
* |
251
|
|
|
* @return string relation model name |
252
|
|
|
*/ |
253
|
|
|
private function getRelationModel($relationString) |
254
|
|
|
{ |
255
|
9 |
|
$result = array_reduce(explode('.', $relationString), function ($obj, $method) { |
256
|
9 |
|
return $obj->$method()->getRelated(); |
257
|
9 |
|
}, $this->model); |
258
|
|
|
|
259
|
9 |
|
return get_class($result); |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|
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
.