Skip to content

Commit

Permalink
make Forms available in unified search
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Hartmann <[email protected]>
  • Loading branch information
Chartman123 committed Jan 5, 2025
1 parent 7afb7e2 commit d935091
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
11 changes: 11 additions & 0 deletions css/forms.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@
html {
scroll-padding-top: calc(var(--header-height) + 60px);
}

.icon-forms {
background-image: url(../img/forms-dark.svg);
filter: var(--background-invert-if-dark);
}

.icon-forms-white,
.icon-forms.icon-white {
background-image: url(../img/forms.svg);
filter: var(--background-invert-if-dark);
}
2 changes: 2 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Forms\FormsMigrator;
use OCA\Forms\Listener\AnalyticsDatasourceListener;
use OCA\Forms\Listener\UserDeletedListener;
use OCA\Forms\Search\SearchProvider;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
Expand Down Expand Up @@ -42,6 +43,7 @@ public function register(IRegistrationContext $context): void {
$context->registerCapability(Capabilities::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(DatasourceEvent::class, AnalyticsDatasourceListener::class);
$context->registerSearchProvider(SearchProvider::class);
$context->registerUserMigrator(FormsMigrator::class);
}

Expand Down
16 changes: 14 additions & 2 deletions lib/Db/FormMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,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 @@ -156,16 +157,22 @@ public function findSharedForms(string $userId, array $groups = [], array $teams
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

if ($queryTerm) {
$qbForms->andWhere($qbForms->expr()->iLike('title', $qbForms->createNamedParameter('%' . $this->db->escapeLikeParameter($queryTerm) . '%')) .
' OR ' . $qbForms->expr()->iLike('description', $qbForms->createNamedParameter('%' . $this->db->escapeLikeParameter($queryTerm) . '%')));
}

// 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 {
$qb = $this->db->getQueryBuilder();

$qb->select('*')
Expand All @@ -177,6 +184,11 @@ public function findAllByOwnerId(string $ownerId): array {
->addOrderBy('last_updated', 'DESC')
->addOrderBy('created', 'DESC');

if ($queryTerm) {
$qb->andWhere($qb->expr()->iLike('title', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($queryTerm) . '%')) .
' OR ' . $qb->expr()->iLike('description', $qb->createNamedParameter('%' . $this->db->escapeLikeParameter($queryTerm) . '%')));
}

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

Expand Down
22 changes: 22 additions & 0 deletions lib/Search/FormsSearchResultEntry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Forms\Search;

use OCA\Forms\AppInfo\Application;
use OCA\Forms\Db\Form;
use OCP\IURLGenerator;
use OCP\Search\SearchResultEntry;

class FormsSearchResultEntry extends SearchResultEntry {
public function __construct(Form $form, IURLGenerator $urlGenerator) {
$formURL = $urlGenerator->linkToRoute('forms.page.views', ['hash' => $form->getHash(), 'view' => 'submit']);
$iconURL = $urlGenerator->getAbsoluteURL(($urlGenerator->imagePath(Application::APP_ID, 'forms-dark.svg')));
parent::__construct($iconURL, $form->getTitle(), $form->getDescription(), $formURL, 'icon-forms-dark');
}
}
67 changes: 67 additions & 0 deletions lib/Search/SearchProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Forms\Search;

use OCA\Forms\AppInfo\Application;
use OCA\Forms\Db\Form;
use OCA\Forms\Service\FormsService;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;

class SearchProvider implements IProvider {
/**
* @psalm-suppress PossiblyUnusedMethod
*/
public function __construct(
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private FormsService $formsService,
) {
}

public function getId(): string {
return 'forms';
}

public function getName(): string {
return $this->l10n->t('Forms');
}

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

$results = array_map(function (Form $form) {
return [
'object' => $form,
'entry' => new FormsSearchResultEntry($form, $this->urlGenerator)
];
}, $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;
}
}
28 changes: 28 additions & 0 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -698,6 +699,33 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType
return true;
}

/**
* Get list of forms
*
* @param ISearchQuery $query the query to search the forms
* @return Form[] list of forms that match the query
*/
public function search(ISearchQuery $query): array {
$formsList = [];
$groups = $this->groupManager->getUserGroupIds($this->currentUser);
$teams = $this->circlesService->getUserTeamIds($this->currentUser->getUID());

try {
$ownedForms = $this->formMapper->findAllByOwnerId($this->currentUser->getUID(), $query->getTerm());
$sharedForms = $this->formMapper->findSharedForms(
$this->currentUser->getUID(),
$groups,
$teams,
true,
$query->getTerm()
);
$formsList = array_merge($ownedForms, $sharedForms);
} catch (DoesNotExistException $e) {
// silent catch
}
return $formsList;
}

public function getFilePath(Form $form): ?string {
$fileId = $form->getFileId();

Expand Down

0 comments on commit d935091

Please sign in to comment.