1
0

CommonSettingsTrait.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\Group\ISubAdmin;
  32. use OCP\IGroupManager;
  33. use OCP\INavigationManager;
  34. use OCP\IUserSession;
  35. use OCP\Settings\IIconSection;
  36. use OCP\Settings\IManager as ISettingsManager;
  37. use OCP\Settings\ISettings;
  38. trait CommonSettingsTrait {
  39. /** @var ISettingsManager */
  40. private $settingsManager;
  41. /** @var INavigationManager */
  42. private $navigationManager;
  43. /** @var IUserSession */
  44. private $userSession;
  45. /** @var IGroupManager */
  46. private $groupManager;
  47. /** @var ISubAdmin */
  48. private $subAdmin;
  49. /**
  50. * @return array{forms: array{personal: array, admin: array}}
  51. */
  52. private function getNavigationParameters(string $currentType, string $currentSection): array {
  53. $templateParameters = [
  54. 'personal' => $this->formatPersonalSections($currentType, $currentSection),
  55. 'admin' => []
  56. ];
  57. $templateParameters['admin'] = $this->formatAdminSections(
  58. $currentType,
  59. $currentSection
  60. );
  61. return [
  62. 'forms' => $templateParameters
  63. ];
  64. }
  65. /**
  66. * @param IIconSection[][] $sections
  67. * @psam-param 'admin'|'personal' $type
  68. * @return list<array{anchor: string, section-name: string, active: bool, icon: string}>
  69. */
  70. protected function formatSections(array $sections, string $currentSection, string $type, string $currentType): array {
  71. $templateParameters = [];
  72. foreach ($sections as $prioritizedSections) {
  73. foreach ($prioritizedSections as $section) {
  74. if ($type === 'admin') {
  75. $settings = $this->settingsManager->getAllowedAdminSettings($section->getID(), $this->userSession->getUser());
  76. } elseif ($type === 'personal') {
  77. $settings = $this->settingsManager->getPersonalSettings($section->getID());
  78. }
  79. if (empty($settings) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
  80. continue;
  81. }
  82. $icon = $section->getIcon();
  83. $active = $section->getID() === $currentSection
  84. && $type === $currentType;
  85. $templateParameters[] = [
  86. 'anchor' => $section->getID(),
  87. 'section-name' => $section->getName(),
  88. 'active' => $active,
  89. 'icon' => $icon,
  90. ];
  91. }
  92. }
  93. return $templateParameters;
  94. }
  95. protected function formatPersonalSections(string $currentType, string $currentSections): array {
  96. $sections = $this->settingsManager->getPersonalSections();
  97. return $this->formatSections($sections, $currentSections, 'personal', $currentType);
  98. }
  99. protected function formatAdminSections(string $currentType, string $currentSections): array {
  100. $sections = $this->settingsManager->getAdminSections();
  101. return $this->formatSections($sections, $currentSections, 'admin', $currentType);
  102. }
  103. /**
  104. * @param array<int, list<\OCP\Settings\ISettings>> $settings
  105. * @return array{content: string}
  106. */
  107. private function formatSettings(array $settings): array {
  108. $html = '';
  109. foreach ($settings as $prioritizedSettings) {
  110. foreach ($prioritizedSettings as $setting) {
  111. /** @var ISettings $setting */
  112. $form = $setting->getForm();
  113. $html .= $form->renderAs('')->render();
  114. }
  115. }
  116. return ['content' => $html];
  117. }
  118. private function getIndexResponse(string $type, string $section): TemplateResponse {
  119. if ($type === 'personal') {
  120. if ($section === 'theming') {
  121. $this->navigationManager->setActiveEntry('accessibility_settings');
  122. } else {
  123. $this->navigationManager->setActiveEntry('settings');
  124. }
  125. } elseif ($type === 'admin') {
  126. $this->navigationManager->setActiveEntry('admin_settings');
  127. }
  128. $templateParams = [];
  129. $templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
  130. $templateParams = array_merge($templateParams, $this->getSettings($section));
  131. $activeSection = $this->settingsManager->getSection($type, $section);
  132. if ($activeSection) {
  133. $templateParams['pageTitle'] = $activeSection->getName();
  134. $templateParams['activeSectionId'] = $activeSection->getID();
  135. }
  136. return new TemplateResponse('settings', 'settings/frame', $templateParams);
  137. }
  138. abstract protected function getSettings($section);
  139. }