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
|
|
|
foreach ($request->input() as $key => $value) { |
|
|
|
|
85
|
|
|
if (empty($value)) { |
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'])); |
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
|
|
|
foreach ($request->input() as $key => $value) { |
|
|
|
|
147
|
|
|
if (empty($value)) { |
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')); |
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'] = trans('backpack::crud.revisions').' '.$this->crud->entity_name; |
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
|
|
|
|
217
|
|
|
$this->crud->hasAccessOrFail('revisions'); |
218
|
|
|
|
219
|
|
|
// @TODO: Currently the route already contains the revision ID, so passing it as a POST param |
220
|
|
|
// is somewhat superfluous.. however if we are POSTing, it makes sense to actually have data to post. |
221
|
|
|
// Perhaps the route shoud be better named to reflect this (e.g. just /model/{id}/revisions) (??) |
222
|
|
|
$revisionId = \Request::input('revision_id', false); |
|
|
|
|
223
|
|
|
if(!$revisionId) { |
224
|
|
|
abort(500, 'Can\'t restore revision without revision_id'); |
225
|
|
|
} else { |
226
|
|
|
$this->crud->restoreRevision($id, $revisionId); // do the update |
227
|
|
|
|
228
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
229
|
|
|
$this->data['crud'] = $this->crud; |
230
|
|
|
$this->data['revisions'] = $this->crud->listRevisions($id); // Reload revisions as they have changed |
231
|
|
|
|
232
|
|
|
// Rebuild the revision timeline HTML and return it to the AJAX call |
233
|
|
|
// @TODO: Return only the latest revision to save bandwidth - 15/9/16 @se1exin |
234
|
|
|
return view('crud::inc.revision_timeline', $this->data); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Remove the specified resource from storage. |
240
|
|
|
* |
241
|
|
|
* @param int $id |
242
|
|
|
* |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function destroy($id) |
246
|
|
|
{ |
247
|
|
|
$this->crud->hasAccessOrFail('delete'); |
248
|
|
|
|
249
|
|
|
return $this->crud->delete($id); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Reorder the items in the database using the Nested Set pattern. |
254
|
|
|
* |
255
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
256
|
|
|
* |
257
|
|
|
* @return Response |
|
|
|
|
258
|
|
|
*/ |
259
|
|
|
public function reorder() |
260
|
|
|
{ |
261
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
262
|
|
|
|
263
|
|
|
if (! $this->crud->isReorderEnabled()) { |
264
|
|
|
abort(403, 'Reorder is disabled.'); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
// get all results for that entity |
268
|
|
|
$this->data['entries'] = $this->crud->getEntries(); |
269
|
|
|
$this->data['crud'] = $this->crud; |
270
|
|
|
$this->data['title'] = trans('backpack::crud.reorder').' '.$this->crud->entity_name; |
271
|
|
|
|
272
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
273
|
|
|
return view('crud::reorder', $this->data); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Save the new order, using the Nested Set pattern. |
278
|
|
|
* |
279
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
280
|
|
|
* |
281
|
|
|
* @return |
282
|
|
|
*/ |
283
|
|
|
public function saveReorder() |
284
|
|
|
{ |
285
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
286
|
|
|
|
287
|
|
|
$all_entries = \Request::input('tree'); |
288
|
|
|
|
289
|
|
|
if (count($all_entries)) { |
290
|
|
|
$count = $this->crud->updateTreeOrder($all_entries); |
291
|
|
|
} else { |
292
|
|
|
return false; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return 'success for '.$count.' items'; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
|
|
|
|
300
|
|
|
* It defaults to showing some dummy text. |
301
|
|
|
* |
302
|
|
|
* It's enabled by: |
303
|
|
|
* - setting: $crud->details_row = true; |
304
|
|
|
* - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
|
|
|
|
305
|
|
|
* - adding a view with the following name to change what the row actually contains: app/resources/views/vendor/backpack/crud/details_row.blade.php |
|
|
|
|
306
|
|
|
*/ |
307
|
|
|
public function showDetailsRow($id) |
308
|
|
|
{ |
309
|
|
|
$this->crud->hasAccessOrFail('details_row'); |
310
|
|
|
|
311
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
312
|
|
|
$this->data['crud'] = $this->crud; |
313
|
|
|
|
314
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
315
|
|
|
return view('crud::details_row', $this->data); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Respond with the JSON of one or more rows, depending on the POST parameters. |
320
|
|
|
* @return JSON Array of cells in HTML form. |
|
|
|
|
321
|
|
|
*/ |
322
|
|
|
public function search() |
323
|
|
|
{ |
324
|
|
|
$this->crud->hasAccessOrFail('list'); |
325
|
|
|
|
326
|
|
|
// add the primary key, even though you don't show it, |
327
|
|
|
// otherwise the buttons won't work |
328
|
|
|
$columns = collect($this->crud->columns)->pluck('name')->merge($this->crud->model->getKeyName())->toArray(); |
|
|
|
|
329
|
|
|
|
330
|
|
|
// structure the response in a DataTable-friendly way |
331
|
|
|
$dataTable = new DataTable($this->crud->query, $columns); |
332
|
|
|
|
333
|
|
|
// make the datatable use the column types instead of just echoing the text |
334
|
|
|
$dataTable->setFormatRowFunction(function ($entry) { |
335
|
|
|
// get the actual HTML for each row's cell |
336
|
|
|
$row_items = $this->crud->getRowViews($entry, $this->crud); |
|
|
|
|
337
|
|
|
|
338
|
|
|
// add the buttons as the last column |
339
|
|
|
if ($this->crud->buttons->where('stack', 'line')->count()) { |
340
|
|
|
$row_items[] = \View::make('crud::inc.button_stack', ['stack' => 'line']) |
341
|
|
|
->with('crud', $this->crud) |
342
|
|
|
->with('entry', $entry) |
343
|
|
|
->render(); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
// add the details_row buttons as the first column |
347
|
|
|
if ($this->crud->details_row) { |
348
|
|
|
array_unshift($row_items, \View::make('crud::columns.details_row_button') |
349
|
|
|
->with('crud', $this->crud) |
350
|
|
|
->with('entry', $entry) |
351
|
|
|
->render()); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
return $row_items; |
355
|
|
|
}); |
356
|
|
|
|
357
|
|
|
return $dataTable->make(); |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
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.