Commit 0f0ab35d authored by Tino Goratsch's avatar Tino Goratsch

further refactoring of the search module

parent 89035ae7
......@@ -2,7 +2,6 @@ services:
articles.search.search_availability:
class: ACP3\Modules\ACP3\Articles\Search\SearchAvailability
arguments:
- '@core.acl'
- '@core.date'
- '@core.router'
- '@articles.model.articlerepository'
......
......@@ -7,7 +7,6 @@
namespace ACP3\Modules\ACP3\Articles\Search;
use ACP3\Core\ACL;
use ACP3\Core\Date;
use ACP3\Core\Router\RouterInterface;
use ACP3\Modules\ACP3\Articles\Installer\Schema;
......@@ -16,10 +15,6 @@ use ACP3\Modules\ACP3\Search\Utility\SearchAvailabilityInterface;
class SearchAvailability implements SearchAvailabilityInterface
{
/**
* @var \ACP3\Core\ACL
*/
private $acl;
/**
* @var \ACP3\Core\Date
*/
......@@ -35,18 +30,15 @@ class SearchAvailability implements SearchAvailabilityInterface
/**
* SearchAvailability constructor.
* @param ACL $acl
* @param Date $date
* @param RouterInterface $router
* @param ArticleRepository $articleRepository
*/
public function __construct(
ACL $acl,
Date $date,
RouterInterface $router,
ArticleRepository $articleRepository
) {
$this->acl = $acl;
$this->date = $date;
$this->router = $router;
$this->articleRepository = $articleRepository;
......@@ -68,32 +60,21 @@ class SearchAvailability implements SearchAvailabilityInterface
*/
public function fetchSearchResults($searchTerm, $areas, $sortDirection)
{
if ($this->acl->hasPermission('frontend/articles') === true) {
$fields = $this->mapSearchAreasToFields($areas);
$results = $this->articleRepository->getAllSearchResults(
$fields,
$searchTerm,
$sortDirection,
$this->date->getCurrentDateTime()
);
$cResults = count($results);
$fields = $this->mapSearchAreasToFields($areas);
if ($cResults > 0) {
$searchResults = [];
$searchResults['dir'] = 'articles';
for ($i = 0; $i < $cResults; ++$i) {
$searchResults['results'][$i] = $results[$i];
$searchResults['results'][$i]['hyperlink'] = $this->router->route(
'articles/index/details/id_' . $results[$i]['id']
);
}
$results = $this->articleRepository->getAllSearchResults(
$fields,
$searchTerm,
$sortDirection,
$this->date->getCurrentDateTime()
);
$cResults = count($results);
return $searchResults;
}
for ($i = 0; $i < $cResults; ++$i) {
$results[$i]['hyperlink'] = $this->router->route('articles/index/details/id_' . $results[$i]['id']);
}
return [];
return $results;
}
/**
......
......@@ -2,7 +2,6 @@ services:
files.search.search_availability:
class: ACP3\Modules\ACP3\Files\Search\SearchAvailability
arguments:
- '@core.acl'
- '@core.date'
- '@core.router'
- '@files.model.filesrepository'
......
......@@ -7,7 +7,6 @@
namespace ACP3\Modules\ACP3\Files\Search;
use ACP3\Core\ACL;
use ACP3\Core\Date;
use ACP3\Core\Router\RouterInterface;
use ACP3\Modules\ACP3\Files\Installer\Schema;
......@@ -16,10 +15,6 @@ use ACP3\Modules\ACP3\Search\Utility\SearchAvailabilityInterface;
class SearchAvailability implements SearchAvailabilityInterface
{
/**
* @var \ACP3\Core\ACL
*/
private $acl;
/**
* @var \ACP3\Core\Date
*/
......@@ -34,20 +29,16 @@ class SearchAvailability implements SearchAvailabilityInterface
private $filesRepository;
/**
* OnDisplaySearchResultsListener constructor.
*
* @param \ACP3\Core\ACL $acl
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Router\RouterInterface $router
* @param \ACP3\Modules\ACP3\Files\Model\Repository\FilesRepository $filesRepository
* SearchAvailability constructor.
* @param Date $date
* @param RouterInterface $router
* @param FilesRepository $filesRepository
*/
public function __construct(
ACL $acl,
Date $date,
RouterInterface $router,
FilesRepository $filesRepository
) {
$this->acl = $acl;
$this->date = $date;
$this->router = $router;
$this->filesRepository = $filesRepository;
......@@ -69,32 +60,21 @@ class SearchAvailability implements SearchAvailabilityInterface
*/
public function fetchSearchResults($searchTerm, $areas, $sortDirection)
{
if ($this->acl->hasPermission('frontend/files') === true) {
$fields = $this->mapSearchAreasToFields($areas);
$results = $this->filesRepository->getAllSearchResults(
$fields,
$searchTerm,
$sortDirection,
$this->date->getCurrentDateTime()
);
$cResults = count($results);
$fields = $this->mapSearchAreasToFields($areas);
if ($cResults > 0) {
$searchResults = [];
$searchResults['dir'] = 'files';
for ($i = 0; $i < $cResults; ++$i) {
$searchResults['results'][$i] = $results[$i];
$searchResults['results'][$i]['hyperlink'] = $this->router->route(
'files/index/details/id_' . $results[$i]['id']
);
}
$results = $this->filesRepository->getAllSearchResults(
$fields,
$searchTerm,
$sortDirection,
$this->date->getCurrentDateTime()
);
$cResults = count($results);
return $searchResults;
}
for ($i = 0; $i < $cResults; ++$i) {
$results[$i]['hyperlink'] = $this->router->route('files/index/details/id_' . $results[$i]['id']);
}
return [];
return $results;
}
/**
......
......@@ -2,7 +2,6 @@ services:
news.search.search_availability:
class: ACP3\Modules\ACP3\News\Search\SearchAvailability
arguments:
- '@core.acl'
- '@core.date'
- '@core.router'
- '@news.model.newsrepository'
......
......@@ -7,7 +7,6 @@
namespace ACP3\Modules\ACP3\News\Search;
use ACP3\Core\ACL;
use ACP3\Core\Date;
use ACP3\Core\Router\RouterInterface;
use ACP3\Modules\ACP3\News\Installer\Schema;
......@@ -16,10 +15,6 @@ use ACP3\Modules\ACP3\Search\Utility\SearchAvailabilityInterface;
class SearchAvailability implements SearchAvailabilityInterface
{
/**
* @var \ACP3\Core\ACL
*/
private $acl;
/**
* @var \ACP3\Core\Date
*/
......@@ -34,20 +29,16 @@ class SearchAvailability implements SearchAvailabilityInterface
private $newsRepository;
/**
* OnDisplaySearchResultsListener constructor.
*
* @param \ACP3\Core\ACL $acl
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\Router\RouterInterface $router
* @param \ACP3\Modules\ACP3\News\Model\Repository\NewsRepository $newsRepository
* SearchAvailability constructor.
* @param Date $date
* @param RouterInterface $router
* @param NewsRepository $newsRepository
*/
public function __construct(
ACL $acl,
Date $date,
RouterInterface $router,
NewsRepository $newsRepository
) {
$this->acl = $acl;
$this->date = $date;
$this->router = $router;
$this->newsRepository = $newsRepository;
......@@ -69,32 +60,21 @@ class SearchAvailability implements SearchAvailabilityInterface
*/
public function fetchSearchResults($searchTerm, $areas, $sortDirection)
{
if ($this->acl->hasPermission('frontend/news') === true) {
$fields = $this->mapSearchAreasToFields($areas);
$results = $this->newsRepository->getAllSearchResults(
$fields,
$searchTerm,
$sortDirection,
$this->date->getCurrentDateTime()
);
$cResults = count($results);
$fields = $this->mapSearchAreasToFields($areas);
if ($cResults > 0) {
$searchResults = [];
$searchResults['dir'] = $this->getModuleName();
for ($i = 0; $i < $cResults; ++$i) {
$searchResults['results'][$i] = $results[$i];
$searchResults['results'][$i]['hyperlink'] = $this->router->route(
'news/index/details/id_' . $results[$i]['id']
);
}
$results = $this->newsRepository->getAllSearchResults(
$fields,
$searchTerm,
$sortDirection,
$this->date->getCurrentDateTime()
);
$cResults = count($results);
return $searchResults;
}
for ($i = 0; $i < $cResults; ++$i) {
$results[$i]['hyperlink'] = $this->router->route('news/index/details/id_' . $results[$i]['id']);
}
return [];
return $results;
}
/**
......
......@@ -152,21 +152,9 @@ class Index extends Core\Controller\AbstractFrontendAction
$this->setTemplate('Search/Frontend/index.results.tpl');
$searchResults = $this->processSearchResults($modules, $searchTerm, $area, $sort);
if (!empty($searchResults)) {
ksort($searchResults);
return [
'results_mods' => $searchResults
];
}
return [
'no_search_results' => $this->translator->t(
'search',
'no_search_results',
['%search_term%' => $searchTerm]
)
'results_mods' => $this->processSearchResults($modules, $searchTerm, $area, $sort),
'search_term' => $searchTerm
];
}
......@@ -181,7 +169,7 @@ class Index extends Core\Controller\AbstractFrontendAction
{
$searchResults = [];
foreach ($this->availableModulesRegistrar->getAvailableModules() as $moduleName => $searchAvailability) {
if (in_array($moduleName, $modules)) {
if (in_array($moduleName, $modules) && $this->acl->hasPermission('frontend/' . $moduleName)) {
$results = $searchAvailability->fetchSearchResults($searchTerm, $area, $sort);
if (!empty($results)) {
......@@ -189,6 +177,8 @@ class Index extends Core\Controller\AbstractFrontendAction
}
}
}
ksort($searchResults);
return $searchResults;
}
}
{extends file="asset:`$LAYOUT`"}
{block CONTENT}
{if isset($results_mods)}
{if !empty($results_mods)}
<div class="tabbable">
<ul class="nav nav-tabs">
{$i=0}
{foreach $results_mods as $module => $values}
<li{if $values@first} class="active"{/if}>
<a href="#tab-{$values.dir}" data-toggle="tab">
<a href="#tab-{$module}" data-toggle="tab">
{lang t="`$module`|`$module`"}
</a>
</li>
{/foreach}
</ul>
<div class="tab-content">
{foreach $results_mods as $module => $values}
<div id="tab-{$values.dir}" class="tab-pane fade{if $values@first} in active{/if}">
{foreach $values.results as $row}
{foreach $results_mods as $module => $results}
<div id="tab-{$module}" class="tab-pane fade{if $results@first} in active{/if}">
{foreach $results as $result}
<div class="dataset-box">
<header class="navbar navbar-default">
<div class="navbar-header">
<h2 class="navbar-brand"><a href="{$row.hyperlink}">{$row.title}</a></h2>
<h2 class="navbar-brand"><a href="{$result.hyperlink}">{$result.title}</a></h2>
</div>
</header>
{if !empty($row.text)}
{if !empty($result.text)}
<div class="content">
{$row.text|strip_tags|truncate:200}
{$result.text|strip_tags|truncate:200}
</div>
{/if}
</div>
......@@ -36,7 +36,7 @@
</div>
{else}
<div class="alert alert-warning text-center">
<strong>{$no_search_results}</strong>
<strong>{lang t="search|no_search_results" args=['%search_term%' => $search_term]}</strong>
</div>
{/if}
{/block}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment