Skip to content

Commit 6c57cf1

Browse files
committed
initial integration of Search Provider
Signed-off-by: Christian Hartmann <[email protected]>
1 parent 7afb7e2 commit 6c57cf1

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

lib/Db/FormMapper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use OCP\AppFramework\Db\QBMapper;
1212
use OCP\DB\QueryBuilder\IQueryBuilder;
1313
use OCP\IDBConnection;
14+
use OCP\IUser;
15+
use OCP\Search\ISearchQuery;
1416
use OCP\Share\IShare;
1517

1618
/**
@@ -180,6 +182,18 @@ public function findAllByOwnerId(string $ownerId): array {
180182
return $this->findEntities($qb);
181183
}
182184

185+
/**
186+
* @param IUser $user the user that performs the search
187+
* @param ISearchQuery $query the query to search the forms
188+
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
189+
* @return Form[] array of Forms
190+
*/
191+
public function search(IUser $user, ISearchQuery $query): array {
192+
$qb = $this->db->getQueryBuilder();
193+
// TODO: implement search for Query Builder
194+
return $this->findEntities($qb);
195+
}
196+
183197
/**
184198
* Delete a Form including connected Questions, Submissions and shares.
185199
* @param Form $form The form instance to delete

lib/Search/FormsSearchResultEntry.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Forms\Search;
10+
11+
use OCA\Forms\Db\Form;
12+
use OCP\Search\SearchResultEntry;
13+
14+
class FormsSearchResultEntry extends SearchResultEntry {
15+
public function __construct(Form $form, string $formUrl) {
16+
parent::__construct('', $form->getTitle(), $form->getDescription(), $formUrl, 'forms-dark');
17+
}
18+
}

lib/Search/Provider.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Forms\Search;
10+
11+
use OCA\Forms\AppInfo\Application;
12+
use OCA\Forms\Db\Form;
13+
use OCA\Forms\Service\FormsService;
14+
use OCP\IL10N;
15+
use OCP\IURLGenerator;
16+
use OCP\IUser;
17+
use OCP\Search\IProvider;
18+
use OCP\Search\ISearchQuery;
19+
use OCP\Search\SearchResult;
20+
21+
class SearchProvider implements IProvider {
22+
/**
23+
* @psalm-suppress PossiblyUnusedMethod
24+
*/
25+
public function __construct(
26+
private IL10N $l10n,
27+
private IURLGenerator $urlGenerator,
28+
private FormsService $formsService,
29+
) {
30+
}
31+
32+
public function getId(): string {
33+
return 'forms';
34+
}
35+
36+
public function getName(): string {
37+
return $this->l10n->t('Forms');
38+
}
39+
40+
public function search(IUser $user, ISearchQuery $query): SearchResult {
41+
$forms = $this->formsService->search($user, $query);
42+
43+
$results = array_map(function (Form $form) {
44+
$formUrl = $this->urlGenerator->linkToRoute('forms.page.views', ['hash' => $form->getHash(), 'view' => 'submit']);
45+
return [
46+
'object' => $form,
47+
'entry' => new FormsSearchResultEntry($form, $formUrl)
48+
];
49+
}, $forms);
50+
51+
$resultEntries = array_map(function (array $result) {
52+
return $result['entry'];
53+
}, $results);
54+
55+
return SearchResult::complete(
56+
$this->l10n->t('Forms'),
57+
$resultEntries
58+
);
59+
}
60+
61+
public function getOrder(string $route, array $routeParameters): int {
62+
if (str_contains($route, Application::APP_ID)) {
63+
// Active app, prefer my results
64+
return -1;
65+
}
66+
return 77;
67+
}
68+
}

lib/Service/FormsService.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use OCA\Forms\Events\FormSubmittedEvent;
2121
use OCP\AppFramework\Db\DoesNotExistException;
2222
use OCP\AppFramework\Db\IMapperException;
23+
use OCP\DB\Exception;
2324
use OCP\EventDispatcher\IEventDispatcher;
2425
use OCP\Files\IRootFolder;
2526
use OCP\Files\NotFoundException;
@@ -29,6 +30,7 @@
2930
use OCP\IUser;
3031
use OCP\IUserManager;
3132
use OCP\IUserSession;
33+
use OCP\Search\ISearchQuery;
3234
use OCP\Security\ISecureRandom;
3335
use OCP\Share\IShare;
3436

@@ -698,6 +700,23 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
698700
return true;
699701
}
700702

703+
/**
704+
* Get list of forms
705+
*
706+
* @param IUser $user the user that performs the search
707+
* @param ISearchQuery $query the query to search the forms
708+
* @return array list of forms that match the query
709+
*/
710+
public function search(IUser $user, ISearchQuery $query): array {
711+
$formsList = [];
712+
try {
713+
$formsList = $this->formMapper->search($user, $query);
714+
} catch (DoesNotExistException $e) {
715+
// silent catch
716+
}
717+
return $formsList;
718+
}
719+
701720
public function getFilePath(Form $form): ?string {
702721
$fileId = $form->getFileId();
703722

0 commit comments

Comments
 (0)