AppSearch.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /** @var INavigationManager */
  34. protected $navigationManager;
  35. /** @var IL10N */
  36. protected $l;
  37. public function __construct(INavigationManager $navigationManager,
  38. IL10N $l) {
  39. $this->navigationManager = $navigationManager;
  40. $this->l = $l;
  41. }
  42. /**
  43. * @inheritDoc
  44. */
  45. public function getId(): string {
  46. return 'settings_apps';
  47. }
  48. /**
  49. * @inheritDoc
  50. */
  51. public function getName(): string {
  52. return $this->l->t('Apps');
  53. }
  54. /**
  55. * @inheritDoc
  56. */
  57. public function getOrder(string $route, array $routeParameters): int {
  58. return -50;
  59. }
  60. /**
  61. * @inheritDoc
  62. */
  63. public function search(IUser $user, ISearchQuery $query): SearchResult {
  64. $entries = $this->navigationManager->getAll('all');
  65. $result = [];
  66. foreach ($entries as $entry) {
  67. if (
  68. stripos($entry['name'], $query->getTerm()) === false &&
  69. stripos($entry['id'], $query->getTerm()) === false
  70. ) {
  71. continue;
  72. }
  73. if (strpos($query->getRoute(), $entry['id'] . '.') === 0) {
  74. // Skip the current app, unlikely this is intended
  75. continue;
  76. }
  77. if ($entry['href'] === '') {
  78. // Nothing we can open, so ignore
  79. continue;
  80. }
  81. $result[] = new SearchResultEntry(
  82. '',
  83. $entry['name'],
  84. '',
  85. $entry['href'],
  86. 'icon-confirm'
  87. );
  88. }
  89. return SearchResult::complete(
  90. $this->l->t('Apps'),
  91. $result
  92. );
  93. }
  94. }