diff --git a/public/main/inc/lib/api.lib.php b/public/main/inc/lib/api.lib.php index 875f7ec5a5d..42419de107a 100644 --- a/public/main/inc/lib/api.lib.php +++ b/public/main/inc/lib/api.lib.php @@ -12,6 +12,7 @@ use Chamilo\CoreBundle\Exception\NotAllowedException; use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\ServiceHelper\MailHelper; +use Chamilo\CoreBundle\ServiceHelper\ThemeHelper; use Chamilo\CourseBundle\Entity\CGroup; use Chamilo\CourseBundle\Entity\CLp; use ChamiloSession as Session; @@ -3730,83 +3731,13 @@ function api_get_language_from_iso($code) } /** - * Returns the name of the visual (CSS) theme to be applied on the current page. - * The returned name depends on the platform, course or user -wide settings. - * - * @return string The visual theme's name, it is the name of a folder inside web/css/themes + * Shortcut to ThemeHelper::getVisualTheme() */ -function api_get_visual_theme() +function api_get_visual_theme(): string { - static $visual_theme; - if (!isset($visual_theme)) { - // Get style directly from DB - /*$styleFromDatabase = api_get_settings_params_simple( - [ - 'variable = ? AND access_url = ?' => [ - 'stylesheets', - api_get_current_access_url_id(), - ], - ] - ); - - if ($styleFromDatabase) { - $platform_theme = $styleFromDatabase['selected_value']; - } else { - $platform_theme = api_get_setting('stylesheets'); - }*/ - $platform_theme = api_get_setting('stylesheets'); - - // Platform's theme. - $visual_theme = $platform_theme; - if ('true' == api_get_setting('user_selected_theme')) { - $user_info = api_get_user_info(); - if (isset($user_info['theme'])) { - $user_theme = $user_info['theme']; - - if (!empty($user_theme)) { - $visual_theme = $user_theme; - // User's theme. - } - } - } - - $course_id = api_get_course_id(); - if (!empty($course_id)) { - if ('true' == api_get_setting('allow_course_theme')) { - $course_theme = api_get_course_setting('course_theme', $course_id); - - if (!empty($course_theme) && -1 != $course_theme) { - if (!empty($course_theme)) { - // Course's theme. - $visual_theme = $course_theme; - } - } - - $allow_lp_theme = api_get_course_setting('allow_learning_path_theme'); - if (1 == $allow_lp_theme) { - /*global $lp_theme_css, $lp_theme_config; - // These variables come from the file lp_controller.php. - if (!$lp_theme_config) { - if (!empty($lp_theme_css)) { - // LP's theme. - $visual_theme = $lp_theme_css; - } - }*/ - } - } - } - - if (empty($visual_theme)) { - $visual_theme = 'chamilo'; - } - - /*global $lp_theme_log; - if ($lp_theme_log) { - $visual_theme = $platform_theme; - }*/ - } + $themeHelper = Container::$container->get(ThemeHelper::class); - return $visual_theme; + return $themeHelper->getVisualTheme(); } /** diff --git a/public/main/inc/lib/display.lib.php b/public/main/inc/lib/display.lib.php index 148e639ea2c..4207fc18a59 100644 --- a/public/main/inc/lib/display.lib.php +++ b/public/main/inc/lib/display.lib.php @@ -10,6 +10,7 @@ use Chamilo\CoreBundle\Entity\ExtraFieldValues; use Chamilo\CoreBundle\Framework\Container; use Chamilo\CoreBundle\Repository\ColorThemeRepository; +use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper; use ChamiloSession as Session; use Symfony\Component\HttpFoundation\Response; @@ -2672,15 +2673,14 @@ public static function getFrameReadyBlock( return false; } - $colorThemeRepo = Container::$container->get(ColorThemeRepository::class); + $accessUrlHelper = Container::$container->get(AccessUrlHelper::class); $router = Container::getRouter(); - $colorTheme = $colorThemeRepo->getActiveOne(); - $colorThemeItem = ''; + $urlRelColorTheme = $accessUrlHelper->getCurrent()->getActiveColorTheme(); - if ($colorTheme) { - $colorThemeItem = '{ type: "stylesheet", src: "'.$router->generate('chamilo_color_theme').'" },'; - } + $colorThemeItem = $urlRelColorTheme + ? '{ type: "stylesheet", src: "'.$router->generate('chamilo_color_theme').'" },' + : ''; return '$.frameReady(function() {}, "'.$frameName.'", diff --git a/src/CoreBundle/EventListener/TwigListener.php b/src/CoreBundle/EventListener/TwigListener.php index 55ff5cf8c69..ba5637c1630 100644 --- a/src/CoreBundle/EventListener/TwigListener.php +++ b/src/CoreBundle/EventListener/TwigListener.php @@ -6,8 +6,8 @@ namespace Chamilo\CoreBundle\EventListener; -use Chamilo\CoreBundle\Repository\ColorThemeRepository; use Chamilo\CoreBundle\Repository\LanguageRepository; +use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper; use Symfony\Component\HttpKernel\Event\ControllerEvent; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; @@ -25,7 +25,7 @@ public function __construct( private readonly SerializerInterface $serializer, private readonly TokenStorageInterface $tokenStorage, private readonly LanguageRepository $languageRepository, - private readonly ColorThemeRepository $colorThemeRepository, + private readonly AccessUrlHelper $accessUrlHelper, private readonly RouterInterface $router, ) {} @@ -62,9 +62,10 @@ private function loadColorTheme(): void { $link = null; - $colorTheme = $this->colorThemeRepository->getActiveOne(); + $accessUrl = $this->accessUrlHelper->getCurrent(); + $urlRelColorTheme = $accessUrl->getActiveColorTheme(); - if ($colorTheme) { + if ($urlRelColorTheme) { $path = $this->router->generate('chamilo_color_theme'); $link = ''; diff --git a/src/CoreBundle/ServiceHelper/MailHelper.php b/src/CoreBundle/ServiceHelper/MailHelper.php index 520c9c2c57b..8ff3c5f53f6 100644 --- a/src/CoreBundle/ServiceHelper/MailHelper.php +++ b/src/CoreBundle/ServiceHelper/MailHelper.php @@ -19,6 +19,7 @@ final class MailHelper public function __construct( private readonly MailerInterface $mailer, private readonly BodyRendererInterface $bodyRenderer, + private readonly ThemeHelper $themeHelper, ) {} public function send( @@ -98,7 +99,7 @@ public function send( 'link' => $additionalParameters['link'] ?? '', 'automatic_email_text' => $automaticEmailText, 'content' => $body, - 'theme' => api_get_visual_theme(), + 'theme' => $this->themeHelper->getVisualTheme(), ]; if (!empty($recipientEmail)) { diff --git a/src/CoreBundle/ServiceHelper/ThemeHelper.php b/src/CoreBundle/ServiceHelper/ThemeHelper.php new file mode 100644 index 00000000000..4cdd9dab7be --- /dev/null +++ b/src/CoreBundle/ServiceHelper/ThemeHelper.php @@ -0,0 +1,66 @@ +accessUrlHelper->getCurrent(); + + $visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug(); + + if ('true' == $this->settingsManager->getSetting('profile.user_selected_theme')) { + $visualTheme = $this->userHelper->getCurrent()?->getTheme(); + } + + if ('true' == $this->settingsManager->getSetting('course.allow_course_theme')) { + $course = $this->cidReqHelper->getCourseEntity(); + + if ($course) { + $this->settingsCourseManager->setCourse($course); + + $visualTheme = $this->settingsCourseManager->getSetting('course_theme'); + + if (1 === (int) $this->settingsCourseManager->getSetting('allow_learning_path_theme')) { + global $lp_theme_css; + + $visualTheme = $lp_theme_css; + } + } + } + + if (empty($visualTheme)) { + return self::DEFAULT_THEME; + } + + return $visualTheme; + } +}