SectionSearch.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public function __construct(
  20. protected IManager $settingsManager,
  21. protected IGroupManager $groupManager,
  22. protected IURLGenerator $urlGenerator,
  23. protected IL10N $l,
  24. ) {
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. public function getId(): string {
  30. return 'settings';
  31. }
  32. /**
  33. * @inheritDoc
  34. */
  35. public function getName(): string {
  36. return $this->l->t('Settings');
  37. }
  38. /**
  39. * @inheritDoc
  40. */
  41. public function getOrder(string $route, array $routeParameters): int {
  42. if ($route === 'settings.PersonalSettings.index' || $route === 'settings.AdminSettings.index') {
  43. return -1;
  44. }
  45. // At the very bottom
  46. return 500;
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. public function search(IUser $user, ISearchQuery $query): SearchResult {
  52. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  53. $personalSections = $this->settingsManager->getPersonalSections();
  54. foreach ($personalSections as $priority => $sections) {
  55. $personalSections[$priority] = array_values(array_filter(
  56. $sections,
  57. fn (IIconSection $section) => !empty($this->settingsManager->getPersonalSettings($section->getID())),
  58. ));
  59. }
  60. $adminSections = $this->settingsManager->getAdminSections();
  61. foreach ($adminSections as $priority => $sections) {
  62. $adminSections[$priority] = array_values(array_filter(
  63. $sections,
  64. fn (IIconSection $section) => !empty($this->settingsManager->getAllowedAdminSettings($section->getID(), $user)),
  65. ));
  66. }
  67. $result = $this->searchSections(
  68. $query,
  69. $personalSections,
  70. $isAdmin ? $this->l->t('Personal') : '',
  71. 'settings.PersonalSettings.index'
  72. );
  73. if ($this->groupManager->isAdmin($user->getUID())) {
  74. $result = array_merge($result, $this->searchSections(
  75. $query,
  76. $adminSections,
  77. $this->l->t('Administration'),
  78. 'settings.AdminSettings.index'
  79. ));
  80. }
  81. return SearchResult::complete(
  82. $this->l->t('Settings'),
  83. $result
  84. );
  85. }
  86. /**
  87. * @param ISearchQuery $query
  88. * @param IIconSection[][] $sections
  89. * @param string $subline
  90. * @param string $routeName
  91. * @return array
  92. */
  93. public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array {
  94. $result = [];
  95. foreach ($sections as $priority => $sectionsByPriority) {
  96. foreach ($sectionsByPriority as $section) {
  97. if (
  98. stripos($section->getName(), $query->getTerm()) === false &&
  99. stripos($section->getID(), $query->getTerm()) === false
  100. ) {
  101. continue;
  102. }
  103. /**
  104. * We can't use the icon URL at the moment as they don't invert correctly for dark theme
  105. * $iconUrl = $section->getIcon();
  106. */
  107. $result[] = new SearchResultEntry(
  108. '',
  109. $section->getName(),
  110. $subline,
  111. $this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()]),
  112. 'icon-settings-dark'
  113. );
  114. }
  115. }
  116. return $result;
  117. }
  118. }