1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin\Models; |
4
|
|
|
|
5
|
|
|
use LaravelFlare\Flare\Events\ModelView; |
6
|
|
|
use LaravelFlare\Flare\Admin\AdminManager; |
7
|
|
|
use LaravelFlare\Flare\Http\Requests\ModelEditRequest; |
8
|
|
|
use LaravelFlare\Flare\Http\Requests\ModelCreateRequest; |
9
|
|
|
use LaravelFlare\Flare\Http\Controllers\FlareController; |
10
|
|
|
|
11
|
|
|
class ModelAdminController extends FlareController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* ModelAdmin instance which has been resolved. |
15
|
|
|
* |
16
|
|
|
* @var ModelAdmin |
17
|
|
|
*/ |
18
|
|
|
protected $modelAdmin; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Model instance. |
22
|
|
|
* |
23
|
|
|
* @var Model |
24
|
|
|
*/ |
25
|
|
|
protected $model; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* __construct. |
29
|
|
|
* |
30
|
|
|
* @param AdminManager $adminManager |
31
|
|
|
*/ |
32
|
|
|
public function __construct(AdminManager $adminManager) |
33
|
|
|
{ |
34
|
|
|
// Must call parent __construct otherwise |
35
|
|
|
// we need to redeclare checkpermissions |
36
|
|
|
// middleware for authentication check |
37
|
|
|
parent::__construct($adminManager); |
38
|
|
|
|
39
|
|
|
$this->middleware('checkmodelfound', ['only' => ['getView', 'edit', 'delete']]); |
40
|
|
|
|
41
|
|
|
$this->modelAdmin = $this->adminManager->getAdminInstance(); |
|
|
|
|
42
|
|
|
$this->model = $this->modelAdmin->model(); |
43
|
|
|
|
44
|
|
|
view()->share('modelAdmin', $this->modelAdmin); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Index page for ModelAdmin. |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Http\Response |
51
|
|
|
*/ |
52
|
|
|
public function getIndex() |
53
|
|
|
{ |
54
|
|
|
return view('flare::admin.modeladmin.index', [ |
55
|
|
|
'modelItems' => $this->modelAdmin->items(), |
56
|
|
|
'totals' => $this->modelAdmin->totals(), |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Lists Trashed Model Items. |
63
|
|
|
* |
64
|
|
|
* @return \Illuminate\Http\Response |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function getTrashed() |
67
|
|
|
{ |
68
|
|
|
if (!$this->modelAdmin->hasSoftDeleting()) { |
69
|
|
|
return $this->missingMethod(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return view('flare::admin.modeladmin.trashed', [ |
|
|
|
|
73
|
|
|
'modelItems' => $this->modelAdmin->onlyTrashedItems(), |
74
|
|
|
'totals' => $this->modelAdmin->totals(), |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* List All Model Items inc Trashed. |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Http\Response |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function getAll() |
85
|
|
|
{ |
86
|
|
|
if (!$this->modelAdmin->hasSoftDeleting()) { |
87
|
|
|
return $this->missingMethod(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return view('flare::admin.modeladmin.all', [ |
|
|
|
|
91
|
|
|
'modelItems' => $this->modelAdmin->allItems(), |
92
|
|
|
'totals' => $this->modelAdmin->totals(), |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create a new Model Entry from ModelAdmin Create Page. |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Http\Response |
101
|
|
|
*/ |
102
|
|
|
public function getCreate() |
103
|
|
|
{ |
104
|
|
|
if (!$this->modelAdmin->hasCreating()) { |
105
|
|
|
return $this->missingMethod(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return view('flare::admin.modeladmin.create', []); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Receive new Model Entry Post Data, validate it and return user. |
113
|
|
|
* |
114
|
|
|
* @return \Illuminate\Http\RedirectResponse |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function postCreate(ModelCreateRequest $request) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
if (!$this->modelAdmin->hasCreating()) { |
119
|
|
|
return $this->missingMethod(); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$this->modelAdmin->create(); |
123
|
|
|
|
124
|
|
|
return redirect($this->modelAdmin->currentUrl())->with('notifications_below_header', [['type' => 'success', 'icon' => 'check-circle', 'title' => 'Success!', 'message' => 'The '.$this->modelAdmin->getTitle().' was successfully created.', 'dismissable' => false]]); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* View a Model Entry from ModelAdmin View Page. |
129
|
|
|
* |
130
|
|
|
* @return \Illuminate\Http\Response |
131
|
|
|
*/ |
132
|
|
|
public function getView($modelitemId) |
133
|
|
|
{ |
134
|
|
|
if (!$this->modelAdmin->hasViewing()) { |
|
|
|
|
135
|
|
|
return $this->missingMethod(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->modelAdmin->find($modelitemId); |
139
|
|
|
|
140
|
|
|
event(new ModelView($this->modelAdmin)); |
141
|
|
|
|
142
|
|
|
return view('flare::admin.modeladmin.view', ['modelItem' => $this->modelAdmin->model]); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Edit Model Entry from ModelAdmin Edit Page. |
147
|
|
|
* |
148
|
|
|
* @param int $modelitemId |
149
|
|
|
* |
150
|
|
|
* @return \Illuminate\Http\Response |
151
|
|
|
*/ |
152
|
|
|
public function getEdit($modelitemId) |
153
|
|
|
{ |
154
|
|
|
if (!$this->modelAdmin->hasEditing()) { |
155
|
|
|
return $this->missingMethod(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$this->modelAdmin->find($modelitemId); |
159
|
|
|
|
160
|
|
|
return view('flare::admin.modeladmin.edit', ['modelItem' => $this->modelAdmin->model]); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Receive Model Entry Update Post Data, validate it and return user. |
165
|
|
|
* |
166
|
|
|
* @param int $modelitemId |
167
|
|
|
* |
168
|
|
|
* @return \Illuminate\Http\RedirectResponse |
169
|
|
|
*/ |
170
|
|
View Code Duplication |
public function postEdit(ModelEditRequest $request, $modelitemId) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
if (!$this->modelAdmin->hasEditing()) { |
173
|
|
|
return $this->missingMethod(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->modelAdmin->edit($modelitemId); |
177
|
|
|
|
178
|
|
|
return redirect($this->modelAdmin->currentUrl())->with('notifications_below_header', [['type' => 'success', 'icon' => 'check-circle', 'title' => 'Success!', 'message' => 'The '.$this->modelAdmin->getTitle().' was successfully updated.', 'dismissable' => false]]); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Delete Model Entry from ModelAdmin Delete Page. |
183
|
|
|
* |
184
|
|
|
* @param int $modelitemId |
185
|
|
|
* |
186
|
|
|
* @return \Illuminate\Http\Response |
187
|
|
|
*/ |
188
|
|
|
public function getDelete($modelitemId) |
189
|
|
|
{ |
190
|
|
|
if (!$this->modelAdmin->hasDeleting()) { |
191
|
|
|
return $this->missingMethod(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($this->modelAdmin->hasSoftDeleting()) { |
195
|
|
|
$this->modelAdmin->findWithTrashed($modelitemId); |
|
|
|
|
196
|
|
|
} else { |
197
|
|
|
$this->modelAdmin->find($modelitemId); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return view('flare::admin.modeladmin.delete', ['modelItem' => $this->modelAdmin->model]); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Receive Model Entry Delete Post Data, validate it and return user. |
205
|
|
|
* |
206
|
|
|
* @param int $modelitemId |
207
|
|
|
* |
208
|
|
|
* @return \Illuminate\Http\RedirectResponse |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
public function postDelete($modelitemId) |
211
|
|
|
{ |
212
|
|
|
if (!$this->modelAdmin->hasDeleting()) { |
213
|
|
|
return $this->missingMethod(); |
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$this->modelAdmin->delete($modelitemId); |
217
|
|
|
|
218
|
|
|
return redirect($this->modelAdmin->currentUrl())->with('notifications_below_header', [['type' => 'success', 'icon' => 'check-circle', 'title' => 'Success!', 'message' => 'The '.$this->modelAdmin->getTitle().' was successfully removed.', 'dismissable' => false]]); |
|
|
|
|
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Restore a ModelItem. |
223
|
|
|
* |
224
|
|
|
* @param int $modelitemId |
225
|
|
|
* |
226
|
|
|
* @return \Illuminate\Http\Response |
227
|
|
|
*/ |
228
|
|
|
public function getRestore($modelitemId) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
if (!$this->modelAdmin->hasSoftDeleting()) { |
231
|
|
|
return $this->missingMethod(); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return view('flare::admin.modeladmin.restore', ['modelItem' => $this->modelAdmin->model]); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Process Restore ModelItem Request. |
239
|
|
|
* |
240
|
|
|
* @param int $page_id |
|
|
|
|
241
|
|
|
* |
242
|
|
|
* @return \Illuminate\Http\RedirectResponse |
243
|
|
|
*/ |
244
|
|
View Code Duplication |
public function postRestore($modelitemId) |
245
|
|
|
{ |
246
|
|
|
if (!$this->modelAdmin->hasSoftDeleting()) { |
247
|
|
|
return $this->missingMethod(); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
$this->modelAdmin->restore($modelitemId); |
|
|
|
|
251
|
|
|
|
252
|
|
|
return redirect($this->modelAdmin->currentUrl())->with('notifications_below_header', [['type' => 'success', 'icon' => 'check-circle', 'title' => 'Success!', 'message' => 'The '.$this->modelAdmin->getTitle().' was successfully restored.', 'dismissable' => false]]); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Clone a Page. |
257
|
|
|
* |
258
|
|
|
* @param int $modelitemId |
259
|
|
|
* |
260
|
|
|
* @return \Illuminate\Http\Response |
261
|
|
|
*/ |
262
|
|
View Code Duplication |
public function getClone($modelitemId) |
263
|
|
|
{ |
264
|
|
|
if (!$this->modelAdmin->hasCloning()) { |
265
|
|
|
return $this->missingMethod(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$this->modelAdmin->clone($modelitemId); |
|
|
|
|
269
|
|
|
|
270
|
|
|
return redirect($this->modelAdmin->currentUrl())->with('notifications_below_header', [['type' => 'success', 'icon' => 'check-circle', 'title' => 'Success!', 'message' => 'The '.$this->modelAdmin->getTitle().' was successfully cloned.', 'dismissable' => false]]); |
|
|
|
|
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.