Personal.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\ThemesService;
  29. use OCA\Theming\ThemingDefaults;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\AppFramework\Services\IInitialState;
  32. use OCP\IConfig;
  33. use OCP\Settings\ISettings;
  34. use OCP\Util;
  35. class Personal implements ISettings {
  36. protected string $appName;
  37. private IConfig $config;
  38. private ThemesService $themesService;
  39. private IInitialState $initialStateService;
  40. private ThemingDefaults $themingDefaults;
  41. public function __construct(string $appName,
  42. IConfig $config,
  43. ThemesService $themesService,
  44. IInitialState $initialStateService,
  45. ThemingDefaults $themingDefaults) {
  46. $this->appName = $appName;
  47. $this->config = $config;
  48. $this->themesService = $themesService;
  49. $this->initialStateService = $initialStateService;
  50. $this->themingDefaults = $themingDefaults;
  51. }
  52. public function getForm(): TemplateResponse {
  53. $enforcedTheme = $this->config->getSystemValueString('enforce_theme', '');
  54. $themes = array_map(function($theme) {
  55. return [
  56. 'id' => $theme->getId(),
  57. 'type' => $theme->getType(),
  58. 'title' => $theme->getTitle(),
  59. 'enableLabel' => $theme->getEnableLabel(),
  60. 'description' => $theme->getDescription(),
  61. 'enabled' => $this->themesService->isEnabled($theme),
  62. ];
  63. }, $this->themesService->getThemes());
  64. if ($enforcedTheme !== '') {
  65. $themes = array_filter($themes, function($theme) use ($enforcedTheme) {
  66. return $theme['type'] !== ITheme::TYPE_THEME || $theme['id'] === $enforcedTheme;
  67. });
  68. }
  69. $this->initialStateService->provideInitialState('themes', array_values($themes));
  70. $this->initialStateService->provideInitialState('enforceTheme', $enforcedTheme);
  71. $this->initialStateService->provideInitialState('isUserThemingDisabled', $this->themingDefaults->isUserThemingDisabled());
  72. Util::addScript($this->appName, 'personal-theming');
  73. return new TemplateResponse($this->appName, 'settings-personal');
  74. }
  75. /**
  76. * @return string the section ID, e.g. 'sharing'
  77. * @since 9.1
  78. */
  79. public function getSection(): string {
  80. return $this->appName;
  81. }
  82. /**
  83. * @return int whether the form should be rather on the top or bottom of
  84. * the admin section. The forms are arranged in ascending order of the
  85. * priority values. It is required to return a value between 0 and 100.
  86. *
  87. * E.g.: 70
  88. * @since 9.1
  89. */
  90. public function getPriority(): int {
  91. return 40;
  92. }
  93. }