Completed
Push — stable10 ( 737591...a2942c )
by Lukas
09:58 queued 09:42
created

AdminSettingsController::getSettings()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
cc 4
eloc 10
c 5
b 0
f 1
nc 6
nop 1
dl 0
loc 15
rs 9.2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
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
25
namespace OC\Settings\Controller;
26
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\TemplateResponse;
29
use OCP\INavigationManager;
30
use OCP\IRequest;
31
use OCP\Settings\IManager as ISettingsManager;
32
use OCP\Template;
33
34
/**
35
 * @package OC\Settings\Controller
36
 */
37
class AdminSettingsController extends Controller {
38
	/** @var INavigationManager */
39
	private $navigationManager;
40
	/** @var ISettingsManager */
41
	private $settingsManager;
42
43
	/**
44
	 * @param string $appName
45
	 * @param IRequest $request
46
	 * @param INavigationManager $navigationManager
47
	 * @param ISettingsManager $settingsManager
48
	 */
49
	public function __construct(
50
		$appName,
51
		IRequest $request,
52
		INavigationManager $navigationManager,
53
		ISettingsManager $settingsManager
54
	) {
55
		parent::__construct($appName, $request);
56
		$this->navigationManager = $navigationManager;
57
		$this->settingsManager = $settingsManager;
58
	}
59
60
	/**
61
	 * @param string $section
62
	 * @return TemplateResponse
63
	 *
64
	 * @NoCSRFRequired
65
	 */
66
	public function index($section) {
67
		$this->navigationManager->setActiveEntry('admin');
68
69
		$templateParams = [];
70
		$templateParams = array_merge($templateParams, $this->getNavigationParameters($section));
71
		$templateParams = array_merge($templateParams, $this->getSettings($section));
72
73
		return new TemplateResponse('settings', 'admin/frame', $templateParams);
74
	}
75
76
	/**
77
	 * @param string $section
78
	 * @return array
79
	 */
80
	private function getSettings($section) {
81
		$html = '';
82
		$settings = $this->settingsManager->getAdminSettings($section);
83
		foreach ($settings as $prioritizedSettings) {
84
			foreach ($prioritizedSettings as $setting) {
85
				/** @var \OCP\Settings\ISettings $setting */
86
				$form = $setting->getForm();
87
				$html .= $form->renderAs('')->render();
88
			}
89
		}
90
		if($section === 'additional') {
91
			$html .= $this->getLegacyForms();
92
		}
93
		return ['content' => $html];
94
	}
95
96
	/**
97
	 * @return bool|string
98
	 */
99
	private function getLegacyForms() {
100
		$forms = \OC_App::getForms('admin');
101
102 View Code Duplication
		$forms = array_map(function ($form) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
			if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
104
				$sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]);
105
				$sectionName = str_replace('</h2>', '', $sectionName);
106
				$anchor = strtolower($sectionName);
107
				$anchor = str_replace(' ', '-', $anchor);
108
109
				return array(
110
					'anchor' => $anchor,
111
					'section-name' => $sectionName,
112
					'form' => $form
113
				);
114
			}
115
			return array(
116
				'form' => $form
117
			);
118
		}, $forms);
119
120
		$out = new Template('settings', 'admin/additional');
121
		$out->assign('forms', $forms);
122
123
		return $out->fetchPage();
124
	}
125
126
	/**
127
	 * @param string $currentSection
128
	 * @return array
129
	 */
130
	private function getNavigationParameters($currentSection) {
131
		$sections = $this->settingsManager->getAdminSections();
132
		$templateParameters = [];
133
		/** @var \OC\Settings\Section[] $prioritizedSections */
134
		foreach($sections as $prioritizedSections) {
135
			foreach ($prioritizedSections as $section) {
136
				$templateParameters[] = [
137
					'anchor'       => $section->getID(),
138
					'section-name' => $section->getName(),
139
					'active'       => $section->getID() === $currentSection,
140
				];
141
			}
142
		}
143
144
		return [
145
			'forms' => $templateParameters
146
		];
147
	}
148
}
149