Completed
Push — master ( 33d925...cc7944 )
by Thomas
09:52
created

SettingsPageController::getPanelsData()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Tom Needham <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud GmbH.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 *  it under the terms of the GNU Affero General Public License, version 3,
10
 *  as published by the Free Software Foundation.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Settings\Controller;
23
24
use OCP\Settings\ISettings;
25
use OCP\Settings\ISettingsManager;
26
use OCP\AppFramework\Controller;
27
use OCP\IURLGenerator;
28
use OCP\IRequest;
29
use OCP\Template;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IGroupManager;
32
use OCP\IUserSession;
33
34
/**
35
 * @package OC\Settings\Controller
36
 */
37
class SettingsPageController extends Controller {
38
39
	/** @var ISettingsManager */
40
	protected $settingsManager;
41
	/** @var IURLGenerator */
42
	protected $urlGenerator;
43
	/** @var IGroupManager */
44
	protected $groupManager;
45
	/** @var IUserSession */
46
	protected $userSession;
47
48
	/**
49
	 * @param string $appName
50
	 * @param IRequest $request
51
	 * @param ISettingsManager $settingsManager
52
	 * @param IURLGenerator $urlGenerator
53
	 * @param IGroupManager $groupManager
54
	 * @param IUserSession $userSession
55
	 */
56 View Code Duplication
	public function __construct($appName,
57
								IRequest $request,
58
								ISettingsManager $settingsManager,
59
								IURLGenerator $urlGenerator,
60
								IGroupManager $groupManager,
61
								IUserSession $userSession) {
62
		parent::__construct($appName, $request);
63
		$this->settingsManager = $settingsManager;
64
		$this->urlGenerator = $urlGenerator;
65
		$this->groupManager = $groupManager;
66
		$this->userSession = $userSession;
67
	}
68
69
	/**
70
	 * @NoAdminRequired
71
	 * @NoSubadminRequired
72
	 * @NoCSRFRequired
73
	 * @param string $sectionid
74
	 * @return \OCP\AppFramework\Http\TemplateResponse
75
	 */
76
	public function getPersonal($sectionid='general') {
77
		$admin = $this->groupManager->isAdmin($this->userSession->getUser()->getUID());
78
		$personalSections = $this->settingsManager->getPersonalSections();
79
		$adminSections = $admin ? $this->settingsManager->getAdminSections() : [];
80
		$panels = $this->settingsManager->getPersonalPanels($sectionid);
81
		$params = [];
82
		$params['type'] = 'personal';
83
		$params['personalNav'] = $this->getNavigation($personalSections, $sectionid, 'personal');
84
		$params['adminNav'] = $admin ? $this->getNavigation($adminSections, '', 'admin') : [];
85
		$params['panels'] = $this->getPanelsData($panels);
86
		$response = new TemplateResponse($this->appName, 'settingsPage', $params);
87
		return $response;
88
	}
89
90
	/**
91
	 * Creates the admin settings page
92
	 * @NoCSRFRequired
93
	 * @param string $sectionid
94
	 * @return \OCP\AppFramework\Http\TemplateResponse
95
	 */
96
	public function getAdmin($sectionid='general') {
97
		$personalSections = $this->settingsManager->getPersonalSections();
98
		$adminSections = $this->settingsManager->getAdminSections();
99
		$panels = $this->settingsManager->getAdminPanels($sectionid);
100
		$params = [];
101
		$params['type'] = 'admin';
102
		$params['personalNav'] = $this->getNavigation($personalSections, '', 'personal');
103
		$params['adminNav'] = $this->getNavigation($adminSections, $sectionid, 'admin');
104
		$params['panels'] = $this->getPanelsData($panels);
105
		$response = new TemplateResponse($this->appName, 'settingsPage', $params);
106
		return $response;
107
	}
108
109
	/**
110
	 * Gets an array used to generate the navigation in the UI
111
	 * @param array $sections array of ISections
112
	 * @param string $currentSectionID
113
	 * @param string $type
114
	 * @return array
115
	 */
116
	protected function getNavigation($sections, $currentSectionID, $type) {
117
		$nav = [];
118
		// Iterate through sections and get id, name and see if currently active
119
		foreach($sections as $section) {
120
			$nav[] = [
121
				'id' => $section->getID(),
122
				'link' => $this->urlGenerator->linkToRoute(
123
					'settings.SettingsPage.get'.ucwords($type),
124
					['sectionid' => $section->getID()]
125
				),
126
				'name' => ucfirst($section->getName()),
127
				'active' => $section->getID() === $currentSectionID,
128
			];
129
		}
130
		return $nav;
131
	}
132
133
	/**
134
	 * Iterate through the panels and retrieve the html content
135
	 * @param ISettings[] $panels array of ISettings
136
	 * @return array containing panel html
137
	 */
138
	protected function getPanelsData($panels) {
139
		$data = [];
140
		foreach($panels as $panel) {
141
			$template = $panel->getPanel();
142
			if($template instanceof Template || $template instanceof TemplateResponse) {
143
				$data[] = [
144
					'id' => get_class($panel),
145
					'content' => ($template instanceof Template) ? $template->fetchPage() : $template->render()
146
				];
147
			}
148
		}
149
		return $data;
150
	}
151
152
}
153