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