Capabilities.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /** @var ThemingDefaults */
  21. protected $theming;
  22. /** @var Util */
  23. protected $util;
  24. /** @var IURLGenerator */
  25. protected $url;
  26. /** @var IConfig */
  27. protected $config;
  28. protected IUserSession $userSession;
  29. /**
  30. * @param ThemingDefaults $theming
  31. * @param Util $util
  32. * @param IURLGenerator $url
  33. * @param IConfig $config
  34. */
  35. public function __construct(ThemingDefaults $theming, Util $util, IURLGenerator $url, IConfig $config, IUserSession $userSession) {
  36. $this->theming = $theming;
  37. $this->util = $util;
  38. $this->url = $url;
  39. $this->config = $config;
  40. $this->userSession = $userSession;
  41. }
  42. /**
  43. * Return this classes capabilities
  44. *
  45. * @return array{
  46. * theming: array{
  47. * name: string,
  48. * url: string,
  49. * slogan: string,
  50. * color: string,
  51. * color-text: string,
  52. * color-element: string,
  53. * color-element-bright: string,
  54. * color-element-dark: string,
  55. * logo: string,
  56. * background: string,
  57. * background-text: string,
  58. * background-plain: bool,
  59. * background-default: bool,
  60. * logoheader: string,
  61. * favicon: string,
  62. * },
  63. * }
  64. */
  65. public function getCapabilities() {
  66. $color = $this->theming->getDefaultColorPrimary();
  67. $colorText = $this->util->invertTextColor($color) ? '#000000' : '#ffffff';
  68. $backgroundLogo = $this->config->getAppValue('theming', 'backgroundMime', '');
  69. $backgroundColor = $this->theming->getColorBackground();
  70. $backgroundText = $this->theming->getTextColorBackground();
  71. $backgroundPlain = $backgroundLogo === 'backgroundColor' || ($backgroundLogo === '' && $backgroundColor !== BackgroundService::DEFAULT_COLOR);
  72. $background = $backgroundPlain ? $backgroundColor : $this->url->getAbsoluteURL($this->theming->getBackground());
  73. $user = $this->userSession->getUser();
  74. if ($user instanceof IUser) {
  75. /**
  76. * Mimics the logic of generateUserBackgroundVariables() that generates the CSS variables.
  77. * Also needs to be updated if the logic changes.
  78. * @see \OCA\Theming\Themes\CommonThemeTrait::generateUserBackgroundVariables()
  79. */
  80. $color = $this->theming->getColorPrimary();
  81. $colorText = $this->theming->getTextColorPrimary();
  82. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  83. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  84. $backgroundPlain = false;
  85. $background = $this->url->linkToRouteAbsolute('theming.userTheme.getBackground');
  86. } elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  87. $backgroundPlain = false;
  88. $background = $this->url->linkTo(Application::APP_ID, "img/background/$backgroundImage");
  89. } elseif ($backgroundImage !== BackgroundService::BACKGROUND_DEFAULT) {
  90. $backgroundPlain = true;
  91. $background = $backgroundColor;
  92. }
  93. }
  94. return [
  95. 'theming' => [
  96. 'name' => $this->theming->getName(),
  97. 'url' => $this->theming->getBaseUrl(),
  98. 'slogan' => $this->theming->getSlogan(),
  99. 'color' => $color,
  100. 'color-text' => $colorText,
  101. 'color-element' => $this->util->elementColor($color),
  102. 'color-element-bright' => $this->util->elementColor($color),
  103. 'color-element-dark' => $this->util->elementColor($color, false),
  104. 'logo' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  105. 'background' => $background,
  106. 'background-text' => $backgroundText,
  107. 'background-plain' => $backgroundPlain,
  108. 'background-default' => !$this->util->isBackgroundThemed(),
  109. 'logoheader' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  110. 'favicon' => $this->url->getAbsoluteURL($this->theming->getLogo()),
  111. ],
  112. ];
  113. }
  114. }