|
13 | 13 | use Chamilo\CoreBundle\Repository\PageCategoryRepository; |
14 | 14 | use Chamilo\CoreBundle\Repository\PageRepository; |
15 | 15 | use Chamilo\CoreBundle\Repository\SysAnnouncementRepository; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
16 | 17 | use Symfony\Component\Security\Core\User\UserInterface; |
17 | 18 |
|
| 19 | +use const PHP_URL_PATH; |
| 20 | + |
18 | 21 | class PageHelper |
19 | 22 | { |
20 | 23 | protected PageRepository $pageRepository; |
@@ -211,4 +214,212 @@ public function isFilePathExposedByVisibleAnnouncement( |
211 | 214 |
|
212 | 215 | return false; |
213 | 216 | } |
| 217 | + |
| 218 | + /** |
| 219 | + * Returns CSS classes that identify the current "global page type". |
| 220 | + * |
| 221 | + * @return string[] |
| 222 | + */ |
| 223 | + public function getPageTypeCssClasses(Request $request): array |
| 224 | + { |
| 225 | + $pathInfo = rtrim((string) $request->getPathInfo(), '/'); |
| 226 | + if ('' === $pathInfo) { |
| 227 | + $pathInfo = '/'; |
| 228 | + } |
| 229 | + |
| 230 | + // For legacy PHP script URLs (/main/.../*.php), Symfony may return "/" as pathInfo. |
| 231 | + // In that case, rely on REQUEST_URI (path part only) to infer the real page type. |
| 232 | + $requestUri = (string) $request->server->get('REQUEST_URI', ''); |
| 233 | + $uriPath = (string) (parse_url($requestUri, PHP_URL_PATH) ?? ''); |
| 234 | + $uriPath = rtrim($uriPath, '/'); |
| 235 | + if ('' === $uriPath) { |
| 236 | + $uriPath = '/'; |
| 237 | + } |
| 238 | + |
| 239 | + // Use REQUEST_URI path when pathInfo is "/" but the actual URL path is not "/". |
| 240 | + $effectivePath = $pathInfo; |
| 241 | + if ('/' === $pathInfo && '/' !== $uriPath) { |
| 242 | + $effectivePath = $uriPath; |
| 243 | + } |
| 244 | + |
| 245 | + $segments = array_values(array_filter( |
| 246 | + explode('/', trim($effectivePath, '/')), |
| 247 | + static fn ($v) => '' !== $v |
| 248 | + )); |
| 249 | + $seg0 = $segments[0] ?? ''; |
| 250 | + $seg1 = $segments[1] ?? ''; |
| 251 | + $seg2 = $segments[2] ?? ''; |
| 252 | + |
| 253 | + // Home (only when the real URL path is "/") |
| 254 | + if ('/' === $effectivePath) { |
| 255 | + return ['page-home']; |
| 256 | + } |
| 257 | + |
| 258 | + if ('home' === $seg0) { |
| 259 | + return ['page-home']; |
| 260 | + } |
| 261 | + |
| 262 | + if ('courses' === $seg0) { |
| 263 | + return ['page-my-courses']; |
| 264 | + } |
| 265 | + |
| 266 | + if ('catalogue' === $seg0) { |
| 267 | + return ['page-catalogue']; |
| 268 | + } |
| 269 | + |
| 270 | + if ('agenda' === $seg0 || 'calendar' === $seg0) { |
| 271 | + return ['page-agenda']; |
| 272 | + } |
| 273 | + |
| 274 | + if ('tracking' === $seg0) { |
| 275 | + return ['page-tracking']; |
| 276 | + } |
| 277 | + |
| 278 | + if ('social' === $seg0) { |
| 279 | + return ['page-social']; |
| 280 | + } |
| 281 | + |
| 282 | + if ('account' === $seg0) { |
| 283 | + return ['page-account-security']; |
| 284 | + } |
| 285 | + |
| 286 | + if ('admin-dashboard' === $seg0) { |
| 287 | + return ['page-administration', 'page-administration-session']; |
| 288 | + } |
| 289 | + |
| 290 | + // Administration + sub-blocks (Vue) |
| 291 | + if ('admin' === $seg0) { |
| 292 | + $classes = ['page-administration']; |
| 293 | + |
| 294 | + if ('' !== $seg1) { |
| 295 | + // Example: /admin/settings -> page-administration page-administration-settings |
| 296 | + $classes[] = 'page-administration-'.$this->slugCss($seg1); |
| 297 | + } |
| 298 | + |
| 299 | + // Most Vue admin pages are platform-level. |
| 300 | + if (!\in_array('page-administration-platform', $classes, true)) { |
| 301 | + $classes[] = 'page-administration-platform'; |
| 302 | + } |
| 303 | + |
| 304 | + return array_values(array_unique($classes)); |
| 305 | + } |
| 306 | + |
| 307 | + // Vue "resources" routes -> optional tool markers |
| 308 | + // Example: /resources/document/... -> page-tool page-tool-document |
| 309 | + if ('resources' === $seg0 && '' !== $seg1) { |
| 310 | + return ['page-tool', 'page-tool-'.$this->slugCss($seg1)]; |
| 311 | + } |
| 312 | + |
| 313 | + // Legacy PHP pages under /main/* |
| 314 | + if ('main' === $seg0 && '' !== $seg1) { |
| 315 | + // Tracking must share the same marker across all its pages. |
| 316 | + if ('tracking' === $seg1) { |
| 317 | + return ['page-tracking']; |
| 318 | + } |
| 319 | + |
| 320 | + // Legacy administration pages are NOT tools. |
| 321 | + // Examples: |
| 322 | + // - /main/admin/user_list.php -> page-administration page-administration-user |
| 323 | + // - /main/admin/course_add.php -> page-administration page-administration-course |
| 324 | + // - /main/admin/session_list.php -> page-administration page-administration-session |
| 325 | + if ('admin' === $seg1) { |
| 326 | + $classes = ['page-administration']; |
| 327 | + |
| 328 | + // Try to detect admin sub-block from the script filename (seg2), or fallback to SCRIPT_NAME. |
| 329 | + $scriptFile = $seg2; |
| 330 | + if ('' === $scriptFile) { |
| 331 | + $scriptName = (string) $request->server->get('SCRIPT_NAME', ''); |
| 332 | + $scriptFile = basename($scriptName); |
| 333 | + } |
| 334 | + |
| 335 | + $block = $this->detectLegacyAdminBlock($scriptFile); |
| 336 | + $classes[] = 'page-administration-'.$block; |
| 337 | + |
| 338 | + // Ensure we always have a stable "platform" marker when no specific block applies. |
| 339 | + if (!\in_array('page-administration-platform', $classes, true) |
| 340 | + && !\in_array('page-administration-user', $classes, true) |
| 341 | + && !\in_array('page-administration-course', $classes, true) |
| 342 | + && !\in_array('page-administration-session', $classes, true) |
| 343 | + ) { |
| 344 | + $classes[] = 'page-administration-platform'; |
| 345 | + } |
| 346 | + |
| 347 | + return array_values(array_unique($classes)); |
| 348 | + } |
| 349 | + |
| 350 | + // Other legacy tools: /main/<tool>/* -> page-tool + page-tool-<tool> |
| 351 | + return ['page-tool', 'page-tool-'.$this->slugCss($seg1)]; |
| 352 | + } |
| 353 | + |
| 354 | + // Legacy tools fallback by script name (extra safety) |
| 355 | + $script = (string) $request->server->get('SCRIPT_NAME', ''); |
| 356 | + if (preg_match('#/main/([a-z_]+)/#', $script, $m)) { |
| 357 | + $tool = (string) $m[1]; |
| 358 | + |
| 359 | + if ('tracking' === $tool) { |
| 360 | + return ['page-tracking']; |
| 361 | + } |
| 362 | + |
| 363 | + // Legacy administration pages are NOT tools. |
| 364 | + if ('admin' === $tool) { |
| 365 | + $classes = ['page-administration']; |
| 366 | + $classes[] = 'page-administration-'.$this->detectLegacyAdminBlock(basename($script)); |
| 367 | + |
| 368 | + // Safe default when no specific block applies. |
| 369 | + if (!\in_array('page-administration-platform', $classes, true) |
| 370 | + && !\in_array('page-administration-user', $classes, true) |
| 371 | + && !\in_array('page-administration-course', $classes, true) |
| 372 | + && !\in_array('page-administration-session', $classes, true) |
| 373 | + ) { |
| 374 | + $classes[] = 'page-administration-platform'; |
| 375 | + } |
| 376 | + |
| 377 | + return array_values(array_unique($classes)); |
| 378 | + } |
| 379 | + |
| 380 | + return ['page-tool', 'page-tool-'.$this->slugCss($tool)]; |
| 381 | + } |
| 382 | + |
| 383 | + // Generic fallback: page-<first segment> |
| 384 | + if ('' !== $seg0) { |
| 385 | + return ['page-'.$this->slugCss($seg0)]; |
| 386 | + } |
| 387 | + |
| 388 | + return ['page-generic']; |
| 389 | + } |
| 390 | + |
| 391 | + /** |
| 392 | + * Detect legacy admin block from filename. |
| 393 | + * Keeps theming markers stable without hardcoding every admin file. |
| 394 | + */ |
| 395 | + private function detectLegacyAdminBlock(string $scriptFile): string |
| 396 | + { |
| 397 | + $file = strtolower($scriptFile); |
| 398 | + |
| 399 | + if (str_starts_with($file, 'user_') || str_starts_with($file, 'user-')) { |
| 400 | + return 'user'; |
| 401 | + } |
| 402 | + |
| 403 | + if (str_starts_with($file, 'course_') || str_starts_with($file, 'course-')) { |
| 404 | + return 'course'; |
| 405 | + } |
| 406 | + |
| 407 | + if (str_starts_with($file, 'session_') || str_starts_with($file, 'session-')) { |
| 408 | + return 'session'; |
| 409 | + } |
| 410 | + |
| 411 | + return 'platform'; |
| 412 | + } |
| 413 | + |
| 414 | + /** |
| 415 | + * Converts a string into a safe CSS class fragment. |
| 416 | + */ |
| 417 | + private function slugCss(string $value): string |
| 418 | + { |
| 419 | + $value = strtolower(trim($value)); |
| 420 | + $value = preg_replace('/[^a-z0-9\-_]+/', '-', $value) ?? $value; |
| 421 | + $value = trim($value, '-'); |
| 422 | + |
| 423 | + return '' !== $value ? $value : 'generic'; |
| 424 | + } |
214 | 425 | } |
0 commit comments