1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest; |
6
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as UpdateRequest; |
7
|
|
|
use Backpack\CRUD\CrudPanel; |
8
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
9
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
10
|
|
|
// VALIDATION: change the requests to match your own file names if you need form validation |
11
|
|
|
use Illuminate\Routing\Controller as BaseController; |
12
|
|
|
use Illuminate\Support\Facades\Form as Form; |
13
|
|
|
use LiveControl\EloquentDataTable\DataTable; |
14
|
|
|
|
15
|
|
|
class CrudController extends BaseController |
16
|
|
|
{ |
17
|
|
|
use DispatchesJobs, ValidatesRequests; |
18
|
|
|
|
19
|
|
|
public $data = []; |
20
|
|
|
public $crud; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->crud = new CrudPanel(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Display all rows in the database for this entity. |
29
|
|
|
* |
30
|
|
|
* @return Response |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public function index() |
33
|
|
|
{ |
34
|
|
|
$this->crud->hasAccessOrFail('list'); |
35
|
|
|
|
36
|
|
|
$this->data['crud'] = $this->crud; |
37
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name_plural); |
38
|
|
|
|
39
|
|
|
// get all entries if AJAX is not enabled |
40
|
|
|
if (! $this->data['crud']->ajaxTable()) { |
41
|
|
|
$this->data['entries'] = $this->data['crud']->getEntries(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
45
|
|
|
// $this->crud->getListView() returns 'list' by default, or 'list_ajax' if ajax was enabled |
|
|
|
|
46
|
|
|
return view('crud::list', $this->data); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Show the form for creating inserting a new row. |
51
|
|
|
* |
52
|
|
|
* @return Response |
|
|
|
|
53
|
|
|
*/ |
54
|
|
View Code Duplication |
public function create() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$this->crud->hasAccessOrFail('create'); |
57
|
|
|
|
58
|
|
|
// prepare the fields you need to show |
59
|
|
|
$this->data['crud'] = $this->crud; |
60
|
|
|
$this->data['fields'] = $this->crud->getCreateFields(); |
61
|
|
|
$this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
62
|
|
|
|
63
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
64
|
|
|
return view('crud::create', $this->data); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Store a newly created resource in the database. |
69
|
|
|
* |
70
|
|
|
* @param StoreRequest $request - type injection used for validation using Requests |
|
|
|
|
71
|
|
|
* |
72
|
|
|
* @return \Illuminate\Http\RedirectResponse |
73
|
|
|
*/ |
74
|
|
|
public function storeCrud(StoreRequest $request = null) |
75
|
|
|
{ |
76
|
|
|
$this->crud->hasAccessOrFail('create'); |
77
|
|
|
|
78
|
|
|
// fallback to global request instance |
79
|
|
|
if (is_null($request)) { |
80
|
|
|
$request = \Request::instance(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
84
|
|
View Code Duplication |
foreach ($request->input() as $key => $value) { |
|
|
|
|
85
|
|
|
if (empty($value) && $value !== '0') { |
86
|
|
|
$request->request->set($key, null); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// insert item in the db |
91
|
|
|
$item = $this->crud->create($request->except(['redirect_after_save', 'password', '_token'])); |
92
|
|
|
|
93
|
|
|
// show a success message |
94
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
95
|
|
|
|
96
|
|
|
// redirect the user where he chose to be redirected |
97
|
|
|
switch ($request->input('redirect_after_save')) { |
98
|
|
|
case 'current_item_edit': |
99
|
|
|
return \Redirect::to($this->crud->route.'/'.$item->getKey().'/edit'); |
100
|
|
|
|
101
|
|
|
default: |
102
|
|
|
return \Redirect::to($request->input('redirect_after_save')); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Show the form for editing the specified resource. |
108
|
|
|
* |
109
|
|
|
* @param int $id |
110
|
|
|
* |
111
|
|
|
* @return Response |
|
|
|
|
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function edit($id) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$this->crud->hasAccessOrFail('update'); |
116
|
|
|
|
117
|
|
|
// get the info for that entry |
118
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
119
|
|
|
$this->data['crud'] = $this->crud; |
120
|
|
|
$this->data['fields'] = $this->crud->getUpdateFields($id); |
121
|
|
|
$this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name; |
122
|
|
|
|
123
|
|
|
$this->data['id'] = $id; |
124
|
|
|
|
125
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
126
|
|
|
return view('crud::edit', $this->data); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Update the specified resource in the database. |
131
|
|
|
* |
132
|
|
|
* @param UpdateRequest $request - type injection used for validation using Requests |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return \Illuminate\Http\RedirectResponse |
135
|
|
|
*/ |
136
|
|
|
public function updateCrud(UpdateRequest $request = null) |
137
|
|
|
{ |
138
|
|
|
$this->crud->hasAccessOrFail('update'); |
139
|
|
|
|
140
|
|
|
// fallback to global request instance |
141
|
|
|
if (is_null($request)) { |
142
|
|
|
$request = \Request::instance(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
146
|
|
View Code Duplication |
foreach ($request->input() as $key => $value) { |
|
|
|
|
147
|
|
|
if (empty($value) && $value !== '0') { |
148
|
|
|
$request->request->set($key, null); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
// update the row in the db |
153
|
|
|
$this->crud->update($request->get($this->crud->model->getKeyName()), |
|
|
|
|
154
|
|
|
$request->except('redirect_after_save', '_token')); |
155
|
|
|
|
156
|
|
|
// show a success message |
157
|
|
|
\Alert::success(trans('backpack::crud.update_success'))->flash(); |
158
|
|
|
|
159
|
|
|
return \Redirect::to($this->crud->route); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Display the specified resource. |
164
|
|
|
* |
165
|
|
|
* @param int $id |
166
|
|
|
* |
167
|
|
|
* @return Response |
|
|
|
|
168
|
|
|
*/ |
169
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
$this->crud->hasAccessOrFail('show'); |
172
|
|
|
|
173
|
|
|
// get the info for that entry |
174
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
175
|
|
|
$this->data['crud'] = $this->crud; |
176
|
|
|
$this->data['title'] = trans('backpack::crud.preview').' '.$this->crud->entity_name; |
177
|
|
|
|
178
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
179
|
|
|
return view('crud::show', $this->data); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Display the revisions for specified resource. |
184
|
|
|
* |
185
|
|
|
* @param int $id |
186
|
|
|
* |
187
|
|
|
* @return Response |
|
|
|
|
188
|
|
|
*/ |
189
|
|
View Code Duplication |
public function listRevisions($id) |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
$this->crud->hasAccessOrFail('revisions'); |
192
|
|
|
|
193
|
|
|
// get the info for that entry |
194
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
195
|
|
|
$this->data['crud'] = $this->crud; |
196
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name).' '.trans('backpack::crud.revisions'); |
197
|
|
|
$this->data['id'] = $id; |
198
|
|
|
$this->data['revisions'] = $this->crud->listRevisions($id); |
199
|
|
|
|
200
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
201
|
|
|
return view('crud::revisions', $this->data); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Restore a specific revision for the specified resource. |
206
|
|
|
* |
207
|
|
|
* Used via AJAX in the revisions view |
208
|
|
|
* |
209
|
|
|
* @param int $id |
210
|
|
|
* |
211
|
|
|
* @return JSON Response containing the new revision that was created from the update |
212
|
|
|
* @return HTTP 500 if the request did not contain the revision ID |
|
|
|
|
213
|
|
|
*/ |
214
|
|
|
public function restoreRevision($id) |
215
|
|
|
{ |
216
|
|
|
$this->crud->hasAccessOrFail('revisions'); |
217
|
|
|
|
218
|
|
|
// @TODO: Currently the route already contains the revision ID, so passing it as a POST param |
219
|
|
|
// is somewhat superfluous.. however if we are POSTing, it makes sense to actually have data to post. |
220
|
|
|
// Perhaps the route shoud be better named to reflect this (e.g. just /model/{id}/revisions) (??) |
221
|
|
|
$revisionId = \Request::input('revision_id', false); |
|
|
|
|
222
|
|
|
if (! $revisionId) { |
223
|
|
|
abort(500, 'Can\'t restore revision without revision_id'); |
224
|
|
|
} else { |
225
|
|
|
$this->crud->restoreRevision($id, $revisionId); // do the update |
226
|
|
|
|
227
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
228
|
|
|
$this->data['crud'] = $this->crud; |
229
|
|
|
$this->data['revisions'] = $this->crud->listRevisions($id); // Reload revisions as they have changed |
230
|
|
|
|
231
|
|
|
// Rebuild the revision timeline HTML and return it to the AJAX call |
232
|
|
|
// @TODO: Return only the latest revision to save bandwidth - 15/9/16 @se1exin |
233
|
|
|
return view('crud::inc.revision_timeline', $this->data); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Remove the specified resource from storage. |
239
|
|
|
* |
240
|
|
|
* @param int $id |
241
|
|
|
* |
242
|
|
|
* @return string |
243
|
|
|
*/ |
244
|
|
|
public function destroy($id) |
245
|
|
|
{ |
246
|
|
|
$this->crud->hasAccessOrFail('delete'); |
247
|
|
|
|
248
|
|
|
return $this->crud->delete($id); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Reorder the items in the database using the Nested Set pattern. |
253
|
|
|
* |
254
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
255
|
|
|
* |
256
|
|
|
* @return Response |
|
|
|
|
257
|
|
|
*/ |
258
|
|
|
public function reorder() |
259
|
|
|
{ |
260
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
261
|
|
|
|
262
|
|
|
if (! $this->crud->isReorderEnabled()) { |
263
|
|
|
abort(403, 'Reorder is disabled.'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
// get all results for that entity |
267
|
|
|
$this->data['entries'] = $this->crud->getEntries(); |
268
|
|
|
$this->data['crud'] = $this->crud; |
269
|
|
|
$this->data['title'] = trans('backpack::crud.reorder').' '.$this->crud->entity_name; |
270
|
|
|
|
271
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
272
|
|
|
return view('crud::reorder', $this->data); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Save the new order, using the Nested Set pattern. |
277
|
|
|
* |
278
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
279
|
|
|
* |
280
|
|
|
* @return |
281
|
|
|
*/ |
282
|
|
|
public function saveReorder() |
283
|
|
|
{ |
284
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
285
|
|
|
|
286
|
|
|
$all_entries = \Request::input('tree'); |
287
|
|
|
|
288
|
|
|
if (count($all_entries)) { |
289
|
|
|
$count = $this->crud->updateTreeOrder($all_entries); |
290
|
|
|
} else { |
291
|
|
|
return false; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return 'success for '.$count.' items'; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
|
|
|
|
299
|
|
|
* It defaults to showing some dummy text. |
300
|
|
|
* |
301
|
|
|
* It's enabled by: |
302
|
|
|
* - setting: $crud->details_row = true; |
303
|
|
|
* - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
|
|
|
|
304
|
|
|
* - adding a view with the following name to change what the row actually contains: app/resources/views/vendor/backpack/crud/details_row.blade.php |
|
|
|
|
305
|
|
|
*/ |
306
|
|
|
public function showDetailsRow($id) |
307
|
|
|
{ |
308
|
|
|
$this->crud->hasAccessOrFail('details_row'); |
309
|
|
|
|
310
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
311
|
|
|
$this->data['crud'] = $this->crud; |
312
|
|
|
|
313
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
314
|
|
|
return view('crud::details_row', $this->data); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Respond with the JSON of one or more rows, depending on the POST parameters. |
319
|
|
|
* @return JSON Array of cells in HTML form. |
|
|
|
|
320
|
|
|
*/ |
321
|
|
|
public function search() |
322
|
|
|
{ |
323
|
|
|
$this->crud->hasAccessOrFail('list'); |
324
|
|
|
|
325
|
|
|
// add the primary key, even though you don't show it, |
326
|
|
|
// otherwise the buttons won't work |
327
|
|
|
$columns = collect($this->crud->columns)->pluck('name')->merge($this->crud->model->getKeyName())->toArray(); |
|
|
|
|
328
|
|
|
|
329
|
|
|
// structure the response in a DataTable-friendly way |
330
|
|
|
$dataTable = new DataTable($this->crud->query, $columns); |
331
|
|
|
|
332
|
|
|
// make the datatable use the column types instead of just echoing the text |
333
|
|
|
$dataTable->setFormatRowFunction(function ($entry) { |
334
|
|
|
// get the actual HTML for each row's cell |
335
|
|
|
$row_items = $this->crud->getRowViews($entry, $this->crud); |
|
|
|
|
336
|
|
|
|
337
|
|
|
// add the buttons as the last column |
338
|
|
|
if ($this->crud->buttons->where('stack', 'line')->count()) { |
339
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
340
|
|
|
->with('crud', $this->crud) |
341
|
|
|
->with('entry', $entry) |
342
|
|
|
->render(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
// add the details_row buttons as the first column |
346
|
|
|
if ($this->crud->details_row) { |
347
|
|
|
array_unshift($row_items, \View::make('crud::columns.details_row_button') |
348
|
|
|
->with('crud', $this->crud) |
349
|
|
|
->with('entry', $entry) |
350
|
|
|
->render()); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $row_items; |
354
|
|
|
}); |
355
|
|
|
|
356
|
|
|
return $dataTable->make(); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Checks if the given string is unique, and return existing entity. |
361
|
|
|
* @return JSON containing success, message and data. |
|
|
|
|
362
|
|
|
*/ |
363
|
|
|
public function unicity() |
364
|
|
|
{ |
365
|
|
|
$response = ['success' => false, 'message' => trans('backpack::crud.unique_error'), 'meta' => ['link' => null, 'snippet' => null, 'entity_key' => null]]; |
|
|
|
|
366
|
|
|
|
367
|
|
|
$field_name = \Request::input('field_name', null); |
368
|
|
|
$check_value = \Request::input('check_value', null); |
369
|
|
|
$display_name = \Request::input('display_name', null); |
370
|
|
|
|
371
|
|
|
if (empty($field_name)) { |
372
|
|
|
$response['message'] = trans('backpack::crud.unique_field_name_missing'); |
373
|
|
|
} elseif (empty($check_value) && $check_value !== '0') { |
374
|
|
|
$response['message'] = trans('backpack::crud.unique_check_value_missing'); |
375
|
|
|
} else { |
376
|
|
|
$existing_entity = $this->crud->model->where([$field_name => $check_value])->first(); |
|
|
|
|
377
|
|
|
|
378
|
|
|
if (! $existing_entity) { |
379
|
|
|
$response['success'] = true; |
380
|
|
|
$response['message'] = null; |
381
|
|
|
} else { |
382
|
|
|
$response['message'] = $this->crud->entity_name.' '.trans('backpack::crud.unique_exists'); |
383
|
|
|
$response['meta'] = [ |
384
|
|
|
'link' => url($this->crud->route.'/'.$existing_entity->getKey().'/edit'), |
385
|
|
|
'snippet' => $display_name ? $existing_entity->{$display_name} : $this->crud->entity_name, |
386
|
|
|
'entity_key' => $existing_entity->getKey(), |
387
|
|
|
]; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return $response; |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.