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