Skip to content

Commit

Permalink
integrate optional search into existing functions
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Jan 4, 2025
1 parent ab8047a commit 68fd18e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
38 changes: 25 additions & 13 deletions lib/Db/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ public function findByHash(string $hash): Form {
* @param string[] $groups IDs of groups the user is memeber of
* @param string[] $teams IDs of teams the user is memeber of
* @param bool $filterShown Set to false to also include forms shared but not visible on sidebar
* @param string $queryTerm optional: The search query for universal search
* @return Form[]
*/
public function findSharedForms(string $userId, array $groups = [], array $teams = [], bool $filterShown = true): array {
public function findSharedForms(string $userId, array $groups = [], array $teams = [], bool $filterShown = true, ?string $queryTerm = null): array {
$qbShares = $this->db->getQueryBuilder();
$qbForms = $this->db->getQueryBuilder();

Expand Down Expand Up @@ -158,16 +159,28 @@ public function findSharedForms(string $userId, array $groups = [], array $teams
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

if ($queryTerm) {
$qbForms->andWhere(
$qbForms->orWhere(
$qbForms->expr()->like('title', $qbForms->createNamedParameter('%' . $queryTerm . '%'))
)
->orWhere(
$qbForms->expr()->like('description', $qbForms->createNamedParameter('%' . $queryTerm . '%'))
)
);

Check warning on line 170 in lib/Db/FormMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/FormMapper.php#L163-L170

Added lines #L163 - L170 were not covered by tests
}

// We need to add the parameters from the shared forms IDs select to the final select query
$qbForms->setParameters($qbShares->getParameters(), $qbShares->getParameterTypes());

return $this->findEntities($qbForms);
}

/**
* @param string $queryTerm optional: The search query for universal search
* @return Form[]
*/
public function findAllByOwnerId(string $ownerId): array {
public function findAllByOwnerId(string $ownerId, ?string $queryTerm = null): array {

Check warning on line 183 in lib/Db/FormMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/FormMapper.php#L183

Added line #L183 was not covered by tests
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand All @@ -179,18 +192,17 @@ public function findAllByOwnerId(string $ownerId): array {
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

return $this->findEntities($qb);
}
if ($queryTerm) {
$qb->andWhere(
$qb->orWhere(
$qb->expr()->like('title', $qb->createNamedParameter('%' . $queryTerm . '%'))
)
->orWhere(
$qb->expr()->like('description', $qb->createNamedParameter('%' . $queryTerm . '%'))
)
);

Check warning on line 203 in lib/Db/FormMapper.php

View check run for this annotation

Codecov / codecov/patch

lib/Db/FormMapper.php#L195-L203

Added lines #L195 - L203 were not covered by tests
}

/**
* @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);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Search/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getName(): string {
}

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

Check warning on line 41 in lib/Search/Provider.php

View check run for this annotation

Codecov / codecov/patch

lib/Search/Provider.php#L40-L41

Added lines #L40 - L41 were not covered by tests

$results = array_map(function (Form $form) {
$formUrl = $this->urlGenerator->linkToRoute('forms.page.views', ['hash' => $form->getHash(), 'view' => 'submit']);
Expand Down
15 changes: 12 additions & 3 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,14 +702,23 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
/**
* 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 {
public function search(ISearchQuery $query): array {
$formsList = [];
$groups = $this->groupManager->getUserGroupIds($this->currentUser);
$teams = $this->circlesService->getUserTeamIds($this->currentUser->getUID());

Check warning on line 711 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L708-L711

Added lines #L708 - L711 were not covered by tests

try {
$formsList = $this->formMapper->search($user, $query);
$formsList = $this->formMapper->findAllByOwnerId($this->currentUser->getUID(), $query->getTerm());
$formsList[] = $this->formMapper->findSharedForms(
$this->currentUser->getUID(),
$groups,
$teams,
true,
$query->getTerm()
);
} catch (DoesNotExistException $e) {

Check warning on line 722 in lib/Service/FormsService.php

View check run for this annotation

Codecov / codecov/patch

lib/Service/FormsService.php#L714-L722

Added lines #L714 - L722 were not covered by tests
// silent catch
}
Expand Down

0 comments on commit 68fd18e

Please sign in to comment.