1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
6
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
7
|
|
|
use Illuminate\Routing\Controller as BaseController; |
8
|
|
|
use Illuminate\Support\Facades\Form as Form; |
9
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest; |
10
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as UpdateRequest; |
11
|
|
|
use Backpack\CRUD\CrudPanel; |
12
|
|
|
// CRUD Traits for non-core features |
13
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\AjaxTable; |
14
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Reorder; |
15
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Revisions; |
16
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\ShowDetailsRow; |
17
|
|
|
|
18
|
|
|
class CrudController extends BaseController |
19
|
|
|
{ |
20
|
|
|
use DispatchesJobs, ValidatesRequests; |
21
|
|
|
use AjaxTable, Reorder, Revisions, ShowDetailsRow; |
22
|
|
|
|
23
|
|
|
public $data = []; |
24
|
|
|
public $crud; |
25
|
|
|
|
26
|
|
|
public function __construct() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
$this->crud = new CrudPanel(); |
29
|
|
|
|
30
|
|
|
// call the setup function inside this closure to also have the request there |
31
|
|
|
// this way, developers can use things stored in session (auth variables, etc) |
32
|
|
|
$this->middleware(function ($request, $next) { |
33
|
|
|
$this->setup(); |
34
|
|
|
|
35
|
|
|
return $next($request); |
36
|
|
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Allow developers to set their configuration options for a CrudPanel. |
41
|
|
|
*/ |
42
|
|
|
public function setup() |
43
|
|
|
{ |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Display all rows in the database for this entity. |
48
|
|
|
* |
49
|
|
|
* @return Response |
50
|
|
|
*/ |
51
|
|
|
public function index() |
52
|
|
|
{ |
53
|
|
|
$this->crud->hasAccessOrFail('list'); |
54
|
|
|
|
55
|
|
|
$this->data['crud'] = $this->crud; |
56
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name_plural); |
57
|
|
|
|
58
|
|
|
// get all entries if AJAX is not enabled |
59
|
|
|
if (! $this->data['crud']->ajaxTable()) { |
60
|
|
|
$this->data['entries'] = $this->data['crud']->getEntries(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
64
|
|
|
// $this->crud->getListView() returns 'list' by default, or 'list_ajax' if ajax was enabled |
|
|
|
|
65
|
|
|
return view('crud::list', $this->data); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Show the form for creating inserting a new row. |
70
|
|
|
* |
71
|
|
|
* @return Response |
72
|
|
|
*/ |
73
|
|
|
public function create() |
74
|
|
|
{ |
75
|
|
|
$this->crud->hasAccessOrFail('create'); |
76
|
|
|
|
77
|
|
|
// prepare the fields you need to show |
78
|
|
|
$this->data['crud'] = $this->crud; |
79
|
|
|
$this->data['fields'] = $this->crud->getCreateFields(); |
80
|
|
|
$this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
81
|
|
|
|
82
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
83
|
|
|
return view('crud::create', $this->data); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Store a newly created resource in the database. |
88
|
|
|
* |
89
|
|
|
* @param StoreRequest $request - type injection used for validation using Requests |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Http\RedirectResponse |
92
|
|
|
*/ |
93
|
|
|
public function storeCrud(StoreRequest $request = null) |
94
|
|
|
{ |
95
|
|
|
$this->crud->hasAccessOrFail('create'); |
96
|
|
|
|
97
|
|
|
// fallback to global request instance |
98
|
|
|
if (is_null($request)) { |
99
|
|
|
$request = \Request::instance(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
103
|
|
|
foreach ($request->input() as $key => $value) { |
104
|
|
|
if (empty($value) && $value !== '0') { |
105
|
|
|
$request->request->set($key, null); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// insert item in the db |
110
|
|
|
$item = $this->crud->create($request->except(['redirect_after_save', '_token'])); |
111
|
|
|
|
112
|
|
|
// show a success message |
113
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
114
|
|
|
|
115
|
|
|
// redirect the user where he chose to be redirected |
116
|
|
|
switch ($request->input('redirect_after_save')) { |
117
|
|
|
case 'current_item_edit': |
118
|
|
|
return \Redirect::to($this->crud->route.'/'.$item->getKey().'/edit'); |
119
|
|
|
|
120
|
|
|
default: |
121
|
|
|
return \Redirect::to($request->input('redirect_after_save')); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Show the form for editing the specified resource. |
127
|
|
|
* |
128
|
|
|
* @param int $id |
129
|
|
|
* |
130
|
|
|
* @return Response |
131
|
|
|
*/ |
132
|
|
|
public function edit($id) |
133
|
|
|
{ |
134
|
|
|
$this->crud->hasAccessOrFail('update'); |
135
|
|
|
|
136
|
|
|
// get the info for that entry |
137
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
138
|
|
|
$this->data['crud'] = $this->crud; |
139
|
|
|
$this->data['fields'] = $this->crud->getUpdateFields($id); |
140
|
|
|
$this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name; |
141
|
|
|
|
142
|
|
|
$this->data['id'] = $id; |
143
|
|
|
|
144
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
145
|
|
|
return view('crud::edit', $this->data); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Update the specified resource in the database. |
150
|
|
|
* |
151
|
|
|
* @param UpdateRequest $request - type injection used for validation using Requests |
152
|
|
|
* |
153
|
|
|
* @return \Illuminate\Http\RedirectResponse |
154
|
|
|
*/ |
155
|
|
|
public function updateCrud(UpdateRequest $request = null) |
156
|
|
|
{ |
157
|
|
|
$this->crud->hasAccessOrFail('update'); |
158
|
|
|
|
159
|
|
|
// fallback to global request instance |
160
|
|
|
if (is_null($request)) { |
161
|
|
|
$request = \Request::instance(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
165
|
|
|
foreach ($request->input() as $key => $value) { |
166
|
|
|
if (empty($value) && $value !== '0') { |
167
|
|
|
$request->request->set($key, null); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// update the row in the db |
172
|
|
|
$this->crud->update($request->get($this->crud->model->getKeyName()), |
173
|
|
|
$request->except('redirect_after_save', '_token')); |
174
|
|
|
|
175
|
|
|
// show a success message |
176
|
|
|
\Alert::success(trans('backpack::crud.update_success'))->flash(); |
177
|
|
|
|
178
|
|
|
return \Redirect::to($this->crud->route); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Display the specified resource. |
183
|
|
|
* |
184
|
|
|
* @param int $id |
185
|
|
|
* |
186
|
|
|
* @return Response |
187
|
|
|
*/ |
188
|
|
|
public function show($id) |
189
|
|
|
{ |
190
|
|
|
$this->crud->hasAccessOrFail('show'); |
191
|
|
|
|
192
|
|
|
// get the info for that entry |
193
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
194
|
|
|
$this->data['crud'] = $this->crud; |
195
|
|
|
$this->data['title'] = trans('backpack::crud.preview').' '.$this->crud->entity_name; |
196
|
|
|
|
197
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
198
|
|
|
return view('crud::show', $this->data); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Remove the specified resource from storage. |
203
|
|
|
* |
204
|
|
|
* @param int $id |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
public function destroy($id) |
209
|
|
|
{ |
210
|
|
|
$this->crud->hasAccessOrFail('delete'); |
211
|
|
|
|
212
|
|
|
return $this->crud->delete($id); |
213
|
|
|
} |
214
|
|
|
<<<<<<< HEAD |
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Reorder the items in the database using the Nested Set pattern. |
218
|
|
|
* |
219
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
220
|
|
|
* |
221
|
|
|
* @return Response |
222
|
|
|
*/ |
223
|
|
|
public function reorder() |
224
|
|
|
{ |
225
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
226
|
|
|
|
227
|
|
|
if (! $this->crud->isReorderEnabled()) { |
228
|
|
|
abort(403, 'Reorder is disabled.'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// get all results for that entity |
232
|
|
|
$this->data['entries'] = $this->crud->getEntries(); |
233
|
|
|
$this->data['crud'] = $this->crud; |
234
|
|
|
$this->data['title'] = trans('backpack::crud.reorder').' '.$this->crud->entity_name; |
235
|
|
|
|
236
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
237
|
|
|
return view('crud::reorder', $this->data); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Save the new order, using the Nested Set pattern. |
242
|
|
|
* |
243
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
244
|
|
|
* |
245
|
|
|
* @return |
246
|
|
|
*/ |
247
|
|
|
public function saveReorder() |
248
|
|
|
{ |
249
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
250
|
|
|
|
251
|
|
|
$all_entries = \Request::input('tree'); |
252
|
|
|
|
253
|
|
|
if (count($all_entries)) { |
254
|
|
|
$count = $this->crud->updateTreeOrder($all_entries); |
255
|
|
|
} else { |
256
|
|
|
return false; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return 'success for '.$count.' items'; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
|
|
|
|
264
|
|
|
* It defaults to showing some dummy text. |
265
|
|
|
* |
266
|
|
|
* It's enabled by: |
267
|
|
|
* - setting: $crud->details_row = true; |
268
|
|
|
* - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
|
|
|
|
269
|
|
|
* - adding a view with the following name to change what the row actually contains: app/resources/views/vendor/backpack/crud/details_row.blade.php |
|
|
|
|
270
|
|
|
*/ |
271
|
|
|
public function showDetailsRow($id) |
272
|
|
|
{ |
273
|
|
|
$this->crud->hasAccessOrFail('details_row'); |
274
|
|
|
|
275
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
276
|
|
|
$this->data['crud'] = $this->crud; |
277
|
|
|
|
278
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
279
|
|
|
return view('crud::details_row', $this->data); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Respond with the JSON of one or more rows, depending on the POST parameters. |
284
|
|
|
* @return JSON Array of cells in HTML form. |
285
|
|
|
*/ |
286
|
|
|
public function search() |
287
|
|
|
{ |
288
|
|
|
$this->crud->hasAccessOrFail('list'); |
289
|
|
|
|
290
|
|
|
// add the primary key, even though you don't show it, |
291
|
|
|
// otherwise the buttons won't work |
292
|
|
|
$columns = collect($this->crud->columns)->pluck('name')->merge($this->crud->model->getKeyName())->toArray(); |
293
|
|
|
|
294
|
|
|
// structure the response in a DataTable-friendly way |
295
|
|
|
$dataTable = new DataTable($this->crud->query, $columns); |
296
|
|
|
|
297
|
|
|
// make the datatable use the column types instead of just echoing the text |
298
|
|
|
$dataTable->setFormatRowFunction(function ($entry) { |
299
|
|
|
// get the actual HTML for each row's cell |
300
|
|
|
$row_items = $this->crud->getRowViews($entry, $this->crud); |
301
|
|
|
|
302
|
|
|
// add the buttons as the last column |
303
|
|
|
if ($this->crud->buttons->where('stack', 'line')->count()) { |
304
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
305
|
|
|
->with('crud', $this->crud) |
306
|
|
|
->with('entry', $entry) |
307
|
|
|
->render(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
// add the details_row buttons as the first column |
311
|
|
|
if ($this->crud->details_row) { |
312
|
|
|
array_unshift($row_items, \View::make('crud::columns.details_row_button') |
313
|
|
|
->with('crud', $this->crud) |
314
|
|
|
->with('entry', $entry) |
315
|
|
|
->render()); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return $row_items; |
319
|
|
|
}); |
320
|
|
|
|
321
|
|
|
return $dataTable->make(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Checks if the given string is unique, and return existing entity. |
326
|
|
|
* @return JSON containing success, message and data. |
327
|
|
|
*/ |
328
|
|
|
public function unicity() |
329
|
|
|
{ |
330
|
|
|
$response = ['success' => false, 'message' => trans('backpack::crud.unique_error'), 'meta' => ['link' => null, 'snippet' => null, 'entity_key' => null]]; |
|
|
|
|
331
|
|
|
|
332
|
|
|
$field_name = \Request::input('field_name', null); |
333
|
|
|
$check_value = \Request::input('check_value', null); |
334
|
|
|
$display_name = \Request::input('display_name', null); |
335
|
|
|
|
336
|
|
|
if (empty($field_name)) { |
337
|
|
|
$response['message'] = trans('backpack::crud.unique_field_name_missing'); |
338
|
|
|
} elseif (empty($check_value) && $check_value !== '0') { |
339
|
|
|
$response['message'] = trans('backpack::crud.unique_check_value_missing'); |
340
|
|
|
} else { |
341
|
|
|
$existing_entity = $this->crud->model->where([$field_name => $check_value])->first(); |
342
|
|
|
|
343
|
|
|
if (! $existing_entity) { |
344
|
|
|
$response['success'] = true; |
345
|
|
|
$response['message'] = null; |
346
|
|
|
} else { |
347
|
|
|
$response['message'] = $this->crud->entity_name.' '.trans('backpack::crud.unique_exists'); |
348
|
|
|
$response['meta'] = [ |
349
|
|
|
'link' => url($this->crud->route.'/'.$existing_entity->getKey().'/edit'), |
350
|
|
|
'snippet' => $display_name ? $existing_entity->{$display_name} : $this->crud->entity_name, |
351
|
|
|
'entity_key' => $existing_entity->getKey(), |
352
|
|
|
]; |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $response; |
357
|
|
|
} |
358
|
|
|
======= |
359
|
|
|
>>>>>>> master |
360
|
|
|
} |
361
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.