You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommonSettingsTrait.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\Settings\Controller;
  30. use OCA\Settings\AppInfo\Application;
  31. use OCP\AppFramework\Http\TemplateResponse;
  32. use OCP\AppFramework\Services\IInitialState;
  33. use OCP\Group\ISubAdmin;
  34. use OCP\IGroupManager;
  35. use OCP\INavigationManager;
  36. use OCP\IUserSession;
  37. use OCP\Settings\IDeclarativeManager;
  38. use OCP\Settings\IDeclarativeSettingsForm;
  39. use OCP\Settings\IIconSection;
  40. use OCP\Settings\IManager as ISettingsManager;
  41. use OCP\Settings\ISettings;
  42. use OCP\Util;
  43. /**
  44. * @psalm-import-type DeclarativeSettingsFormField from IDeclarativeSettingsForm
  45. */
  46. trait CommonSettingsTrait {
  47. /** @var ISettingsManager */
  48. private $settingsManager;
  49. /** @var INavigationManager */
  50. private $navigationManager;
  51. /** @var IUserSession */
  52. private $userSession;
  53. /** @var IGroupManager */
  54. private $groupManager;
  55. /** @var ISubAdmin */
  56. private $subAdmin;
  57. private IDeclarativeManager $declarativeSettingsManager;
  58. /** @var IInitialState */
  59. private $initialState;
  60. /**
  61. * @return array{forms: array{personal: array, admin: array}}
  62. */
  63. private function getNavigationParameters(string $currentType, string $currentSection): array {
  64. return [
  65. 'forms' => [
  66. 'personal' => $this->formatPersonalSections($currentType, $currentSection),
  67. 'admin' => $this->formatAdminSections($currentType, $currentSection),
  68. ],
  69. ];
  70. }
  71. /**
  72. * @param IIconSection[][] $sections
  73. * @psalm-param 'admin'|'personal' $type
  74. * @return list<array{anchor: string, section-name: string, active: bool, icon: string}>
  75. */
  76. protected function formatSections(array $sections, string $currentSection, string $type, string $currentType): array {
  77. $templateParameters = [];
  78. foreach ($sections as $prioritizedSections) {
  79. foreach ($prioritizedSections as $section) {
  80. if ($type === 'admin') {
  81. $settings = $this->settingsManager->getAllowedAdminSettings($section->getID(), $this->userSession->getUser());
  82. } elseif ($type === 'personal') {
  83. $settings = $this->settingsManager->getPersonalSettings($section->getID());
  84. }
  85. /** @psalm-suppress PossiblyNullArgument */
  86. $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section->getID());
  87. if (empty($settings) && empty($declarativeFormIDs) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
  88. continue;
  89. }
  90. $icon = $section->getIcon();
  91. $active = $section->getID() === $currentSection
  92. && $type === $currentType;
  93. $templateParameters[] = [
  94. 'anchor' => $section->getID(),
  95. 'section-name' => $section->getName(),
  96. 'active' => $active,
  97. 'icon' => $icon,
  98. ];
  99. }
  100. }
  101. return $templateParameters;
  102. }
  103. protected function formatPersonalSections(string $currentType, string $currentSection): array {
  104. $sections = $this->settingsManager->getPersonalSections();
  105. return $this->formatSections($sections, $currentSection, 'personal', $currentType);
  106. }
  107. protected function formatAdminSections(string $currentType, string $currentSection): array {
  108. $sections = $this->settingsManager->getAdminSections();
  109. return $this->formatSections($sections, $currentSection, 'admin', $currentType);
  110. }
  111. /**
  112. * @param array<int, list<\OCP\Settings\ISettings>> $settings
  113. * @return array{content: string}
  114. */
  115. private function formatSettings(array $settings): array {
  116. $html = '';
  117. foreach ($settings as $prioritizedSettings) {
  118. foreach ($prioritizedSettings as $setting) {
  119. /** @var ISettings $setting */
  120. $form = $setting->getForm();
  121. $html .= $form->renderAs('')->render();
  122. }
  123. }
  124. return ['content' => $html];
  125. }
  126. /**
  127. * @psalm-param 'admin'|'personal' $type
  128. */
  129. private function getIndexResponse(string $type, string $section): TemplateResponse {
  130. if ($type === 'personal') {
  131. if ($section === 'theming') {
  132. $this->navigationManager->setActiveEntry('accessibility_settings');
  133. } else {
  134. $this->navigationManager->setActiveEntry('settings');
  135. }
  136. } elseif ($type === 'admin') {
  137. $this->navigationManager->setActiveEntry('admin_settings');
  138. }
  139. $this->declarativeSettingsManager->loadSchemas();
  140. $templateParams = [];
  141. $templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
  142. $templateParams = array_merge($templateParams, $this->getSettings($section));
  143. /** @psalm-suppress PossiblyNullArgument */
  144. $declarativeFormIDs = $this->declarativeSettingsManager->getFormIDs($this->userSession->getUser(), $type, $section);
  145. if (!empty($declarativeFormIDs)) {
  146. foreach ($declarativeFormIDs as $app => $ids) {
  147. /** @psalm-suppress PossiblyUndefinedArrayOffset */
  148. $templateParams['content'] .= join(array_map(fn (string $id) => '<div id="' . $app . '_' . $id . '"></div>', $ids));
  149. }
  150. Util::addScript(Application::APP_ID, 'declarative-settings-forms');
  151. /** @psalm-suppress PossiblyNullArgument */
  152. $this->initialState->provideInitialState('declarative-settings-forms', $this->declarativeSettingsManager->getFormsWithValues($this->userSession->getUser(), $type, $section));
  153. }
  154. $activeSection = $this->settingsManager->getSection($type, $section);
  155. if ($activeSection) {
  156. $templateParams['pageTitle'] = $activeSection->getName();
  157. $templateParams['activeSectionId'] = $activeSection->getID();
  158. $templateParams['activeSectionType'] = $type;
  159. }
  160. return new TemplateResponse('settings', 'settings/frame', $templateParams);
  161. }
  162. abstract protected function getSettings($section);
  163. }