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 4.8KB

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