2017-05-16 02:38:12 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-05-16 02:38:12 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-05-16 02:38:12 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-09-17 17:33:27 +03:00
|
|
|
namespace OCA\Settings\Controller;
|
2017-05-16 02:38:12 +03:00
|
|
|
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2019-05-22 11:48:51 +03:00
|
|
|
use OCP\Group\ISubAdmin;
|
|
|
|
use OCP\IGroupManager;
|
2018-03-05 19:06:28 +03:00
|
|
|
use OCP\INavigationManager;
|
2019-05-22 11:48:51 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
2017-05-16 02:38:12 +03:00
|
|
|
use OCP\Settings\IIconSection;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\Settings\IManager as ISettingsManager;
|
2017-05-16 02:38:12 +03:00
|
|
|
use OCP\Settings\ISettings;
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
trait CommonSettingsTrait {
|
2019-05-22 11:48:51 +03:00
|
|
|
|
2017-05-16 02:38:12 +03:00
|
|
|
/** @var ISettingsManager */
|
|
|
|
private $settingsManager;
|
|
|
|
|
2018-03-05 19:06:28 +03:00
|
|
|
/** @var INavigationManager */
|
|
|
|
private $navigationManager;
|
|
|
|
|
2019-05-22 11:48:51 +03:00
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
/** @var IGroupManager */
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/** @var ISubAdmin */
|
|
|
|
private $subAdmin;
|
|
|
|
|
2017-05-16 02:38:12 +03:00
|
|
|
/**
|
|
|
|
* @param string $currentSection
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-06-22 19:15:33 +03:00
|
|
|
private function getNavigationParameters($currentType, $currentSection) {
|
2017-05-16 02:38:12 +03:00
|
|
|
$templateParameters = [
|
2017-06-22 19:15:33 +03:00
|
|
|
'personal' => $this->formatPersonalSections($currentType, $currentSection),
|
2017-05-16 02:38:12 +03:00
|
|
|
'admin' => []
|
|
|
|
];
|
|
|
|
|
2019-05-22 11:48:51 +03:00
|
|
|
/** @var IUser $user */
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
$isAdmin = $this->groupManager->isAdmin($user->getUID());
|
|
|
|
$isSubAdmin = $this->subAdmin->isSubAdmin($user);
|
|
|
|
if ($isAdmin || $isSubAdmin) {
|
|
|
|
$templateParameters['admin'] = $this->formatAdminSections(
|
|
|
|
$currentType,
|
|
|
|
$currentSection,
|
|
|
|
!$isAdmin && $isSubAdmin
|
|
|
|
);
|
2017-05-16 02:38:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'forms' => $templateParameters
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-05-22 11:48:51 +03:00
|
|
|
protected function formatSections($sections, $currentSection, $type, $currentType, bool $subAdminOnly = false) {
|
2017-05-16 02:38:12 +03:00
|
|
|
$templateParameters = [];
|
|
|
|
/** @var \OCP\Settings\ISection[] $prioritizedSections */
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($sections as $prioritizedSections) {
|
2017-05-16 02:38:12 +03:00
|
|
|
foreach ($prioritizedSections as $section) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($type === 'admin') {
|
2019-05-22 11:48:51 +03:00
|
|
|
$settings = $this->settingsManager->getAdminSettings($section->getID(), $subAdminOnly);
|
2020-04-10 15:19:56 +03:00
|
|
|
} elseif ($type === 'personal') {
|
2017-05-16 02:38:12 +03:00
|
|
|
$settings = $this->settingsManager->getPersonalSettings($section->getID());
|
|
|
|
}
|
2018-04-09 22:49:03 +03:00
|
|
|
if (empty($settings) && !($section->getID() === 'additional' && count(\OC_App::getForms('admin')) > 0)) {
|
2017-05-16 02:38:12 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$icon = '';
|
|
|
|
if ($section instanceof IIconSection) {
|
|
|
|
$icon = $section->getIcon();
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:15:33 +03:00
|
|
|
$active = $section->getID() === $currentSection
|
|
|
|
&& $type === $currentType;
|
|
|
|
|
2017-05-16 02:38:12 +03:00
|
|
|
$templateParameters[] = [
|
2020-10-05 16:12:57 +03:00
|
|
|
'anchor' => $section->getID(),
|
2017-05-16 02:38:12 +03:00
|
|
|
'section-name' => $section->getName(),
|
2020-10-05 16:12:57 +03:00
|
|
|
'active' => $active,
|
|
|
|
'icon' => $icon,
|
2017-05-16 02:38:12 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $templateParameters;
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:15:33 +03:00
|
|
|
protected function formatPersonalSections($currentType, $currentSections) {
|
2017-05-16 02:38:12 +03:00
|
|
|
$sections = $this->settingsManager->getPersonalSections();
|
2017-06-22 19:15:33 +03:00
|
|
|
$templateParameters = $this->formatSections($sections, $currentSections, 'personal', $currentType);
|
2017-05-16 02:38:12 +03:00
|
|
|
|
|
|
|
return $templateParameters;
|
|
|
|
}
|
|
|
|
|
2019-05-22 11:48:51 +03:00
|
|
|
protected function formatAdminSections($currentType, $currentSections, bool $subAdminOnly) {
|
2017-05-16 02:38:12 +03:00
|
|
|
$sections = $this->settingsManager->getAdminSections();
|
2019-05-22 11:48:51 +03:00
|
|
|
$templateParameters = $this->formatSections($sections, $currentSections, 'admin', $currentType, $subAdminOnly);
|
2017-05-16 02:38:12 +03:00
|
|
|
|
|
|
|
return $templateParameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ISettings[] $settings
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function formatSettings($settings) {
|
|
|
|
$html = '';
|
|
|
|
foreach ($settings as $prioritizedSettings) {
|
|
|
|
foreach ($prioritizedSettings as $setting) {
|
|
|
|
/** @var \OCP\Settings\ISettings $setting */
|
|
|
|
$form = $setting->getForm();
|
|
|
|
$html .= $form->renderAs('')->render();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ['content' => $html];
|
|
|
|
}
|
|
|
|
|
2017-06-22 19:15:33 +03:00
|
|
|
private function getIndexResponse($type, $section) {
|
2018-03-05 19:06:28 +03:00
|
|
|
$this->navigationManager->setActiveEntry('settings');
|
2017-05-16 02:38:12 +03:00
|
|
|
$templateParams = [];
|
2017-06-22 19:15:33 +03:00
|
|
|
$templateParams = array_merge($templateParams, $this->getNavigationParameters($type, $section));
|
2017-05-16 02:38:12 +03:00
|
|
|
$templateParams = array_merge($templateParams, $this->getSettings($section));
|
|
|
|
|
|
|
|
return new TemplateResponse('settings', 'settings/frame', $templateParams);
|
|
|
|
}
|
|
|
|
|
2017-05-31 13:44:52 +03:00
|
|
|
abstract protected function getSettings($section);
|
2017-05-16 02:38:12 +03:00
|
|
|
}
|