Personal.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 John Molakvoæ <skjnldsv@protonmail.com>
  4. * @copyright Copyright (c) 2019 Janis Köhr <janiskoehr@icloud.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Theming\Settings;
  27. use OCA\Theming\ITheme;
  28. use OCA\Theming\Service\BackgroundService;
  29. use OCA\Theming\Service\ThemesService;
  30. use OCA\Theming\ThemingDefaults;
  31. use OCP\App\IAppManager;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\AppFramework\Services\IInitialState;
  34. use OCP\IConfig;
  35. use OCP\Settings\ISettings;
  36. use OCP\Util;
  37. class Personal implements ISettings {
  38. public function __construct(
  39. protected string $appName,
  40. private string $userId,
  41. private IConfig $config,
  42. private ThemesService $themesService,
  43. private IInitialState $initialStateService,
  44. private ThemingDefaults $themingDefaults,
  45. private IAppManager $appManager,
  46. ) {
  47. }
  48. public function getForm(): TemplateResponse {
  49. $enforcedTheme = $this->config->getSystemValueString('enforce_theme', '');
  50. $themes = array_map(function ($theme) {
  51. return [
  52. 'id' => $theme->getId(),
  53. 'type' => $theme->getType(),
  54. 'title' => $theme->getTitle(),
  55. 'enableLabel' => $theme->getEnableLabel(),
  56. 'description' => $theme->getDescription(),
  57. 'enabled' => $this->themesService->isEnabled($theme),
  58. ];
  59. }, $this->themesService->getThemes());
  60. if ($enforcedTheme !== '') {
  61. $themes = array_filter($themes, function ($theme) use ($enforcedTheme) {
  62. return $theme['type'] !== ITheme::TYPE_THEME || $theme['id'] === $enforcedTheme;
  63. });
  64. }
  65. // Get the default app enforced by admin
  66. $forcedDefaultApp = $this->appManager->getDefaultAppForUser(null, false);
  67. /** List of all shipped backgrounds */
  68. $this->initialStateService->provideInitialState('shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS);
  69. /**
  70. * Admin theming
  71. */
  72. $this->initialStateService->provideInitialState('themingDefaults', [
  73. /** URL of admin configured background image */
  74. 'backgroundImage' => $this->themingDefaults->getBackground(),
  75. /** `backgroundColor` if disabled, mime type if defined and empty by default */
  76. 'backgroundMime' => $this->config->getAppValue('theming', 'backgroundMime', ''),
  77. /** Admin configured background color */
  78. 'backgroundColor' => $this->themingDefaults->getDefaultColorBackground(),
  79. /** Admin configured primary color */
  80. 'primaryColor' => $this->themingDefaults->getDefaultColorPrimary(),
  81. /** Nextcloud default background image */
  82. 'defaultShippedBackground' => BackgroundService::DEFAULT_BACKGROUND_IMAGE,
  83. ]);
  84. $this->initialStateService->provideInitialState('userBackgroundImage', $this->config->getUserValue($this->userId, 'theming', 'background_image', BackgroundService::BACKGROUND_DEFAULT));
  85. $this->initialStateService->provideInitialState('themes', array_values($themes));
  86. $this->initialStateService->provideInitialState('enforceTheme', $enforcedTheme);
  87. $this->initialStateService->provideInitialState('isUserThemingDisabled', $this->themingDefaults->isUserThemingDisabled());
  88. $this->initialStateService->provideInitialState('navigationBar', [
  89. 'userAppOrder' => json_decode($this->config->getUserValue($this->userId, 'core', 'apporder', '[]'), true, flags:JSON_THROW_ON_ERROR),
  90. 'enforcedDefaultApp' => $forcedDefaultApp
  91. ]);
  92. Util::addScript($this->appName, 'personal-theming');
  93. return new TemplateResponse($this->appName, 'settings-personal');
  94. }
  95. /**
  96. * @return string the section ID, e.g. 'sharing'
  97. * @since 9.1
  98. */
  99. public function getSection(): string {
  100. return $this->appName;
  101. }
  102. /**
  103. * @return int whether the form should be rather on the top or bottom of
  104. * the admin section. The forms are arranged in ascending order of the
  105. * priority values. It is required to return a value between 0 and 100.
  106. *
  107. * E.g.: 70
  108. * @since 9.1
  109. */
  110. public function getPriority(): int {
  111. return 40;
  112. }
  113. }