Passed
Push — master ( 17cdcf...aa80aa )
by John
09:42 queued 11s
created

CommonSettingsTrait   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getNavigationParameters() 0 20 4
B formatSections() 0 31 10
A formatAdminSections() 0 5 1
A getIndexResponse() 0 7 1
A formatSettings() 0 10 3
A formatPersonalSections() 0 5 1
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 OCA\Settings\Controller;
26
27
use OCP\AppFramework\Http\TemplateResponse;
28
use OCP\Group\ISubAdmin;
29
use OCP\IGroupManager;
30
use OCP\INavigationManager;
31
use OCP\IUser;
32
use OCP\IUserSession;
33
use OCP\Settings\IManager as ISettingsManager;
34
use OCP\Settings\IIconSection;
35
use OCP\Settings\ISettings;
36
37
trait CommonSettingsTrait  {
38
39
	/** @var ISettingsManager */
40
	private $settingsManager;
41
42
	/** @var INavigationManager */
43
	private $navigationManager;
44
45
	/** @var IUserSession */
46
	private $userSession;
47
48
	/** @var IGroupManager */
49
	private $groupManager;
50
51
	/** @var ISubAdmin */
52
	private $subAdmin;
53
54
	/**
55
	 * @param string $currentSection
56
	 * @return array
57
	 */
58
	private function getNavigationParameters($currentType, $currentSection) {
59
		$templateParameters = [
60
			'personal' => $this->formatPersonalSections($currentType, $currentSection),
61
			'admin' => []
62
		];
63
64
		/** @var IUser $user */
65
		$user = $this->userSession->getUser();
66
		$isAdmin = $this->groupManager->isAdmin($user->getUID());
67
		$isSubAdmin = $this->subAdmin->isSubAdmin($user);
68
		if ($isAdmin || $isSubAdmin) {
69
			$templateParameters['admin'] = $this->formatAdminSections(
70
				$currentType,
71
				$currentSection,
72
				!$isAdmin && $isSubAdmin
73
			);
74
		}
75
76
		return [
77
			'forms' => $templateParameters
78
		];
79
	}
80
81
	protected function formatSections($sections, $currentSection, $type, $currentType, bool $subAdminOnly = false) {
82
		$templateParameters = [];
83
		/** @var \OCP\Settings\ISection[] $prioritizedSections */
84
		foreach($sections as $prioritizedSections) {
85
			foreach ($prioritizedSections as $section) {
86
				if($type === 'admin') {
87
					$settings = $this->settingsManager->getAdminSettings($section->getID(), $subAdminOnly);
88
				} else if($type === 'personal') {
89
					$settings = $this->settingsManager->getPersonalSettings($section->getID());
90
				}
91
				if (empty($settings) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
92
					continue;
93
				}
94
95
				$icon = '';
96
				if ($section instanceof IIconSection) {
97
					$icon = $section->getIcon();
98
				}
99
100
				$active = $section->getID() === $currentSection
101
					&& $type === $currentType;
102
103
				$templateParameters[] = [
104
					'anchor'       => $section->getID(),
105
					'section-name' => $section->getName(),
106
					'active'       => $active,
107
					'icon'         => $icon,
108
				];
109
			}
110
		}
111
		return $templateParameters;
112
	}
113
114
	protected function formatPersonalSections($currentType, $currentSections) {
115
		$sections = $this->settingsManager->getPersonalSections();
116
		$templateParameters = $this->formatSections($sections, $currentSections, 'personal', $currentType);
117
118
		return $templateParameters;
119
	}
120
121
	protected function formatAdminSections($currentType, $currentSections, bool $subAdminOnly) {
122
		$sections = $this->settingsManager->getAdminSections();
123
		$templateParameters = $this->formatSections($sections, $currentSections, 'admin', $currentType, $subAdminOnly);
124
125
		return $templateParameters;
126
	}
127
128
	/**
129
	 * @param ISettings[] $settings
130
	 * @return array
131
	 */
132
	private function formatSettings($settings) {
133
		$html = '';
134
		foreach ($settings as $prioritizedSettings) {
135
			foreach ($prioritizedSettings as $setting) {
136
				/** @var \OCP\Settings\ISettings $setting */
137
				$form = $setting->getForm();
138
				$html .= $form->renderAs('')->render();
139
			}
140
		}
141
		return ['content' => $html];
142
	}
143
144
	private function getIndexResponse($type, $section) {
145
		$this->navigationManager->setActiveEntry('settings');
146
		$templateParams = [];
147
		$templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
148
		$templateParams = array_merge($templateParams, $this->getSettings($section));
149
150
		return new TemplateResponse('settings', 'settings/frame', $templateParams);
151
	}
152
153
	abstract protected function getSettings($section);
154
}
155