Skip to content

Commit

Permalink
Internal: Add ThemeHelper to get the current color theme - refs BT#21621
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelFQC committed Jun 25, 2024
1 parent 0395a69 commit 9c6ed18
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 85 deletions.
79 changes: 5 additions & 74 deletions public/main/inc/lib/api.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down
12 changes: 6 additions & 6 deletions public/main/inc/lib/display.lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.'",
Expand Down
9 changes: 5 additions & 4 deletions src/CoreBundle/EventListener/TwigListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {}

Expand Down Expand Up @@ -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 = '<link rel="stylesheet" href="'.$path.'">';
Expand Down
3 changes: 2 additions & 1 deletion src/CoreBundle/ServiceHelper/MailHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)) {
Expand Down
66 changes: 66 additions & 0 deletions src/CoreBundle/ServiceHelper/ThemeHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/* For licensing terms, see /license.txt */

declare(strict_types=1);

namespace Chamilo\CoreBundle\ServiceHelper;

use Chamilo\CoreBundle\Settings\SettingsManager;
use Chamilo\CourseBundle\Settings\SettingsCourseManager;

final class ThemeHelper
{
public const DEFAULT_THEME = 'chamilo';

public function __construct(
private readonly AccessUrlHelper $accessUrlHelper,
private readonly SettingsManager $settingsManager,
private readonly UserHelper $userHelper,
private readonly CidReqHelper $cidReqHelper,
private readonly SettingsCourseManager $settingsCourseManager,
) {}

/**
* Returns the name of the color theme configured to be applied on the current page.
* The returned name depends on the platform, course or user settings.
*/
public function getVisualTheme(): string
{
static $visualTheme;

if (isset($visualTheme)) {
return $visualTheme;

Check warning on line 33 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/ServiceHelper/ThemeHelper.php#L33

Added line #L33 was not covered by tests
}

$accessUrl = $this->accessUrlHelper->getCurrent();

$visualTheme = $accessUrl->getActiveColorTheme()?->getColorTheme()->getSlug();

if ('true' == $this->settingsManager->getSetting('profile.user_selected_theme')) {
$visualTheme = $this->userHelper->getCurrent()?->getTheme();

Check warning on line 41 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/ServiceHelper/ThemeHelper.php#L41

Added line #L41 was not covered by tests
}

if ('true' == $this->settingsManager->getSetting('course.allow_course_theme')) {
$course = $this->cidReqHelper->getCourseEntity();

if ($course) {
$this->settingsCourseManager->setCourse($course);

Check warning on line 48 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/ServiceHelper/ThemeHelper.php#L48

Added line #L48 was not covered by tests

$visualTheme = $this->settingsCourseManager->getSetting('course_theme');

Check warning on line 50 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/ServiceHelper/ThemeHelper.php#L50

Added line #L50 was not covered by tests

if (1 === (int) $this->settingsCourseManager->getSetting('allow_learning_path_theme')) {

Check warning on line 52 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/ServiceHelper/ThemeHelper.php#L52

Added line #L52 was not covered by tests
global $lp_theme_css;

Check failure on line 53 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 Test on ubuntu-latest

InvalidGlobal

src/CoreBundle/ServiceHelper/ThemeHelper.php:53:21: InvalidGlobal: Cannot use global scope here (unless this file is included from a non-global scope) (see https://psalm.dev/046)

$visualTheme = $lp_theme_css;

Check warning on line 55 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/ServiceHelper/ThemeHelper.php#L55

Added line #L55 was not covered by tests
}
}
}

if (empty($visualTheme)) {
return self::DEFAULT_THEME;
}

return $visualTheme;

Check warning on line 64 in src/CoreBundle/ServiceHelper/ThemeHelper.php

View check run for this annotation

Codecov / codecov/patch

src/CoreBundle/ServiceHelper/ThemeHelper.php#L64

Added line #L64 was not covered by tests
}
}

0 comments on commit 9c6ed18

Please sign in to comment.