]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add settings sections to unified search 22095/head
authorJoas Schilling <coding@schilljs.com>
Mon, 3 Aug 2020 15:03:06 +0000 (17:03 +0200)
committerJoas Schilling <coding@schilljs.com>
Mon, 3 Aug 2020 15:03:06 +0000 (17:03 +0200)
Signed-off-by: Joas Schilling <coding@schilljs.com>
apps/settings/composer/composer/autoload_classmap.php
apps/settings/composer/composer/autoload_static.php
apps/settings/lib/AppInfo/Application.php
apps/settings/lib/Search/SectionResult.php [new file with mode: 0644]
apps/settings/lib/Search/SectionSearch.php [new file with mode: 0644]

index ce594d9b46d4a92effa03645f6cd687030d645a1..a20138455e711be2aaeeb5a4fa19d5806e5359cc 100644 (file)
@@ -31,6 +31,8 @@ return array(
     'OCA\\Settings\\Hooks' => $baseDir . '/../lib/Hooks.php',
     'OCA\\Settings\\Mailer\\NewUserMailHelper' => $baseDir . '/../lib/Mailer/NewUserMailHelper.php',
     'OCA\\Settings\\Middleware\\SubadminMiddleware' => $baseDir . '/../lib/Middleware/SubadminMiddleware.php',
+    'OCA\\Settings\\Search\\SectionResult' => $baseDir . '/../lib/Search/SectionResult.php',
+    'OCA\\Settings\\Search\\SectionSearch' => $baseDir . '/../lib/Search/SectionSearch.php',
     'OCA\\Settings\\Sections\\Admin\\Additional' => $baseDir . '/../lib/Sections/Admin/Additional.php',
     'OCA\\Settings\\Sections\\Admin\\Groupware' => $baseDir . '/../lib/Sections/Admin/Groupware.php',
     'OCA\\Settings\\Sections\\Admin\\Overview' => $baseDir . '/../lib/Sections/Admin/Overview.php',
index b77b581a0fc97e6166df4dbd5018cddf2d989989..6cddcda5e44254478ae654a5a3f77f8730a851f9 100644 (file)
@@ -46,6 +46,8 @@ class ComposerStaticInitSettings
         'OCA\\Settings\\Hooks' => __DIR__ . '/..' . '/../lib/Hooks.php',
         'OCA\\Settings\\Mailer\\NewUserMailHelper' => __DIR__ . '/..' . '/../lib/Mailer/NewUserMailHelper.php',
         'OCA\\Settings\\Middleware\\SubadminMiddleware' => __DIR__ . '/..' . '/../lib/Middleware/SubadminMiddleware.php',
+        'OCA\\Settings\\Search\\SectionResult' => __DIR__ . '/..' . '/../lib/Search/SectionResult.php',
+        'OCA\\Settings\\Search\\SectionSearch' => __DIR__ . '/..' . '/../lib/Search/SectionSearch.php',
         'OCA\\Settings\\Sections\\Admin\\Additional' => __DIR__ . '/..' . '/../lib/Sections/Admin/Additional.php',
         'OCA\\Settings\\Sections\\Admin\\Groupware' => __DIR__ . '/..' . '/../lib/Sections/Admin/Groupware.php',
         'OCA\\Settings\\Sections\\Admin\\Overview' => __DIR__ . '/..' . '/../lib/Sections/Admin/Overview.php',
index 6e6eb26d01161ca1a0343cc16ada5e2ac17b5935..0fd39f71b36d92927db38905e8e75d7a8cea05a4 100644 (file)
@@ -46,6 +46,7 @@ use OCA\Settings\Activity\Provider;
 use OCA\Settings\Hooks;
 use OCA\Settings\Mailer\NewUserMailHelper;
 use OCA\Settings\Middleware\SubadminMiddleware;
+use OCA\Settings\Search\SectionSearch;
 use OCP\Activity\IManager as IActivityManager;
 use OCP\AppFramework\App;
 use OCP\AppFramework\Bootstrap\IBootContext;
@@ -77,6 +78,7 @@ class Application extends App implements IBootstrap {
                // Register Middleware
                $context->registerServiceAlias('SubadminMiddleware', SubadminMiddleware::class);
                $context->registerMiddleware(SubadminMiddleware::class);
+               $context->registerSearchProvider(SectionSearch::class);
 
                /**
                 * Core class wrappers
diff --git a/apps/settings/lib/Search/SectionResult.php b/apps/settings/lib/Search/SectionResult.php
new file mode 100644 (file)
index 0000000..d7e4e6b
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
+ *
+ * @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
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Settings\Search;
+
+use OCP\Search\ASearchResultEntry;
+
+class SectionResult extends ASearchResultEntry {
+}
diff --git a/apps/settings/lib/Search/SectionSearch.php b/apps/settings/lib/Search/SectionSearch.php
new file mode 100644 (file)
index 0000000..818fa05
--- /dev/null
@@ -0,0 +1,134 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
+ *
+ * @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
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Settings\Search;
+
+use OCP\IGroupManager;
+use OCP\IL10N;
+use OCP\IURLGenerator;
+use OCP\IUser;
+use OCP\Search\IProvider;
+use OCP\Search\ISearchQuery;
+use OCP\Search\SearchResult;
+use OCP\Settings\IIconSection;
+use OCP\Settings\ISection;
+use OCP\Settings\IManager;
+
+class SectionSearch implements IProvider {
+
+       /** @var IManager */
+       protected $settingsManager;
+       /** @var IGroupManager */
+       protected $groupManager;
+       /** @var IURLGenerator */
+       protected $urlGenerator;
+       /** @var IL10N */
+       protected $l;
+
+       public function __construct(IManager $settingsManager,
+                                                               IGroupManager $groupManager,
+                                                               IURLGenerator $urlGenerator,
+                                                               IL10N $l) {
+               $this->settingsManager = $settingsManager;
+               $this->groupManager = $groupManager;
+               $this->urlGenerator = $urlGenerator;
+               $this->l = $l;
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function getId(): string {
+               return 'settings_sections';
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function getName(): string {
+               return $this->l->t('Settings');
+       }
+
+       /**
+        * @inheritDoc
+        */
+       public function search(IUser $user, ISearchQuery $query): SearchResult {
+               $isAdmin = $this->groupManager->isAdmin($user->getUID());
+
+               $result = $this->searchSections(
+                       $query,
+                       $this->settingsManager->getPersonalSections(),
+                       $isAdmin ? $this->l->t('Personal') : '',
+                       'settings.PersonalSettings.index'
+               );
+
+               if ($this->groupManager->isAdmin($user->getUID())) {
+                       $result = array_merge($result, $this->searchSections(
+                               $query,
+                               $this->settingsManager->getAdminSections(),
+                               $this->l->t('Administration'),
+                               'settings.AdminSettings.index'
+                       ));
+               }
+
+               return SearchResult::complete(
+                       $this->l->t('Settings'),
+                       $result
+               );
+       }
+
+       /**
+        * @param ISearchQuery $query
+        * @param ISection[][] $sections
+        * @param string $subline
+        * @param string $routeName
+        * @return array
+        */
+       public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array {
+               $result = [];
+               foreach ($sections as $priority => $sectionsByPriority) {
+                       foreach ($sectionsByPriority as $section) {
+                               if (
+                                       stripos($section->getName(), $query->getTerm()) === false &&
+                                       stripos($section->getID(), $query->getTerm()) === false
+                               ) {
+                                       continue;
+                               }
+
+                               $iconUrl = '';
+                               if ($section instanceof IIconSection) {
+                                       $iconUrl = $section->getIcon();
+                               }
+
+                               $result[] = new SectionResult(
+                                       $iconUrl,
+                                       $section->getName(),
+                                       $subline,
+                                       $this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()])
+                               );
+                       }
+               }
+
+               return $result;
+       }
+}