1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Http\JsonResponse; |
7
|
|
|
use Illuminate\Routing\Controller; |
8
|
|
|
use SleepingOwl\Admin\Form\FormElements; |
9
|
|
|
use SleepingOwl\Admin\Form\Columns\Column; |
10
|
|
|
use SleepingOwl\Admin\Display\DisplayTabbed; |
11
|
|
|
use Illuminate\Contracts\Foundation\Application; |
12
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
13
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
14
|
|
|
use SleepingOwl\Admin\Display\DisplayDatatablesAsyncAlterPaginate; |
15
|
|
|
|
16
|
|
|
class AlterPaginateDisplayController extends Controller |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param ModelConfigurationInterface $model |
20
|
|
|
* @param Request $request |
21
|
|
|
* @param Application $application |
22
|
|
|
* @param null $name |
|
|
|
|
23
|
|
|
* @return JsonResponse |
24
|
|
|
* @throws NotFoundHttpException |
25
|
|
|
*/ |
26
|
|
|
public function async(ModelConfigurationInterface $model, Request $request, Application $application, $name = null) |
27
|
|
|
{ |
28
|
|
|
$payload = $request->payload ?: []; |
29
|
|
|
|
30
|
|
|
$display = $model->fireDisplay($payload); |
|
|
|
|
31
|
|
|
|
32
|
|
|
if ($display instanceof DisplayTabbed) { |
33
|
|
|
$tabs = $display->getTabs(); |
34
|
|
|
|
35
|
|
|
foreach ($tabs as $tab) { |
36
|
|
|
$content = $tab->getContent(); |
37
|
|
|
|
38
|
|
|
if ($content instanceof FormElements) { |
39
|
|
|
foreach ($content->getElements() as $element) { |
40
|
|
|
|
41
|
|
|
//Return data-table if inside FormElements |
42
|
|
|
if ($element instanceof DisplayDatatablesAsyncAlterPaginate) { |
43
|
|
|
if ($element->getName() == $name) { |
44
|
|
|
return $this->renderFindedTable($element, $application, $request); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//Try to find data table in columns |
49
|
|
|
if ($element instanceof Column) { |
50
|
|
|
foreach ($element->getElements() as $columnElement) { |
51
|
|
|
if ($columnElement instanceof DisplayDatatablesAsyncAlterPaginate) { |
52
|
|
|
if ($columnElement->getName() == $name) { |
53
|
|
|
return $this->renderFindedTable($columnElement, $application, $request); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
//Finded trully in content-tab |
62
|
|
|
if ($content instanceof DisplayDatatablesAsyncAlterPaginate) { |
63
|
|
|
if ($content->getName() == $name) { |
64
|
|
|
return $this->renderFindedTable($content, $application, $request); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($display instanceof DisplayDatatablesAsyncAlterPaginate) { |
71
|
|
|
return $this->renderFindedTable($display, $application, $request); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
abort(404); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ModelConfigurationInterface $model |
79
|
|
|
* @param Request $request |
80
|
|
|
*/ |
81
|
|
|
public function treeReorder(ModelConfigurationInterface $model, Request $request) |
82
|
|
|
{ |
83
|
|
|
$display = $model->fireDisplay(); |
84
|
|
|
|
85
|
|
|
if ($display instanceof DisplayTabbed) { |
86
|
|
|
$display->getTabs()->each(function (DisplayTab $tab) use ($request) { |
|
|
|
|
87
|
|
|
$content = $tab->getContent(); |
88
|
|
|
if ($content instanceof DisplayTree) { |
|
|
|
|
89
|
|
|
$content->getRepository()->reorder( |
90
|
|
|
$request->input('data') |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
}); |
94
|
|
|
} else { |
95
|
|
|
$display->getRepository()->reorder( |
|
|
|
|
96
|
|
|
$request->input('data') |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param DisplayDatatablesAsyncAlterPaginate $datatable |
103
|
|
|
* @param Application $application |
104
|
|
|
* @param Request $request |
105
|
|
|
* @return array|JsonResponse |
106
|
|
|
*/ |
107
|
|
|
protected function renderFindedTable(DisplayDatatablesAsyncAlterPaginate $datatable, Application $application, Request $request) |
108
|
|
|
{ |
109
|
|
|
try { |
110
|
|
|
return $datatable->renderAsync($request); |
111
|
|
|
} catch (\Exception $exception) { |
112
|
|
|
\Log::error('unable to render finded table!', [ |
113
|
|
|
'exception' => $exception, |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
return new JsonResponse([ |
117
|
|
|
'message' => $application->isLocal() |
118
|
|
|
? $exception->getMessage() |
119
|
|
|
: trans('sleeping_owl::lang.table.error'), |
120
|
|
|
], 403); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|