CommonSettingsTrait.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Settings\Controller;
  25. use OCP\AppFramework\Http\TemplateResponse;
  26. use OCP\Group\ISubAdmin;
  27. use OCP\IGroupManager;
  28. use OCP\INavigationManager;
  29. use OCP\IUser;
  30. use OCP\IUserSession;
  31. use OCP\Settings\IManager as ISettingsManager;
  32. use OCP\Settings\IIconSection;
  33. use OCP\Settings\ISettings;
  34. trait CommonSettingsTrait {
  35. /** @var ISettingsManager */
  36. private $settingsManager;
  37. /** @var INavigationManager */
  38. private $navigationManager;
  39. /** @var IUserSession */
  40. private $userSession;
  41. /** @var IGroupManager */
  42. private $groupManager;
  43. /** @var ISubAdmin */
  44. private $subAdmin;
  45. /**
  46. * @param string $currentSection
  47. * @return array
  48. */
  49. private function getNavigationParameters($currentType, $currentSection) {
  50. $templateParameters = [
  51. 'personal' => $this->formatPersonalSections($currentType, $currentSection),
  52. 'admin' => []
  53. ];
  54. /** @var IUser $user */
  55. $user = $this->userSession->getUser();
  56. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  57. $isSubAdmin = $this->subAdmin->isSubAdmin($user);
  58. if ($isAdmin || $isSubAdmin) {
  59. $templateParameters['admin'] = $this->formatAdminSections(
  60. $currentType,
  61. $currentSection,
  62. !$isAdmin && $isSubAdmin
  63. );
  64. }
  65. return [
  66. 'forms' => $templateParameters
  67. ];
  68. }
  69. protected function formatSections($sections, $currentSection, $type, $currentType, bool $subAdminOnly = false) {
  70. $templateParameters = [];
  71. /** @var \OCP\Settings\ISection[] $prioritizedSections */
  72. foreach($sections as $prioritizedSections) {
  73. foreach ($prioritizedSections as $section) {
  74. if($type === 'admin') {
  75. $settings = $this->settingsManager->getAdminSettings($section->getID(), $subAdminOnly);
  76. } else if($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 = '';
  83. if ($section instanceof IIconSection) {
  84. $icon = $section->getIcon();
  85. }
  86. $active = $section->getID() === $currentSection
  87. && $type === $currentType;
  88. $templateParameters[] = [
  89. 'anchor' => $section->getID(),
  90. 'section-name' => $section->getName(),
  91. 'active' => $active,
  92. 'icon' => $icon,
  93. ];
  94. }
  95. }
  96. return $templateParameters;
  97. }
  98. protected function formatPersonalSections($currentType, $currentSections) {
  99. $sections = $this->settingsManager->getPersonalSections();
  100. $templateParameters = $this->formatSections($sections, $currentSections, 'personal', $currentType);
  101. return $templateParameters;
  102. }
  103. protected function formatAdminSections($currentType, $currentSections, bool $subAdminOnly) {
  104. $sections = $this->settingsManager->getAdminSections();
  105. $templateParameters = $this->formatSections($sections, $currentSections, 'admin', $currentType, $subAdminOnly);
  106. return $templateParameters;
  107. }
  108. /**
  109. * @param ISettings[] $settings
  110. * @return array
  111. */
  112. private function formatSettings($settings) {
  113. $html = '';
  114. foreach ($settings as $prioritizedSettings) {
  115. foreach ($prioritizedSettings as $setting) {
  116. /** @var \OCP\Settings\ISettings $setting */
  117. $form = $setting->getForm();
  118. $html .= $form->renderAs('')->render();
  119. }
  120. }
  121. return ['content' => $html];
  122. }
  123. private function getIndexResponse($type, $section) {
  124. $this->navigationManager->setActiveEntry('settings');
  125. $templateParams = [];
  126. $templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
  127. $templateParams = array_merge($templateParams, $this->getSettings($section));
  128. return new TemplateResponse('settings', 'settings/frame', $templateParams);
  129. }
  130. abstract protected function getSettings($section);
  131. }