diff --git a/Admin/Install/SearchCommands.php b/Admin/Install/SearchCommands.php index 4210fd0..9fcb6cf 100644 --- a/Admin/Install/SearchCommands.php +++ b/Admin/Install/SearchCommands.php @@ -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, + ], + ], + ], ]; diff --git a/Controller/SearchController.php b/Controller/SearchController.php index 72a2467..9e1637d 100644 --- a/Controller/SearchController.php +++ b/Controller/SearchController.php @@ -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 * diff --git a/Theme/Backend/news-dashboard.tpl.php b/Theme/Backend/news-dashboard.tpl.php index 4af9310..bdbaa04 100755 --- a/Theme/Backend/news-dashboard.tpl.php +++ b/Theme/Backend/news-dashboard.tpl.php @@ -43,7 +43,10 @@ echo $this->data['nav']->render(); ?>