77namespace Chamilo \CoreBundle \Controller \Admin ;
88
99use Chamilo \CoreBundle \Controller \BaseController ;
10+ use Chamilo \CoreBundle \Entity \AccessUrl ;
1011use Chamilo \CoreBundle \Entity \SearchEngineField ;
1112use Chamilo \CoreBundle \Entity \SettingsCurrent ;
1213use Chamilo \CoreBundle \Entity \SettingsValueTemplate ;
@@ -62,9 +63,14 @@ public function toggleChangeable(Request $request, AccessUrlHelper $accessUrlHel
6263 }
6364
6465 $ currentUrl = $ accessUrlHelper ->getCurrent ();
65- $ currentUrlId = $ currentUrl ->getId ();
66+ if (!$ currentUrl ) {
67+ return $ this ->json ([
68+ 'error ' => 'Access URL not resolved. ' ,
69+ ], 500 );
70+ }
71+
72+ $ currentUrlId = (int ) $ currentUrl ->getId ();
6673
67- // Only main URL (ID = 1) can toggle the flag.
6874 if (1 !== $ currentUrlId ) {
6975 return $ this ->json ([
7076 'error ' => 'Only the main URL (ID 1) can toggle this setting. ' ,
@@ -79,8 +85,15 @@ public function toggleChangeable(Request $request, AccessUrlHelper $accessUrlHel
7985 ], 400 );
8086 }
8187
82- $ variable = ( string ) $ payload ['variable ' ];
88+ $ variable = trim (( string ) $ payload ['variable ' ]) ;
8389 $ status = (int ) $ payload ['status ' ];
90+ $ status = $ status === 1 ? 1 : 0 ;
91+
92+ if ('' === $ variable ) {
93+ return $ this ->json ([
94+ 'error ' => 'Invalid variable. ' ,
95+ ], 400 );
96+ }
8497
8598 $ repo = $ this ->entityManager ->getRepository (SettingsCurrent::class);
8699
@@ -143,9 +156,22 @@ public function searchSetting(Request $request, AccessUrlHelper $accessUrlHelper
143156
144157 // Template map for current URL (existing behavior – JSON helper)
145158 $ settingsRepo = $ this ->entityManager ->getRepository (SettingsCurrent::class);
146- $ settingsWithTemplate = $ settingsRepo ->findBy (['url ' => $ url ]);
147159
148- foreach ($ settingsWithTemplate as $ s ) {
160+ // Build template map: current URL overrides main URL when missing.
161+ $ currentUrlId = (int ) $ url ->getId ();
162+ $ mainUrl = $ this ->entityManager ->getRepository (AccessUrl::class)->find (1 );
163+
164+ if ($ mainUrl instanceof AccessUrl && 1 !== $ currentUrlId ) {
165+ $ mainRows = $ settingsRepo ->findBy (['url ' => $ mainUrl ]);
166+ foreach ($ mainRows as $ s ) {
167+ if ($ s ->getValueTemplate ()) {
168+ $ templateMap [$ s ->getVariable ()] = $ s ->getValueTemplate ()->getId ();
169+ }
170+ }
171+ }
172+
173+ $ currentRows = $ settingsRepo ->findBy (['url ' => $ url ]);
174+ foreach ($ currentRows as $ s ) {
149175 if ($ s ->getValueTemplate ()) {
150176 $ templateMap [$ s ->getVariable ()] = $ s ->getValueTemplate ()->getId ();
151177 }
@@ -166,7 +192,6 @@ public function searchSetting(Request $request, AccessUrlHelper $accessUrlHelper
166192 }
167193 }
168194
169- $ currentUrlId = $ url ->getId ();
170195 // Only platform admins on the main URL can toggle the MultiURL flag.
171196 $ canToggleMultiUrlSetting = $ this ->isGranted ('ROLE_ADMIN ' ) && 1 === $ currentUrlId ;
172197
@@ -187,14 +212,6 @@ public function searchSetting(Request $request, AccessUrlHelper $accessUrlHelper
187212 ]);
188213 }
189214
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-
198215 $ settingsFromKeyword = $ manager ->getParametersFromKeywordOrderedByCategory ($ keyword );
199216 if (!empty ($ settingsFromKeyword )) {
200217 foreach ($ settingsFromKeyword as $ category => $ parameterList ) {
@@ -331,9 +348,20 @@ public function updateSetting(Request $request, AccessUrlHelper $accessUrlHelper
331348 $ templateMap = [];
332349 $ settingsRepo = $ this ->entityManager ->getRepository (SettingsCurrent::class);
333350
334- // Template map for current URL (existing behavior – JSON helper)
335- $ settingsWithTemplate = $ settingsRepo -> findBy ([ ' url ' => $ url ] );
351+ $ currentUrlId = ( int ) $ url -> getId ();
352+ $ mainUrl = $ this -> entityManager -> getRepository (AccessUrl::class)-> find ( 1 );
336353
354+ // Build template map: fallback to main URL templates when sub-URL has no row for a locked setting.
355+ if ($ mainUrl instanceof AccessUrl && 1 !== $ currentUrlId ) {
356+ $ mainRows = $ settingsRepo ->findBy (['url ' => $ mainUrl ]);
357+ foreach ($ mainRows as $ s ) {
358+ if ($ s ->getValueTemplate ()) {
359+ $ templateMap [$ s ->getVariable ()] = $ s ->getValueTemplate ()->getId ();
360+ }
361+ }
362+ }
363+
364+ $ settingsWithTemplate = $ settingsRepo ->findBy (['url ' => $ url ]);
337365 foreach ($ settingsWithTemplate as $ s ) {
338366 if ($ s ->getValueTemplate ()) {
339367 $ templateMap [$ s ->getVariable ()] = $ s ->getValueTemplate ()->getId ();
@@ -359,7 +387,6 @@ public function updateSetting(Request $request, AccessUrlHelper $accessUrlHelper
359387 'server_type ' => (string ) $ manager ->getSetting ('platform.server_type ' , true ),
360388 ];
361389
362- $ currentUrlId = $ url ->getId ();
363390 // Only platform admins on the main URL can toggle the MultiURL flag.
364391 $ canToggleMultiUrlSetting = $ this ->isGranted ('ROLE_ADMIN ' ) && 1 === $ currentUrlId ;
365392
@@ -494,12 +521,6 @@ private function computeOrderedNamespacesByTranslatedLabel(array $schemas, Reque
494521
495522 /**
496523 * 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, ...)
503524 */
504525 private function buildSearchDiagnostics (SettingsManager $ manager ): array
505526 {
0 commit comments