use OCP\IConfig;
class JSDataService implements \JsonSerializable {
-
- /** @var ThemingDefaults */
- private $themingDefaults;
- /** @var Util */
- private $util;
- /** @var IConfig */
- private $appConfig;
+ private ThemingDefaults $themingDefaults;
+ private Util $util;
+ private IConfig $appConfig;
+ private ThemesService $themesService;
public function __construct(
ThemingDefaults $themingDefaults,
Util $util,
- IConfig $appConfig
+ IConfig $appConfig,
+ ThemesService $themesService
) {
$this->themingDefaults = $themingDefaults;
$this->util = $util;
$this->appConfig = $appConfig;
+ $this->themesService = $themesService;
}
public function jsonSerialize(): array {
'privacyUrl' => $this->themingDefaults->getPrivacyUrl(),
'inverted' => $this->util->invertTextColor($this->themingDefaults->getColorPrimary()),
'cacheBuster' => $this->appConfig->getAppValue(Application::APP_ID, 'cachebuster', '0'),
+ 'enabledThemes' => $this->themesService->getEnabledThemes(),
];
}
}
*/
namespace OCA\Theming\Service;
+use OCA\Theming\AppInfo\Application;
use OCA\Theming\Themes\DefaultTheme;
use OCA\Theming\Themes\DarkTheme;
use OCA\Theming\Themes\DarkHighContrastTheme;
use OCA\Theming\Themes\HighContrastTheme;
use OCA\Theming\ITheme;
+use OCP\IAppConfig;
+use OCP\IConfig;
+use OCP\IUser;
+use OCP\IUserSession;
class ThemesService {
+ private IUserSession $session;
+ private IConfig $config;
/** @var ITheme[] */
private array $themesProviders;
- public function __construct(DefaultTheme $defaultTheme,
+ public function __construct(IUserSession $userSession,
+ IConfig $config,
+ DefaultTheme $defaultTheme,
DarkTheme $darkTheme,
DarkHighContrastTheme $darkHighContrastTheme,
HighContrastTheme $highContrastTheme) {
+ $this->userSession = $userSession;
+ $this->config = $config;
+
// Register themes
$this->themesProviders = [
$defaultTheme->getId() => $defaultTheme,
];
}
- public function getThemes() {
+ public function getThemes(): array {
return $this->themesProviders;
}
- public function getThemeVariables(string $id) {
+ public function getThemeVariables(string $id): array {
return $this->themesProviders[$id]->getCSSVariables();
}
+
+ public function enableTheme(ITheme $theme): void {
+ $themes = $this->getEnabledThemes();
+ array_push($themes, $theme->getId());
+ $this->setEnabledThemes($themes);
+ }
+
+ public function disableTheme(ITheme $theme): void {
+ // Using keys as it's faster
+ $themes = $this->getEnabledThemes();
+ // If enabled, removing it
+ if (in_array($theme->getId(), $themes)) {
+ $this->setEnabledThemes(array_filter($themes, function($themeId) use ($theme) {
+ return $themeId !== $theme->getId();
+ }));
+ }
+ }
+
+ public function isEnabled(ITheme $theme): bool {
+ $user = $this->userSession->getUser();
+ if ($user instanceof IUser) {
+ // Using keys as it's faster
+ $themes = $this->getEnabledThemes();
+ return in_array($theme->getId(), $themes);
+ }
+ }
+
+ public function getEnabledThemes(): array {
+ $user = $this->userSession->getUser();
+ $enabledThemes = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]');
+ return json_decode($enabledThemes);
+ }
+
+ private function setEnabledThemes(array $themes): void {
+ $user = $this->userSession->getUser();
+ $this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique($themes)));
+ }
}
/** @var IInitialStateService */
$this->initialState = \OC::$server->get(IInitialStateService::class);
+
// Decide which page we show
if ($renderAs === TemplateResponse::RENDER_AS_USER) {
/** @var INavigationManager */
$this->initialState->provideInitialState('unified-search', 'live-search', $this->config->getAppValue('core', 'unified-search.live-search', 'yes') === 'yes');
Util::addScript('core', 'unified-search', 'core');
+ // Set body data-theme
+ if (\OC::$server->getAppManager()->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
+ /** @var \OCA\Theming\Service\ThemesService */
+ $themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
+ $this->assign('enabledThemes', $themesService->getEnabledThemes());
+ }
+
// set logo link target
$logoUrl = $this->config->getSystemValueString('logo_url', '');
$this->assign('logoUrl', $logoUrl);