1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RafflesArgentina\ResourceController; |
4
|
|
|
|
5
|
|
|
use Lang; |
6
|
|
|
use Validator; |
7
|
|
|
|
8
|
|
|
use Illuminate\Http\Request; |
9
|
|
|
use Illuminate\Routing\Controller as BaseController; |
10
|
|
|
use Illuminate\Support\MessageBag; |
11
|
|
|
use Illuminate\Support\Facades\View; |
12
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
13
|
|
|
|
14
|
|
|
use RafflesArgentina\ResourceController\Contracts\ResourceControllerInterface; |
15
|
|
|
use RafflesArgentina\ResourceController\Exceptions\ResourceControllerException; |
16
|
|
|
|
17
|
|
|
abstract class AbstractResourceController extends BaseController |
18
|
|
|
implements ResourceControllerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The alias for named routes. |
22
|
|
|
* |
23
|
|
|
* @var string|null |
24
|
|
|
*/ |
25
|
|
|
protected $alias; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The location for themed views. |
29
|
|
|
* |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
protected $theme; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The vendor views prefix. |
36
|
|
|
* |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
protected $module; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The prefix for named routes. |
43
|
|
|
* |
44
|
|
|
* @var string|null |
45
|
|
|
*/ |
46
|
|
|
protected $prefix; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The Repository class to instantiate. |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $repository; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The FormRequest class to instantiate. |
57
|
|
|
* |
58
|
|
|
* @var mixed|null |
59
|
|
|
*/ |
60
|
|
|
protected $formRequest; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The name of the resource. |
64
|
|
|
* |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $resourceName; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Define if model uses SoftDeletes. |
71
|
|
|
* |
72
|
|
|
* @var boolean |
73
|
|
|
*/ |
74
|
|
|
protected $useSoftDeletes; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The info flash message key. |
78
|
|
|
* |
79
|
|
|
* @var string|null |
80
|
|
|
*/ |
81
|
|
|
protected $infoFlashMessageKey = 'rafflesargentina.status.info'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The error flash message key. |
85
|
|
|
* |
86
|
|
|
* @var string|null |
87
|
|
|
*/ |
88
|
|
|
protected $errorFlashMessageKey = 'rafflesargentina.status.error'; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The success flash message key. |
92
|
|
|
* |
93
|
|
|
* @var string|null |
94
|
|
|
*/ |
95
|
|
|
protected $successFlashMessageKey = 'rafflesargentina.status.success'; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* The warning flash message key. |
99
|
|
|
* |
100
|
|
|
* @var string|null |
101
|
|
|
*/ |
102
|
|
|
protected $warningFlashMessageKey = 'rafflesargentina.status.warning'; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create a new AbstractResourceController instance. |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function __construct() |
110
|
|
|
{ |
111
|
|
|
$this->_checkRepositoryProperty(); |
112
|
|
|
$this->_checkResourceNameProperty(); |
113
|
|
|
$this->_formatRouteNameAndViewPathModifiers(); |
114
|
|
|
|
115
|
|
|
$this->repository = app()->make($this->repository); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Display a listing of the resource. |
120
|
|
|
* |
121
|
|
|
* @param Request $request The request object. |
122
|
|
|
* |
123
|
|
|
* @return mixed |
124
|
|
|
*/ |
125
|
|
|
public abstract function index(Request $request); |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Show the form for creating a new resource. |
129
|
|
|
* |
130
|
|
|
* @param Request $request The request object. |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public abstract function create(Request $request); |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Store a newly created resource in storage. |
138
|
|
|
* |
139
|
|
|
* @param Request $request The request object. |
140
|
|
|
* |
141
|
|
|
* @throws ResourceControllerException |
142
|
|
|
* |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
|
|
public abstract function store(Request $request); |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Display the specified resource. |
149
|
|
|
* |
150
|
|
|
* @param Request $request The request object. |
151
|
|
|
* @param string $key The model key. |
152
|
|
|
* |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
public abstract function show(Request $request, $key); |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Show the form for editing the specified resource. |
159
|
|
|
* |
160
|
|
|
* @param Request $request The request object. |
161
|
|
|
* @param string $key The model key. |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
|
|
public abstract function edit(Request $request, $key); |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Update the specified resource in storage. |
169
|
|
|
* |
170
|
|
|
* @param Request $request The request object. |
171
|
|
|
* @param string $key The model key. |
172
|
|
|
* |
173
|
|
|
* @throws ResourceControllerException |
174
|
|
|
* |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
public abstract function update(Request $request, $key); |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Remove the specified resource from storage. |
181
|
|
|
* |
182
|
|
|
* @param Request $request The request object. |
183
|
|
|
* @param string $key The model key. |
184
|
|
|
* |
185
|
|
|
* @throws ResourceControllerException |
186
|
|
|
* |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
|
|
public abstract function destroy(Request $request, $key); |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get named route for the specified action. |
193
|
|
|
* |
194
|
|
|
* @param string $action The action. |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
public function getRouteName($action) |
199
|
|
|
{ |
200
|
|
|
return $this->alias.$this->resourceName.$action; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Validate rules from a FormRequest instance. |
205
|
|
|
* |
206
|
|
|
* @return \Illuminate\Validation\Validator |
207
|
|
|
*/ |
208
|
|
|
public function validateRules() |
209
|
|
|
{ |
210
|
|
|
$input = request()->all(); |
211
|
|
|
$rules = []; |
212
|
|
|
$messages = []; |
213
|
|
|
|
214
|
|
|
if ($this->formRequest) { |
215
|
|
|
$this->formRequest = new $this->formRequest; |
216
|
|
|
$rules = $this->formRequest->rules(); |
217
|
|
|
$messages = $this->formRequest->messages(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return Validator::make($input, $rules, $messages); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Throw an exception if the view doesn't exist. |
225
|
|
|
* |
226
|
|
|
* @param string $view The view. |
227
|
|
|
* |
228
|
|
|
* @throws ResourceControllerException |
229
|
|
|
* |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
public function checkViewExists($view) |
233
|
|
|
{ |
234
|
|
|
if (!View::exists($view)) { |
235
|
|
|
if (Lang::has('resource-controller.viewnotfound')) { |
236
|
|
|
$message = trans('resource-controller.viewnotfound', ['view' => '$view']); |
237
|
|
|
} else { |
238
|
|
|
$message = 'Requested page couldn\'t be loaded because the view file is missing: '.$view; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
throw new ResourceControllerException($message); |
|
|
|
|
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get view location for the specified action. |
247
|
|
|
* |
248
|
|
|
* @param string $action The action. |
249
|
|
|
* |
250
|
|
|
* @return string |
251
|
|
|
*/ |
252
|
|
|
public function getViewLocation($action) |
253
|
|
|
{ |
254
|
|
|
if (request()->ajax()) { |
255
|
|
|
return $this->module.$this->theme.$this->resourceName.'ajax.'.$action; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $this->module.$this->theme.$this->resourceName.$action; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Get redirection route. |
263
|
|
|
* |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public function getRedirectionRoute() |
267
|
|
|
{ |
268
|
|
|
return $this->getRouteName('index'); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get the FormRequest instance. |
273
|
|
|
* |
274
|
|
|
* @return mixed |
275
|
|
|
*/ |
276
|
|
|
public function getFormRequestInstance() |
277
|
|
|
{ |
278
|
|
|
if (!$this->formRequest) { |
279
|
|
|
return new FormRequest; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return app()->make($this->formRequest); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Redirect back with errors. |
287
|
|
|
* |
288
|
|
|
* @param \Illuminate\Validation\Validator $validator The validator instance. |
289
|
|
|
* |
290
|
|
|
* @return \Illuminate\Http\RedirectResponse |
291
|
|
|
*/ |
292
|
|
|
public function redirectBackWithErrors($validator) |
293
|
|
|
{ |
294
|
|
|
if (request()->wantsJson()) { |
295
|
|
|
return $this->validUnprocessableEntityJsonResponse($validator->errors()); |
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return back()->withErrors($validator)->withInput(); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Return a valid 200 Success json response. |
303
|
|
|
* |
304
|
|
|
* @param string $message The response message. |
305
|
|
|
* |
306
|
|
|
* @return \Illuminate\Http\JsonResponse |
307
|
|
|
*/ |
308
|
|
|
public function validSuccessJsonResponse($message = 'Success') |
309
|
|
|
{ |
310
|
|
|
return response()->json( |
311
|
|
|
[ |
312
|
|
|
'code' => '200', |
313
|
|
|
'message' => $message, |
314
|
|
|
'errors' => [], |
315
|
|
|
'redirect' => route($this->getRedirectionRoute()), |
316
|
|
|
], 200, [], JSON_PRETTY_PRINT |
317
|
|
|
); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Return a valid 404 Not found json response. |
322
|
|
|
* |
323
|
|
|
* @param string $message The response message. |
324
|
|
|
* |
325
|
|
|
* @return \Illuminate\Http\JsonResponse |
326
|
|
|
*/ |
327
|
|
|
public function validNotFoundJsonResponse($message = 'Not found') |
328
|
|
|
{ |
329
|
|
|
return response()->json( |
330
|
|
|
[ |
331
|
|
|
'code' => '404', |
332
|
|
|
'message' => $message, |
333
|
|
|
'errors' => [], |
334
|
|
|
'redirect' => route($this->getRedirectionRoute()), |
335
|
|
|
], 404, [], JSON_PRETTY_PRINT |
336
|
|
|
); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Return a valid 422 Unprocessable entity json response. |
341
|
|
|
* |
342
|
|
|
* @param \Illuminate\Support\MessageBag $errors The message bag errors. |
343
|
|
|
* @param string $message The response message. |
344
|
|
|
* |
345
|
|
|
* @return \Illuminate\Http\JsonResponse |
346
|
|
|
*/ |
347
|
|
|
public function validUnprocessableEntityJsonResponse(MessageBag $errors, $message = 'Unprocessable Entity') |
348
|
|
|
{ |
349
|
|
|
return response()->json( |
350
|
|
|
[ |
351
|
|
|
'code' => '422', |
352
|
|
|
'message' => $message, |
353
|
|
|
'errors' => $errors, |
354
|
|
|
'redirect' => route($this->getRedirectionRoute()), |
355
|
|
|
], 422, [], JSON_PRETTY_PRINT |
356
|
|
|
); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Format route name and view path modifiers. |
361
|
|
|
* |
362
|
|
|
* @return void |
363
|
|
|
*/ |
364
|
|
|
private function _formatRouteNameAndViewPathModifiers() |
365
|
|
|
{ |
366
|
|
|
if ($this->alias) { |
367
|
|
|
$this->alias = str_finish($this->alias, '.'); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
if ($this->theme) { |
371
|
|
|
$this->theme = str_finish($this->theme, '.'); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
if ($this->module) { |
375
|
|
|
if (!ends_with($this->module, '::')) { |
376
|
|
|
$this->module .= '::'; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
if ($this->prefix) { |
381
|
|
|
$this->prefix = str_finish($this->prefix, '.'); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
if ($this->resourceName) { |
385
|
|
|
$this->resourceName = str_finish($this->resourceName, '.'); |
386
|
|
|
} |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Throw an exception if repository property is not set. |
391
|
|
|
* |
392
|
|
|
* @throws ResourceControllerException |
393
|
|
|
* |
394
|
|
|
* @return void |
395
|
|
|
*/ |
396
|
|
|
private function _checkRepositoryProperty() |
397
|
|
|
{ |
398
|
|
|
if (!$this->repository) { |
399
|
|
|
if (Lang::has('resource-controller.propertynotset')) { |
400
|
|
|
$message = trans('resource-controller.propertynotset', ['property' => '$repository']); |
401
|
|
|
} else { |
402
|
|
|
$message = '$repository property must be set.'; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
throw new ResourceControllerException($message); |
|
|
|
|
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Throw an exception if resourceName property is not set. |
411
|
|
|
* |
412
|
|
|
* @throws ResourceControllerException |
413
|
|
|
* |
414
|
|
|
* @return void |
415
|
|
|
*/ |
416
|
|
|
private function _checkResourceNameProperty() |
417
|
|
|
{ |
418
|
|
|
if (!$this->resourceName) { |
419
|
|
|
if (Lang::has('resource-controller.propertynotset')) { |
420
|
|
|
$message = trans('resource-controller.propertynotset', ['property' => '$resourceName']); |
421
|
|
|
} else { |
422
|
|
|
$message = '$resourceName property must be set.'; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
throw new ResourceControllerException($message); |
|
|
|
|
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|