From 27674985bd3aee6b9c753a76a5c7ee163dcc3043 Mon Sep 17 00:00:00 2001 From: nobohan Date: Fri, 17 Sep 2021 16:29:55 +0200 Subject: [PATCH] rdv: add pagination in calendar item list + refactor list views --- .../Controller/CalendarController.php | 45 ++++-- .../Repository/CalendarRepository.php | 6 + .../Resources/views/Calendar/list.html.twig | 135 ------------------ .../listByAccompanyingCourse.html.twig | 121 +++++++++++++++- .../views/Calendar/listByUser.html.twig | 5 +- 5 files changed, 161 insertions(+), 151 deletions(-) delete mode 100644 src/Bundle/ChillCalendarBundle/Resources/views/Calendar/list.html.twig diff --git a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php index fff20940a..e7226b1bd 100644 --- a/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php +++ b/src/Bundle/ChillCalendarBundle/Controller/CalendarController.php @@ -36,7 +36,9 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Security\Core\Role\Role; use Chill\CalendarBundle\Entity\Calendar; use Chill\CalendarBundle\Form\CalendarType; +use Chill\CalendarBundle\Repository\CalendarRepository; use Chill\MainBundle\Entity\User; +use Chill\MainBundle\Pagination\PaginatorFactory; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Routing\Annotation\Route; @@ -55,16 +57,24 @@ class CalendarController extends AbstractController protected SerializerInterface $serializer; + protected PaginatorFactory $paginator; + + private CalendarRepository $calendarRepository; + public function __construct( EventDispatcherInterface $eventDispatcher, AuthorizationHelper $authorizationHelper, LoggerInterface $logger, - SerializerInterface $serializer + SerializerInterface $serializer, + PaginatorFactory $paginator, + CalendarRepository $calendarRepository ) { $this->eventDispatcher = $eventDispatcher; $this->authorizationHelper = $authorizationHelper; $this->logger = $logger; $this->serializer = $serializer; + $this->paginator = $paginator; + $this->calendarRepository = $calendarRepository; } /** @@ -73,31 +83,40 @@ class CalendarController extends AbstractController */ public function listAction(Request $request): Response { - $em = $this->getDoctrine()->getManager(); $view = null; [$user, $accompanyingPeriod] = $this->getEntity($request); if ($user instanceof User) { - $calendarItems = $em->getRepository(Calendar::class) - ->findByUser($user) - ; + $calendarItems = $this->calendarRepository->findByUser($user); + $view = '@ChillCalendar/Calendar/listByUser.html.twig'; + + return $this->render($view, [ + 'calendarItems' => $calendarItems, + 'user' => $user + ]); + } elseif ($accompanyingPeriod instanceof AccompanyingPeriod) { - $calendarItems = $em->getRepository(Calendar::class)->findBy( + + $total = $this->calendarRepository->countByAccompanyingPeriod($accompanyingPeriod); + $paginator = $this->paginator->create($total); + $calendarItems = $this->calendarRepository->findBy( ['accompanyingPeriod' => $accompanyingPeriod], - ['startDate' => 'DESC'] + ['startDate' => 'DESC'], + $paginator->getItemsPerPage(), + $paginator->getCurrentPageFirstItemNumber() ); $view = '@ChillCalendar/Calendar/listByAccompanyingCourse.html.twig'; - } - return $this->render($view, [ - 'calendarItems' => $calendarItems, - 'user' => $user, - 'accompanyingCourse' => $accompanyingPeriod, - ]); + return $this->render($view, [ + 'calendarItems' => $calendarItems, + 'accompanyingCourse' => $accompanyingPeriod, + 'paginator' => $paginator + ]); + } } /** diff --git a/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php b/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php index edb51bb35..b87f4f525 100644 --- a/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php +++ b/src/Bundle/ChillCalendarBundle/Repository/CalendarRepository.php @@ -3,7 +3,9 @@ namespace Chill\CalendarBundle\Repository; use Chill\CalendarBundle\Entity\Calendar; +use Chill\PersonBundle\Entity\AccompanyingPeriod; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; +use Doctrine\ORM\EntityRepository; use Doctrine\Persistence\ManagerRegistry; /** @@ -14,9 +16,13 @@ use Doctrine\Persistence\ManagerRegistry; */ class CalendarRepository extends ServiceEntityRepository { + + // private EntityRepository $repository; + public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Calendar::class); + // $this->repository = $entityManager->getRepository(AccompanyingPeriodWork::class); } // /** diff --git a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/list.html.twig b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/list.html.twig deleted file mode 100644 index 44295e192..000000000 --- a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/list.html.twig +++ /dev/null @@ -1,135 +0,0 @@ -{% set user_id = null %} -{% if user %} - {% set user_id = user.id %} -{% endif %} - -{% set accompanying_course_id = null %} -{% if accompanyingCourse %} - {% set accompanying_course_id = accompanyingCourse.id %} -{% endif %} - -{% if context == 'user' %} -

{{ 'My calendar list' |trans }}

-{% else %} -

{{ 'Calendar list' |trans }}

-{% endif %} - -{% if context == 'user' %} -
-{% else %} - - {% if calendarItems|length == 0 %} -

- {{ "There is no calendar items."|trans }} - -

- {% else %} - -
- - {% for calendar in calendarItems %} - -
-
-
- - - {% if calendar.startDate and calendar.endDate %} - {% if calendar.endDate.diff(calendar.startDate).days >= 1 %} -

{{ "From the day"|trans }} {{ calendar.startDate|format_datetime('medium', 'short') }}

-

{{ "to the day"|trans }} {{ calendar.endDate|format_datetime('medium', 'short') }}

- {% else %} -

{{ calendar.startDate|format_date('full') }}

-

{{ calendar.startDate|format_datetime('none', 'short', locale='fr') }} - {{ calendar.endDate|format_datetime('none', 'short', locale='fr') }}

- -
-

- - {{ calendar.endDate.diff(calendar.startDate)|date("%H:%M")}} -

-
- {% endif %} - - {% endif %} - - -
-
-
    - {% if calendar.user %} -
  • - {{ 'by'|trans }}{{ calendar.user.usernameCanonical }} -
  • - {% endif %} - - {% if calendar.mainUser is not empty %} -
  • - {{ 'main user concerned'|trans }}: {{ calendar.mainUser.usernameCanonical }} -
  • - {% endif %} - -
-
    -
  • - -
  • - {# TOOD - {% if is_granted('CHILL_ACTIVITY_UPDATE', calendar) %} - #} -
  • - -
  • - {# TOOD - {% endif %} - {% if is_granted('CHILL_ACTIVITY_DELETE', calendar) %} - #} -
  • - -
  • - {# - {% endif %} - #} -
-
-
- - {% - if calendar.comment.comment is not empty - or calendar.users|length > 0 - or calendar.thirdParties|length > 0 - or calendar.users|length > 0 - %} -
-
- - {% include 'ChillActivityBundle:Activity:concernedGroups.html.twig' with {'context': context, 'with_display': 'row', 'entity': calendar } %} -
- - {% if calendar.comment.comment is not empty %} -
- {{ calendar.comment|chill_entity_render_box( { 'limit_lines': 3, 'metadata': false } ) }} -
- {% endif %} -
- {% endif %} - -
- {% endfor %} -
- {% endif %} - - {% if context != 'user' %} - {# TODO set this condition in configuration #} - - {% endif %} - - - -{% endif %} - diff --git a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig index c0230344d..1ade76a26 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig +++ b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByAccompanyingCourse.html.twig @@ -4,6 +4,123 @@ {% block title %}{{ 'Calendar list' |trans }}{% endblock title %} +{% set user_id = null %} +{% set accompanying_course_id = accompanyingCourse.id %} + {% block content %} - {% include 'ChillCalendarBundle:Calendar:list.html.twig' with {'context': 'accompanyingCourse'} %} -{% endblock %} + +

{{ 'Calendar list' |trans }}

+ +{% if calendarItems|length == 0 %} +

+ {{ "There is no calendar items."|trans }} + +

+{% else %} + +
+ + {% for calendar in calendarItems %} + +
+
+
+ + {% if calendar.startDate and calendar.endDate %} + {% if calendar.endDate.diff(calendar.startDate).days >= 1 %} +

{{ "From the day"|trans }} {{ calendar.startDate|format_datetime('medium', 'short') }}

+

{{ "to the day"|trans }} {{ calendar.endDate|format_datetime('medium', 'short') }}

+ {% else %} +

{{ calendar.startDate|format_date('full') }}

+

{{ calendar.startDate|format_datetime('none', 'short', locale='fr') }} - {{ calendar.endDate|format_datetime('none', 'short', locale='fr') }}

+ +
+

+ + {{ calendar.endDate.diff(calendar.startDate)|date("%H:%M")}} +

+
+ {% endif %} + + {% endif %} + +
+
+
    + {% if calendar.user %} +
  • + {{ 'by'|trans }}{{ calendar.user.usernameCanonical }} +
  • + {% endif %} + + {% if calendar.mainUser is not empty %} +
  • + {{ 'main user concerned'|trans }}: {{ calendar.mainUser.usernameCanonical }} +
  • + {% endif %} + +
+
    +
  • + +
  • + {# TOOD + {% if is_granted('CHILL_ACTIVITY_UPDATE', calendar) %} + #} +
  • + +
  • + {# TOOD + {% endif %} + {% if is_granted('CHILL_ACTIVITY_DELETE', calendar) %} + #} +
  • + +
  • + {# + {% endif %} + #} +
+
+
+ + {% + if calendar.comment.comment is not empty + or calendar.users|length > 0 + or calendar.thirdParties|length > 0 + or calendar.users|length > 0 + %} +
+
+ + {% include 'ChillActivityBundle:Activity:concernedGroups.html.twig' with {'context': accompanyingCourse, 'with_display': 'row', 'entity': calendar } %} +
+ + {% if calendar.comment.comment is not empty %} +
+ {{ calendar.comment|chill_entity_render_box( { 'limit_lines': 3, 'metadata': false } ) }} +
+ {% endif %} +
+ {% endif %} + +
+ {% endfor %} + + {% if calendarItems|length < paginator.getTotalItems %} + {{ chill_pagination(paginator) }} + {% endif %} + +
+{% endif %} + + + + +{% endblock %} \ No newline at end of file diff --git a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByUser.html.twig b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByUser.html.twig index 004000fc6..6171a0026 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByUser.html.twig +++ b/src/Bundle/ChillCalendarBundle/Resources/views/Calendar/listByUser.html.twig @@ -5,7 +5,10 @@ {% block title %}{{ 'My calendar list' |trans }}{% endblock title %} {% block content %} - {% include 'ChillCalendarBundle:Calendar:list.html.twig' with {'context': 'user'} %} + +

{{ 'My calendar list' |trans }}

+
+ {% endblock %} {% block js %}