Completed
Pull Request — master (#841)
by Blizzz
10:54 queued 01:57
created

AdminSettingsController::form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Settings\Controller;
25
26
use Doctrine\DBAL\Connection;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\TemplateResponse;
29
use OC\Encryption\Manager as EncryptionManager;
30
use OCP\IConfig;
31
use OCP\IDBConnection;
32
use OCP\IL10N;
33
use OCP\INavigationManager;
34
use OCP\IRequest;
35
use OCP\IUserManager;
36
use OCP\Settings\IManager as ISettingsManager;
37
38
/**
39
 * @package OC\Settings\Controller
40
 */
41
class AdminSettingsController extends Controller {
42
43
	/** @var INavigationManager */
44
	private $navigationManager;
45
46
	/** @var ISettingsManager */
47
	private $settingsManager;
48
49
	public function __construct(
50
		$appName,
51
		IRequest $request,
52
		INavigationManager $navigationManager,
53
		IL10N $l,
0 ignored issues
show
Unused Code introduced by
The parameter $l is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
54
		IConfig $config,
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
		EncryptionManager $encryptionManager,
0 ignored issues
show
Unused Code introduced by
The parameter $encryptionManager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
56
		IUserManager $userManager,
0 ignored issues
show
Unused Code introduced by
The parameter $userManager is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
		IDBConnection $db,
0 ignored issues
show
Unused Code introduced by
The parameter $db is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
		ISettingsManager $settingsManager
59
	) {
60
		parent::__construct($appName, $request);
61
		$this->navigationManager = $navigationManager;
62
		$this->settingsManager = $settingsManager;
63
	}
64
65
	/**
66
	 * @param string $section
67
	 * @return TemplateResponse
68
	 *
69
	 * @NoCSRFRequired
70
	 */
71
	public function index($section) {
72
		$this->navigationManager->setActiveEntry('admin');
73
74
		$templateParams = [];
75
		$templateParams = array_merge($templateParams, $this->getNavigationParameters());
76
		$templateParams = array_merge($templateParams, $this->getSettings($section));
77
78
		return new TemplateResponse('settings', 'admin/frame', $templateParams);
79
	}
80
81
	public function form() {
82
83
	}
84
85
	private function getSettings($section) {
86
		if($section === 'additional') {
87
			return $this->getLegacyForms();
88
		}
89
90
		$settings = $this->settingsManager->getAdminSettings($section);
91
		$html = '';
92
		foreach ($settings as $prioritizedSettings) {
93
			foreach ($prioritizedSettings as $setting) {
94
				/** @var \OCP\Settings\ISettings $setting */
95
				$form = $setting->getForm();
96
				$html .= $form->renderAs('')->render();
97
			}
98
		}
99
		return ['content' => $html];
100
	}
101
102
	private function getLegacyForms() {
103
		$forms = \OC_App::getForms('admin');
104
105 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...
106
			if (preg_match('%(<h2(?P<class>[^>]*)>.*?</h2>)%i', $form, $regs)) {
107
				$sectionName = str_replace('<h2' . $regs['class'] . '>', '', $regs[0]);
108
				$sectionName = str_replace('</h2>', '', $sectionName);
109
				$anchor = strtolower($sectionName);
110
				$anchor = str_replace(' ', '-', $anchor);
111
112
				return array(
113
					'anchor' => $anchor,
114
					'section-name' => $sectionName,
115
					'form' => $form
116
				);
117
			}
118
			return array(
119
				'form' => $form
120
			);
121
		}, $forms);
122
123
		$out = new \OCP\Template('settings', 'admin/additional');
124
		$out->assign('forms', $forms);
125
126
		return ['content' => $out->fetchPage()];
127
	}
128
129
	private function getNavigationParameters() {
130
		$a = 'anchor';
131
		$name = 'section-name';
132
133
		$sections = $this->settingsManager->getAdminSections();
134
		$templateParameters = [];
135
		foreach($sections as $prioritizedSections) {
136
			foreach ($prioritizedSections as $section) {
137
				$templateParameters[] = [$a => $section->getID(), $name => $section->getName()];
138
			}
139
		}
140
141
		return [
142
			'forms' => $templateParameters
143
		];
144
	}
145
}
146