SectionSearch.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Settings\Search;
  27. use OCP\IGroupManager;
  28. use OCP\IL10N;
  29. use OCP\IURLGenerator;
  30. use OCP\IUser;
  31. use OCP\Search\IProvider;
  32. use OCP\Search\ISearchQuery;
  33. use OCP\Search\SearchResult;
  34. use OCP\Search\SearchResultEntry;
  35. use OCP\Settings\IIconSection;
  36. use OCP\Settings\IManager;
  37. class SectionSearch implements IProvider {
  38. /** @var IManager */
  39. protected $settingsManager;
  40. /** @var IGroupManager */
  41. protected $groupManager;
  42. /** @var IURLGenerator */
  43. protected $urlGenerator;
  44. /** @var IL10N */
  45. protected $l;
  46. public function __construct(IManager $settingsManager,
  47. IGroupManager $groupManager,
  48. IURLGenerator $urlGenerator,
  49. IL10N $l) {
  50. $this->settingsManager = $settingsManager;
  51. $this->groupManager = $groupManager;
  52. $this->urlGenerator = $urlGenerator;
  53. $this->l = $l;
  54. }
  55. /**
  56. * @inheritDoc
  57. */
  58. public function getId(): string {
  59. return 'settings';
  60. }
  61. /**
  62. * @inheritDoc
  63. */
  64. public function getName(): string {
  65. return $this->l->t('Settings');
  66. }
  67. /**
  68. * @inheritDoc
  69. */
  70. public function getOrder(string $route, array $routeParameters): int {
  71. if ($route === 'settings.PersonalSettings.index' || $route === 'settings.AdminSettings.index') {
  72. return -1;
  73. }
  74. // At the very bottom
  75. return 500;
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function search(IUser $user, ISearchQuery $query): SearchResult {
  81. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  82. $result = $this->searchSections(
  83. $query,
  84. $this->settingsManager->getPersonalSections(),
  85. $isAdmin ? $this->l->t('Personal') : '',
  86. 'settings.PersonalSettings.index'
  87. );
  88. if ($this->groupManager->isAdmin($user->getUID())) {
  89. $result = array_merge($result, $this->searchSections(
  90. $query,
  91. $this->settingsManager->getAdminSections(),
  92. $this->l->t('Administration'),
  93. 'settings.AdminSettings.index'
  94. ));
  95. }
  96. return SearchResult::complete(
  97. $this->l->t('Settings'),
  98. $result
  99. );
  100. }
  101. /**
  102. * @param ISearchQuery $query
  103. * @param IIconSection[][] $sections
  104. * @param string $subline
  105. * @param string $routeName
  106. * @return array
  107. */
  108. public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array {
  109. $result = [];
  110. foreach ($sections as $priority => $sectionsByPriority) {
  111. foreach ($sectionsByPriority as $section) {
  112. if (
  113. stripos($section->getName(), $query->getTerm()) === false &&
  114. stripos($section->getID(), $query->getTerm()) === false
  115. ) {
  116. continue;
  117. }
  118. /**
  119. * We can't use the icon URL at the moment as they don't invert correctly for dark theme
  120. * $iconUrl = $section->getIcon();
  121. */
  122. $result[] = new SearchResultEntry(
  123. '',
  124. $section->getName(),
  125. $subline,
  126. $this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()]),
  127. 'icon-settings'
  128. );
  129. }
  130. }
  131. return $result;
  132. }
  133. }