1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Routing\Controller; |
8
|
|
|
use Illuminate\Http\RedirectResponse; |
9
|
|
|
use SleepingOwl\Admin\Form\FormElements; |
10
|
|
|
use SleepingOwl\Admin\Form\Columns\Column; |
11
|
|
|
use SleepingOwl\Admin\Display\DisplayTable; |
12
|
|
|
use Illuminate\Contracts\Support\Renderable; |
13
|
|
|
use SleepingOwl\Admin\Display\DisplayTabbed; |
14
|
|
|
use Illuminate\Validation\ValidationException; |
15
|
|
|
use SleepingOwl\Admin\Contracts\AdminInterface; |
16
|
|
|
use SleepingOwl\Admin\Model\ModelConfiguration; |
17
|
|
|
use Illuminate\Contracts\Foundation\Application; |
18
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormInterface; |
19
|
|
|
use DaveJamesMiller\Breadcrumbs\BreadcrumbsGenerator; |
20
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
21
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface; |
22
|
|
|
|
23
|
|
|
class AdminController extends Controller |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \DaveJamesMiller\Breadcrumbs\BreadcrumbsManager |
27
|
|
|
*/ |
28
|
|
|
protected $breadcrumbs; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var |
32
|
|
|
*/ |
33
|
|
|
protected $breadCrumbsData; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var AdminInterface |
37
|
|
|
*/ |
38
|
|
|
protected $admin; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var |
42
|
|
|
*/ |
43
|
|
|
private $parentBreadcrumb = 'home'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Application |
47
|
|
|
*/ |
48
|
|
|
public $app; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var |
52
|
|
|
*/ |
53
|
|
|
protected $envPolicy; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* AdminController constructor. |
57
|
|
|
* |
58
|
|
|
* @param Request $request |
59
|
|
|
* @param AdminInterface $admin |
60
|
|
|
* @param Application $application |
61
|
|
|
* |
62
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException |
63
|
|
|
*/ |
64
|
|
|
public function __construct(Request $request, AdminInterface $admin, Application $application) |
65
|
|
|
{ |
66
|
|
|
$this->app = $application; |
67
|
|
|
$this->admin = $admin; |
68
|
|
|
$this->breadcrumbs = $admin->template()->breadcrumbs(); |
69
|
|
|
|
70
|
|
|
if ($this->envPolicy = config('sleeping_owl.env_editor_policy')) { |
71
|
|
|
$this->envPolicy = new $this->envPolicy; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$admin->navigation()->setCurrentUrl($request->getUri()); |
|
|
|
|
75
|
|
|
|
76
|
|
|
if (! $this->breadcrumbs->exists('home')) { |
77
|
|
|
$this->breadcrumbs->register('home', function (BreadcrumbsGenerator $breadcrumbs) { |
78
|
|
|
$breadcrumbs->push(trans('sleeping_owl::lang.dashboard'), route('admin.dashboard')); |
|
|
|
|
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->breadCrumbsData = []; |
83
|
|
|
|
84
|
|
|
if ($currentPage = $admin->navigation()->getCurrentPage()) { |
85
|
|
|
foreach ($currentPage->getPathArray() as $page) { |
86
|
|
|
$this->breadCrumbsData[] = [ |
87
|
|
|
'id' => $page['id'], |
88
|
|
|
'title' => $page['title'], |
89
|
|
|
'url' => $page['url'], |
90
|
|
|
'parent' => $this->parentBreadcrumb, |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
$this->parentBreadcrumb = $page['id']; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getParentBreadcrumb() |
102
|
|
|
{ |
103
|
|
|
return $this->parentBreadcrumb; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $parentBreadcrumb |
108
|
|
|
*/ |
109
|
|
|
public function setParentBreadcrumb($parentBreadcrumb) |
110
|
|
|
{ |
111
|
|
|
$this->parentBreadcrumb = $parentBreadcrumb; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
116
|
|
|
*/ |
117
|
|
|
public function getDashboard() |
118
|
|
|
{ |
119
|
|
|
return $this->renderContent( |
120
|
|
|
$this->admin->template()->view('dashboard'), |
|
|
|
|
121
|
|
|
trans('sleeping_owl::lang.dashboard') |
|
|
|
|
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
127
|
|
|
*/ |
128
|
|
|
public function getEnvEditor() |
129
|
|
|
{ |
130
|
|
|
$envFile = app()->environmentFilePath(); |
|
|
|
|
131
|
|
|
$envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW)); |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Use filter masks. |
135
|
|
|
* @param $key |
136
|
|
|
* @return bool |
137
|
|
|
*/ |
138
|
|
|
$envContent = $envContent->filter(function ($value, $key) { |
139
|
|
|
return ! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
$envContent = $envContent->filter(function ($value, $key) { |
143
|
|
|
return $this->validatePolicy('display', $key); |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
$envContent = $envContent->map(function ($value, $key) { |
147
|
|
|
return (object) [ |
148
|
|
|
'value' => $value, |
149
|
|
|
'editable' => $this->validatePolicy('edit', $key), |
150
|
|
|
'deletable' => $this->validatePolicy('delete', $key), |
151
|
|
|
]; |
152
|
|
|
}); |
153
|
|
|
|
154
|
|
|
return $this->renderContent( |
155
|
|
|
$this->admin->template()->view('env_editor', ['data' => $envContent]), |
|
|
|
|
156
|
|
|
trans('sleeping_owl::lang.env_editor.title') |
|
|
|
|
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $permission |
162
|
|
|
* @param $key |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
protected function validatePolicy($permission, $key) |
166
|
|
|
{ |
167
|
|
|
return ($this->envPolicy && ((method_exists($this->envPolicy, $permission) |
168
|
|
|
&& $this->envPolicy->$permission(\Auth::user(), $key) !== false))) |
169
|
|
|
|| ! method_exists($this->envPolicy, $permission) || ! $this->envPolicy || $this->validateBeforePolicy($key); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param $key |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
protected function validateBeforePolicy($key) |
177
|
|
|
{ |
178
|
|
|
return ($this->envPolicy && (method_exists($this->envPolicy, 'before')) |
179
|
|
|
&& $this->envPolicy->before(\Auth::user(), $key) == true) |
180
|
|
|
|| ! method_exists($this->envPolicy, 'before') || ! $this->envPolicy; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param Request $request |
185
|
|
|
* @return \Illuminate\Http\RedirectResponse |
186
|
|
|
*/ |
187
|
|
|
public function postEnvEditor(Request $request) |
188
|
|
|
{ |
189
|
|
|
$envFile = app()->environmentFilePath(); |
190
|
|
|
$envContent = collect(parse_ini_file($envFile, false, INI_SCANNER_RAW)); |
191
|
|
|
|
192
|
|
|
$requestContent = collect($request->input('variables')); |
193
|
|
|
$removeContent = collect(); |
194
|
|
|
|
195
|
|
|
foreach ($envContent as $key => $value) { |
196
|
|
|
if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key)) { |
197
|
|
|
if ($requestContent->has($key)) { |
198
|
|
|
if ($this->validatePolicy('edit', $key)) { |
199
|
|
|
$envContent[$key] = $requestContent[$key]['value']; |
200
|
|
|
} |
201
|
|
|
$requestContent->forget($key); |
202
|
|
|
} else { |
203
|
|
|
$envContent->forget($key); |
204
|
|
|
$removeContent->put($key, null); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
foreach ($requestContent as $key => $value) { |
210
|
|
|
if (! in_array($key, config('sleeping_owl.env_editor_excluded_keys')) && ! $this->filterKey($key) |
211
|
|
|
&& $this->validatePolicy('create', $key)) { |
212
|
|
|
$this->writeEnvData($key, $value['value'], 1); |
213
|
|
|
} |
214
|
|
|
$requestContent->forget($key); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
foreach ($removeContent as $key => $value) { |
218
|
|
|
if ($this->validatePolicy('delete', $key)) { |
219
|
|
|
$this->writeEnvData($key); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
foreach ($envContent as $key => $value) { |
224
|
|
|
$this->writeEnvData($key, $value); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return redirect()->back()->with('success_message', 'Env Updated'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param $key |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function filterKey($key) |
235
|
|
|
{ |
236
|
|
|
foreach (config('sleeping_owl.env_editor_excluded_keys') as $val) { |
237
|
|
|
if (strpos($val, '*') !== false) { |
238
|
|
|
$val = str_replace('*', '', $val); |
239
|
|
|
if (strpos($key, $val) !== false) { |
240
|
|
|
return true; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return false; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param $key |
250
|
|
|
* @param null $data |
|
|
|
|
251
|
|
|
* @param bool $new |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
public function writeEnvData($key, $data = null, $new = null) |
255
|
|
|
{ |
256
|
|
|
$envFile = app()->environmentFilePath(); |
257
|
|
|
$str = file_get_contents($envFile); |
258
|
|
|
|
259
|
|
|
if (is_null($data)) { |
|
|
|
|
260
|
|
|
$str = preg_replace("/$key=.*/m", '', $str); |
261
|
|
|
file_put_contents($envFile, $str); |
262
|
|
|
|
263
|
|
|
return false; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if (is_null($new)) { |
267
|
|
|
$str = preg_replace("/$key=.*/m", "$key=$data", $str); |
268
|
|
|
file_put_contents($envFile, $str); |
269
|
|
|
|
270
|
|
|
return false; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$str = $str."\r\n$key=$data"; |
274
|
|
|
file_put_contents($envFile, $str); |
275
|
|
|
|
276
|
|
|
return true; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param ModelConfigurationInterface $model |
281
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
282
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException |
283
|
|
|
*/ |
284
|
|
|
public function getDisplay(ModelConfigurationInterface $model) |
285
|
|
|
{ |
286
|
|
|
if (! $model->isDisplayable()) { |
287
|
|
|
abort(404); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$display = $model->fireDisplay(); |
291
|
|
|
|
292
|
|
|
$this->registerBreadcrumbs($model); |
293
|
|
|
|
294
|
|
|
return $this->render($model, $display); |
|
|
|
|
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param ModelConfigurationInterface $model |
299
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
300
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException |
301
|
|
|
*/ |
302
|
|
|
public function getCreate(ModelConfigurationInterface $model) |
303
|
|
|
{ |
304
|
|
|
if (! $model->isCreatable()) { |
305
|
|
|
abort(404); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$create = $model->fireCreate(); |
309
|
|
|
|
310
|
|
|
$this->registerBreadcrumbs($model); |
311
|
|
|
$this->registerBreadcrumb($model->getCreateTitle(), $this->parentBreadcrumb); |
312
|
|
|
|
313
|
|
|
return $this->render($model, $create, $model->getCreateTitle()); |
|
|
|
|
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param ModelConfigurationInterface $model |
318
|
|
|
* @param Request $request |
319
|
|
|
* |
320
|
|
|
* @return \Illuminate\Http\RedirectResponse |
321
|
|
|
*/ |
322
|
|
|
public function postStore(ModelConfigurationInterface $model, Request $request) |
323
|
|
|
{ |
324
|
|
|
if (! $model->isCreatable()) { |
325
|
|
|
abort(404); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$createForm = $model->fireCreate(); |
329
|
|
|
$nextAction = $request->input('next_action'); |
330
|
|
|
|
331
|
|
|
$backUrl = $this->getBackUrl($request); |
332
|
|
|
|
333
|
|
|
if ($createForm instanceof FormInterface) { |
334
|
|
|
try { |
335
|
|
|
$createForm->validateForm($request, $model); |
336
|
|
|
|
337
|
|
|
if ($createForm->saveForm($request, $model) === false) { |
|
|
|
|
338
|
|
|
return redirect()->back()->with([ |
339
|
|
|
'_redirectBack' => $backUrl, |
340
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
341
|
|
|
]); |
342
|
|
|
} |
343
|
|
|
} catch (ValidationException $exception) { |
344
|
|
|
return redirect()->back() |
345
|
|
|
->withErrors($exception->validator) |
346
|
|
|
->withInput() |
347
|
|
|
->with([ |
348
|
|
|
'_redirectBack' => $backUrl, |
349
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
350
|
|
|
]); |
351
|
|
|
} |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
if ($nextAction == 'save_and_continue') { |
355
|
|
|
$newModel = $createForm->getModel(); |
356
|
|
|
$primaryKey = $newModel->getKeyName(); |
357
|
|
|
|
358
|
|
|
$redirectUrl = $model->getEditUrl($newModel->{$primaryKey}); |
359
|
|
|
$redirectPolicy = $model->getRedirect(); |
360
|
|
|
|
361
|
|
|
/* |
362
|
|
|
* @see Make redirect when use in model config && Fix editable redirect |
363
|
|
|
*/ |
364
|
|
|
if ($redirectPolicy->get('create') == 'display' || ! $model->isEditable($newModel)) { |
365
|
|
|
$redirectUrl = $model->getDisplayUrl(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
$response = redirect()->to( |
369
|
|
|
$redirectUrl |
370
|
|
|
)->with([ |
371
|
|
|
'_redirectBack' => $backUrl, |
372
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
373
|
|
|
]); |
374
|
|
|
} elseif ($nextAction == 'save_and_create') { |
375
|
|
|
$response = redirect()->to($model->getCreateUrl($request->except([ |
376
|
|
|
'_redirectBack', |
377
|
|
|
'_token', |
378
|
|
|
'url', |
379
|
|
|
'next_action', |
380
|
|
|
])))->with([ |
381
|
|
|
'_redirectBack' => $backUrl, |
382
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
383
|
|
|
]); |
384
|
|
|
} else { |
385
|
|
|
$response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl())); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return $response->with('success_message', $model->getMessageOnCreate()); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @param ModelConfigurationInterface $model |
393
|
|
|
* @param $id |
394
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
395
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException |
396
|
|
|
*/ |
397
|
|
|
public function getEdit(ModelConfigurationInterface $model, $id) |
398
|
|
|
{ |
399
|
|
|
$item = $model->getRepository()->find($id); |
400
|
|
|
|
401
|
|
|
if (is_null($item) || ! $model->isEditable($item)) { |
402
|
|
|
abort(404); |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
if (method_exists($model, 'setModelValue')) { |
406
|
|
|
$model->setModelValue($item); |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
$edit = $model->fireEdit($id); |
410
|
|
|
|
411
|
|
|
$this->registerBreadcrumbs($model); |
412
|
|
|
$this->registerBreadcrumb($model->getEditTitle(), $this->parentBreadcrumb); |
413
|
|
|
|
414
|
|
|
return $this->render($model, $edit, $model->getEditTitle()); |
|
|
|
|
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @param ModelConfigurationInterface $model |
419
|
|
|
* @param Request $request |
420
|
|
|
* @param int $id |
421
|
|
|
* |
422
|
|
|
* @return \Illuminate\Http\RedirectResponse |
423
|
|
|
* |
424
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
425
|
|
|
*/ |
426
|
|
|
public function postUpdate(ModelConfigurationInterface $model, Request $request, $id) |
427
|
|
|
{ |
428
|
|
|
/** @var FormInterface $editForm */ |
429
|
|
|
$editForm = $model->fireEdit($id); |
430
|
|
|
$item = $editForm->getModel(); |
431
|
|
|
|
432
|
|
|
if (is_null($item) || ! $model->isEditable($item)) { |
433
|
|
|
abort(404); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
$nextAction = $request->input('next_action'); |
437
|
|
|
|
438
|
|
|
$backUrl = $this->getBackUrl($request); |
439
|
|
|
|
440
|
|
|
if ($editForm instanceof FormInterface) { |
|
|
|
|
441
|
|
|
try { |
442
|
|
|
$editForm->validateForm($request, $model); |
443
|
|
|
|
444
|
|
|
if ($editForm->saveForm($request, $model) === false) { |
|
|
|
|
445
|
|
|
return redirect()->back()->with([ |
446
|
|
|
'_redirectBack' => $backUrl, |
447
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
448
|
|
|
]); |
449
|
|
|
} |
450
|
|
|
} catch (ValidationException $exception) { |
451
|
|
|
return redirect()->back() |
452
|
|
|
->withErrors($exception->validator) |
453
|
|
|
->withInput() |
454
|
|
|
->with([ |
455
|
|
|
'_redirectBack' => $backUrl, |
456
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
457
|
|
|
]); |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
$redirectPolicy = $model->getRedirect(); |
462
|
|
|
|
463
|
|
|
if ($nextAction == 'save_and_continue') { |
464
|
|
|
$response = redirect()->back()->with([ |
465
|
|
|
'_redirectBack' => $backUrl, |
466
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
467
|
|
|
]); |
468
|
|
|
|
469
|
|
|
if ($redirectPolicy->get('edit') == 'display') { |
470
|
|
|
$response = redirect()->to( |
471
|
|
|
$model->getDisplayUrl() |
472
|
|
|
)->with([ |
473
|
|
|
'_redirectBack' => $backUrl, |
474
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
475
|
|
|
]); |
476
|
|
|
} |
477
|
|
|
} elseif ($nextAction == 'save_and_create') { |
478
|
|
|
$response = redirect()->to($model->getCreateUrl($request->except([ |
479
|
|
|
'_redirectBack', |
480
|
|
|
'_token', |
481
|
|
|
'url', |
482
|
|
|
'next_action', |
483
|
|
|
])))->with([ |
484
|
|
|
'_redirectBack' => $backUrl, |
485
|
|
|
'sleeping_owl_tab_id' => $request->get('sleeping_owl_tab_id') ?: null, |
486
|
|
|
]); |
487
|
|
|
} else { |
488
|
|
|
$response = redirect()->to($request->input('_redirectBack', $model->getDisplayUrl())); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
return $response->with('success_message', $model->getMessageOnUpdate()); |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* @param ModelConfigurationInterface $model |
496
|
|
|
* @param Request $request |
497
|
|
|
* |
498
|
|
|
* @return bool |
499
|
|
|
* |
500
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
501
|
|
|
*/ |
502
|
|
|
public function inlineEdit(ModelConfigurationInterface $model, Request $request) |
503
|
|
|
{ |
504
|
|
|
$field = $request->input('name'); |
505
|
|
|
$id = $request->input('pk'); |
506
|
|
|
$display = $model->fireDisplay(); |
507
|
|
|
$column = null; |
508
|
|
|
|
509
|
|
|
/* |
|
|
|
|
510
|
|
|
* @var ColumnEditableInterface|null $column |
511
|
|
|
*/ |
512
|
|
|
if (is_callable([$display, 'getColumns'])) { |
513
|
|
|
$column = $display->getColumns()->all()->filter(function ($column) use ($field) { |
|
|
|
|
514
|
|
|
return ($column instanceof ColumnEditableInterface) && $field == $column->getName(); |
|
|
|
|
515
|
|
|
})->first(); |
516
|
|
|
} else { |
517
|
|
|
if ($display instanceof DisplayTabbed) { |
518
|
|
|
foreach ($display->getTabs() as $tab) { |
519
|
|
|
$content = $tab->getContent(); |
520
|
|
|
|
521
|
|
|
if ($content instanceof DisplayTable) { |
522
|
|
|
$column = $content->getColumns()->all()->filter(function ($column) use ($field) { |
523
|
|
|
return ($column instanceof ColumnEditableInterface) && $field == $column->getName(); |
524
|
|
|
})->first(); |
525
|
|
|
} |
526
|
|
|
if ($content instanceof FormElements) { |
527
|
|
|
foreach ($content->getElements() as $element) { |
528
|
|
|
|
529
|
|
|
/* |
530
|
|
|
* Return data-table if inside FormElements |
531
|
|
|
*/ |
532
|
|
|
if ($element instanceof DisplayTable) { |
533
|
|
|
$column = $element->getColumns()->all()->filter(function ($column) use ($field) { |
534
|
|
|
return ($column instanceof ColumnEditableInterface) && $field == $column->getName(); |
535
|
|
|
})->first(); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/* |
539
|
|
|
* Try to find inline Editable in columns |
540
|
|
|
*/ |
541
|
|
|
if ($element instanceof Column) { |
542
|
|
|
foreach ($element->getElements() as $columnElement) { |
543
|
|
|
if ($columnElement instanceof DisplayTable) { |
544
|
|
|
$column = $columnElement->getColumns()->all()->filter(function ($column) use ( |
545
|
|
|
$field |
546
|
|
|
) { |
547
|
|
|
return ($column instanceof ColumnEditableInterface) && $field == $column->getName(); |
548
|
|
|
})->first(); |
549
|
|
|
} |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
if (is_null($column)) { |
559
|
|
|
abort(404); |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
$repository = $model->getRepository(); |
563
|
|
|
$item = $repository->find($id); |
564
|
|
|
|
565
|
|
|
if (is_null($item) || ! $model->isEditable($item)) { |
566
|
|
|
abort(404); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
$column->setModel($item); |
570
|
|
|
|
571
|
|
|
if ($model->fireEvent('updating', true, $item, $request) === false) { |
|
|
|
|
572
|
|
|
return; |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
$column->save($request, $model); |
576
|
|
|
|
577
|
|
|
$model->fireEvent('updated', false, $item, $request); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* @param ModelConfigurationInterface $model |
582
|
|
|
* @param Request $request |
583
|
|
|
* @param int $id |
584
|
|
|
* @return \Illuminate\Http\RedirectResponse |
585
|
|
|
*/ |
586
|
|
|
public function deleteDelete(ModelConfigurationInterface $model, Request $request, $id) |
587
|
|
|
{ |
588
|
|
|
$item = $model->getRepository()->find($id); |
589
|
|
|
|
590
|
|
|
if (is_null($item) || ! $model->isDeletable($item)) { |
591
|
|
|
abort(404); |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
$model->fireDelete($id); |
595
|
|
|
|
596
|
|
|
if ($model->fireEvent('deleting', true, $item, $request) === false) { |
597
|
|
|
return redirect()->back(); |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
$model->getRepository()->delete($id); |
601
|
|
|
|
602
|
|
|
$model->fireEvent('deleted', false, $item, $request); |
603
|
|
|
|
604
|
|
|
return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
|
|
|
|
605
|
|
|
->with('success_message', $model->getMessageOnDelete()); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* @param ModelConfigurationInterface $model |
610
|
|
|
* @param Request $request |
611
|
|
|
* @param int $id |
612
|
|
|
* |
613
|
|
|
* @return \Illuminate\Http\RedirectResponse |
614
|
|
|
* |
615
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
616
|
|
|
*/ |
617
|
|
|
public function deleteDestroy(ModelConfigurationInterface $model, Request $request, $id) |
618
|
|
|
{ |
619
|
|
|
if (! $model->isRestorableModel()) { |
620
|
|
|
abort(404); |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
$item = $model->getRepository()->findOnlyTrashed($id); |
624
|
|
|
|
625
|
|
|
if (is_null($item) || ! $model->isRestorable($item)) { |
626
|
|
|
abort(404); |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
$model->fireDestroy($id); |
630
|
|
|
|
631
|
|
|
if ($model->fireEvent('destroying', true, $item, $request) === false) { |
632
|
|
|
return redirect()->back(); |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
$model->getRepository()->forceDelete($id); |
636
|
|
|
|
637
|
|
|
$model->fireEvent('destroyed', false, $item, $request); |
638
|
|
|
|
639
|
|
|
return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
|
|
|
|
640
|
|
|
->with('success_message', $model->getMessageOnDestroy()); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* @param ModelConfigurationInterface|ModelConfiguration $model |
645
|
|
|
* @param Request $request |
646
|
|
|
* @param int $id |
647
|
|
|
* |
648
|
|
|
* @return \Illuminate\Http\RedirectResponse |
649
|
|
|
* |
650
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
651
|
|
|
*/ |
652
|
|
|
public function postRestore(ModelConfigurationInterface $model, Request $request, $id) |
653
|
|
|
{ |
654
|
|
|
if (! $model->isRestorableModel()) { |
655
|
|
|
abort(404); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
$item = $model->getRepository()->findOnlyTrashed($id); |
659
|
|
|
|
660
|
|
|
if (is_null($item) || ! $model->isRestorable($item)) { |
661
|
|
|
abort(404); |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
$model->fireRestore($id); |
665
|
|
|
|
666
|
|
|
if ($model->fireEvent('restoring', true, $item, $request) === false) { |
667
|
|
|
return redirect()->back(); |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
$model->getRepository()->restore($id); |
671
|
|
|
|
672
|
|
|
$model->fireEvent('restored', false, $item, $request); |
673
|
|
|
|
674
|
|
|
return redirect($request->input('_redirectBack', back()->getTargetUrl())) |
|
|
|
|
675
|
|
|
->with('success_message', $model->getMessageOnRestore()); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* @param ModelConfigurationInterface $model |
680
|
|
|
* @param Renderable|RedirectResponse|string $content |
681
|
|
|
* @param string|null $title |
682
|
|
|
* |
683
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\Illuminate\Http\RedirectResponse |
684
|
|
|
*/ |
685
|
|
|
public function render(ModelConfigurationInterface $model, $content, $title = null) |
686
|
|
|
{ |
687
|
|
|
if ($content instanceof RedirectResponse) { |
688
|
|
|
return $content; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
if ($content instanceof Renderable) { |
692
|
|
|
$content = $content->render(); |
693
|
|
|
} |
694
|
|
|
|
695
|
|
|
if (is_null($title)) { |
696
|
|
|
$title = $model->getTitle(); |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
return $this->admin->template()->view('_layout.inner') |
|
|
|
|
700
|
|
|
->with('title', $title) |
701
|
|
|
->with('content', $content) |
702
|
|
|
->with('breadcrumbKey', $this->parentBreadcrumb); |
703
|
|
|
} |
704
|
|
|
|
705
|
|
|
/** |
706
|
|
|
* @param Renderable|string $content |
707
|
|
|
* @param string|null $title |
708
|
|
|
* |
709
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
710
|
|
|
*/ |
711
|
|
|
public function renderContent($content, $title = null) |
712
|
|
|
{ |
713
|
|
|
if ($content instanceof Renderable) { |
714
|
|
|
$content = $content->render(); |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
return $this->admin->template()->view('_layout.inner') |
|
|
|
|
718
|
|
|
->with('title', $title) |
719
|
|
|
->with('content', $content) |
720
|
|
|
->with('breadcrumbKey', $this->parentBreadcrumb); |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
/** |
724
|
|
|
* @param Request $request |
725
|
|
|
* |
726
|
|
|
* @return null|string |
727
|
|
|
*/ |
728
|
|
|
protected function getBackUrl(Request $request) |
729
|
|
|
{ |
730
|
|
|
if (($backUrl = $request->input('_redirectBack')) == \URL::previous()) { |
731
|
|
|
$backUrl = null; |
732
|
|
|
$request->merge(['_redirectBack' => $backUrl]); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
return $backUrl; |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
public function getWildcard() |
739
|
|
|
{ |
740
|
|
|
abort(404); |
741
|
|
|
} |
742
|
|
|
|
743
|
|
|
/** |
744
|
|
|
* @param $title |
745
|
|
|
* @param $parent |
746
|
|
|
* @param $name |
747
|
|
|
* @param $url |
748
|
|
|
* |
749
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException |
750
|
|
|
*/ |
751
|
|
|
protected function registerBreadcrumb($title, $parent, $name = 'render', $url = null) |
752
|
|
|
{ |
753
|
|
|
$this->breadcrumbs->register($name, function (BreadcrumbsGenerator $breadcrumbs) use ($title, $parent, $url) { |
754
|
|
|
$breadcrumbs->parent($parent); |
755
|
|
|
$breadcrumbs->push($title, $url); |
756
|
|
|
}); |
757
|
|
|
|
758
|
|
|
$this->parentBreadcrumb = $name; |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* @param ModelConfigurationInterface $model |
763
|
|
|
* @throws \DaveJamesMiller\Breadcrumbs\Exceptions\DuplicateBreadcrumbException |
764
|
|
|
*/ |
765
|
|
|
protected function registerBreadcrumbs(ModelConfigurationInterface $model) |
766
|
|
|
{ |
767
|
|
|
$this->breadCrumbsData = array_merge($this->breadCrumbsData, $model->getBreadCrumbs()); |
|
|
|
|
768
|
|
|
|
769
|
|
|
foreach ($this->breadCrumbsData as $breadcrumb) { |
770
|
|
|
if (! $this->breadcrumbs->exists($breadcrumb['id'])) { |
771
|
|
|
$this->breadcrumbs->register($breadcrumb['id'], function (BreadcrumbsGenerator $breadcrumbs) use ($breadcrumb) { |
772
|
|
|
$breadcrumbs->parent($breadcrumb['parent']); |
773
|
|
|
$breadcrumbs->push($breadcrumb['title'], $breadcrumb['url']); |
774
|
|
|
}); |
775
|
|
|
} |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
$this->parentBreadcrumb = data_get(Arr::last($this->breadCrumbsData), 'id', 'render'); |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
/** |
782
|
|
|
* @param ModelConfigurationInterface $model |
783
|
|
|
* @param Request $request |
784
|
|
|
* @param int $id |
785
|
|
|
* @return \Illuminate\Http\RedirectResponse |
786
|
|
|
*/ |
787
|
|
|
public function deletedAll(ModelConfigurationInterface $model, Request $request) |
788
|
|
|
{ |
789
|
|
|
if (is_null($request->_id)) { |
790
|
|
|
return redirect()->back(); |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
$items = $request->_id; |
794
|
|
|
|
795
|
|
|
foreach ($items as $id) { |
796
|
|
|
$item = $model->getRepository()->find($id); |
797
|
|
|
|
798
|
|
|
if (! $item) { |
799
|
|
|
return response()->Json(['error' => 'Haven`t row']); |
|
|
|
|
800
|
|
|
} |
801
|
|
|
|
802
|
|
|
if (isset($item->deleted_at) && $item->deleted_at) { |
803
|
|
|
$model->getRepository()->forceDelete($id); |
804
|
|
|
} else { |
805
|
|
|
$model->getRepository()->delete($id); |
806
|
|
|
} |
807
|
|
|
} |
808
|
|
|
|
809
|
|
|
$response = redirect() |
810
|
|
|
->to($request |
811
|
|
|
->input('_redirectBack', $model->getDisplayUrl())); |
812
|
|
|
|
813
|
|
|
return $response |
814
|
|
|
->with('success_message', $model->getMessageOnDelete()); |
815
|
|
|
} |
816
|
|
|
} |
817
|
|
|
|