Completed
Push — master ( 94010e...d161d4 )
by Morris
17:23
created

CommonSettingsTrait::formatAdminSections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Robin Appelman <[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\Http\TemplateResponse;
28
use OCP\INavigationManager;
29
use OCP\Settings\IManager as ISettingsManager;
30
use OCP\Settings\IIconSection;
31
use OCP\Settings\ISettings;
32
33
trait CommonSettingsTrait  {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after trait name; 2 found
Loading history...
34
	/** @var ISettingsManager */
35
	private $settingsManager;
36
37
	/** @var INavigationManager */
38
	private $navigationManager;
39
40
	/**
41
	 * @param string $currentSection
42
	 * @return array
43
	 */
44
	private function getNavigationParameters($currentType, $currentSection) {
45
		$templateParameters = [
46
			'personal' => $this->formatPersonalSections($currentType, $currentSection),
47
			'admin' => []
48
		];
49
50
		if(\OC_User::isAdminUser(\OC_User::getUser())) {
51
			$templateParameters['admin'] = $this->formatAdminSections($currentType, $currentSection);
52
		}
53
54
		return [
55
			'forms' => $templateParameters
56
		];
57
	}
58
59
	protected function formatSections($sections, $currentSection, $type, $currentType) {
60
		$templateParameters = [];
61
		/** @var \OCP\Settings\ISection[] $prioritizedSections */
62
		foreach($sections as $prioritizedSections) {
63
			foreach ($prioritizedSections as $section) {
64
				if($type === 'admin') {
65
					$settings = $this->settingsManager->getAdminSettings($section->getID());
66
				} else if($type === 'personal') {
67
					$settings = $this->settingsManager->getPersonalSettings($section->getID());
68
				}
69
				if (empty($settings) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
70
					continue;
71
				}
72
73
				$icon = '';
74
				if ($section instanceof IIconSection) {
75
					$icon = $section->getIcon();
76
				}
77
78
				$active = $section->getID() === $currentSection
79
					&& $type === $currentType;
80
81
				$templateParameters[] = [
82
					'anchor'       => $section->getID(),
83
					'section-name' => $section->getName(),
84
					'active'       => $active,
85
					'icon'         => $icon,
86
				];
87
			}
88
		}
89
		return $templateParameters;
90
	}
91
92
	protected function formatPersonalSections($currentType, $currentSections) {
93
		$sections = $this->settingsManager->getPersonalSections();
94
		$templateParameters = $this->formatSections($sections, $currentSections, 'personal', $currentType);
95
96
		return $templateParameters;
97
	}
98
99
	protected function formatAdminSections($currentType, $currentSections) {
100
		$sections = $this->settingsManager->getAdminSections();
101
		$templateParameters = $this->formatSections($sections, $currentSections, 'admin', $currentType);
102
103
		return $templateParameters;
104
	}
105
106
	/**
107
	 * @param ISettings[] $settings
108
	 * @return array
109
	 */
110
	private function formatSettings($settings) {
111
		$html = '';
112
		foreach ($settings as $prioritizedSettings) {
113
			foreach ($prioritizedSettings as $setting) {
0 ignored issues
show
Bug introduced by
The expression $prioritizedSettings of type object<OCP\Settings\ISettings> is not traversable.
Loading history...
114
				/** @var \OCP\Settings\ISettings $setting */
115
				$form = $setting->getForm();
116
				$html .= $form->renderAs('')->render();
117
			}
118
		}
119
		return ['content' => $html];
120
	}
121
122
	private function getIndexResponse($type, $section) {
123
		$this->navigationManager->setActiveEntry('settings');
124
		$templateParams = [];
125
		$templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
126
		$templateParams = array_merge($templateParams, $this->getSettings($section));
127
128
		return new TemplateResponse('settings', 'settings/frame', $templateParams);
129
	}
130
131
	abstract protected function getSettings($section);
132
}
133