This commit is contained in:
Dennis Eichhorn 2024-03-10 02:24:56 +00:00
parent f746af1495
commit 0f1632672c
3 changed files with 71 additions and 1 deletions

View File

@ -29,4 +29,15 @@ return [
],
],
],
'^:tag .*?' => [
[
'dest' => '\Modules\News\Controller\SearchController:searchTag',
'verb' => RouteVerb::ANY,
'permission' => [
'module' => SearchController::NAME,
'type' => PermissionType::READ,
'state' => PermissionCategory::NEWS,
],
],
],
];

View File

@ -31,6 +31,62 @@ use phpOMS\System\MimeType;
*/
final class SearchController extends Controller
{
/**
* Api method to search for tags
*
* @param RequestAbstract $request Request
* @param ResponseAbstract $response Response
* @param array $data Generic data
*
* @return void
*
* @api
*
* @since 1.0.0
*/
public function searchTag(RequestAbstract $request, ResponseAbstract $response, array $data = []) : void
{
$search = $request->getDataString('search') ?? '';
$searchIdStartPos = \stripos($search, ':');
$patternStartPos = $searchIdStartPos === false
? -1
: \stripos($search, ' ', $searchIdStartPos);
$pattern = \substr($search, $patternStartPos + 1);
/** @var \Modules\News\Models\NewsArticle[] $news */
$news = NewsArticleMapper::getAll()
->with('tags')
->with('tags/title')
->where('status', NewsStatus::VISIBLE)
->where('publish', new \DateTime('now'), '<=')
->where('language', $response->header->l11n->language)
->where('tags/title/language', $response->header->l11n->language)
->where('tags/title/content', $pattern)
->sort('publish', OrderType::DESC)
->limit(8)
->execute();
$results = [];
foreach ($news as $article) {
$results[] = [
'title' => $article->title,
'summary' => '',
'link' => '{/base}/news/article?id=' . $article->id,
'account' => '',
'createdAt' => $article->createdAt,
'image' => '',
'tags' => $article->tags,
'type' => 'list_links',
'module' => 'News',
];
}
$response->header->set('Content-Type', MimeType::M_JSON . '; charset=utf-8', true);
$response->add($request->uri->__toString(), $results);
}
/**
* Api method to search for tags
*

View File

@ -43,7 +43,10 @@ echo $this->data['nav']->render(); ?>
<div class="portlet-body">
<article><?= Markdown::parse(\substr($news->plain, 0, 500)); ?></article>
<?php foreach ($news->tags as $tag) : ?>
<span class="tag" style="background: <?= $this->printHtml($tag->color); ?>"><?= empty($tag->icon) ? '' : ''; ?><?= $this->printHtml($tag->getL11n()); ?></span>
<span class="tag" style="background: <?= $this->printHtml($tag->color); ?>">
<?= empty($tag->icon) ? '' : '<i class="g-icon">' . $this->printHtml($tag->icon) . '</i>'; ?>
<?= $this->printHtml($tag->getL11n()); ?>
</span>
<?php endforeach; ?>
</div>
<div class="portlet-foot">