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