aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/lib/Search/AppSearch.php
blob: fbc799e6f045327576cabf7401a1d96d7dafd516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\Settings\Search;

use OCP\IL10N;
use OCP\INavigationManager;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;

class AppSearch implements IProvider {
	public function __construct(
		protected INavigationManager $navigationManager,
		protected IL10N $l,
	) {
	}

	public function getId(): string {
		return 'settings_apps';
	}

	public function getName(): string {
		return $this->l->t('Apps');
	}

	public function getOrder(string $route, array $routeParameters): int {
		return $route === 'settings.AppSettings.viewApps' ? -50 : 100;
	}

	public function search(IUser $user, ISearchQuery $query): SearchResult {
		$entries = $this->navigationManager->getAll('all');

		$searchTitle = $this->l->t('Apps');
		$term = $query->getFilter('term')?->get();
		if (empty($term)) {
			return SearchResult::complete($searchTitle, []);
		}

		$result = [];
		foreach ($entries as $entry) {
			if (
				stripos($entry['name'], $term) === false &&
				stripos($entry['id'], $term) === false
			) {
				continue;
			}

			if (str_starts_with($query->getRoute(), $entry['id'] . '.')) {
				// Skip the current app, unlikely this is intended
				continue;
			}

			if ($entry['href'] === '') {
				// Nothing we can open, so ignore
				continue;
			}

			$result[] = new SearchResultEntry(
				'',
				$entry['name'],
				'',
				$entry['href'],
				'icon-confirm'
			);
		}

		return SearchResult::complete($searchTitle, $result);
	}
}