Add history page for all news items with a search filter on the basis of the title or content

This commit is contained in:
Julie Lenaerts 2023-11-20 17:13:04 +01:00
parent e8b8f30e3c
commit c185c35c44
5 changed files with 174 additions and 4 deletions

View File

@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Repository\NewsItemRepository;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class UserNewsItemsController extends AbstractController
{
public function __construct(
private readonly NewsItemRepository $newsItemRepository,
private readonly PaginatorFactory $paginatorFactory,
private readonly FilterOrderHelperFactoryInterface $filterOrderHelperFactory,
) {}
/**
* @Route("/{_locale}/news-items/history", name="chill_main_news_items_history")
*/
public function listAction(Request $request): Response
{
$filter = $this->buildFilterOrder(false);
$total = $this->newsItemRepository->countAllFilteredByUser($filter->getQueryString());
$newsItems = $this->newsItemRepository->findAllFilteredByUser($filter->getQueryString());
$pagination = $this->paginatorFactory->create($total);
return $this->render('@ChillMain/NewsItem/news_items_history.html.twig', [
'entities' => $newsItems,
'paginator' => $pagination,
'filter_order' => $filter,
]);
}
private function buildFilterOrder($includeFilterByUser = true, $includeMissionType = false): FilterOrderHelper
{
$filterBuilder = $this->filterOrderHelperFactory
->create(self::class)
->addSearchBox();
return $filterBuilder->build();
}
}

View File

@ -57,17 +57,46 @@ class NewsItemRepository implements ObjectRepository
return NewsItem::class;
}
public function buildBaseQuery(
string $pattern = null
): QueryBuilder {
$qb = $this->createQueryBuilder('n');
if (null !== $pattern && '' !== $pattern) {
$qb->andWhere($qb->expr()->like('LOWER(UNACCENT(n.title))', 'LOWER(UNACCENT(:pattern))'))
->orWhere($qb->expr()->like('LOWER(UNACCENT(n.content))', 'LOWER(UNACCENT(:pattern))'))
->setParameter('pattern', '%'.$pattern.'%');
}
return $qb;
}
public function findAllFilteredByUser(string $pattern = null)
{
$qb = $this->buildBaseQuery($pattern);
$qb->addOrderBy('n.startDate', 'DESC')
->addOrderBy('n.id', 'DESC');
return $qb->getQuery()->getResult();
}
public function findWithDateFilter()
{
dump($this->buildQueryWithDateFilter()
->getQuery()
->getResult());
return $this->buildQueryWithDateFilter()
->getQuery()
->getResult();
}
public function countAllFilteredByUser(string $pattern = null)
{
$qb = $this->buildBaseQuery($pattern);
return $qb
->select('COUNT(n)')
->getQuery()
->getSingleScalarResult();
}
public function countWithDateFilter()
{
return $this->buildQueryWithDateFilter()

View File

@ -0,0 +1,71 @@
{% extends "@ChillMain/layout.html.twig" %}
{% block title %}
{{ 'news_history.title'|trans }}
{% endblock title %}
{% block content %}
<div class="col-md-10 asideactivity-list">
<h2>{{ 'news_history.title'|trans }}</h2>
{{ filter_order|chill_render_filter_order_helper }}
{% if entities|length == 0 %}
<p class="chill-no-data-statement">
{{ "news_history.no_data"|trans }}
</p>
{% else %}
<div class="flex-table">
{% for entity in entities %}
<div class="item-bloc">
<div class="item-row wrap-header">
<div class="item-col">
<h3>
{{ entity.title }}
</h3>
<div>
{% if entity.startDate %}
<span>{{ entity.startDate|format_date('long') }}</span>
{% endif %}
{% if entity.endDate %}
<span> - {{ entity.endDate|format_date('long') }}</span>
{% endif %}
</div>
</div>
<div class="item-col" style="justify-content: flex-end;">
<div class="box">
<div>
{% apply markdown_to_html %}
{{ entity.content }}
{% endapply %}
</div>
{# <div class="action">#}
{# <ul class="record_actions">#}
{# <li>#}
{# <a href="{{ chill_path_add_return_path('chill_crud_news_item_view', { 'id': entity.id } ) }}" class="btn btn-show btn-mini"></a>#}
{# </li>#}
{# </ul>#}
{# </div>#}
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{{ chill_pagination(paginator) }}
<ul class="record_actions sticky-form-buttons">
<li>
<a href="{{ chill_path_add_return_path('chill_crud_aside_activity_new') }}" class="btn btn-create">
{{ 'Create'|trans }}
</a>
</li>
</ul>
{% endif %}
</div>
{% endblock %}

View File

@ -58,6 +58,14 @@ class SectionMenuBuilder implements LocalMenuBuilderInterface
'order' => 20,
]);
}
$menu->addChild($this->translator->trans('news_history.menu'), [
'route' => 'chill_main_news_items_history',
])
->setExtras([
'icons' => ['newspaper-o'],
'order' => 5,
]);
}
public static function getMenuIds(): array

View File

@ -688,3 +688,8 @@ dashboard:
noDate: Pas de date de fin
startDate: Date de début
endDate: Date de fin
news_history:
title: Historique des actualités
menu: Actualités
no_data: Aucune actualité