1<?php 2/** 3 * @author Tom Needham <tom@owncloud.com> 4 * 5 * @copyright Copyright (c) 2018, 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 22namespace OC\Settings\Controller; 23 24use OCP\Settings\ISettings; 25use OCP\Settings\ISettingsManager; 26use OCP\AppFramework\Controller; 27use OCP\IURLGenerator; 28use OCP\IRequest; 29use OCP\Template; 30use OCP\AppFramework\Http\TemplateResponse; 31use OCP\IGroupManager; 32use OCP\IUserSession; 33 34/** 35 * @package OC\Settings\Controller 36 */ 37class 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 public function __construct( 57 $appName, 58 IRequest $request, 59 ISettingsManager $settingsManager, 60 IURLGenerator $urlGenerator, 61 IGroupManager $groupManager, 62 IUserSession $userSession 63 ) { 64 parent::__construct($appName, $request); 65 $this->settingsManager = $settingsManager; 66 $this->urlGenerator = $urlGenerator; 67 $this->groupManager = $groupManager; 68 $this->userSession = $userSession; 69 } 70 71 /** 72 * @NoAdminRequired 73 * @NoSubadminRequired 74 * @NoCSRFRequired 75 * @param string $sectionid 76 * @return \OCP\AppFramework\Http\TemplateResponse 77 */ 78 public function getPersonal($sectionid='general') { 79 $admin = $this->groupManager->isAdmin($this->userSession->getUser()->getUID()); 80 $personalSections = $this->settingsManager->getPersonalSections(); 81 $adminSections = $admin ? $this->settingsManager->getAdminSections() : []; 82 $panels = $this->settingsManager->getPersonalPanels($sectionid); 83 $params = []; 84 $params['type'] = 'personal'; 85 $params['personalNav'] = $this->getNavigation($personalSections, $sectionid, 'personal'); 86 $params['adminNav'] = $admin ? $this->getNavigation($adminSections, '', 'admin') : []; 87 $params['panels'] = $this->getPanelsData($panels); 88 $response = new TemplateResponse($this->appName, 'settingsPage', $params); 89 return $response; 90 } 91 92 /** 93 * Creates the admin settings page 94 * @NoCSRFRequired 95 * @param string $sectionid 96 * @return \OCP\AppFramework\Http\TemplateResponse 97 */ 98 public function getAdmin($sectionid='general') { 99 $personalSections = $this->settingsManager->getPersonalSections(); 100 $adminSections = $this->settingsManager->getAdminSections(); 101 $panels = $this->settingsManager->getAdminPanels($sectionid); 102 $params = []; 103 $params['type'] = 'admin'; 104 $params['personalNav'] = $this->getNavigation($personalSections, '', 'personal'); 105 $params['adminNav'] = $this->getNavigation($adminSections, $sectionid, 'admin'); 106 $params['panels'] = $this->getPanelsData($panels); 107 $response = new TemplateResponse($this->appName, 'settingsPage', $params); 108 return $response; 109 } 110 111 /** 112 * Gets the icon for the settings panel. The idea is 113 * to get either URL or the icon name ( for backward 114 * compatibility ). The icons returned will be svg 115 * format and no other formats are supported. 116 * 117 * @param \OCP\Settings\ISection $section 118 * @return string 119 */ 120 121 protected function getIconForSettingsPanel($section) { 122 $icon = $section->getIconName() . '.svg'; 123 $appPath = \OC_App::getAppPath($section->getID()); 124 125 if (\file_exists($appPath . '/img/' . $icon)) { 126 $icon = $this->urlGenerator->imagePath($section->getID(), $icon); 127 } else { 128 $icon = $section->getIconName(); 129 } 130 131 return $icon; 132 } 133 134 /** 135 * Gets an array used to generate the navigation in the UI 136 * @param array $sections array of ISections 137 * @param string $currentSectionID 138 * @param string $type 139 * @return array 140 */ 141 protected function getNavigation($sections, $currentSectionID, $type) { 142 $nav = []; 143 // Iterate through sections and get id, name and see if currently active 144 foreach ($sections as $section) { 145 $icon = $this->getIconForSettingsPanel($section); 146 147 $nav[] = [ 148 'id' => $section->getID(), 149 'link' => $this->urlGenerator->linkToRoute( 150 'settings.SettingsPage.get'.\ucwords($type), 151 ['sectionid' => $section->getID()] 152 ), 153 'name' => \ucfirst($section->getName()), 154 'active' => $section->getID() === $currentSectionID, 155 'icon' => $icon 156 ]; 157 } 158 return $nav; 159 } 160 161 /** 162 * Iterate through the panels and retrieve the html content 163 * @param ISettings[] $panels array of ISettings 164 * @return array containing panel html 165 */ 166 protected function getPanelsData($panels) { 167 $data = []; 168 foreach ($panels as $panel) { 169 $template = $panel->getPanel(); 170 if ($template instanceof Template || $template instanceof TemplateResponse) { 171 $data[] = [ 172 'id' => \get_class($panel), 173 'content' => ($template instanceof Template) ? $template->fetchPage() : $template->render() 174 ]; 175 } 176 } 177 return $data; 178 } 179} 180