CommonSettingsTrait.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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\INavigationManager;
  27. use OCP\Settings\IManager as ISettingsManager;
  28. use OCP\Settings\IIconSection;
  29. use OCP\Settings\ISettings;
  30. trait CommonSettingsTrait {
  31. /** @var ISettingsManager */
  32. private $settingsManager;
  33. /** @var INavigationManager */
  34. private $navigationManager;
  35. /**
  36. * @param string $currentSection
  37. * @return array
  38. */
  39. private function getNavigationParameters($currentType, $currentSection) {
  40. $templateParameters = [
  41. 'personal' => $this->formatPersonalSections($currentType, $currentSection),
  42. 'admin' => []
  43. ];
  44. if(\OC_User::isAdminUser(\OC_User::getUser())) {
  45. $templateParameters['admin'] = $this->formatAdminSections($currentType, $currentSection);
  46. }
  47. return [
  48. 'forms' => $templateParameters
  49. ];
  50. }
  51. protected function formatSections($sections, $currentSection, $type, $currentType) {
  52. $templateParameters = [];
  53. /** @var \OCP\Settings\ISection[] $prioritizedSections */
  54. foreach($sections as $prioritizedSections) {
  55. foreach ($prioritizedSections as $section) {
  56. if($type === 'admin') {
  57. $settings = $this->settingsManager->getAdminSettings($section->getID());
  58. } else if($type === 'personal') {
  59. $settings = $this->settingsManager->getPersonalSettings($section->getID());
  60. }
  61. if (empty($settings) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
  62. continue;
  63. }
  64. $icon = '';
  65. if ($section instanceof IIconSection) {
  66. $icon = $section->getIcon();
  67. }
  68. $active = $section->getID() === $currentSection
  69. && $type === $currentType;
  70. $templateParameters[] = [
  71. 'anchor' => $section->getID(),
  72. 'section-name' => $section->getName(),
  73. 'active' => $active,
  74. 'icon' => $icon,
  75. ];
  76. }
  77. }
  78. return $templateParameters;
  79. }
  80. protected function formatPersonalSections($currentType, $currentSections) {
  81. $sections = $this->settingsManager->getPersonalSections();
  82. $templateParameters = $this->formatSections($sections, $currentSections, 'personal', $currentType);
  83. return $templateParameters;
  84. }
  85. protected function formatAdminSections($currentType, $currentSections) {
  86. $sections = $this->settingsManager->getAdminSections();
  87. $templateParameters = $this->formatSections($sections, $currentSections, 'admin', $currentType);
  88. return $templateParameters;
  89. }
  90. /**
  91. * @param ISettings[] $settings
  92. * @return array
  93. */
  94. private function formatSettings($settings) {
  95. $html = '';
  96. foreach ($settings as $prioritizedSettings) {
  97. foreach ($prioritizedSettings as $setting) {
  98. /** @var \OCP\Settings\ISettings $setting */
  99. $form = $setting->getForm();
  100. $html .= $form->renderAs('')->render();
  101. }
  102. }
  103. return ['content' => $html];
  104. }
  105. private function getIndexResponse($type, $section) {
  106. $this->navigationManager->setActiveEntry('settings');
  107. $templateParams = [];
  108. $templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
  109. $templateParams = array_merge($templateParams, $this->getSettings($section));
  110. return new TemplateResponse('settings', 'settings/frame', $templateParams);
  111. }
  112. abstract protected function getSettings($section);
  113. }