AdminSettingsController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  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\Controller;
  26. use OCP\AppFramework\Http\TemplateResponse;
  27. use OCP\INavigationManager;
  28. use OCP\IRequest;
  29. use OCP\Settings\IIconSection;
  30. use OCP\Settings\IManager as ISettingsManager;
  31. use OCP\Settings\ISection;
  32. use OCP\Template;
  33. /**
  34. * @package OC\Settings\Controller
  35. */
  36. class AdminSettingsController extends Controller {
  37. /** @var INavigationManager */
  38. private $navigationManager;
  39. /** @var ISettingsManager */
  40. private $settingsManager;
  41. /**
  42. * @param string $appName
  43. * @param IRequest $request
  44. * @param INavigationManager $navigationManager
  45. * @param ISettingsManager $settingsManager
  46. */
  47. public function __construct(
  48. $appName,
  49. IRequest $request,
  50. INavigationManager $navigationManager,
  51. ISettingsManager $settingsManager
  52. ) {
  53. parent::__construct($appName, $request);
  54. $this->navigationManager = $navigationManager;
  55. $this->settingsManager = $settingsManager;
  56. }
  57. /**
  58. * @param string $section
  59. * @return TemplateResponse
  60. *
  61. * @NoCSRFRequired
  62. */
  63. public function index($section) {
  64. $this->navigationManager->setActiveEntry('admin');
  65. $templateParams = [];
  66. $templateParams = array_merge($templateParams, $this->getNavigationParameters($section));
  67. $templateParams = array_merge($templateParams, $this->getSettings($section));
  68. return new TemplateResponse('settings', 'admin/frame', $templateParams);
  69. }
  70. /**
  71. * @param string $section
  72. * @return array
  73. */
  74. private function getSettings($section) {
  75. $html = '';
  76. $settings = $this->settingsManager->getAdminSettings($section);
  77. foreach ($settings as $prioritizedSettings) {
  78. foreach ($prioritizedSettings as $setting) {
  79. /** @var \OCP\Settings\ISettings $setting */
  80. $form = $setting->getForm();
  81. $html .= $form->renderAs('')->render();
  82. }
  83. }
  84. if($section === 'additional') {
  85. $html .= $this->getLegacyForms();
  86. }
  87. return ['content' => $html];
  88. }
  89. /**
  90. * @return bool|string
  91. */
  92. private function getLegacyForms() {
  93. $forms = \OC_App::getForms('admin');
  94. $forms = array_map(function ($form) {
  95. if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
  96. $sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]);
  97. $sectionName = str_replace('</h2>', '', $sectionName);
  98. $anchor = strtolower($sectionName);
  99. $anchor = str_replace(' ', '-', $anchor);
  100. return array(
  101. 'anchor' => $anchor,
  102. 'section-name' => $sectionName,
  103. 'form' => $form
  104. );
  105. }
  106. return array(
  107. 'form' => $form
  108. );
  109. }, $forms);
  110. $out = new Template('settings', 'admin/additional');
  111. $out->assign('forms', $forms);
  112. return $out->fetchPage();
  113. }
  114. /**
  115. * @param string $currentSection
  116. * @return array
  117. */
  118. private function getNavigationParameters($currentSection) {
  119. $sections = $this->settingsManager->getAdminSections();
  120. $templateParameters = [];
  121. /** @var \OC\Settings\Section[] $prioritizedSections */
  122. foreach($sections as $prioritizedSections) {
  123. foreach ($prioritizedSections as $section) {
  124. if (empty($this->settingsManager->getAdminSettings($section->getID()))) {
  125. continue;
  126. }
  127. $icon = '';
  128. if ($section instanceof IIconSection) {
  129. $icon = $section->getIcon();
  130. }
  131. $templateParameters[] = [
  132. 'anchor' => $section->getID(),
  133. 'section-name' => $section->getName(),
  134. 'active' => $section->getID() === $currentSection,
  135. 'icon' => $icon,
  136. ];
  137. }
  138. }
  139. return [
  140. 'forms' => $templateParameters
  141. ];
  142. }
  143. }