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