Skip to content

Commit 6df2898

Browse files
committed
integrate optional search into existing functions
Signed-off-by: Christian Hartmann <[email protected]>
1 parent ab8047a commit 6df2898

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

lib/Db/FormMapper.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
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;
1614
use OCP\Share\IShare;
1715

1816
/**
@@ -99,9 +97,10 @@ public function findByHash(string $hash): Form {
9997
* @param string[] $groups IDs of groups the user is memeber of
10098
* @param string[] $teams IDs of teams the user is memeber of
10199
* @param bool $filterShown Set to false to also include forms shared but not visible on sidebar
100+
* @param string $queryTerm optional: The search query for universal search
102101
* @return Form[]
103102
*/
104-
public function findSharedForms(string $userId, array $groups = [], array $teams = [], bool $filterShown = true): array {
103+
public function findSharedForms(string $userId, array $groups = [], array $teams = [], bool $filterShown = true, ?string $queryTerm = null): array {
105104
$qbShares = $this->db->getQueryBuilder();
106105
$qbForms = $this->db->getQueryBuilder();
107106

@@ -158,16 +157,28 @@ public function findSharedForms(string $userId, array $groups = [], array $teams
158157
->addOrderBy('last_updated', 'DESC')
159158
->addOrderBy('created', 'DESC');
160159

160+
if ($queryTerm) {
161+
$qbForms->andWhere(
162+
$qbForms->orWhere(
163+
$qbForms->expr()->like('title', $qbForms->createNamedParameter('%' . $queryTerm . '%'))
164+
)
165+
->orWhere(
166+
$qbForms->expr()->like('description', $qbForms->createNamedParameter('%' . $queryTerm . '%'))
167+
)
168+
);
169+
}
170+
161171
// We need to add the parameters from the shared forms IDs select to the final select query
162172
$qbForms->setParameters($qbShares->getParameters(), $qbShares->getParameterTypes());
163173

164174
return $this->findEntities($qbForms);
165175
}
166176

167177
/**
178+
* @param string $queryTerm optional: The search query for universal search
168179
* @return Form[]
169180
*/
170-
public function findAllByOwnerId(string $ownerId): array {
181+
public function findAllByOwnerId(string $ownerId, ?string $queryTerm = null): array {
171182
$qb = $this->db->getQueryBuilder();
172183

173184
$qb->select('*')
@@ -179,18 +190,17 @@ public function findAllByOwnerId(string $ownerId): array {
179190
->addOrderBy('last_updated', 'DESC')
180191
->addOrderBy('created', 'DESC');
181192

182-
return $this->findEntities($qb);
183-
}
193+
if ($queryTerm) {
194+
$qb->andWhere(
195+
$qb->orWhere(
196+
$qb->expr()->like('title', $qb->createNamedParameter('%' . $queryTerm . '%'))
197+
)
198+
->orWhere(
199+
$qb->expr()->like('description', $qb->createNamedParameter('%' . $queryTerm . '%'))
200+
)
201+
);
202+
}
184203

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
194204
return $this->findEntities($qb);
195205
}
196206

lib/Search/Provider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getName(): string {
3838
}
3939

4040
public function search(IUser $user, ISearchQuery $query): SearchResult {
41-
$forms = $this->formsService->search($user, $query);
41+
$forms = $this->formsService->search($query);
4242

4343
$results = array_map(function (Form $form) {
4444
$formUrl = $this->urlGenerator->linkToRoute('forms.page.views', ['hash' => $form->getHash(), 'view' => 'submit']);

lib/Service/FormsService.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,14 +702,23 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
702702
/**
703703
* Get list of forms
704704
*
705-
* @param IUser $user the user that performs the search
706705
* @param ISearchQuery $query the query to search the forms
707706
* @return array list of forms that match the query
708707
*/
709-
public function search(IUser $user, ISearchQuery $query): array {
708+
public function search(ISearchQuery $query): array {
710709
$formsList = [];
710+
$groups = $this->groupManager->getUserGroupIds($this->currentUser);
711+
$teams = $this->circlesService->getUserTeamIds($this->currentUser->getUID());
712+
711713
try {
712-
$formsList = $this->formMapper->search($user, $query);
714+
$formsList = $this->formMapper->findAllByOwnerId($this->currentUser->getUID(), $query->getTerm());
715+
$formsList[] = $this->formMapper->findSharedForms(
716+
$this->currentUser->getUID(),
717+
$groups,
718+
$teams,
719+
true,
720+
$query->getTerm()
721+
);
713722
} catch (DoesNotExistException $e) {
714723
// silent catch
715724
}

0 commit comments

Comments
 (0)