1
|
|
|
<?php namespace Backpack\CRUD\app\Http\Controllers; |
2
|
|
|
|
3
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
4
|
|
|
use Illuminate\Routing\Controller as BaseController; |
5
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
6
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
7
|
|
|
use Illuminate\Support\Facades\Form as Form; |
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Backpack\CRUD\Crud; |
10
|
|
|
use Crypt; |
11
|
|
|
use Alert; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
// VALIDATION: change the requests to match your own file names if you need form validation |
15
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as StoreRequest; |
16
|
|
|
use Backpack\CRUD\app\Http\Requests\CrudRequest as UpdateRequest; |
17
|
|
|
|
18
|
|
|
class CrudController extends BaseController { |
19
|
|
|
|
20
|
|
|
use DispatchesJobs, ValidatesRequests; |
21
|
|
|
|
22
|
|
|
public $data = []; |
23
|
|
|
public $crud; |
24
|
|
|
|
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
$this->crud = new Crud(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Display all rows in the database for this entity. |
32
|
|
|
* |
33
|
|
|
* @return Response |
34
|
|
|
*/ |
35
|
|
|
public function index() |
36
|
|
|
{ |
37
|
|
|
$this->crud->hasAccessOrFail('list'); |
38
|
|
|
|
39
|
|
|
$this->data['entries'] = $this->crud->getEntries(); |
40
|
|
|
$this->data['crud'] = $this->crud; |
41
|
|
|
$this->data['title'] = ucfirst($this->crud->entity_name_plural); |
42
|
|
|
|
43
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
44
|
|
|
return view('crud::list', $this->data); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Show the form for creating inserting a new row. |
50
|
|
|
* |
51
|
|
|
* @return Response |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function create() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->crud->hasAccessOrFail('create'); |
56
|
|
|
|
57
|
|
|
// prepare the fields you need to show |
58
|
|
|
$this->data['crud'] = $this->crud; |
59
|
|
|
$this->data['fields'] = $this->crud->getCreateFields(); |
60
|
|
|
$this->data['title'] = trans('backpack::crud.add').' '.$this->crud->entity_name; |
61
|
|
|
|
62
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
63
|
|
|
return view('crud::create', $this->data); |
64
|
|
|
} |
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
|
|
|
* @return \Illuminate\Http\RedirectResponse |
72
|
|
|
*/ |
73
|
|
|
public function storeCrud(StoreRequest $request = null) |
74
|
|
|
{ |
75
|
|
|
$this->crud->hasAccessOrFail('create'); |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
// insert item in the db |
79
|
|
|
$item = $this->crud->create(\Request::except(['redirect_after_save', 'password'])); |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
// show a success message |
83
|
|
|
\Alert::success(trans('backpack::crud.insert_success'))->flash(); |
84
|
|
|
|
85
|
|
|
// redirect the user where he chose to be redirected |
86
|
|
|
switch (\Request::input('redirect_after_save')) { |
87
|
|
|
case 'current_item_edit': |
88
|
|
|
return \Redirect::to($this->crud->route.'/'.$item->id.'/edit'); |
89
|
|
|
|
90
|
|
|
default: |
91
|
|
|
return \Redirect::to(\Request::input('redirect_after_save')); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Show the form for editing the specified resource. |
98
|
|
|
* |
99
|
|
|
* @param int $id |
100
|
|
|
* @return Response |
101
|
|
|
*/ |
102
|
|
|
public function edit($id) |
103
|
|
|
{ |
104
|
|
|
$this->crud->hasAccessOrFail('update'); |
105
|
|
|
|
106
|
|
|
// get the info for that entry |
107
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
108
|
|
|
$this->data['crud'] = $this->crud; |
109
|
|
|
$this->data['fields'] = $this->crud->getUpdateFields($id); |
110
|
|
|
$this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name; |
111
|
|
|
|
112
|
|
|
$this->data['id'] = $id; |
113
|
|
|
|
114
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
115
|
|
|
return view('crud::edit', $this->data); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Update the specified resource in the database. |
121
|
|
|
* |
122
|
|
|
* @param UpdateRequest $request - type injection used for validation using Requests |
123
|
|
|
* @return \Illuminate\Http\RedirectResponse |
124
|
|
|
*/ |
125
|
|
|
public function updateCrud(UpdateRequest $request = null) |
126
|
|
|
{ |
127
|
|
|
$this->crud->hasAccessOrFail('update'); |
128
|
|
|
|
129
|
|
|
// update the row in the db |
130
|
|
|
|
131
|
|
|
$this->crud->update(\Request::get('id'), \Request::except('redirect_after_save')); |
132
|
|
|
|
133
|
|
|
// show a success message |
134
|
|
|
\Alert::success(trans('backpack::crud.update_success'))->flash(); |
135
|
|
|
|
136
|
|
|
return \Redirect::to($this->crud->route); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Display the specified resource. |
142
|
|
|
* |
143
|
|
|
* @param int $id |
144
|
|
|
* @return Response |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
$this->crud->hasAccessOrFail('show'); |
149
|
|
|
|
150
|
|
|
// get the info for that entry |
151
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
152
|
|
|
$this->data['crud'] = $this->crud; |
153
|
|
|
$this->data['title'] = trans('backpack::crud.preview').' '.$this->crud->entity_name; |
154
|
|
|
|
155
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
156
|
|
|
return view('crud::show', $this->data); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Remove the specified resource from storage. |
162
|
|
|
* |
163
|
|
|
* @param int $id |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
public function destroy($id) |
167
|
|
|
{ |
168
|
|
|
$this->crud->hasAccessOrFail('delete'); |
169
|
|
|
return $this->crud->delete($id); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Reorder the items in the database using the Nested Set pattern. |
175
|
|
|
* |
176
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
177
|
|
|
* |
178
|
|
|
* @return Response |
179
|
|
|
*/ |
180
|
|
|
public function reorder($lang = false) |
181
|
|
|
{ |
182
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
183
|
|
|
|
184
|
|
|
if (!$this->crud->isReorderEnabled()) { |
185
|
|
|
abort(403, 'Reorder is disabled.'); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if ($lang == false) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$lang = \Lang::locale(); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
// get all results for that entity |
194
|
|
|
$this->data['entries'] = $this->crud->getEntries(); |
195
|
|
|
$this->data['crud'] = $this->crud; |
196
|
|
|
$this->data['title'] = trans('backpack::crud.reorder').' '.$this->crud->entity_name; |
197
|
|
|
|
198
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
199
|
|
|
return view('crud::reorder', $this->data); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Save the new order, using the Nested Set pattern. |
205
|
|
|
* |
206
|
|
|
* Database columns needed: id, parent_id, lft, rgt, depth, name/title |
207
|
|
|
* |
208
|
|
|
* @return |
209
|
|
|
*/ |
210
|
|
|
public function saveReorder() |
211
|
|
|
{ |
212
|
|
|
$this->crud->hasAccessOrFail('reorder'); |
213
|
|
|
|
214
|
|
|
$all_entries = \Request::input('tree'); |
215
|
|
|
|
216
|
|
|
if (count($all_entries)) { |
217
|
|
|
$count = $this->crud->updateTreeOrder($all_entries); |
218
|
|
|
} else |
219
|
|
|
{ |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return 'success for '.$count." items"; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Used with AJAX in the list view (datatables) to show extra information about that row that didn't fit in the table. |
229
|
|
|
* It defaults to showing some dummy text. |
230
|
|
|
* |
231
|
|
|
* It's enabled by: |
232
|
|
|
* - setting: $crud->details_row = true; |
233
|
|
|
* - adding the details route for the entity; ex: Route::get('page/{id}/details', 'PageCrudController@showDetailsRow'); |
234
|
|
|
* - adding a view with the following name to change what the row actually contains: app/resources/views/vendor/backpack/crud/details_row.blade.php |
235
|
|
|
*/ |
236
|
|
|
public function showDetailsRow($id) |
237
|
|
|
{ |
238
|
|
|
$this->crud->hasAccessOrFail('details_row'); |
239
|
|
|
|
240
|
|
|
$this->data['entry'] = $this->crud->getEntry($id); |
241
|
|
|
$this->data['crud'] = $this->crud; |
242
|
|
|
|
243
|
|
|
// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package |
244
|
|
|
return view('crud::details_row', $this->data); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.