|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
|
6
|
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Controller\Admin; |
|
8
|
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Controller\BaseController; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\SettingsCurrent; |
|
11
|
|
|
use Chamilo\CoreBundle\Entity\SettingsValueTemplate; |
|
12
|
|
|
use Chamilo\CoreBundle\Helpers\AccessUrlHelper; |
|
13
|
|
|
use Chamilo\CoreBundle\Traits\ControllerTrait; |
|
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
15
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
17
|
|
|
use Symfony\Component\Form\FormInterface; |
|
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
22
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted; |
|
23
|
|
|
use Symfony\Component\Validator\Exception\ValidatorException; |
|
24
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
25
|
|
|
|
|
26
|
|
|
#[Route('/admin')] |
|
27
|
|
|
class SettingsController extends BaseController |
|
28
|
|
|
{ |
|
29
|
|
|
use ControllerTrait; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
private readonly EntityManagerInterface $entityManager, |
|
33
|
|
|
private readonly TranslatorInterface $translator |
|
34
|
|
|
) {} |
|
35
|
|
|
|
|
36
|
|
|
#[Route('/settings', name: 'admin_settings')] |
|
37
|
|
|
public function index(): Response |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->redirectToRoute('chamilo_platform_settings', ['namespace' => 'platform']); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Edit configuration with given namespace. |
|
44
|
|
|
*/ |
|
45
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
46
|
|
|
#[Route('/settings/search_settings', name: 'chamilo_platform_settings_search')] |
|
47
|
|
|
public function searchSetting(Request $request, AccessUrlHelper $accessUrlHelper): Response |
|
48
|
|
|
{ |
|
49
|
|
|
$manager = $this->getSettingsManager(); |
|
50
|
|
|
|
|
51
|
|
|
$url = $accessUrlHelper->getCurrent(); |
|
52
|
|
|
$manager->setUrl($url); |
|
53
|
|
|
|
|
54
|
|
|
$formList = []; |
|
55
|
|
|
$templateMap = []; |
|
56
|
|
|
$templateMapByCategory = []; |
|
57
|
|
|
$settings = []; |
|
58
|
|
|
|
|
59
|
|
|
$keyword = trim((string) $request->query->get('keyword', '')); |
|
60
|
|
|
|
|
61
|
|
|
$searchForm = $this->getSearchForm(); |
|
62
|
|
|
$searchForm->handleRequest($request); |
|
63
|
|
|
if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
|
64
|
|
|
$values = $searchForm->getData(); |
|
65
|
|
|
$keyword = trim((string) ($values['keyword'] ?? '')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$schemas = $manager->getSchemas(); |
|
69
|
|
|
|
|
70
|
|
|
if ($keyword === '') { |
|
71
|
|
|
return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
|
72
|
|
|
'keyword' => $keyword, |
|
73
|
|
|
'schemas' => $schemas, |
|
74
|
|
|
'settings' => $settings, |
|
75
|
|
|
'form_list' => $formList, |
|
76
|
|
|
'search_form' => $searchForm->createView(), |
|
77
|
|
|
'template_map' => $templateMap, |
|
78
|
|
|
'template_map_by_category' => $templateMapByCategory, |
|
79
|
|
|
]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$settingsRepo = $this->entityManager->getRepository(SettingsCurrent::class); |
|
83
|
|
|
$settingsWithTemplate = $settingsRepo->findBy(['url' => $url]); |
|
84
|
|
|
foreach ($settingsWithTemplate as $s) { |
|
85
|
|
|
if ($s->getValueTemplate()) { |
|
86
|
|
|
$templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$settingsFromKeyword = $manager->getParametersFromKeywordOrderedByCategory($keyword); |
|
91
|
|
|
if (!empty($settingsFromKeyword)) { |
|
92
|
|
|
foreach ($settingsFromKeyword as $category => $parameterList) { |
|
93
|
|
|
if (empty($category)) { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$variablesInCategory = []; |
|
98
|
|
|
foreach ($parameterList as $parameter) { |
|
99
|
|
|
$var = $parameter->getVariable(); |
|
100
|
|
|
$variablesInCategory[] = $var; |
|
101
|
|
|
if (isset($templateMap[$var])) { |
|
102
|
|
|
$templateMapByCategory[$category][$var] = $templateMap[$var]; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
$settings = $manager->load($category); |
|
106
|
|
|
$schemaAlias = $manager->convertNameSpaceToService($category); |
|
107
|
|
|
$form = $this->getSettingsFormFactory()->create($schemaAlias); |
|
108
|
|
|
|
|
109
|
|
|
foreach (array_keys($settings->getParameters()) as $name) { |
|
110
|
|
|
if (!in_array($name, $variablesInCategory, true)) { |
|
111
|
|
|
$form->remove($name); |
|
112
|
|
|
$settings->remove($name); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
$form->setData($settings); |
|
116
|
|
|
$formList[$category] = $form->createView(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
|
121
|
|
|
'keyword' => $keyword, |
|
122
|
|
|
'schemas' => $schemas, |
|
123
|
|
|
'settings' => $settings, |
|
124
|
|
|
'form_list' => $formList, |
|
125
|
|
|
'search_form' => $searchForm->createView(), |
|
126
|
|
|
'template_map' => $templateMap, |
|
127
|
|
|
'template_map_by_category' => $templateMapByCategory, |
|
128
|
|
|
]); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Edit configuration with given namespace. |
|
133
|
|
|
*/ |
|
134
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
135
|
|
|
#[Route('/settings/{namespace}', name: 'chamilo_platform_settings')] |
|
136
|
|
|
public function updateSetting(Request $request, AccessUrlHelper $accessUrlHelper, string $namespace): Response |
|
137
|
|
|
{ |
|
138
|
|
|
$manager = $this->getSettingsManager(); |
|
139
|
|
|
$url = $accessUrlHelper->getCurrent(); |
|
140
|
|
|
$manager->setUrl($url); |
|
141
|
|
|
$schemaAlias = $manager->convertNameSpaceToService($namespace); |
|
142
|
|
|
|
|
143
|
|
|
$keyword = (string) $request->query->get('keyword', ''); |
|
144
|
|
|
$settings = $manager->load($namespace); |
|
145
|
|
|
|
|
146
|
|
|
$form = $this->getSettingsFormFactory()->create( |
|
147
|
|
|
$schemaAlias, |
|
148
|
|
|
null, |
|
149
|
|
|
['allow_extra_fields' => true] |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
$form->setData($settings); |
|
153
|
|
|
$form->handleRequest($request); |
|
154
|
|
|
|
|
155
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
156
|
|
|
$messageType = 'success'; |
|
157
|
|
|
|
|
158
|
|
|
try { |
|
159
|
|
|
$manager->save($form->getData()); |
|
160
|
|
|
$message = $this->trans('The settings have been stored'); |
|
161
|
|
|
} catch (ValidatorException $validatorException) { |
|
162
|
|
|
$message = $this->trans($validatorException->getMessage()); |
|
163
|
|
|
$messageType = 'error'; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$this->addFlash($messageType, $message); |
|
167
|
|
|
|
|
168
|
|
|
if ($keyword !== '') { |
|
169
|
|
|
return $this->redirectToRoute('chamilo_platform_settings_search', [ |
|
170
|
|
|
'keyword' => $keyword, |
|
171
|
|
|
]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $this->redirectToRoute('chamilo_platform_settings', [ |
|
175
|
|
|
'namespace' => $namespace, |
|
176
|
|
|
]); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
$schemas = $manager->getSchemas(); |
|
180
|
|
|
|
|
181
|
|
|
$templateMap = []; |
|
182
|
|
|
$settingsRepo = $this->entityManager->getRepository(SettingsCurrent::class); |
|
183
|
|
|
|
|
184
|
|
|
$settingsWithTemplate = $settingsRepo->findBy(['url' => $url]); |
|
185
|
|
|
|
|
186
|
|
|
foreach ($settingsWithTemplate as $s) { |
|
187
|
|
|
if ($s->getValueTemplate()) { |
|
188
|
|
|
$templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return $this->render('@ChamiloCore/Admin/Settings/default.html.twig', [ |
|
193
|
|
|
'schemas' => $schemas, |
|
194
|
|
|
'settings' => $settings, |
|
195
|
|
|
'form' => $form->createView(), |
|
196
|
|
|
'keyword' => $keyword, |
|
197
|
|
|
'search_form' => $this->getSearchForm()->createView(), |
|
198
|
|
|
'template_map'=> $templateMap, |
|
199
|
|
|
]); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Sync settings from classes with the database. |
|
204
|
|
|
*/ |
|
205
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
206
|
|
|
#[Route('/settings_sync', name: 'sync_settings')] |
|
207
|
|
|
public function syncSettings(AccessUrlHelper $accessUrlHelper): Response |
|
208
|
|
|
{ |
|
209
|
|
|
$manager = $this->getSettingsManager(); |
|
210
|
|
|
$url = $accessUrlHelper->getCurrent(); |
|
211
|
|
|
$manager->setUrl($url); |
|
212
|
|
|
$manager->installSchemas($url); |
|
213
|
|
|
|
|
214
|
|
|
return new Response('Updated'); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
218
|
|
|
#[Route('/settings/template/{id}', name: 'chamilo_platform_settings_template')] |
|
219
|
|
|
public function getTemplateExample(int $id): JsonResponse |
|
220
|
|
|
{ |
|
221
|
|
|
$repo = $this->entityManager->getRepository(SettingsValueTemplate::class); |
|
222
|
|
|
$template = $repo->find($id); |
|
223
|
|
|
|
|
224
|
|
|
if (!$template) { |
|
225
|
|
|
return $this->json([ |
|
226
|
|
|
'error' => $this->translator->trans('Template not found.'), |
|
227
|
|
|
], Response::HTTP_NOT_FOUND); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return $this->json([ |
|
231
|
|
|
'variable' => $template->getVariable(), |
|
232
|
|
|
'json_example' => $template->getJsonExample(), |
|
233
|
|
|
'description' => $template->getDescription(), |
|
234
|
|
|
]); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return FormInterface |
|
239
|
|
|
*/ |
|
240
|
|
|
private function getSearchForm() |
|
241
|
|
|
{ |
|
242
|
|
|
$builder = $this->container->get('form.factory')->createNamedBuilder('search'); |
|
243
|
|
|
$builder->add('keyword', TextType::class); |
|
244
|
|
|
$builder->add('search', SubmitType::class, ['attr' => ['class' => 'btn btn--primary']]); |
|
245
|
|
|
|
|
246
|
|
|
return $builder->getForm(); |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|