|
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\ServiceHelper\AccessUrlHelper; |
|
11
|
|
|
use Chamilo\CoreBundle\Traits\ControllerTrait; |
|
12
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
|
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
|
14
|
|
|
use Symfony\Component\Form\FormInterface; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
18
|
|
|
use Symfony\Component\Security\Http\Attribute\IsGranted; |
|
19
|
|
|
use Symfony\Component\Validator\Exception\ValidatorException; |
|
20
|
|
|
|
|
21
|
|
|
#[Route('/admin')] |
|
22
|
|
|
class SettingsController extends BaseController |
|
23
|
|
|
{ |
|
24
|
|
|
use ControllerTrait; |
|
25
|
|
|
|
|
26
|
|
|
#[Route('/settings', name: 'admin_settings')] |
|
27
|
|
|
public function index(): Response |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->redirectToRoute('chamilo_platform_settings', ['namespace' => 'platform']); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Edit configuration with given namespace. |
|
34
|
|
|
*/ |
|
35
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
36
|
|
|
#[Route('/settings/search_settings', name: 'chamilo_platform_settings_search')] |
|
37
|
|
|
public function searchSetting(Request $request): Response |
|
38
|
|
|
{ |
|
39
|
|
|
$manager = $this->getSettingsManager(); |
|
40
|
|
|
$formList = []; |
|
41
|
|
|
$keyword = $request->get('keyword'); |
|
42
|
|
|
|
|
43
|
|
|
$searchForm = $this->getSearchForm(); |
|
44
|
|
|
$searchForm->handleRequest($request); |
|
45
|
|
|
if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
|
46
|
|
|
$values = $searchForm->getData(); |
|
47
|
|
|
$keyword = $values['keyword']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if (empty($keyword)) { |
|
51
|
|
|
throw $this->createNotFoundException(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$settingsFromKeyword = $manager->getParametersFromKeywordOrderedByCategory($keyword); |
|
55
|
|
|
|
|
56
|
|
|
$settings = []; |
|
57
|
|
|
if (!empty($settingsFromKeyword)) { |
|
58
|
|
|
foreach ($settingsFromKeyword as $category => $parameterList) { |
|
59
|
|
|
$list = []; |
|
60
|
|
|
foreach ($parameterList as $parameter) { |
|
61
|
|
|
$list[] = $parameter->getVariable(); |
|
62
|
|
|
} |
|
63
|
|
|
$settings = $manager->load($category, null); |
|
64
|
|
|
$schemaAlias = $manager->convertNameSpaceToService($category); |
|
65
|
|
|
$form = $this->getSettingsFormFactory()->create($schemaAlias); |
|
66
|
|
|
|
|
67
|
|
|
foreach (array_keys($settings->getParameters()) as $name) { |
|
68
|
|
|
if (!\in_array($name, $list, true)) { |
|
69
|
|
|
$form->remove($name); |
|
70
|
|
|
$settings->remove($name); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
$form->setData($settings); |
|
74
|
|
|
$formList[$category] = $form->createView(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$schemas = $manager->getSchemas(); |
|
79
|
|
|
|
|
80
|
|
|
return $this->render( |
|
81
|
|
|
'@ChamiloCore/Admin/Settings/search.html.twig', |
|
82
|
|
|
[ |
|
83
|
|
|
'keyword' => $keyword, |
|
84
|
|
|
'schemas' => $schemas, |
|
85
|
|
|
'settings' => $settings, |
|
86
|
|
|
'form_list' => $formList, |
|
87
|
|
|
'search_form' => $searchForm, |
|
88
|
|
|
] |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Edit configuration with given namespace. |
|
94
|
|
|
*/ |
|
95
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
96
|
|
|
#[Route('/settings/{namespace}', name: 'chamilo_platform_settings')] |
|
97
|
|
|
public function updateSetting(Request $request, AccessUrlHelper $accessUrlHelper, string $namespace): Response |
|
98
|
|
|
{ |
|
99
|
|
|
$manager = $this->getSettingsManager(); |
|
100
|
|
|
$url = $accessUrlHelper->getCurrent(); |
|
101
|
|
|
$manager->setUrl($url); |
|
102
|
|
|
$schemaAlias = $manager->convertNameSpaceToService($namespace); |
|
103
|
|
|
$searchForm = $this->getSearchForm(); |
|
104
|
|
|
|
|
105
|
|
|
$keyword = ''; |
|
106
|
|
|
$settingsFromKeyword = null; |
|
107
|
|
|
$searchForm->handleRequest($request); |
|
108
|
|
|
if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
|
109
|
|
|
$values = $searchForm->getData(); |
|
110
|
|
|
$keyword = $values['keyword']; |
|
111
|
|
|
$settingsFromKeyword = $manager->getParametersFromKeyword( |
|
112
|
|
|
$schemaAlias, |
|
113
|
|
|
$keyword |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$keywordFromGet = $request->query->get('keyword'); |
|
118
|
|
|
if ($keywordFromGet) { |
|
119
|
|
|
$keyword = $keywordFromGet; |
|
120
|
|
|
$searchForm->setData([ |
|
121
|
|
|
'keyword' => $keyword, |
|
122
|
|
|
]); |
|
123
|
|
|
$settingsFromKeyword = $manager->getParametersFromKeyword( |
|
124
|
|
|
$schemaAlias, |
|
125
|
|
|
$keywordFromGet |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$settings = $manager->load($namespace); |
|
130
|
|
|
$form = $this->getSettingsFormFactory()->create($schemaAlias); |
|
131
|
|
|
|
|
132
|
|
|
if (!empty($keyword)) { |
|
133
|
|
|
$params = $settings->getParameters(); |
|
134
|
|
|
foreach (array_keys($params) as $name) { |
|
135
|
|
|
if (!\array_key_exists($name, $settingsFromKeyword)) { |
|
136
|
|
|
$form->remove($name); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$form->setData($settings); |
|
142
|
|
|
$form->handleRequest($request); |
|
143
|
|
|
|
|
144
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
145
|
|
|
$messageType = 'success'; |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
$manager->save($form->getData()); |
|
149
|
|
|
$message = $this->trans('Settings have been successfully updated'); |
|
150
|
|
|
} catch (ValidatorException $validatorException) { |
|
151
|
|
|
// $message = $this->trans($exception->getMessage(), [], 'validators'); |
|
152
|
|
|
$message = $this->trans($validatorException->getMessage()); |
|
153
|
|
|
$messageType = 'error'; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->addFlash($messageType, $message); |
|
157
|
|
|
if (!empty($keywordFromGet)) { |
|
158
|
|
|
return $this->redirect($request->headers->get('referer')); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
$schemas = $manager->getSchemas(); |
|
162
|
|
|
|
|
163
|
|
|
return $this->render( |
|
164
|
|
|
'@ChamiloCore/Admin/Settings/default.html.twig', |
|
165
|
|
|
[ |
|
166
|
|
|
'schemas' => $schemas, |
|
167
|
|
|
'settings' => $settings, |
|
168
|
|
|
'form' => $form->createView(), |
|
169
|
|
|
'keyword' => $keyword, |
|
170
|
|
|
'search_form' => $searchForm, |
|
171
|
|
|
] |
|
172
|
|
|
); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Sync settings from classes with the database. |
|
177
|
|
|
*/ |
|
178
|
|
|
#[IsGranted('ROLE_ADMIN')] |
|
179
|
|
|
#[Route('/settings_sync', name: 'sync_settings')] |
|
180
|
|
|
public function syncSettings(AccessUrlHelper $accessUrlHelper): Response |
|
181
|
|
|
{ |
|
182
|
|
|
$manager = $this->getSettingsManager(); |
|
183
|
|
|
$url = $accessUrlHelper->getCurrent(); |
|
184
|
|
|
$manager->setUrl($url); |
|
185
|
|
|
$manager->installSchemas($url); |
|
186
|
|
|
|
|
187
|
|
|
return new Response('Updated'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @return FormInterface |
|
192
|
|
|
*/ |
|
193
|
|
|
private function getSearchForm() |
|
194
|
|
|
{ |
|
195
|
|
|
$builder = $this->container->get('form.factory')->createNamedBuilder('search'); |
|
196
|
|
|
$builder->add('keyword', TextType::class); |
|
197
|
|
|
$builder->add('search', SubmitType::class, ['attr' => ['class' => 'btn btn--primary']]); |
|
198
|
|
|
|
|
199
|
|
|
return $builder->getForm(); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|