1
0

SectionSearch.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Search;
  8. use OCP\IGroupManager;
  9. use OCP\IL10N;
  10. use OCP\IURLGenerator;
  11. use OCP\IUser;
  12. use OCP\Search\IProvider;
  13. use OCP\Search\ISearchQuery;
  14. use OCP\Search\SearchResult;
  15. use OCP\Search\SearchResultEntry;
  16. use OCP\Settings\IIconSection;
  17. use OCP\Settings\IManager;
  18. class SectionSearch implements IProvider {
  19. /** @var IManager */
  20. protected $settingsManager;
  21. /** @var IGroupManager */
  22. protected $groupManager;
  23. /** @var IURLGenerator */
  24. protected $urlGenerator;
  25. /** @var IL10N */
  26. protected $l;
  27. public function __construct(IManager $settingsManager,
  28. IGroupManager $groupManager,
  29. IURLGenerator $urlGenerator,
  30. IL10N $l) {
  31. $this->settingsManager = $settingsManager;
  32. $this->groupManager = $groupManager;
  33. $this->urlGenerator = $urlGenerator;
  34. $this->l = $l;
  35. }
  36. /**
  37. * @inheritDoc
  38. */
  39. public function getId(): string {
  40. return 'settings';
  41. }
  42. /**
  43. * @inheritDoc
  44. */
  45. public function getName(): string {
  46. return $this->l->t('Settings');
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. public function getOrder(string $route, array $routeParameters): int {
  52. if ($route === 'settings.PersonalSettings.index' || $route === 'settings.AdminSettings.index') {
  53. return -1;
  54. }
  55. // At the very bottom
  56. return 500;
  57. }
  58. /**
  59. * @inheritDoc
  60. */
  61. public function search(IUser $user, ISearchQuery $query): SearchResult {
  62. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  63. $result = $this->searchSections(
  64. $query,
  65. $this->settingsManager->getPersonalSections(),
  66. $isAdmin ? $this->l->t('Personal') : '',
  67. 'settings.PersonalSettings.index'
  68. );
  69. if ($this->groupManager->isAdmin($user->getUID())) {
  70. $result = array_merge($result, $this->searchSections(
  71. $query,
  72. $this->settingsManager->getAdminSections(),
  73. $this->l->t('Administration'),
  74. 'settings.AdminSettings.index'
  75. ));
  76. }
  77. return SearchResult::complete(
  78. $this->l->t('Settings'),
  79. $result
  80. );
  81. }
  82. /**
  83. * @param ISearchQuery $query
  84. * @param IIconSection[][] $sections
  85. * @param string $subline
  86. * @param string $routeName
  87. * @return array
  88. */
  89. public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array {
  90. $result = [];
  91. foreach ($sections as $priority => $sectionsByPriority) {
  92. foreach ($sectionsByPriority as $section) {
  93. if (
  94. stripos($section->getName(), $query->getTerm()) === false &&
  95. stripos($section->getID(), $query->getTerm()) === false
  96. ) {
  97. continue;
  98. }
  99. /**
  100. * We can't use the icon URL at the moment as they don't invert correctly for dark theme
  101. * $iconUrl = $section->getIcon();
  102. */
  103. $result[] = new SearchResultEntry(
  104. '',
  105. $section->getName(),
  106. $subline,
  107. $this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()]),
  108. 'icon-settings-dark'
  109. );
  110. }
  111. }
  112. return $result;
  113. }
  114. }