|
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
|
|
|
|
|
10
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest; |
|
11
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as UpdateRequest; |
|
12
|
|
|
use Backpack\CRUD\CrudPanel; |
|
13
|
|
|
|
|
14
|
|
|
// CRUD Traits for non-core features |
|
15
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\AjaxTable; |
|
16
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Reorder; |
|
17
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Revisions; |
|
18
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\ShowDetailsRow; |
|
19
|
|
|
|
|
20
|
|
|
class CrudController extends BaseController |
|
21
|
|
|
{ |
|
22
|
|
|
use DispatchesJobs, ValidatesRequests; |
|
23
|
|
|
use AjaxTable, Reorder, Revisions, ShowDetailsRow; |
|
24
|
|
|
|
|
25
|
|
|
public $data = []; |
|
26
|
|
|
public $crud; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->crud = new CrudPanel(); |
|
31
|
|
|
|
|
32
|
|
|
// call the setup function inside this closure to also have the request there |
|
33
|
|
|
// this way, developers can use things stored in session (auth variables, etc) |
|
34
|
|
|
$this->middleware(function ($request, $next) { |
|
35
|
|
|
$this->setup(); |
|
36
|
|
|
return $next($request); |
|
37
|
|
|
}); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Allow developers to set their configuration options for a CrudPanel. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setup() {} |
|
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
} |
|
215
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.