Passed
Push — master ( 058135...a4d511 )
by John
13:28 queued 12s
created

SectionSearch::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2020 Joas Schilling <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Settings\Search;
25
26
use OCP\IGroupManager;
27
use OCP\IL10N;
28
use OCP\IURLGenerator;
29
use OCP\IUser;
30
use OCP\Search\IProvider;
31
use OCP\Search\ISearchQuery;
32
use OCP\Search\SearchResult;
33
use OCP\Search\SearchResultEntry;
34
use OCP\Settings\ISection;
35
use OCP\Settings\IManager;
36
37
class SectionSearch implements IProvider {
38
39
	/** @var IManager */
40
	protected $settingsManager;
41
42
	/** @var IGroupManager */
43
	protected $groupManager;
44
45
	/** @var IURLGenerator */
46
	protected $urlGenerator;
47
48
	/** @var IL10N */
49
	protected $l;
50
51
	public function __construct(IManager $settingsManager,
52
								IGroupManager $groupManager,
53
								IURLGenerator $urlGenerator,
54
								IL10N $l) {
55
		$this->settingsManager = $settingsManager;
56
		$this->groupManager = $groupManager;
57
		$this->urlGenerator = $urlGenerator;
58
		$this->l = $l;
59
	}
60
61
	/**
62
	 * @inheritDoc
63
	 */
64
	public function getId(): string {
65
		return 'settings_sections';
66
	}
67
68
	/**
69
	 * @inheritDoc
70
	 */
71
	public function getName(): string {
72
		return $this->l->t('Settings');
73
	}
74
75
	/**
76
	 * @inheritDoc
77
	 */
78
	public function getOrder(): int {
79
		return 20;
80
	}
81
82
	/**
83
	 * @inheritDoc
84
	 */
85
	public function search(IUser $user, ISearchQuery $query): SearchResult {
86
		$isAdmin = $this->groupManager->isAdmin($user->getUID());
87
88
		$result = $this->searchSections(
89
			$query,
90
			$this->settingsManager->getPersonalSections(),
91
			$isAdmin ? $this->l->t('Personal') : '',
92
			'settings.PersonalSettings.index'
93
		);
94
95
		if ($this->groupManager->isAdmin($user->getUID())) {
96
			$result = array_merge($result, $this->searchSections(
97
				$query,
98
				$this->settingsManager->getAdminSections(),
99
				$this->l->t('Administration'),
100
				'settings.AdminSettings.index'
101
			));
102
		}
103
104
		return SearchResult::complete(
105
			$this->l->t('Settings'),
106
			$result
107
		);
108
	}
109
110
	/**
111
	 * @param ISearchQuery $query
112
	 * @param ISection[][] $sections
113
	 * @param string $subline
114
	 * @param string $routeName
115
	 * @return array
116
	 */
117
	public function searchSections(ISearchQuery $query, array $sections, string $subline, string $routeName): array {
118
		$result = [];
119
		foreach ($sections as $priority => $sectionsByPriority) {
120
			foreach ($sectionsByPriority as $section) {
121
				if (
122
					stripos($section->getName(), $query->getTerm()) === false &&
123
					stripos($section->getID(), $query->getTerm()) === false
124
				) {
125
					continue;
126
				}
127
128
				/**
129
				 * We can't use the icon URL at the moment as they don't invert correctly for dark theme
130
				 * $iconUrl = '';
131
				 * if ($section instanceof IIconSection) {
132
				 * $iconUrl = $section->getIcon();
133
				 * }
134
				 */
135
136
				$result[] = new SearchResultEntry(
137
					'',
138
					$section->getName(),
139
					$subline,
140
					$this->urlGenerator->linkToRouteAbsolute($routeName, ['section' => $section->getID()]),
141
					'icon-settings'
142
				);
143
			}
144
		}
145
146
		return $result;
147
	}
148
}
149