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 Illuminate\Database\Eloquent\Model; |
9
|
|
|
use SleepingOwl\Admin\Form\Element\DependentSelect; |
10
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
11
|
|
|
|
12
|
|
|
class FormElementController extends Controller |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param ModelConfigurationInterface $model |
16
|
|
|
* @param null $id |
17
|
|
|
* @return JsonResponse|mixed |
18
|
|
|
*/ |
19
|
|
|
public function getModelLogic(ModelConfigurationInterface $model, $id = null) |
20
|
|
|
{ |
21
|
|
|
if (! is_null($id)) { |
22
|
|
|
$item = $model->getRepository()->find($id); |
23
|
|
View Code Duplication |
if (is_null($item) || ! $model->isEditable($item)) { |
|
|
|
|
24
|
|
|
return new JsonResponse([ |
25
|
|
|
'message' => trans('lang.message.access_denied'), |
26
|
|
|
], 403); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $model->fireEdit($id); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (! $model->isCreatable()) { |
33
|
|
|
return new JsonResponse([ |
34
|
|
|
'message' => trans('lang.message.access_denied'), |
35
|
|
|
], 403); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $model->fireCreate(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Request $request |
43
|
|
|
* @param ModelConfigurationInterface $model |
44
|
|
|
* @param string $field |
45
|
|
|
* @param int|null $id |
46
|
|
|
* |
47
|
|
|
* @return JsonResponse |
48
|
|
|
*/ |
49
|
|
|
public function dependentSelect(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
50
|
|
|
{ |
51
|
|
|
$form = $this->getModelLogic($model, $id); |
|
|
|
|
52
|
|
|
|
53
|
|
|
if ($form instanceof JsonResponse) { |
54
|
|
|
return $form; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @var DependentSelect $element */ |
58
|
|
|
$element = $form->getElement($field); |
59
|
|
|
|
60
|
|
|
if (is_null($element)) { |
61
|
|
|
return new JsonResponse([ |
62
|
|
|
'message' => 'Element not found', |
63
|
|
|
], 404); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$element->setAjaxParameters( |
67
|
|
|
$request->input('depdrop_all_params', []) |
|
|
|
|
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$options = $element->getOptions(); |
71
|
|
|
|
72
|
|
|
if ($element->isNullable()) { |
73
|
|
|
$options = [null => trans('sleeping_owl::lang.select.nothing')] + $options; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return new JsonResponse([ |
77
|
|
|
'output' => collect($options)->map(function ($value, $key) { |
78
|
|
|
return ['id' => $key, 'name' => $value]; |
79
|
|
|
}), |
80
|
|
|
'selected' => $element->getValueFromModel(), |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Request $request |
86
|
|
|
* @param ModelConfigurationInterface $model |
87
|
|
|
* @param string $field |
88
|
|
|
* @param int|null $id |
89
|
|
|
* |
90
|
|
|
* @return JsonResponse |
91
|
|
|
*/ |
92
|
|
|
public function multiselectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
93
|
|
|
{ |
94
|
|
|
return $this->selectSearch($request, $model, $field, $id); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Request $request |
99
|
|
|
* @param ModelConfigurationInterface $model |
100
|
|
|
* @param string $field |
101
|
|
|
* @param int|null $id |
102
|
|
|
* |
103
|
|
|
* @return JsonResponse |
104
|
|
|
*/ |
105
|
|
|
public function selectSearch(Request $request, ModelConfigurationInterface $model, $field, $id = null) |
106
|
|
|
{ |
107
|
|
|
$form = $this->getModelLogic($model, $id); |
|
|
|
|
108
|
|
|
|
109
|
|
|
if ($form instanceof JsonResponse) { |
110
|
|
|
return $form; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** @var DependentSelect $element */ |
114
|
|
|
$element = $form->getElement($field); |
115
|
|
|
|
116
|
|
|
if (is_null($element)) { |
117
|
|
|
return new JsonResponse([ |
118
|
|
|
'message' => 'Element not found', |
119
|
|
|
], 404); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$field = $request->field; |
123
|
|
|
$model = new $request->model; |
124
|
|
|
|
125
|
|
|
if ($request->q && is_object($model)) { |
126
|
|
|
return new JsonResponse( |
127
|
|
|
$model::where($request->search, 'like', "%{$request->q}%") |
128
|
|
|
->get() |
129
|
|
|
->map(function (Model $item) use ($field) { |
130
|
|
|
return [ |
131
|
|
|
'tag_name' => $item->{$field}, |
132
|
|
|
'id' => $item->id, |
133
|
|
|
'custom_name' => $item->custom_name, |
134
|
|
|
]; |
135
|
|
|
}) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.