deblan.io-murph/src/Repository/Blog/PostRepositoryQuery.php

131 lines
3.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Repository\Blog;
use App\Core\Repository\RepositoryQuery;
use App\Entity\Blog\Category;
use Knp\Component\Pager\PaginatorInterface;
/**
* class PostRepositoryQuery.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class PostRepositoryQuery extends RepositoryQuery
{
public function __construct(PostRepository $repository, PaginatorInterface $paginator)
{
parent::__construct($repository, 'p', $paginator);
}
2022-09-09 20:10:51 +02:00
public function inCategory(Category $category, bool $strict = true): self
{
$c = 'c'.mt_rand();
if ($strict) {
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id = :category')
->setParameter(':category', $category->getId())
;
} else {
$ids = [$category->getId()];
foreach ($category->getCategories() as $childCategory) {
$ids[] = $childCategory->getId();
}
$this
->innerJoin('p.categories', $c)
->andWhere($c.'.id IN (:categories)')
->setParameter(':categories', $ids)
;
}
return $this;
}
2022-09-09 20:10:51 +02:00
public function published(): self
{
return $this
->andWhere('.status = 1')
->andWhere('.publishedAt <= :now')
2021-03-29 22:45:51 +02:00
->setParameter(':now', (new \DateTime('now'))->format('Y-m-d H:i:s'))
;
}
2021-04-26 11:29:57 +02:00
protected function filterHandler(string $name, $value)
{
2021-04-26 11:29:57 +02:00
if ('category' === $name) {
$this->inCategory($value);
}
}
2021-03-29 22:45:51 +02:00
2021-03-30 21:16:52 +02:00
public function search(?string $keywords, ?string $tag)
2021-03-29 22:45:51 +02:00
{
2021-03-30 21:16:52 +02:00
if ($keywords) {
$conn = $this->repository->getEm()->getConnection();
2022-01-29 22:54:39 +01:00
$query = $conn->prepare(
2021-03-30 21:16:52 +02:00
'SELECT
post.id,
post.title,
MATCH(post.title) AGAINST(:search) AS MATCH_TITLE,
MATCH(post.content) AGAINST(:search) AS MATCH_CONTENT
FROM post
WHERE
post.status = 1 AND
post.published_at < :date
ORDER BY
MATCH_TITLE DESC,
MATCH_CONTENT DESC
');
2022-01-29 22:54:39 +01:00
$statement = $query->execute([
2021-03-30 21:16:52 +02:00
':search' => $keywords,
':date' => (new \DateTime())->format('Y-m-d H:i:s'),
]);
$results = $statement->fetchAll();
$ids = [];
2021-03-29 22:45:51 +02:00
foreach ($results as $k => $v) {
$rate = ($v['MATCH_TITLE'] * 2) + $v['MATCH_CONTENT'];
2021-03-30 21:16:52 +02:00
if ($rate >= 7) {
2021-03-29 22:45:51 +02:00
$ids[] = $v['id'];
}
}
2021-03-30 21:16:52 +02:00
if (0 == count($ids)) {
foreach ($results as $k => $v) {
$rate = ($v['MATCH_TITLE'] * 2) + $v['MATCH_CONTENT'];
if ($rate >= 6) {
$ids[] = $v['id'];
}
}
}
if (!$ids) {
$ids = [-1];
}
$this
->orderBy('FIELD(p.id, :ids)')
->andWhere('.id IN(:ids)')
->setParameter(':ids', $ids)
;
2021-03-29 22:45:51 +02:00
}
2021-03-30 21:16:52 +02:00
if ($tag) {
$this
->andWhere('.tags LIKE :tag')
2021-03-30 21:27:49 +02:00
->setParameter(':tag', '%"'.$tag.'"%')
2021-03-30 21:17:05 +02:00
;
2021-03-29 22:45:51 +02:00
}
2021-03-30 21:16:52 +02:00
return $this;
2021-03-29 22:45:51 +02:00
}
}