@@ -30,134 +30,134 @@ |
||
| 30 | 30 | use OCA\Theming\Service\BackgroundService; |
| 31 | 31 | |
| 32 | 32 | trait CommonThemeTrait { |
| 33 | - public Util $util; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Generate primary-related variables |
|
| 37 | - * This is shared between multiple themes because colorMainBackground and colorMainText |
|
| 38 | - * will change in between. |
|
| 39 | - */ |
|
| 40 | - protected function generatePrimaryVariables(string $colorMainBackground, string $colorMainText): array { |
|
| 41 | - $colorPrimaryLight = $this->util->mix($this->primaryColor, $colorMainBackground, -80); |
|
| 42 | - $colorPrimaryElement = $this->util->elementColor($this->primaryColor); |
|
| 43 | - $colorPrimaryElementDefault = $this->util->elementColor($this->defaultPrimaryColor); |
|
| 44 | - $colorPrimaryElementLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80); |
|
| 45 | - |
|
| 46 | - // primary related colours |
|
| 47 | - return [ |
|
| 48 | - // invert filter if primary is too bright |
|
| 49 | - // to be used for legacy reasons only. Use inline |
|
| 50 | - // svg with proper css variable instead or material |
|
| 51 | - // design icons. |
|
| 52 | - // ⚠️ Using 'no' as a value to make sure we specify an |
|
| 53 | - // invalid one with no fallback. 'unset' could here fallback to some |
|
| 54 | - // other theme with media queries |
|
| 55 | - '--primary-invert-if-bright' => $this->util->invertTextColor($this->primaryColor) ? 'invert(100%)' : 'no', |
|
| 56 | - |
|
| 57 | - '--color-primary' => $this->primaryColor, |
|
| 58 | - '--color-primary-default' => $this->defaultPrimaryColor, |
|
| 59 | - '--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff', |
|
| 60 | - '--color-primary-hover' => $this->util->mix($this->primaryColor, $colorMainBackground, 60), |
|
| 61 | - '--color-primary-light' => $colorPrimaryLight, |
|
| 62 | - '--color-primary-light-text' => $this->util->mix($this->primaryColor, $this->util->invertTextColor($colorPrimaryLight) ? '#000000' : '#ffffff', -20), |
|
| 63 | - '--color-primary-light-hover' => $this->util->mix($colorPrimaryLight, $colorMainText, 90), |
|
| 64 | - '--color-primary-text-dark' => $this->util->darken($this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff', 7), |
|
| 65 | - |
|
| 66 | - // used for buttons, inputs... |
|
| 67 | - '--color-primary-element' => $colorPrimaryElement, |
|
| 68 | - '--color-primary-element-default-hover' => $this->util->mix($colorPrimaryElementDefault, $colorMainBackground, 60), |
|
| 69 | - '--color-primary-element-text' => $this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff', |
|
| 70 | - '--color-primary-element-hover' => $this->util->mix($colorPrimaryElement, $colorMainBackground, 60), |
|
| 71 | - '--color-primary-element-light' => $colorPrimaryElementLight, |
|
| 72 | - '--color-primary-element-light-text' => $this->util->mix($colorPrimaryElement, $this->util->invertTextColor($colorPrimaryElementLight) ? '#000000' : '#ffffff', -20), |
|
| 73 | - '--color-primary-element-light-hover' => $this->util->mix($colorPrimaryElementLight, $colorMainText, 90), |
|
| 74 | - '--color-primary-element-text-dark' => $this->util->darken($this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff', 7), |
|
| 75 | - |
|
| 76 | - // to use like this: background-image: var(--gradient-primary-background); |
|
| 77 | - '--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)', |
|
| 78 | - ]; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Generate admin theming background-related variables |
|
| 83 | - */ |
|
| 84 | - protected function generateGlobalBackgroundVariables(): array { |
|
| 85 | - $backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor'; |
|
| 86 | - $hasCustomLogoHeader = $this->util->isLogoThemed(); |
|
| 87 | - $isDefaultPrimaryBright = $this->util->invertTextColor($this->defaultPrimaryColor); |
|
| 88 | - |
|
| 89 | - $variables = []; |
|
| 90 | - |
|
| 91 | - // Default last fallback values |
|
| 92 | - $variables['--image-background-default'] = "url('" . $this->themingDefaults->getBackground() . "')"; |
|
| 93 | - $variables['--color-background-plain'] = $this->defaultPrimaryColor; |
|
| 94 | - |
|
| 95 | - // If primary as background has been request or if we have a custom primary colour |
|
| 96 | - // let's not define the background image |
|
| 97 | - if ($backgroundDeleted) { |
|
| 98 | - $variables['--color-background-plain'] = $this->defaultPrimaryColor; |
|
| 99 | - $variables['--image-background-plain'] = 'yes'; |
|
| 100 | - // If no background image is set, we need to check against the shown primary colour |
|
| 101 | - $variables['--background-image-invert-if-bright'] = $isDefaultPrimaryBright ? 'invert(100%)' : 'no'; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - // Register image variables only if custom-defined |
|
| 105 | - foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $image) { |
|
| 106 | - if ($this->imageManager->hasImage($image)) { |
|
| 107 | - $imageUrl = $this->imageManager->getImageUrl($image); |
|
| 108 | - // --image-background is overridden by user theming |
|
| 109 | - $variables["--image-$image"] = "url('" . $imageUrl . "')"; |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - if ($hasCustomLogoHeader) { |
|
| 114 | - $variables["--image-logoheader-custom"] = 'true'; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - return $variables; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Generate user theming background-related variables |
|
| 122 | - */ |
|
| 123 | - protected function generateUserBackgroundVariables(): array { |
|
| 124 | - $user = $this->userSession->getUser(); |
|
| 125 | - if ($user !== null |
|
| 126 | - && !$this->themingDefaults->isUserThemingDisabled() |
|
| 127 | - && $this->appManager->isEnabledForUser(Application::APP_ID)) { |
|
| 128 | - $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT); |
|
| 129 | - $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0'); |
|
| 130 | - $isPrimaryBright = $this->util->invertTextColor($this->primaryColor); |
|
| 131 | - |
|
| 132 | - // The user removed the background |
|
| 133 | - if ($backgroundImage === BackgroundService::BACKGROUND_DISABLED) { |
|
| 134 | - return [ |
|
| 135 | - '--image-background' => 'no', |
|
| 136 | - '--color-background-plain' => $this->primaryColor, |
|
| 137 | - // If no background image is set, we need to check against the shown primary colour |
|
| 138 | - '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no', |
|
| 139 | - ]; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - // The user uploaded a custom background |
|
| 143 | - if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) { |
|
| 144 | - $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8); |
|
| 145 | - return [ |
|
| 146 | - '--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')", |
|
| 147 | - '--color-background-plain' => $this->themingDefaults->getColorPrimary(), |
|
| 148 | - ]; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // The user picked a shipped background |
|
| 152 | - if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) { |
|
| 153 | - return [ |
|
| 154 | - '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')", |
|
| 155 | - '--color-background-plain' => $this->themingDefaults->getColorPrimary(), |
|
| 156 | - '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no', |
|
| 157 | - ]; |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - return []; |
|
| 162 | - } |
|
| 33 | + public Util $util; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Generate primary-related variables |
|
| 37 | + * This is shared between multiple themes because colorMainBackground and colorMainText |
|
| 38 | + * will change in between. |
|
| 39 | + */ |
|
| 40 | + protected function generatePrimaryVariables(string $colorMainBackground, string $colorMainText): array { |
|
| 41 | + $colorPrimaryLight = $this->util->mix($this->primaryColor, $colorMainBackground, -80); |
|
| 42 | + $colorPrimaryElement = $this->util->elementColor($this->primaryColor); |
|
| 43 | + $colorPrimaryElementDefault = $this->util->elementColor($this->defaultPrimaryColor); |
|
| 44 | + $colorPrimaryElementLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80); |
|
| 45 | + |
|
| 46 | + // primary related colours |
|
| 47 | + return [ |
|
| 48 | + // invert filter if primary is too bright |
|
| 49 | + // to be used for legacy reasons only. Use inline |
|
| 50 | + // svg with proper css variable instead or material |
|
| 51 | + // design icons. |
|
| 52 | + // ⚠️ Using 'no' as a value to make sure we specify an |
|
| 53 | + // invalid one with no fallback. 'unset' could here fallback to some |
|
| 54 | + // other theme with media queries |
|
| 55 | + '--primary-invert-if-bright' => $this->util->invertTextColor($this->primaryColor) ? 'invert(100%)' : 'no', |
|
| 56 | + |
|
| 57 | + '--color-primary' => $this->primaryColor, |
|
| 58 | + '--color-primary-default' => $this->defaultPrimaryColor, |
|
| 59 | + '--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff', |
|
| 60 | + '--color-primary-hover' => $this->util->mix($this->primaryColor, $colorMainBackground, 60), |
|
| 61 | + '--color-primary-light' => $colorPrimaryLight, |
|
| 62 | + '--color-primary-light-text' => $this->util->mix($this->primaryColor, $this->util->invertTextColor($colorPrimaryLight) ? '#000000' : '#ffffff', -20), |
|
| 63 | + '--color-primary-light-hover' => $this->util->mix($colorPrimaryLight, $colorMainText, 90), |
|
| 64 | + '--color-primary-text-dark' => $this->util->darken($this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff', 7), |
|
| 65 | + |
|
| 66 | + // used for buttons, inputs... |
|
| 67 | + '--color-primary-element' => $colorPrimaryElement, |
|
| 68 | + '--color-primary-element-default-hover' => $this->util->mix($colorPrimaryElementDefault, $colorMainBackground, 60), |
|
| 69 | + '--color-primary-element-text' => $this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff', |
|
| 70 | + '--color-primary-element-hover' => $this->util->mix($colorPrimaryElement, $colorMainBackground, 60), |
|
| 71 | + '--color-primary-element-light' => $colorPrimaryElementLight, |
|
| 72 | + '--color-primary-element-light-text' => $this->util->mix($colorPrimaryElement, $this->util->invertTextColor($colorPrimaryElementLight) ? '#000000' : '#ffffff', -20), |
|
| 73 | + '--color-primary-element-light-hover' => $this->util->mix($colorPrimaryElementLight, $colorMainText, 90), |
|
| 74 | + '--color-primary-element-text-dark' => $this->util->darken($this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff', 7), |
|
| 75 | + |
|
| 76 | + // to use like this: background-image: var(--gradient-primary-background); |
|
| 77 | + '--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)', |
|
| 78 | + ]; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Generate admin theming background-related variables |
|
| 83 | + */ |
|
| 84 | + protected function generateGlobalBackgroundVariables(): array { |
|
| 85 | + $backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor'; |
|
| 86 | + $hasCustomLogoHeader = $this->util->isLogoThemed(); |
|
| 87 | + $isDefaultPrimaryBright = $this->util->invertTextColor($this->defaultPrimaryColor); |
|
| 88 | + |
|
| 89 | + $variables = []; |
|
| 90 | + |
|
| 91 | + // Default last fallback values |
|
| 92 | + $variables['--image-background-default'] = "url('" . $this->themingDefaults->getBackground() . "')"; |
|
| 93 | + $variables['--color-background-plain'] = $this->defaultPrimaryColor; |
|
| 94 | + |
|
| 95 | + // If primary as background has been request or if we have a custom primary colour |
|
| 96 | + // let's not define the background image |
|
| 97 | + if ($backgroundDeleted) { |
|
| 98 | + $variables['--color-background-plain'] = $this->defaultPrimaryColor; |
|
| 99 | + $variables['--image-background-plain'] = 'yes'; |
|
| 100 | + // If no background image is set, we need to check against the shown primary colour |
|
| 101 | + $variables['--background-image-invert-if-bright'] = $isDefaultPrimaryBright ? 'invert(100%)' : 'no'; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + // Register image variables only if custom-defined |
|
| 105 | + foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $image) { |
|
| 106 | + if ($this->imageManager->hasImage($image)) { |
|
| 107 | + $imageUrl = $this->imageManager->getImageUrl($image); |
|
| 108 | + // --image-background is overridden by user theming |
|
| 109 | + $variables["--image-$image"] = "url('" . $imageUrl . "')"; |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + if ($hasCustomLogoHeader) { |
|
| 114 | + $variables["--image-logoheader-custom"] = 'true'; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + return $variables; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Generate user theming background-related variables |
|
| 122 | + */ |
|
| 123 | + protected function generateUserBackgroundVariables(): array { |
|
| 124 | + $user = $this->userSession->getUser(); |
|
| 125 | + if ($user !== null |
|
| 126 | + && !$this->themingDefaults->isUserThemingDisabled() |
|
| 127 | + && $this->appManager->isEnabledForUser(Application::APP_ID)) { |
|
| 128 | + $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT); |
|
| 129 | + $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0'); |
|
| 130 | + $isPrimaryBright = $this->util->invertTextColor($this->primaryColor); |
|
| 131 | + |
|
| 132 | + // The user removed the background |
|
| 133 | + if ($backgroundImage === BackgroundService::BACKGROUND_DISABLED) { |
|
| 134 | + return [ |
|
| 135 | + '--image-background' => 'no', |
|
| 136 | + '--color-background-plain' => $this->primaryColor, |
|
| 137 | + // If no background image is set, we need to check against the shown primary colour |
|
| 138 | + '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no', |
|
| 139 | + ]; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + // The user uploaded a custom background |
|
| 143 | + if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) { |
|
| 144 | + $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8); |
|
| 145 | + return [ |
|
| 146 | + '--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')", |
|
| 147 | + '--color-background-plain' => $this->themingDefaults->getColorPrimary(), |
|
| 148 | + ]; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // The user picked a shipped background |
|
| 152 | + if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) { |
|
| 153 | + return [ |
|
| 154 | + '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')", |
|
| 155 | + '--color-background-plain' => $this->themingDefaults->getColorPrimary(), |
|
| 156 | + '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no', |
|
| 157 | + ]; |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + return []; |
|
| 162 | + } |
|
| 163 | 163 | } |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | $variables = []; |
| 90 | 90 | |
| 91 | 91 | // Default last fallback values |
| 92 | - $variables['--image-background-default'] = "url('" . $this->themingDefaults->getBackground() . "')"; |
|
| 92 | + $variables['--image-background-default'] = "url('".$this->themingDefaults->getBackground()."')"; |
|
| 93 | 93 | $variables['--color-background-plain'] = $this->defaultPrimaryColor; |
| 94 | 94 | |
| 95 | 95 | // If primary as background has been request or if we have a custom primary colour |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | if ($this->imageManager->hasImage($image)) { |
| 107 | 107 | $imageUrl = $this->imageManager->getImageUrl($image); |
| 108 | 108 | // --image-background is overridden by user theming |
| 109 | - $variables["--image-$image"] = "url('" . $imageUrl . "')"; |
|
| 109 | + $variables["--image-$image"] = "url('".$imageUrl."')"; |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | && !$this->themingDefaults->isUserThemingDisabled() |
| 127 | 127 | && $this->appManager->isEnabledForUser(Application::APP_ID)) { |
| 128 | 128 | $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT); |
| 129 | - $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0'); |
|
| 129 | + $currentVersion = (int) $this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0'); |
|
| 130 | 130 | $isPrimaryBright = $this->util->invertTextColor($this->primaryColor); |
| 131 | 131 | |
| 132 | 132 | // The user removed the background |
@@ -141,9 +141,9 @@ discard block |
||
| 141 | 141 | |
| 142 | 142 | // The user uploaded a custom background |
| 143 | 143 | if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) { |
| 144 | - $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8); |
|
| 144 | + $cacheBuster = substr(sha1($user->getUID().'_'.$currentVersion), 0, 8); |
|
| 145 | 145 | return [ |
| 146 | - '--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')", |
|
| 146 | + '--image-background' => "url('".$this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground')."?v=$cacheBuster')", |
|
| 147 | 147 | '--color-background-plain' => $this->themingDefaults->getColorPrimary(), |
| 148 | 148 | ]; |
| 149 | 149 | } |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | // The user picked a shipped background |
| 152 | 152 | if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) { |
| 153 | 153 | return [ |
| 154 | - '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')", |
|
| 154 | + '--image-background' => "url('".$this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage")."')", |
|
| 155 | 155 | '--color-background-plain' => $this->themingDefaults->getColorPrimary(), |
| 156 | 156 | '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no', |
| 157 | 157 | ]; |
@@ -50,165 +50,165 @@ |
||
| 50 | 50 | |
| 51 | 51 | class UserThemeController extends OCSController { |
| 52 | 52 | |
| 53 | - protected ?string $userId = null; |
|
| 53 | + protected ?string $userId = null; |
|
| 54 | 54 | |
| 55 | - private IConfig $config; |
|
| 56 | - private IUserSession $userSession; |
|
| 57 | - private ThemesService $themesService; |
|
| 58 | - private ThemingDefaults $themingDefaults; |
|
| 59 | - private BackgroundService $backgroundService; |
|
| 60 | - |
|
| 61 | - public function __construct(string $appName, |
|
| 62 | - IRequest $request, |
|
| 63 | - IConfig $config, |
|
| 64 | - IUserSession $userSession, |
|
| 65 | - ThemesService $themesService, |
|
| 66 | - ThemingDefaults $themingDefaults, |
|
| 67 | - BackgroundService $backgroundService) { |
|
| 68 | - parent::__construct($appName, $request); |
|
| 69 | - $this->config = $config; |
|
| 70 | - $this->userSession = $userSession; |
|
| 71 | - $this->themesService = $themesService; |
|
| 72 | - $this->themingDefaults = $themingDefaults; |
|
| 73 | - $this->backgroundService = $backgroundService; |
|
| 74 | - |
|
| 75 | - $user = $userSession->getUser(); |
|
| 76 | - if ($user !== null) { |
|
| 77 | - $this->userId = $user->getUID(); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @NoAdminRequired |
|
| 83 | - * |
|
| 84 | - * Enable theme |
|
| 85 | - * |
|
| 86 | - * @param string $themeId the theme ID |
|
| 87 | - * @return DataResponse |
|
| 88 | - * @throws OCSBadRequestException|PreConditionNotMetException |
|
| 89 | - */ |
|
| 90 | - public function enableTheme(string $themeId): DataResponse { |
|
| 91 | - $theme = $this->validateTheme($themeId); |
|
| 92 | - |
|
| 93 | - // Enable selected theme |
|
| 94 | - $this->themesService->enableTheme($theme); |
|
| 95 | - return new DataResponse(); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @NoAdminRequired |
|
| 100 | - * |
|
| 101 | - * Disable theme |
|
| 102 | - * |
|
| 103 | - * @param string $themeId the theme ID |
|
| 104 | - * @return DataResponse |
|
| 105 | - * @throws OCSBadRequestException|PreConditionNotMetException |
|
| 106 | - */ |
|
| 107 | - public function disableTheme(string $themeId): DataResponse { |
|
| 108 | - $theme = $this->validateTheme($themeId); |
|
| 109 | - |
|
| 110 | - // Enable selected theme |
|
| 111 | - $this->themesService->disableTheme($theme); |
|
| 112 | - return new DataResponse(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Validate and return the matching ITheme |
|
| 117 | - * |
|
| 118 | - * Disable theme |
|
| 119 | - * |
|
| 120 | - * @param string $themeId the theme ID |
|
| 121 | - * @return ITheme |
|
| 122 | - * @throws OCSBadRequestException|PreConditionNotMetException |
|
| 123 | - */ |
|
| 124 | - private function validateTheme(string $themeId): ITheme { |
|
| 125 | - if ($themeId === '' || !$themeId) { |
|
| 126 | - throw new OCSBadRequestException('Invalid theme id: ' . $themeId); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $themes = $this->themesService->getThemes(); |
|
| 130 | - if (!isset($themes[$themeId])) { |
|
| 131 | - throw new OCSBadRequestException('Invalid theme id: ' . $themeId); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - // If trying to toggle another theme but this is enforced |
|
| 135 | - if ($this->config->getSystemValueString('enforce_theme', '') !== '' |
|
| 136 | - && $themes[$themeId]->getType() === ITheme::TYPE_THEME) { |
|
| 137 | - throw new OCSForbiddenException('Theme switching is disabled'); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $themes[$themeId]; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @NoAdminRequired |
|
| 145 | - * @NoCSRFRequired |
|
| 146 | - */ |
|
| 147 | - public function getBackground(): Http\Response { |
|
| 148 | - $file = $this->backgroundService->getBackground(); |
|
| 149 | - if ($file !== null) { |
|
| 150 | - $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); |
|
| 151 | - $response->cacheFor(24 * 60 * 60, false, true); |
|
| 152 | - return $response; |
|
| 153 | - } |
|
| 154 | - return new NotFoundResponse(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @NoAdminRequired |
|
| 159 | - */ |
|
| 160 | - public function deleteBackground(): JSONResponse { |
|
| 161 | - $currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0'); |
|
| 162 | - $this->backgroundService->deleteBackgroundImage(); |
|
| 163 | - return new JSONResponse([ |
|
| 164 | - 'backgroundImage' => null, |
|
| 165 | - 'backgroundColor' => $this->themingDefaults->getColorPrimary(), |
|
| 166 | - 'version' => $currentVersion, |
|
| 167 | - ]); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @NoAdminRequired |
|
| 172 | - */ |
|
| 173 | - public function setBackground(string $type = BackgroundService::BACKGROUND_DEFAULT, string $value = '', string $color = null): JSONResponse { |
|
| 174 | - $currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0'); |
|
| 175 | - |
|
| 176 | - // Set color if provided |
|
| 177 | - if ($color) { |
|
| 178 | - $this->backgroundService->setColorBackground($color); |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - // Set background image if provided |
|
| 182 | - try { |
|
| 183 | - switch ($type) { |
|
| 184 | - case BackgroundService::BACKGROUND_SHIPPED: |
|
| 185 | - $this->backgroundService->setShippedBackground($value); |
|
| 186 | - break; |
|
| 187 | - case BackgroundService::BACKGROUND_CUSTOM: |
|
| 188 | - $this->backgroundService->setFileBackground($value); |
|
| 189 | - break; |
|
| 190 | - case BackgroundService::BACKGROUND_DEFAULT: |
|
| 191 | - // Delete both background and color keys |
|
| 192 | - $this->backgroundService->setDefaultBackground(); |
|
| 193 | - break; |
|
| 194 | - default: |
|
| 195 | - if (!$color) { |
|
| 196 | - return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - } catch (\InvalidArgumentException $e) { |
|
| 200 | - return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
| 201 | - } catch (\Throwable $e) { |
|
| 202 | - return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $currentVersion++; |
|
| 206 | - $this->config->setUserValue($this->userId, Application::APP_ID, 'userCacheBuster', (string)$currentVersion); |
|
| 207 | - |
|
| 208 | - return new JSONResponse([ |
|
| 209 | - 'backgroundImage' => $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT), |
|
| 210 | - 'backgroundColor' => $this->themingDefaults->getColorPrimary(), |
|
| 211 | - 'version' => $currentVersion, |
|
| 212 | - ]); |
|
| 213 | - } |
|
| 55 | + private IConfig $config; |
|
| 56 | + private IUserSession $userSession; |
|
| 57 | + private ThemesService $themesService; |
|
| 58 | + private ThemingDefaults $themingDefaults; |
|
| 59 | + private BackgroundService $backgroundService; |
|
| 60 | + |
|
| 61 | + public function __construct(string $appName, |
|
| 62 | + IRequest $request, |
|
| 63 | + IConfig $config, |
|
| 64 | + IUserSession $userSession, |
|
| 65 | + ThemesService $themesService, |
|
| 66 | + ThemingDefaults $themingDefaults, |
|
| 67 | + BackgroundService $backgroundService) { |
|
| 68 | + parent::__construct($appName, $request); |
|
| 69 | + $this->config = $config; |
|
| 70 | + $this->userSession = $userSession; |
|
| 71 | + $this->themesService = $themesService; |
|
| 72 | + $this->themingDefaults = $themingDefaults; |
|
| 73 | + $this->backgroundService = $backgroundService; |
|
| 74 | + |
|
| 75 | + $user = $userSession->getUser(); |
|
| 76 | + if ($user !== null) { |
|
| 77 | + $this->userId = $user->getUID(); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @NoAdminRequired |
|
| 83 | + * |
|
| 84 | + * Enable theme |
|
| 85 | + * |
|
| 86 | + * @param string $themeId the theme ID |
|
| 87 | + * @return DataResponse |
|
| 88 | + * @throws OCSBadRequestException|PreConditionNotMetException |
|
| 89 | + */ |
|
| 90 | + public function enableTheme(string $themeId): DataResponse { |
|
| 91 | + $theme = $this->validateTheme($themeId); |
|
| 92 | + |
|
| 93 | + // Enable selected theme |
|
| 94 | + $this->themesService->enableTheme($theme); |
|
| 95 | + return new DataResponse(); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @NoAdminRequired |
|
| 100 | + * |
|
| 101 | + * Disable theme |
|
| 102 | + * |
|
| 103 | + * @param string $themeId the theme ID |
|
| 104 | + * @return DataResponse |
|
| 105 | + * @throws OCSBadRequestException|PreConditionNotMetException |
|
| 106 | + */ |
|
| 107 | + public function disableTheme(string $themeId): DataResponse { |
|
| 108 | + $theme = $this->validateTheme($themeId); |
|
| 109 | + |
|
| 110 | + // Enable selected theme |
|
| 111 | + $this->themesService->disableTheme($theme); |
|
| 112 | + return new DataResponse(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Validate and return the matching ITheme |
|
| 117 | + * |
|
| 118 | + * Disable theme |
|
| 119 | + * |
|
| 120 | + * @param string $themeId the theme ID |
|
| 121 | + * @return ITheme |
|
| 122 | + * @throws OCSBadRequestException|PreConditionNotMetException |
|
| 123 | + */ |
|
| 124 | + private function validateTheme(string $themeId): ITheme { |
|
| 125 | + if ($themeId === '' || !$themeId) { |
|
| 126 | + throw new OCSBadRequestException('Invalid theme id: ' . $themeId); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $themes = $this->themesService->getThemes(); |
|
| 130 | + if (!isset($themes[$themeId])) { |
|
| 131 | + throw new OCSBadRequestException('Invalid theme id: ' . $themeId); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + // If trying to toggle another theme but this is enforced |
|
| 135 | + if ($this->config->getSystemValueString('enforce_theme', '') !== '' |
|
| 136 | + && $themes[$themeId]->getType() === ITheme::TYPE_THEME) { |
|
| 137 | + throw new OCSForbiddenException('Theme switching is disabled'); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $themes[$themeId]; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @NoAdminRequired |
|
| 145 | + * @NoCSRFRequired |
|
| 146 | + */ |
|
| 147 | + public function getBackground(): Http\Response { |
|
| 148 | + $file = $this->backgroundService->getBackground(); |
|
| 149 | + if ($file !== null) { |
|
| 150 | + $response = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => $file->getMimeType()]); |
|
| 151 | + $response->cacheFor(24 * 60 * 60, false, true); |
|
| 152 | + return $response; |
|
| 153 | + } |
|
| 154 | + return new NotFoundResponse(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @NoAdminRequired |
|
| 159 | + */ |
|
| 160 | + public function deleteBackground(): JSONResponse { |
|
| 161 | + $currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0'); |
|
| 162 | + $this->backgroundService->deleteBackgroundImage(); |
|
| 163 | + return new JSONResponse([ |
|
| 164 | + 'backgroundImage' => null, |
|
| 165 | + 'backgroundColor' => $this->themingDefaults->getColorPrimary(), |
|
| 166 | + 'version' => $currentVersion, |
|
| 167 | + ]); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @NoAdminRequired |
|
| 172 | + */ |
|
| 173 | + public function setBackground(string $type = BackgroundService::BACKGROUND_DEFAULT, string $value = '', string $color = null): JSONResponse { |
|
| 174 | + $currentVersion = (int)$this->config->getUserValue($this->userId, Application::APP_ID, 'userCacheBuster', '0'); |
|
| 175 | + |
|
| 176 | + // Set color if provided |
|
| 177 | + if ($color) { |
|
| 178 | + $this->backgroundService->setColorBackground($color); |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + // Set background image if provided |
|
| 182 | + try { |
|
| 183 | + switch ($type) { |
|
| 184 | + case BackgroundService::BACKGROUND_SHIPPED: |
|
| 185 | + $this->backgroundService->setShippedBackground($value); |
|
| 186 | + break; |
|
| 187 | + case BackgroundService::BACKGROUND_CUSTOM: |
|
| 188 | + $this->backgroundService->setFileBackground($value); |
|
| 189 | + break; |
|
| 190 | + case BackgroundService::BACKGROUND_DEFAULT: |
|
| 191 | + // Delete both background and color keys |
|
| 192 | + $this->backgroundService->setDefaultBackground(); |
|
| 193 | + break; |
|
| 194 | + default: |
|
| 195 | + if (!$color) { |
|
| 196 | + return new JSONResponse(['error' => 'Invalid type provided'], Http::STATUS_BAD_REQUEST); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + } catch (\InvalidArgumentException $e) { |
|
| 200 | + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
| 201 | + } catch (\Throwable $e) { |
|
| 202 | + return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $currentVersion++; |
|
| 206 | + $this->config->setUserValue($this->userId, Application::APP_ID, 'userCacheBuster', (string)$currentVersion); |
|
| 207 | + |
|
| 208 | + return new JSONResponse([ |
|
| 209 | + 'backgroundImage' => $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT), |
|
| 210 | + 'backgroundColor' => $this->themingDefaults->getColorPrimary(), |
|
| 211 | + 'version' => $currentVersion, |
|
| 212 | + ]); |
|
| 213 | + } |
|
| 214 | 214 | } |