Skip to content

Commit

Permalink
NC20 Search integration #66
Browse files Browse the repository at this point in the history
  • Loading branch information
Rello committed Sep 3, 2020
1 parent 2ab278d commit f93ee01
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/AppInfo/Application20.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Analytics\Flow\Operation;
use OCA\Analytics\Listener\LoadAdditionalScripts;
use OCA\Analytics\Notification\Notifier;
use OCA\Analytics\Search\Provider;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
Expand All @@ -34,6 +35,7 @@ public function register(IRegistrationContext $context): void
{
$context->registerDashboardWidget(Widget::class);
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScripts::class);
$context->registerSearchProvider(Provider::class);
$this->registerNavigationEntry();
$this->registerNotifications();
}
Expand Down
12 changes: 12 additions & 0 deletions lib/Controller/DatasetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ public function read(int $datasetId)
return $this->getOwnDataset($datasetId);
}

/**
* search for datasets
*
* @NoAdminRequired
* @param string $searchString
* @return array
*/
public function search(string $searchString)
{
return $this->DatasetMapper->search($searchString);
}

/**
* get own dataset details
*
Expand Down
21 changes: 21 additions & 0 deletions lib/Db/DatasetMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,27 @@ public function getDatasets()
return $result;
}

/**
* search datasets by searchstring
* @param $searchString
* @return array
*/
public function search($searchString)
{
$sql = $this->db->getQueryBuilder();
$sql->from(self::TABLE_NAME)
->select('id')
->addSelect('name')
->addSelect('type')
->where($sql->expr()->eq('user_id', $sql->createNamedParameter($this->userId)))
->andWhere($sql->expr()->like('name', $sql->createNamedParameter('%' . $searchString . '%')))
->orderBy('name', 'ASC');
$statement = $sql->execute();
$result = $statement->fetchAll();
$statement->closeCursor();
return $result;
}

/**
* get datasets
* @param int $id
Expand Down
79 changes: 79 additions & 0 deletions lib/Search/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Analytics
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the LICENSE.md file.
*
* @author Marcel Scherello <audioplayer@scherello.de>
* @copyright 2020 Marcel Scherello
*/

declare(strict_types=1);

namespace OCA\Analytics\Search;

use OCA\Analytics\Controller\DatasetController;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Search\IProvider;
use OCP\Search\ISearchQuery;
use OCP\Search\SearchResult;
use OCP\Search\SearchResultEntry;

class Provider implements IProvider
{

/** @var IL10N */
private $l10n;

/** @var IURLGenerator */
private $urlGenerator;

private $datasetController;

public function __construct(IL10N $l10n,
IURLGenerator $urlGenerator,
DatasetController $datasetController)
{
$this->l10n = $l10n;
$this->urlGenerator = $urlGenerator;
$this->datasetController = $datasetController;
}

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

public function search(IUser $user, ISearchQuery $query): SearchResult
{
$datasets = $this->datasetController->search($query->getTerm());

foreach ($datasets as $dataset) {
$result[] = new SearchResultEntry(
'',
$dataset['name'],
'',
$this->urlGenerator->linkToRoute('analytics.page.index') . '#/r/' . $dataset['id'],
$this->urlGenerator->imagePath('analytics', 'app-dark.svg')
);
}

return SearchResult::complete(
$this->l10n->t('Analytics'),
$result
);
}

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

public function getOrder(string $route, array $routeParameters): int
{
return 10;
}
}

0 comments on commit f93ee01

Please sign in to comment.