diff --git a/lib/Db/FormMapper.php b/lib/Db/FormMapper.php index 89a032f3b..2e459e49b 100644 --- a/lib/Db/FormMapper.php +++ b/lib/Db/FormMapper.php @@ -11,6 +11,8 @@ use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; +use OCP\IUser; +use OCP\Search\ISearchQuery; use OCP\Share\IShare; /** @@ -180,6 +182,18 @@ public function findAllByOwnerId(string $ownerId): array { return $this->findEntities($qb); } + /** + * @param IUser $user the user that performs the search + * @param ISearchQuery $query the query to search the forms + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @return Form[] array of Forms + */ + public function search(IUser $user, ISearchQuery $query): array { + $qb = $this->db->getQueryBuilder(); + // TODO: implement search for Query Builder + return $this->findEntities($qb); + } + /** * Delete a Form including connected Questions, Submissions and shares. * @param Form $form The form instance to delete diff --git a/lib/Search/FormsSearchResultEntry.php b/lib/Search/FormsSearchResultEntry.php new file mode 100644 index 000000000..7e0814e5a --- /dev/null +++ b/lib/Search/FormsSearchResultEntry.php @@ -0,0 +1,18 @@ +getTitle(), $form->getDescription(), $formUrl, 'forms-dark'); + } +} diff --git a/lib/Search/Provider.php b/lib/Search/Provider.php new file mode 100644 index 000000000..712a6b776 --- /dev/null +++ b/lib/Search/Provider.php @@ -0,0 +1,68 @@ +l10n->t('Forms'); + } + + public function search(IUser $user, ISearchQuery $query): SearchResult { + $forms = $this->formsService->search($user, $query); + + $results = array_map(function (Form $form) { + $formUrl = $this->urlGenerator->linkToRoute('forms.page.views', ['hash' => $form->getHash(), 'view' => 'submit']); + return [ + 'object' => $form, + 'entry' => new FormsSearchResultEntry($form, $formUrl) + ]; + }, $forms); + + $resultEntries = array_map(function (array $result) { + return $result['entry']; + }, $results); + + return SearchResult::complete( + $this->l10n->t('Forms'), + $resultEntries + ); + } + + public function getOrder(string $route, array $routeParameters): int { + if (str_contains($route, Application::APP_ID)) { + // Active app, prefer my results + return -1; + } + return 77; + } +} diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index d9be9a3c8..c992447a5 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -29,6 +29,7 @@ use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; +use OCP\Search\ISearchQuery; use OCP\Security\ISecureRandom; use OCP\Share\IShare; @@ -698,6 +699,23 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType return true; } + /** + * Get list of forms + * + * @param IUser $user the user that performs the search + * @param ISearchQuery $query the query to search the forms + * @return array list of forms that match the query + */ + public function search(IUser $user, ISearchQuery $query): array { + $formsList = []; + try { + $formsList = $this->formMapper->search($user, $query); + } catch (DoesNotExistException $e) { + // silent catch + } + return $formsList; + } + public function getFilePath(Form $form): ?string { $fileId = $form->getFileId();