AppSearch.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Search;
  25. use OCP\IL10N;
  26. use OCP\INavigationManager;
  27. use OCP\IUser;
  28. use OCP\Search\IProvider;
  29. use OCP\Search\ISearchQuery;
  30. use OCP\Search\SearchResult;
  31. use OCP\Search\SearchResultEntry;
  32. class AppSearch implements IProvider {
  33. public function __construct(
  34. protected INavigationManager $navigationManager,
  35. protected IL10N $l,
  36. ) {
  37. }
  38. public function getId(): string {
  39. return 'settings_apps';
  40. }
  41. public function getName(): string {
  42. return $this->l->t('Apps');
  43. }
  44. public function getOrder(string $route, array $routeParameters): int {
  45. return $route === 'settings.AppSettings.viewApps' ? -50 : 100;
  46. }
  47. public function search(IUser $user, ISearchQuery $query): SearchResult {
  48. $entries = $this->navigationManager->getAll('all');
  49. $searchTitle = $this->l->t('Apps');
  50. $term = $query->getFilter('term')?->get();
  51. if (empty($term)) {
  52. return SearchResult::complete($searchTitle, []);
  53. }
  54. $result = [];
  55. foreach ($entries as $entry) {
  56. if (
  57. stripos($entry['name'], $term) === false &&
  58. stripos($entry['id'], $term) === false
  59. ) {
  60. continue;
  61. }
  62. if (str_starts_with($query->getRoute(), $entry['id'] . '.')) {
  63. // Skip the current app, unlikely this is intended
  64. continue;
  65. }
  66. if ($entry['href'] === '') {
  67. // Nothing we can open, so ignore
  68. continue;
  69. }
  70. $result[] = new SearchResultEntry(
  71. '',
  72. $entry['name'],
  73. '',
  74. $entry['href'],
  75. 'icon-confirm'
  76. );
  77. }
  78. return SearchResult::complete($searchTitle, $result);
  79. }
  80. }