1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFlare\Flare\Admin; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
|
8
|
|
|
abstract class Admin |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Admin Section Icon. |
12
|
|
|
* |
13
|
|
|
* Font Awesome Defined Icon, eg 'user' = 'fa-user' |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $icon; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Title of Admin Section. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $title; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Plural Title of Admin Section. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $pluralTitle; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* URL Prefix of Admin Section. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $urlPrefix; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The Controller to be used by the Admin. |
42
|
|
|
* |
43
|
|
|
* This defaults to parent::getController() |
44
|
|
|
* if it has been left undefined. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $controller = \LaravelFlare\Flare\Http\Controllers\AdminController::class; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The Policy used for the Admin Authorization logic. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $policy = '\LaravelFlare\Flare\Permissions\AdminPolicy'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* An array of subclasses of Admin |
59
|
|
|
* which allows hierachy in a Module. |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $subAdmin = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The Admin Default View. |
67
|
|
|
* |
68
|
|
|
* By Default this is the 404 page |
69
|
|
|
* |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
protected $view = 'admin.404'; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Array of View Data to Render. |
76
|
|
|
* |
77
|
|
|
* @var array |
78
|
|
|
*/ |
79
|
|
|
protected $viewData = []; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Class Suffix used for matching and removing term |
83
|
|
|
* from user provided Admin sections. |
84
|
|
|
* |
85
|
|
|
* @var string |
86
|
|
|
*/ |
87
|
|
|
const CLASS_SUFFIX = 'Admin'; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* __construct. |
91
|
|
|
*/ |
92
|
|
|
public function __construct() |
93
|
|
|
{ |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Register the routes for this Admin Section. |
98
|
|
|
* |
99
|
|
|
* Default routes include, create:, read:, update:, delete: |
100
|
|
|
* |
101
|
|
|
* Also attempts to load in ModelAdminController |
102
|
|
|
* based on the shortName of the class, for |
103
|
|
|
* overloading and adding additional routes |
104
|
|
|
* |
105
|
|
|
* @param \Illuminate\Routing\Router $router |
106
|
|
|
*/ |
107
|
|
|
public function registerRoutes(Router $router) |
108
|
|
|
{ |
109
|
|
|
// We will need to throw an exception if a ModelAdmin manages a Model which conflicts with an internal flare endpoint |
110
|
|
|
// such as (create, edit, view, delete etc) |
111
|
|
|
$router->group(['prefix' => $this->urlPrefix(), 'namespace' => get_called_class(), 'as' => $this->urlPrefix()], function ($router) { |
112
|
|
|
$this->registerSubRoutes(); |
113
|
|
|
$router->controller('/', $this->getController()); |
114
|
|
|
}); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Register subRoutes for Defined Admin instances. |
119
|
|
|
* |
120
|
|
|
* @return |
121
|
|
|
*/ |
122
|
|
|
public function registerSubRoutes() |
123
|
|
|
{ |
124
|
|
|
if (!is_array($this->subAdmin)) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
foreach ($this->subAdmin as $adminItem) { |
129
|
|
|
$this->registerRoute($adminItem->getController(), $adminItem->routeParameters()); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Register an individual route. |
135
|
|
|
* |
136
|
|
|
* @param string $controller |
137
|
|
|
* @param array $parameters |
138
|
|
|
* |
139
|
|
|
* @return |
140
|
|
|
*/ |
141
|
|
|
public static function registerRoute($controller, $parameters = []) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
\Route::group($parameters, function ($controller) { |
144
|
|
|
\Route::controller('/', $controller); |
|
|
|
|
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns the Route Paramets. |
150
|
|
|
* |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
public function routeParameters() |
154
|
|
|
{ |
155
|
|
|
return [ |
156
|
|
|
'prefix' => $this->urlPrefix(), |
157
|
|
|
'as' => $this->urlPrefix(), |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the Requested Route Action as a |
163
|
|
|
* string, namespace is returned by default. |
164
|
|
|
* |
165
|
|
|
* @param string $key |
166
|
|
|
* |
167
|
|
|
* @return string|void |
168
|
|
|
*/ |
169
|
|
|
public static function getRequested($key = 'namespace') |
170
|
|
|
{ |
171
|
|
|
if (!\Route::current()) { |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$currentAction = \Route::current()->getAction(); |
176
|
|
|
|
177
|
|
|
if (isset($currentAction[$key])) { |
178
|
|
|
return $currentAction[$key]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Returns the Controller Class for the current Admin section. |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getController() |
190
|
|
|
{ |
191
|
|
|
return $this->controller; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Set the Controller Class for the current Admin section. |
196
|
|
|
* |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function setController($controller = null) |
200
|
|
|
{ |
201
|
|
|
$this->controller = $controller; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns the Module Admin View. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getView() |
210
|
|
|
{ |
211
|
|
|
if (view()->exists($this->view)) { |
|
|
|
|
212
|
|
|
return $this->view; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return 'flare::'.$this->view; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Set the Module Admin View. |
220
|
|
|
* |
221
|
|
|
* @param string $view |
222
|
|
|
*/ |
223
|
|
|
public function setView($view = null) |
224
|
|
|
{ |
225
|
|
|
$this->view = $view; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns the View Data. |
230
|
|
|
* |
231
|
|
|
* @return array |
232
|
|
|
*/ |
233
|
|
|
public function getViewData() |
234
|
|
|
{ |
235
|
|
|
return $this->viewData; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Set the View Data. |
240
|
|
|
* |
241
|
|
|
* @param array $viewData |
242
|
|
|
*/ |
243
|
|
|
public function setViewData($viewData = []) |
244
|
|
|
{ |
245
|
|
|
$this->viewData = $viewData; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Menu Items. |
250
|
|
|
* |
251
|
|
|
* @return array |
252
|
|
|
*/ |
253
|
|
|
public function menuItems() |
254
|
|
|
{ |
255
|
|
|
return []; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Icon of a Admin Section Class. |
260
|
|
|
* |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
|
|
public function getIcon() |
264
|
|
|
{ |
265
|
|
|
return $this->icon; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Set Icon of a Admin Section Class. |
270
|
|
|
* |
271
|
|
|
* @param string $icon |
272
|
|
|
*/ |
273
|
|
|
public function setIcon($icon = null) |
274
|
|
|
{ |
275
|
|
|
$this->icon = $icon; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Shortname of a Admin Section Class. |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public static function shortName() |
284
|
|
|
{ |
285
|
|
|
return (new \ReflectionClass(new static()))->getShortName(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Title of a Admin Section Class. |
290
|
|
|
* |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
public function getTitle() |
294
|
|
|
{ |
295
|
|
|
if (!isset($this->title) || !$this->title) { |
296
|
|
|
return Str::title(str_replace('_', ' ', snake_case(str_replace(static::CLASS_SUFFIX, '', static::shortName())))); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $this->title; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Set Title of a Admin Section Class. |
304
|
|
|
* |
305
|
|
|
* @param string $title |
306
|
|
|
*/ |
307
|
|
|
public function setTitle($title = null) |
308
|
|
|
{ |
309
|
|
|
$this->title = $title; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Plural of the Admin Section Class Title. |
314
|
|
|
* |
315
|
|
|
* @return string |
316
|
|
|
*/ |
317
|
|
|
public function getPluralTitle() |
318
|
|
|
{ |
319
|
|
|
if (!isset($this->getPluralTitle) || !$this->getPluralTitle) { |
|
|
|
|
320
|
|
|
return Str::plural($this->getTitle()); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
return $this->getPluralTitle; |
|
|
|
|
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Set Plural Title. |
328
|
|
|
* |
329
|
|
|
* @param string $pluralTitle |
330
|
|
|
*/ |
331
|
|
|
public function setPluralTitle($pluralTitle = null) |
332
|
|
|
{ |
333
|
|
|
$this->getPluralTitle = $pluralTitle; |
|
|
|
|
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* URL Prefix to a Admin Section Top Level Page. |
338
|
|
|
* |
339
|
|
|
* @return string |
340
|
|
|
*/ |
341
|
|
|
public function urlPrefix() |
342
|
|
|
{ |
343
|
|
|
if (!isset($this->urlPrefix) || !$this->urlPrefix) { |
344
|
|
|
return str_slug($this->getPluralTitle()); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
return $this->urlPrefix; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* URL to a Admin Top Level Page. |
352
|
|
|
* |
353
|
|
|
* @param string $path |
354
|
|
|
* |
355
|
|
|
* @return string |
356
|
|
|
*/ |
357
|
|
|
public function url($path = '') |
358
|
|
|
{ |
359
|
|
|
return url($this->relativeUrl($path)); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Relative URL to an Admin Top Level Page. |
364
|
|
|
* |
365
|
|
|
* @param string $path |
366
|
|
|
* |
367
|
|
|
* @return string |
368
|
|
|
*/ |
369
|
|
|
public function relativeUrl($path = '') |
370
|
|
|
{ |
371
|
|
|
return \Flare::relativeAdminUrl($this->urlPrefix().($path ? '/'.$path : '')); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Retrieves the Current Admin Route URL. |
376
|
|
|
* |
377
|
|
|
* @param string $path |
378
|
|
|
* |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
|
|
public function currentUrl($path = '') |
382
|
|
|
{ |
383
|
|
|
return url($this->relativeCurrentUrl($path)); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Retrieves the Current Admin Route URL. |
388
|
|
|
* |
389
|
|
|
* @param string $path |
390
|
|
|
* |
391
|
|
|
* @return string |
392
|
|
|
*/ |
393
|
|
|
public function relativeCurrentUrl($path) |
394
|
|
|
{ |
395
|
|
|
return \Route::current() ? \Route::current()->getPrefix().'/'.$path : null; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Handle dynamic static method calls into the Admin. |
400
|
|
|
* |
401
|
|
|
* @param string $method |
402
|
|
|
* @param array $parameters |
403
|
|
|
* |
404
|
|
|
* @return mixed |
405
|
|
|
*/ |
406
|
|
|
// public static function __callStatic($method, $parameters) |
|
|
|
|
407
|
|
|
// { |
408
|
|
|
// $instance = new static(); |
|
|
|
|
409
|
|
|
|
410
|
|
|
// return call_user_func_array([$instance, $method], $parameters); |
|
|
|
|
411
|
|
|
// } |
412
|
|
|
} |
413
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.