AppSearch.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\IL10N;
  9. use OCP\INavigationManager;
  10. use OCP\IUser;
  11. use OCP\Search\IProvider;
  12. use OCP\Search\ISearchQuery;
  13. use OCP\Search\SearchResult;
  14. use OCP\Search\SearchResultEntry;
  15. class AppSearch implements IProvider {
  16. public function __construct(
  17. protected INavigationManager $navigationManager,
  18. protected IL10N $l,
  19. ) {
  20. }
  21. public function getId(): string {
  22. return 'settings_apps';
  23. }
  24. public function getName(): string {
  25. return $this->l->t('Apps');
  26. }
  27. public function getOrder(string $route, array $routeParameters): int {
  28. return $route === 'settings.AppSettings.viewApps' ? -50 : 100;
  29. }
  30. public function search(IUser $user, ISearchQuery $query): SearchResult {
  31. $entries = $this->navigationManager->getAll('all');
  32. $searchTitle = $this->l->t('Apps');
  33. $term = $query->getFilter('term')?->get();
  34. if (empty($term)) {
  35. return SearchResult::complete($searchTitle, []);
  36. }
  37. $result = [];
  38. foreach ($entries as $entry) {
  39. if (
  40. stripos($entry['name'], $term) === false &&
  41. stripos($entry['id'], $term) === false
  42. ) {
  43. continue;
  44. }
  45. if (str_starts_with($query->getRoute(), $entry['id'] . '.')) {
  46. // Skip the current app, unlikely this is intended
  47. continue;
  48. }
  49. if ($entry['href'] === '') {
  50. // Nothing we can open, so ignore
  51. continue;
  52. }
  53. $result[] = new SearchResultEntry(
  54. '',
  55. $entry['name'],
  56. '',
  57. $entry['href'],
  58. 'icon-confirm'
  59. );
  60. }
  61. return SearchResult::complete($searchTitle, $result);
  62. }
  63. }