SectionSearch.php 3.8 KB

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