mirror of
https://github.com/JanGross/quicknotes.git
synced 2025-12-01 07:37:18 +01:00
Implement an basic search provider. Just search within the title and main note.
On the other hand, it implements consistent urls, which allow you to mark a note, tag or color as a favorite in the browser to easily access the notes.
This commit is contained in:
@@ -31,6 +31,7 @@ use OCP\IL10N;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IServerContainer;
|
||||
|
||||
use OCA\QuickNotes\Search\NoteSearchProvider;
|
||||
|
||||
class Application extends App implements IBootstrap {
|
||||
|
||||
@@ -45,6 +46,7 @@ class Application extends App implements IBootstrap {
|
||||
}
|
||||
|
||||
public function register(IRegistrationContext $context): void {
|
||||
$context->registerSearchProvider(NoteSearchProvider::class);
|
||||
$context->registerCapability(Capabilities::class);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,34 @@ class NoteMapper extends QBMapper {
|
||||
return $this->findEntity($qb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userId
|
||||
* @param string $queryStr
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
* @return Note[]
|
||||
*/
|
||||
public function findLike($userId, $queryStr, $offset = null, $limit = null) {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->select('*')
|
||||
->from($this->tableName)
|
||||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)))
|
||||
->andWhere(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->like($qb->func()->lower('title'), $qb->createParameter('query')),
|
||||
$qb->expr()->like($qb->func()->lower('content'), $qb->createParameter('query'))
|
||||
)
|
||||
);
|
||||
|
||||
$query = '%' . $this->db->escapeLikeParameter(strtolower($queryStr)) . '%';
|
||||
$qb->setParameter('query', $query);
|
||||
|
||||
$qb->setFirstResult($offset);
|
||||
$qb->setMaxResults($limit);
|
||||
|
||||
return $this->findEntities($qb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
|
||||
114
lib/Search/NoteSearchProvider.php
Normal file
114
lib/Search/NoteSearchProvider.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2021 Matias De lellis <mati86dl@gmail.com>
|
||||
*
|
||||
* @author Matias De lellis <mati86dl@gmail.com>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\QuickNotes\Search;
|
||||
|
||||
use OCP\Search\IProvider;
|
||||
use OCP\IL10N;
|
||||
use OCP\IURLGenerator;
|
||||
use OCP\IUser;
|
||||
use OCP\Search\ISearchQuery;
|
||||
use OCP\Search\SearchResult;
|
||||
use OCP\Search\SearchResultEntry;
|
||||
|
||||
use OCA\QuickNotes\Db\Note;
|
||||
use OCA\QuickNotes\Db\NoteMapper;
|
||||
|
||||
/**
|
||||
* Provide search results from the 'quicknotes' app
|
||||
*/
|
||||
class NoteSearchProvider implements IProvider {
|
||||
|
||||
/** @var NoteMapper noteMapper */
|
||||
private $noteMapper;
|
||||
|
||||
/** @var IL10N */
|
||||
private $l10n;
|
||||
|
||||
/** @var IURLGenerator */
|
||||
private $urlGenerator;
|
||||
|
||||
public function __construct(
|
||||
IL10N $l10n,
|
||||
IURLGenerator $urlGenerator,
|
||||
NoteMapper $noteMapper
|
||||
) {
|
||||
$this->l10n = $l10n;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->noteMapper = $noteMapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getId(): string {
|
||||
return 'quicknotes';
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getName(): string {
|
||||
return $this->l10n->t('Quick notes');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getOrder(string $route, array $routeParameters): int {
|
||||
if (strpos($route, 'quicknotes.') === 0) {
|
||||
return -1;
|
||||
}
|
||||
return 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function search(IUser $user, ISearchQuery $query) : SearchResult {
|
||||
$page = $query->getCursor() ?? 0;
|
||||
$limit = $query->getLimit();
|
||||
return SearchResult::paginated(
|
||||
$this->l10n->t('Quick notes'),
|
||||
array_map(function (Note $result) {
|
||||
$noteId = $result->getId();
|
||||
$noteTitle = strip_tags($result->getTitle());
|
||||
return new SearchResultEntry(
|
||||
'',
|
||||
$noteTitle,
|
||||
'',
|
||||
$this->urlGenerator->linkToRoute('quicknotes.page.index', ['n' => $noteId]),
|
||||
'icon-edit',
|
||||
true,
|
||||
);
|
||||
},
|
||||
$this->noteMapper->findLike($user->getUID(),
|
||||
$query->getTerm(),
|
||||
$page * $limit,
|
||||
$limit)
|
||||
),
|
||||
$page);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user