1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\CrudPanel; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Facades\Form as Form; |
8
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
9
|
|
|
use Illuminate\Routing\Controller as BaseController; |
10
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
11
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Reorder; |
12
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\AjaxTable; |
13
|
|
|
// CRUD Traits for non-core features |
14
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\Revisions; |
15
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions; |
16
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest; |
17
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as UpdateRequest; |
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, SaveActions; |
24
|
|
|
|
25
|
|
|
public $data = []; |
26
|
|
|
public $crud; |
27
|
|
|
public $request; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
if (! $this->crud) { |
32
|
|
|
$this->crud = app()->make(CrudPanel::class); |
33
|
|
|
|
34
|
|
|
// call the setup function inside this closure to also have the request there |
35
|
|
|
// this way, developers can use things stored in session (auth variables, etc) |
36
|
|
|
$this->middleware(function ($request, $next) { |
37
|
|
|
$this->request = $request; |
38
|
|
|
$this->crud->request = $request; |
39
|
|
|
$this->setup(); |
40
|
|
|
|
41
|
|
|
return $next($request); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Allow developers to set their configuration options for a CrudPanel. |
48
|
|
|
*/ |
49
|
|
|
public function setup() |
50
|
|
|
{ |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Display all rows in the database for this entity. |
55
|
|
|
* |
56
|
|
|
* @return Response |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function index() |
59
|
|
|
{ |
60
|
|
|
$this->crud->hasAccessOrFail('list'); |
61
|
|
|
|
62
|
|
|
$this->data['crud'] = $this->crud; |
63
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name_plural); |
64
|
|
|
|
65
|
|
|
// get all entries if AJAX is not enabled |
66
|
|
|
if (! $this->data['crud']->ajaxTable()) { |
67
|
|
|
$this->data['entries'] = $this->data['crud']->getEntries(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
71
|
|
|
return view($this->crud->getListView(), $this->data); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Show the form for creating inserting a new row. |
76
|
|
|
* |
77
|
|
|
* @return Response |
|
|
|
|
78
|
|
|
*/ |
79
|
|
|
public function create() |
80
|
|
|
{ |
81
|
|
|
$this->crud->hasAccessOrFail('create'); |
82
|
|
|
|
83
|
|
|
// prepare the fields you need to show |
84
|
|
|
$this->data['crud'] = $this->crud; |
85
|
|
|
$this->data['saveAction'] = $this->getSaveAction(); |
86
|
|
|
$this->data['fields'] = $this->crud->getCreateFields(); |
87
|
|
|
$this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
88
|
|
|
|
89
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
90
|
|
|
return view($this->crud->getCreateView(), $this->data); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Store a newly created resource in the database. |
95
|
|
|
* |
96
|
|
|
* @param StoreRequest $request - type injection used for validation using Requests |
|
|
|
|
97
|
|
|
* |
98
|
|
|
* @return \Illuminate\Http\RedirectResponse |
99
|
|
|
*/ |
100
|
|
|
public function storeCrud(StoreRequest $request = null) |
101
|
|
|
{ |
102
|
|
|
$this->crud->hasAccessOrFail('create'); |
103
|
|
|
|
104
|
|
|
// fallback to global request instance |
105
|
|
|
if (is_null($request)) { |
106
|
|
|
$request = \Request::instance(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
110
|
|
View Code Duplication |
foreach ($request->input() as $key => $value) { |
|
|
|
|
111
|
|
|
if (empty($value) && $value !== '0') { |
112
|
|
|
$request->request->set($key, null); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
// insert item in the db |
117
|
|
|
$item = $this->crud->create($request->except(['save_action', '_token', '_method'])); |
118
|
|
|
$this->data['entry'] = $this->crud->entry = $item; |
119
|
|
|
|
120
|
|
|
// show a success message |
121
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
122
|
|
|
|
123
|
|
|
// save the redirect choice for next time |
124
|
|
|
$this->setSaveAction(); |
125
|
|
|
|
126
|
|
|
return $this->performSaveAction($item->getKey()); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Show the form for editing the specified resource. |
131
|
|
|
* |
132
|
|
|
* @param int $id |
133
|
|
|
* |
134
|
|
|
* @return Response |
|
|
|
|
135
|
|
|
*/ |
136
|
|
|
public function edit($id) |
137
|
|
|
{ |
138
|
|
|
$this->crud->hasAccessOrFail('update'); |
139
|
|
|
|
140
|
|
|
// get the info for that entry |
141
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
142
|
|
|
$this->data['crud'] = $this->crud; |
143
|
|
|
$this->data['saveAction'] = $this->getSaveAction(); |
144
|
|
|
$this->data['fields'] = $this->crud->getUpdateFields($id); |
145
|
|
|
$this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name; |
146
|
|
|
|
147
|
|
|
$this->data['id'] = $id; |
148
|
|
|
|
149
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
150
|
|
|
return view($this->crud->getEditView(), $this->data); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Update the specified resource in the database. |
155
|
|
|
* |
156
|
|
|
* @param UpdateRequest $request - type injection used for validation using Requests |
|
|
|
|
157
|
|
|
* |
158
|
|
|
* @return \Illuminate\Http\RedirectResponse |
159
|
|
|
*/ |
160
|
|
|
public function updateCrud(UpdateRequest $request = null) |
161
|
|
|
{ |
162
|
|
|
$this->crud->hasAccessOrFail('update'); |
163
|
|
|
|
164
|
|
|
// fallback to global request instance |
165
|
|
|
if (is_null($request)) { |
166
|
|
|
$request = \Request::instance(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
// replace empty values with NULL, so that it will work with MySQL strict mode on |
170
|
|
View Code Duplication |
foreach ($request->input() as $key => $value) { |
|
|
|
|
171
|
|
|
if (empty($value) && $value !== '0') { |
172
|
|
|
$request->request->set($key, null); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// update the row in the db |
177
|
|
|
$item = $this->crud->update($request->get($this->crud->model->getKeyName()), |
178
|
|
|
$request->except('save_action', '_token', '_method')); |
179
|
|
|
$this->data['entry'] = $this->crud->entry = $item; |
180
|
|
|
|
181
|
|
|
// show a success message |
182
|
|
|
\Alert::success(trans('backpack::crud.update_success'))->flash(); |
183
|
|
|
|
184
|
|
|
// save the redirect choice for next time |
185
|
|
|
$this->setSaveAction(); |
186
|
|
|
|
187
|
|
|
return $this->performSaveAction($item->getKey()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Display the specified resource. |
192
|
|
|
* |
193
|
|
|
* @param int $id |
194
|
|
|
* |
195
|
|
|
* @return Response |
|
|
|
|
196
|
|
|
*/ |
197
|
|
|
public function show($id) |
198
|
|
|
{ |
199
|
|
|
$this->crud->hasAccessOrFail('show'); |
200
|
|
|
|
201
|
|
|
// set columns from db |
202
|
|
|
$this->crud->setFromDb(); |
203
|
|
|
|
204
|
|
|
// cycle through columns |
205
|
|
|
foreach ($this->crud->columns as $key => $column) { |
206
|
|
|
// remove any autoset relationship columns |
207
|
|
|
if (array_key_exists('model', $column) && array_key_exists('autoset', $column) && $column['autoset']) { |
208
|
|
|
$this->crud->removeColumn($column['name']); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// get the info for that entry |
213
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
214
|
|
|
$this->data['crud'] = $this->crud; |
215
|
|
|
$this->data['title'] = trans('backpack::crud.preview').' '.$this->crud->entity_name; |
216
|
|
|
|
217
|
|
|
// remove preview button from stack:line |
218
|
|
|
$this->crud->removeButton('preview'); |
219
|
|
|
$this->crud->removeButton('delete'); |
220
|
|
|
|
221
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
222
|
|
|
return view($this->crud->getShowView(), $this->data); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Remove the specified resource from storage. |
227
|
|
|
* |
228
|
|
|
* @param int $id |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function destroy($id) |
233
|
|
|
{ |
234
|
|
|
$this->crud->hasAccessOrFail('delete'); |
235
|
|
|
|
236
|
|
|
return $this->crud->delete($id); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
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.