1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Terranet\Administrator\Contracts\AutoTranslatable; |
7
|
|
|
use Terranet\Administrator\Contracts\Module; |
8
|
|
|
use Terranet\Administrator\Contracts\Services\TemplateProvider; |
9
|
|
|
use Terranet\Administrator\Services\Breadcrumbs; |
10
|
|
|
use Terranet\Administrator\Services\CrudActions; |
11
|
|
|
use Terranet\Administrator\Services\Finder; |
12
|
|
|
use Terranet\Administrator\Services\Saver; |
13
|
|
|
use Terranet\Administrator\Services\Template; |
14
|
|
|
use Terranet\Administrator\Traits\AutoTranslatesInstances; |
15
|
|
|
use Terranet\Administrator\Traits\Module\AllowsNavigation; |
16
|
|
|
use Terranet\Administrator\Traits\Module\HasColumns; |
17
|
|
|
|
18
|
|
|
class Scaffolding implements Module, AutoTranslatable |
19
|
|
|
{ |
20
|
|
|
use AllowsNavigation, HasColumns, AutoTranslatesInstances; |
21
|
|
|
|
22
|
|
|
const PAGE_INDEX = 'index'; |
23
|
|
|
const PAGE_VIEW = 'view'; |
24
|
|
|
const PAGE_EDIT = 'edit'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The module Eloquent model class. |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
protected $model; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Breadcrumbs provider. |
35
|
|
|
* |
36
|
|
|
* @var Breadcrumbs |
37
|
|
|
*/ |
38
|
|
|
protected $breadcrumbs = Breadcrumbs::class; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Service layer responsible for searching items. |
42
|
|
|
* |
43
|
|
|
* @var \Terranet\Administrator\Contracts\Services\Finder |
44
|
|
|
*/ |
45
|
|
|
protected $finder = Finder::class; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Service layer responsible for persisting request. |
49
|
|
|
* |
50
|
|
|
* @var \Terranet\Administrator\Contracts\Services\Saver |
51
|
|
|
*/ |
52
|
|
|
protected $saver = Saver::class; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Actions manager class. |
56
|
|
|
* |
57
|
|
|
* @var \Terranet\Administrator\Contracts\Services\CrudActions |
58
|
|
|
*/ |
59
|
|
|
protected $actions = CrudActions::class; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* View templates provider. |
63
|
|
|
* |
64
|
|
|
* @var TemplateProvider |
65
|
|
|
*/ |
66
|
|
|
protected $template = Template::class; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Include or not columns of Date type in index listing. |
70
|
|
|
* |
71
|
|
|
* @var bool |
72
|
|
|
*/ |
73
|
|
|
protected $includeDateColumns; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Global ACL Manager. |
77
|
|
|
* |
78
|
|
|
* @var mixed null |
79
|
|
|
*/ |
80
|
|
|
protected $guard; |
81
|
|
|
|
82
|
|
|
/** @var array */ |
83
|
|
|
protected static $methods = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Scaffolding constructor. |
87
|
|
|
*/ |
88
|
|
|
public function __construct() |
89
|
|
|
{ |
90
|
|
|
if (null === $this->includeDateColumns) { |
91
|
|
|
$this->includeDateColumns = $this->defaultIncludeDateColumnsValue(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $method |
97
|
|
|
* @param $arguments |
98
|
|
|
* @return mixed|null |
99
|
|
|
*/ |
100
|
|
|
public function __call($method, $arguments) |
101
|
|
|
{ |
102
|
|
|
// Call user-defined method if exists. |
103
|
|
|
if ($closure = array_get(static::$methods, $method)) { |
104
|
|
|
return \call_user_func_array($closure, $arguments); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Extend functionality by adding new methods. |
112
|
|
|
* |
113
|
|
|
* @param $name |
114
|
|
|
* @param $closure |
115
|
|
|
* |
116
|
|
|
* @throws Exception |
117
|
|
|
*/ |
118
|
|
|
public static function addMethod($name, $closure) |
119
|
|
|
{ |
120
|
|
|
if (!(new static())->hasMethod($name)) { |
121
|
|
|
static::$methods[$name] = $closure; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
public function guard() |
129
|
|
|
{ |
130
|
|
|
// set the default guard |
131
|
|
|
if (null === $this->guard) { |
132
|
|
|
$this->guard = config('administrator.acl.manager'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (\is_string($this->guard)) { |
136
|
|
|
$this->guard = new $this->guard($this); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this->guard; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Disable Actions column totally for Readonly Resources. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function hasActions() |
148
|
|
|
{ |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Check if method exists. |
154
|
|
|
* |
155
|
|
|
* @param $name |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function hasMethod($name) |
160
|
|
|
{ |
161
|
|
|
return method_exists($this, $name) || array_has(static::$methods, $name); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* The module Templates manager. |
166
|
|
|
* |
167
|
|
|
* @return string |
168
|
|
|
*/ |
169
|
|
|
public function template() |
170
|
|
|
{ |
171
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Templates'))) { |
172
|
|
|
return $file; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $this->template; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return Model |
180
|
|
|
*/ |
181
|
|
|
public function model() |
182
|
|
|
{ |
183
|
|
|
static $model = null; |
184
|
|
|
|
185
|
|
|
if (null === $model && ($class = $this->getModelClass())) { |
186
|
|
|
$model = new $class(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $model; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param $model |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
|
|
public function setModel($model) |
198
|
|
|
{ |
199
|
|
|
$this->model = $model; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Define the class responsive for fetching items. |
206
|
|
|
* |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
|
|
public function finder() |
210
|
|
|
{ |
211
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Finders'))) { |
212
|
|
|
return $file; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $this->finder; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Define the class responsive for persisting items. |
220
|
|
|
* |
221
|
|
|
* @return mixed |
222
|
|
|
*/ |
223
|
|
|
public function saver() |
224
|
|
|
{ |
225
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Savers'))) { |
226
|
|
|
return $file; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->saver; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Breadcrumbs provider |
234
|
|
|
* First parse Module doc block for provider declaration. |
235
|
|
|
* |
236
|
|
|
* @return mixed |
237
|
|
|
*/ |
238
|
|
|
public function breadcrumbs() |
239
|
|
|
{ |
240
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Breadcrumbs'))) { |
241
|
|
|
return $file; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $this->breadcrumbs; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Define the Actions provider - object responsive for |
249
|
|
|
* CRUD operations, Export, etc... |
250
|
|
|
* as like as checks action permissions. |
251
|
|
|
* |
252
|
|
|
* @return mixed |
253
|
|
|
*/ |
254
|
|
|
public function actions() |
255
|
|
|
{ |
256
|
|
|
if (class_exists($file = $this->getQualifiedClassNameOfType('Actions'))) { |
257
|
|
|
return $file; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this->actions; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @throws Exception |
265
|
|
|
* |
266
|
|
|
* @return ActionsManager |
267
|
|
|
*/ |
268
|
|
|
public function actionsManager() |
269
|
|
|
{ |
270
|
|
|
$handler = $this->actions(); |
271
|
|
|
$handler = new $handler($this); |
272
|
|
|
|
273
|
|
|
if (!$handler instanceof CrudActions) { |
274
|
|
|
throw new Exception('Actions handler must implement '.CrudActions::class.' contract'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
return new ActionsManager($handler, $this); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* The module Eloquent model. |
282
|
|
|
* |
283
|
|
|
* @throws \Exception |
284
|
|
|
* |
285
|
|
|
* @return mixed |
286
|
|
|
*/ |
287
|
|
|
protected function getModelClass() |
288
|
|
|
{ |
289
|
|
|
return $this->model; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get the full path to class of special type. |
294
|
|
|
* |
295
|
|
|
* @param $type |
296
|
|
|
* |
297
|
|
|
* @return string |
298
|
|
|
*/ |
299
|
|
|
protected function getQualifiedClassNameOfType($type) |
300
|
|
|
{ |
301
|
|
|
return app()->getNamespace()."Http\\Terranet\\Administrator\\{$type}\\".class_basename($this); |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @return mixed |
306
|
|
|
*/ |
307
|
|
|
protected function defaultIncludeDateColumnsValue() |
308
|
|
|
{ |
309
|
|
|
return config( |
310
|
|
|
"administrator.grid.timestamps.{$this->url()}", |
311
|
|
|
config('administrator.grid.timestamps.enabled') |
312
|
|
|
); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|