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