| Total Complexity | 56 |
| Total Lines | 544 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SettingsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SettingsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | #[Route('/admin')] |
||
| 33 | class SettingsController extends BaseController |
||
| 34 | { |
||
| 35 | use ControllerTrait; |
||
| 36 | |||
| 37 | public function __construct( |
||
| 38 | private readonly EntityManagerInterface $entityManager, |
||
| 39 | private readonly TranslatorInterface $translator, |
||
| 40 | private readonly SearchIndexPathResolver $searchIndexPathResolver |
||
| 41 | ) {} |
||
| 42 | |||
| 43 | #[Route('/settings', name: 'admin_settings')] |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Toggle access_url_changeable for a given setting variable. |
||
| 51 | * Only platform admins on the main URL (ID = 1) are allowed to change it, |
||
| 52 | */ |
||
| 53 | #[IsGranted('ROLE_ADMIN')] |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Edit configuration with given namespace (search page). |
||
| 117 | */ |
||
| 118 | #[IsGranted('ROLE_ADMIN')] |
||
| 119 | #[Route('/settings/search_settings', name: 'chamilo_platform_settings_search')] |
||
| 120 | public function searchSetting(Request $request, AccessUrlHelper $accessUrlHelper): Response |
||
| 121 | { |
||
| 122 | $manager = $this->getSettingsManager(); |
||
| 123 | |||
| 124 | $url = $accessUrlHelper->getCurrent(); |
||
| 125 | $manager->setUrl($url); |
||
| 126 | |||
| 127 | $formList = []; |
||
| 128 | $templateMap = []; |
||
| 129 | $templateMapByCategory = []; |
||
| 130 | $settings = []; |
||
| 131 | |||
| 132 | $keyword = trim((string) $request->query->get('keyword', '')); |
||
| 133 | |||
| 134 | $searchForm = $this->getSearchForm(); |
||
| 135 | $searchForm->handleRequest($request); |
||
| 136 | if ($searchForm->isSubmitted() && $searchForm->isValid()) { |
||
| 137 | $values = $searchForm->getData(); |
||
| 138 | $keyword = trim((string) ($values['keyword'] ?? '')); |
||
| 139 | } |
||
| 140 | |||
| 141 | $schemas = $manager->getSchemas(); |
||
| 142 | [$ordered, $labelMap] = $this->computeOrderedNamespacesByTranslatedLabel($schemas, $request); |
||
| 143 | |||
| 144 | // Template map for current URL (existing behavior – JSON helper) |
||
| 145 | $settingsRepo = $this->entityManager->getRepository(SettingsCurrent::class); |
||
| 146 | $settingsWithTemplate = $settingsRepo->findBy(['url' => $url]); |
||
| 147 | |||
| 148 | foreach ($settingsWithTemplate as $s) { |
||
| 149 | if ($s->getValueTemplate()) { |
||
| 150 | $templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | // MultiURL changeable flags: read from main URL (ID = 1) only |
||
| 155 | $changeableMap = []; |
||
| 156 | $mainUrlRows = $settingsRepo->createQueryBuilder('sc') |
||
| 157 | ->join('sc.url', 'u') |
||
| 158 | ->andWhere('u.id = :mainId') |
||
| 159 | ->setParameter('mainId', 1) |
||
| 160 | ->getQuery() |
||
| 161 | ->getResult(); |
||
| 162 | |||
| 163 | foreach ($mainUrlRows as $row) { |
||
| 164 | if ($row instanceof SettingsCurrent) { |
||
| 165 | $changeableMap[$row->getVariable()] = $row->getAccessUrlChangeable(); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | $currentUrlId = $url->getId(); |
||
| 170 | // Only platform admins on the main URL can toggle the MultiURL flag. |
||
| 171 | $canToggleMultiUrlSetting = $this->isGranted('ROLE_ADMIN') && 1 === $currentUrlId; |
||
| 172 | |||
| 173 | if ('' === $keyword) { |
||
| 174 | return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
||
| 175 | 'keyword' => $keyword, |
||
| 176 | 'schemas' => $schemas, |
||
| 177 | 'settings' => $settings, |
||
| 178 | 'form_list' => $formList, |
||
| 179 | 'search_form' => $searchForm->createView(), |
||
| 180 | 'template_map' => $templateMap, |
||
| 181 | 'template_map_by_category' => $templateMapByCategory, |
||
| 182 | 'ordered_namespaces' => $ordered, |
||
| 183 | 'namespace_labels' => $labelMap, |
||
| 184 | 'changeable_map' => $changeableMap, |
||
| 185 | 'current_url_id' => $currentUrlId, |
||
| 186 | 'can_toggle_multiurl_setting' => $canToggleMultiUrlSetting, |
||
| 187 | ]); |
||
| 188 | } |
||
| 189 | |||
| 190 | $settingsRepo = $this->entityManager->getRepository(SettingsCurrent::class); |
||
| 191 | $settingsWithTemplate = $settingsRepo->findBy(['url' => $url]); |
||
| 192 | foreach ($settingsWithTemplate as $s) { |
||
| 193 | if ($s->getValueTemplate()) { |
||
| 194 | $templateMap[$s->getVariable()] = $s->getValueTemplate()->getId(); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | $settingsFromKeyword = $manager->getParametersFromKeywordOrderedByCategory($keyword); |
||
| 199 | if (!empty($settingsFromKeyword)) { |
||
| 200 | foreach ($settingsFromKeyword as $category => $parameterList) { |
||
| 201 | if (empty($category)) { |
||
| 202 | continue; |
||
| 203 | } |
||
| 204 | |||
| 205 | $variablesInCategory = []; |
||
| 206 | foreach ($parameterList as $parameter) { |
||
| 207 | $var = $parameter->getVariable(); |
||
| 208 | $variablesInCategory[] = $var; |
||
| 209 | if (isset($templateMap[$var])) { |
||
| 210 | $templateMapByCategory[$category][$var] = $templateMap[$var]; |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // Convert category to schema alias and validate it BEFORE loading/creating the form |
||
| 215 | $schemaAlias = $manager->convertNameSpaceToService($category); |
||
| 216 | |||
| 217 | // skip unknown/legacy categories (e.g., "tools") |
||
| 218 | if (!isset($schemas[$schemaAlias])) { |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | |||
| 222 | $settings = $manager->load($category); |
||
| 223 | $form = $this->getSettingsFormFactory()->create($schemaAlias); |
||
| 224 | |||
| 225 | foreach (array_keys($settings->getParameters()) as $name) { |
||
| 226 | if (!\in_array($name, $variablesInCategory, true)) { |
||
| 227 | $form->remove($name); |
||
| 228 | $settings->remove($name); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | $form->setData($settings); |
||
| 232 | $formList[$category] = $form->createView(); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | return $this->render('@ChamiloCore/Admin/Settings/search.html.twig', [ |
||
| 237 | 'keyword' => $keyword, |
||
| 238 | 'schemas' => $schemas, |
||
| 239 | 'settings' => $settings, |
||
| 240 | 'form_list' => $formList, |
||
| 241 | 'search_form' => $searchForm->createView(), |
||
| 242 | 'template_map' => $templateMap, |
||
| 243 | 'template_map_by_category' => $templateMapByCategory, |
||
| 244 | 'ordered_namespaces' => $ordered, |
||
| 245 | 'namespace_labels' => $labelMap, |
||
| 246 | 'changeable_map' => $changeableMap, |
||
| 247 | 'current_url_id' => $currentUrlId, |
||
| 248 | 'can_toggle_multiurl_setting' => $canToggleMultiUrlSetting, |
||
| 249 | ]); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Edit configuration with given namespace. |
||
| 254 | */ |
||
| 255 | #[IsGranted('ROLE_ADMIN')] |
||
| 380 | ]); |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Sync settings from classes with the database. |
||
| 385 | */ |
||
| 386 | #[IsGranted('ROLE_ADMIN')] |
||
| 387 | #[Route('/settings_sync', name: 'sync_settings')] |
||
| 388 | public function syncSettings(AccessUrlHelper $accessUrlHelper): Response |
||
| 389 | { |
||
| 390 | $manager = $this->getSettingsManager(); |
||
| 391 | $url = $accessUrlHelper->getCurrent(); |
||
| 392 | $manager->setUrl($url); |
||
| 393 | $manager->installSchemas($url); |
||
| 394 | |||
| 395 | return new Response('Updated'); |
||
| 396 | } |
||
| 397 | |||
| 398 | #[IsGranted('ROLE_ADMIN')] |
||
| 399 | #[Route('/settings/template/{id}', name: 'chamilo_platform_settings_template')] |
||
| 400 | public function getTemplateExample(int $id): JsonResponse |
||
| 401 | { |
||
| 402 | $repo = $this->entityManager->getRepository(SettingsValueTemplate::class); |
||
| 403 | $template = $repo->find($id); |
||
| 404 | |||
| 405 | if (!$template) { |
||
| 406 | return $this->json([ |
||
| 407 | 'error' => $this->translator->trans('Template not found.'), |
||
| 408 | ], Response::HTTP_NOT_FOUND); |
||
| 409 | } |
||
| 410 | |||
| 411 | return $this->json([ |
||
| 412 | 'variable' => $template->getVariable(), |
||
| 413 | 'json_example' => $template->getJsonExample(), |
||
| 414 | 'description' => $template->getDescription(), |
||
| 415 | ]); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @return FormInterface |
||
| 420 | */ |
||
| 421 | private function getSearchForm() |
||
| 428 | } |
||
| 429 | |||
| 430 | private function computeOrderedNamespacesByTranslatedLabel(array $schemas, Request $request): array |
||
| 431 | { |
||
| 432 | // Extract raw namespaces from schema service ids |
||
| 433 | $namespaces = array_map( |
||
| 434 | static fn ($k) => str_replace('chamilo_core.settings.', '', $k), |
||
| 435 | array_keys($schemas) |
||
| 436 | ); |
||
| 437 | |||
| 438 | $transform = [ |
||
| 439 | 'announcement' => 'Announcements', |
||
| 440 | 'attendance' => 'Attendances', |
||
| 441 | 'cas' => 'CAS', |
||
| 442 | 'certificate' => 'Certificates', |
||
| 443 | 'course' => 'Courses', |
||
| 444 | 'document' => 'Documents', |
||
| 445 | 'exercise' => 'Tests', |
||
| 446 | 'forum' => 'Forums', |
||
| 447 | 'group' => 'Groups', |
||
| 448 | 'language' => 'Internationalization', |
||
| 449 | 'lp' => 'Learning paths', |
||
| 450 | 'mail' => 'E-mail', |
||
| 451 | 'message' => 'Messages', |
||
| 452 | 'profile' => 'User profiles', |
||
| 453 | 'session' => 'Sessions', |
||
| 454 | 'skill' => 'Skills', |
||
| 455 | 'social' => 'Social network', |
||
| 456 | 'survey' => 'Surveys', |
||
| 457 | 'work' => 'Assignments', |
||
| 458 | 'ticket' => 'Support tickets', |
||
| 459 | 'tracking' => 'Reporting', |
||
| 460 | 'webservice' => 'Webservices', |
||
| 461 | 'catalog' => 'Catalogue', |
||
| 462 | 'catalogue' => 'Catalogue', |
||
| 463 | 'ai_helpers' => 'AI helpers', |
||
| 464 | ]; |
||
| 465 | |||
| 466 | // Build label map (translated). For keys not in $transform, use Title Case of ns. |
||
| 467 | $labelMap = []; |
||
| 468 | foreach ($namespaces as $ns) { |
||
| 469 | if (isset($transform[$ns])) { |
||
| 470 | $labelMap[$ns] = $this->translator->trans($transform[$ns]); |
||
| 471 | } else { |
||
| 472 | $key = ucfirst(str_replace('_', ' ', $ns)); |
||
| 473 | $labelMap[$ns] = $this->translator->trans($key); |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | // Sort by translated label (locale-aware) |
||
| 478 | $collator = class_exists(Collator::class) ? new Collator($request->getLocale()) : null; |
||
| 479 | usort($namespaces, function ($a, $b) use ($labelMap, $collator) { |
||
| 480 | return $collator |
||
| 481 | ? $collator->compare($labelMap[$a], $labelMap[$b]) |
||
| 482 | : strcasecmp($labelMap[$a], $labelMap[$b]); |
||
| 483 | }); |
||
| 484 | |||
| 485 | // Optional: keep AI helpers near the top (second position) |
||
| 486 | $idx = array_search('ai_helpers', $namespaces, true); |
||
| 487 | if (false !== $idx) { |
||
| 488 | array_splice($namespaces, $idx, 1); |
||
| 489 | array_splice($namespaces, 1, 0, ['ai_helpers']); |
||
| 490 | } |
||
| 491 | |||
| 492 | return [$namespaces, $labelMap]; |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Build environment diagnostics for the "search" settings page. |
||
| 497 | * |
||
| 498 | * This replicates the legacy Chamilo 1 behaviour: |
||
| 499 | * - Check Xapian PHP extension |
||
| 500 | * - Check the index directory and permissions |
||
| 501 | * - Check custom search fields |
||
| 502 | * - Check external converters (pdftotext, ps2pdf, ...) |
||
| 503 | */ |
||
| 504 | private function buildSearchDiagnostics(SettingsManager $manager): array |
||
| 576 | ]; |
||
| 577 | } |
||
| 578 | } |
||
| 579 |
If you suppress an error, we recommend checking for the error condition explicitly: