1
0

Capabilities.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming;
  7. use OCA\Theming\AppInfo\Application;
  8. use OCA\Theming\Service\BackgroundService;
  9. use OCP\Capabilities\IPublicCapability;
  10. use OCP\IConfig;
  11. use OCP\IURLGenerator;
  12. use OCP\IUser;
  13. use OCP\IUserSession;
  14. /**
  15. * Class Capabilities
  16. *
  17. * @package OCA\Theming
  18. */
  19. class Capabilities implements IPublicCapability {
  20. /**
  21. * @param ThemingDefaults $theming
  22. * @param Util $util
  23. * @param IURLGenerator $url
  24. * @param IConfig $config
  25. */
  26. public function __construct(
  27. protected ThemingDefaults $theming,
  28. protected Util $util,
  29. protected IURLGenerator $url,
  30. protected IConfig $config,
  31. protected IUserSession $userSession,
  32. ) {
  33. }
  34. /**
  35. * Return this classes capabilities
  36. *
  37. * @return array{
  38. * theming: array{
  39. * name: string,
  40. * url: string,
  41. * slogan: string,
  42. * color: string,
  43. * color-text: string,
  44. * color-element: string,
  45. * color-element-bright: string,
  46. * color-element-dark: string,
  47. * logo: string,
  48. * background: string,
  49. * background-text: string,
  50. * background-plain: bool,
  51. * background-default: bool,
  52. * logoheader: string,
  53. * favicon: string,
  54. * },
  55. * }
  56. */
  57. public function getCapabilities() {
  58. $color = $this->theming->getDefaultColorPrimary();
  59. $colorText = $this->util->invertTextColor($color) ? '#000000' : '#ffffff';
  60. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  61. $backgroundColor = $this->theming->getColorBackground();
  62. $backgroundText = $this->theming->getTextColorBackground();
  63. $backgroundPlain = $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $backgroundColor !== BackgroundService::DEFAULT_COLOR);
  64. $background = $backgroundPlain ? $backgroundColor : $this->url->getAbsoluteURL($this->theming->getBackground());
  65. $user = $this->userSession->getUser();
  66. if ($user instanceof IUser) {
  67. /**
  68. * Mimics the logic of generateUserBackgroundVariables() that generates the CSS variables.
  69. * Also needs to be updated if the logic changes.
  70. * @see \OCA\Theming\Themes\CommonThemeTrait::generateUserBackgroundVariables()
  71. */
  72. $color = $this->theming->getColorPrimary();
  73. $colorText = $this->theming->getTextColorPrimary();
  74. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  75. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  76. $backgroundPlain = false;
  77. $background = $this->url->linkToRouteAbsolute('theming.userTheme.getBackground');
  78. } elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  79. $backgroundPlain = false;
  80. $background = $this->url->linkTo(Application::APP_ID, "img/background/$backgroundImage");
  81. } elseif ($backgroundImage !== BackgroundService::BACKGROUND_DEFAULT) {
  82. $backgroundPlain = true;
  83. $background = $backgroundColor;
  84. }
  85. }
  86. return [
  87. 'theming' => [
  88. 'name' => $this->theming->getName(),
  89. 'url' => $this->theming->getBaseUrl(),
  90. 'slogan' => $this->theming->getSlogan(),
  91. 'color' => $color,
  92. 'color-text' => $colorText,
  93. 'color-element' => $this->util->elementColor($color),
  94. 'color-element-bright' => $this->util->elementColor($color),
  95. 'color-element-dark' => $this->util->elementColor($color, false),
  96. 'logo' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  97. 'background' => $background,
  98. 'background-text' => $backgroundText,
  99. 'background-plain' => $backgroundPlain,
  100. 'background-default' => !$this->util->isBackgroundThemed(),
  101. 'logoheader' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  102. 'favicon' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  103. ],
  104. ];
  105. }
  106. }