From 25dd65fbd8695bf8bef61052b62db1e3dd25e2f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 13 Jan 2023 15:35:09 +0100 Subject: [PATCH] Feature: [admin][ActivityReason] improve administration for activity reason * list alphabetically; * show "active" in index --- .../Controller/ActivityReasonController.php | 15 ++++++++-- .../Entity/ActivityReason.php | 9 ++++-- .../Form/ActivityReasonType.php | 7 +++-- .../Repository/ActivityReasonRepository.php | 27 +++++++++++++++-- .../views/ActivityReason/index.html.twig | 30 +++++++++++++------ .../ActivityReasonCategory/index.html.twig | 7 ++++- .../config/services/controller.yaml | 9 +++++- .../config/services/form.yaml | 9 ++---- .../config/services/repositories.yaml | 3 ++ .../translations/messages.fr.yml | 3 +- .../translations/messages.fr.yml | 2 ++ 11 files changed, 91 insertions(+), 30 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Controller/ActivityReasonController.php b/src/Bundle/ChillActivityBundle/Controller/ActivityReasonController.php index 6af42a53c..95a49d0ba 100644 --- a/src/Bundle/ChillActivityBundle/Controller/ActivityReasonController.php +++ b/src/Bundle/ChillActivityBundle/Controller/ActivityReasonController.php @@ -13,15 +13,24 @@ namespace Chill\ActivityBundle\Controller; use Chill\ActivityBundle\Entity\ActivityReason; use Chill\ActivityBundle\Form\ActivityReasonType; +use Chill\ActivityBundle\Repository\ActivityReasonRepository; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** * ActivityReason controller. */ class ActivityReasonController extends AbstractController { + private ActivityReasonRepository $activityReasonRepository; + + public function __construct(ActivityReasonRepository $activityReasonRepository) + { + $this->activityReasonRepository = $activityReasonRepository; + } + /** * Creates a new ActivityReason entity. */ @@ -56,8 +65,8 @@ class ActivityReasonController extends AbstractController $entity = $em->getRepository(\Chill\ActivityBundle\Entity\ActivityReason::class)->find($id); - if (!$entity) { - throw $this->createNotFoundException('Unable to find ActivityReason entity.'); + if (null === $entity) { + throw new NotFoundHttpException('Unable to find ActivityReason entity.'); } $editForm = $this->createEditForm($entity); @@ -75,7 +84,7 @@ class ActivityReasonController extends AbstractController { $em = $this->getDoctrine()->getManager(); - $entities = $em->getRepository(\Chill\ActivityBundle\Entity\ActivityReason::class)->findAll(); + $entities = $this->activityReasonRepository->findAll(); return $this->render('ChillActivityBundle:ActivityReason:index.html.twig', [ 'entities' => $entities, diff --git a/src/Bundle/ChillActivityBundle/Entity/ActivityReason.php b/src/Bundle/ChillActivityBundle/Entity/ActivityReason.php index c0cc21209..e6da6b7e0 100644 --- a/src/Bundle/ChillActivityBundle/Entity/ActivityReason.php +++ b/src/Bundle/ChillActivityBundle/Entity/ActivityReason.php @@ -63,10 +63,8 @@ class ActivityReason /** * Get category. - * - * @return ActivityReasonCategory */ - public function getCategory() + public function getCategory(): ?ActivityReasonCategory { return $this->category; } @@ -107,6 +105,11 @@ class ActivityReason return $this->name; } + public function isActiveAndParentActive(): bool + { + return $this->active && null !== $this->getCategory() && $this->getCategory()->getActive(); + } + /** * Set active. * diff --git a/src/Bundle/ChillActivityBundle/Form/ActivityReasonType.php b/src/Bundle/ChillActivityBundle/Form/ActivityReasonType.php index 84a3e08ed..f47a101bd 100644 --- a/src/Bundle/ChillActivityBundle/Form/ActivityReasonType.php +++ b/src/Bundle/ChillActivityBundle/Form/ActivityReasonType.php @@ -11,7 +11,8 @@ declare(strict_types=1); namespace Chill\ActivityBundle\Form; -use Chill\ActivityBundle\Form\Type\TranslatableActivityReasonCategory; +use Chill\ActivityBundle\Entity\ActivityReason; +use Chill\ActivityBundle\Form\Type\TranslatableActivityReasonCategoryType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; @@ -25,13 +26,13 @@ class ActivityReasonType extends AbstractType $builder ->add('name', TranslatableStringFormType::class) ->add('active', CheckboxType::class, ['required' => false]) - ->add('category', TranslatableActivityReasonCategory::class); + ->add('category', TranslatableActivityReasonCategoryType::class); } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ - 'data_class' => 'Chill\ActivityBundle\Entity\ActivityReason', + 'data_class' => ActivityReason::class, ]); } diff --git a/src/Bundle/ChillActivityBundle/Repository/ActivityReasonRepository.php b/src/Bundle/ChillActivityBundle/Repository/ActivityReasonRepository.php index 275c49b2c..c6b69319e 100644 --- a/src/Bundle/ChillActivityBundle/Repository/ActivityReasonRepository.php +++ b/src/Bundle/ChillActivityBundle/Repository/ActivityReasonRepository.php @@ -14,17 +14,38 @@ namespace Chill\ActivityBundle\Repository; use Chill\ActivityBundle\Entity\ActivityReason; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\HttpFoundation\RequestStack; /** * @method ActivityReason|null find($id, $lockMode = null, $lockVersion = null) * @method ActivityReason|null findOneBy(array $criteria, array $orderBy = null) - * @method ActivityReason[] findAll() * @method ActivityReason[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class ActivityReasonRepository extends ServiceEntityRepository { - public function __construct(ManagerRegistry $registry) - { + private RequestStack $requestStack; + + public function __construct( + ManagerRegistry $registry, + RequestStack $requestStack + ) { parent::__construct($registry, ActivityReason::class); + + $this->requestStack = $requestStack; + } + + /** + * @return ActivityReason[] + */ + public function findAll(): array + { + $qb = $this->createQueryBuilder('ar'); + $qb->select(['ar']) + ->leftJoin('ar.category', 'category') + ->addOrderBy('JSON_EXTRACT(category.name, :lang)') + ->addOrderBy('JSON_EXTRACT(ar.name, :lang)') + ->setParameter('lang', $this->requestStack->getCurrentRequest()->getLocale() ?? 'fr'); + + return $qb->getQuery()->getResult(); } } diff --git a/src/Bundle/ChillActivityBundle/Resources/views/ActivityReason/index.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/ActivityReason/index.html.twig index 7d9e5f71d..855c9386d 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/ActivityReason/index.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/ActivityReason/index.html.twig @@ -7,22 +7,34 @@ {{ 'Name'|trans }} + {{ 'Active'|trans }} {{ 'Actions'|trans }} {% for entity in entities %} - {{ entity.name|localize_translatable_string }} - + {% if entity.category is not null -%} + {{ entity.category.name|localize_translatable_string }} > + {% endif -%} + {{ entity.name|localize_translatable_string }} + + + + {% if entity.active and not entity.isActiveAndParentActive %} + {{ 'Associated activity reason category is inactive'|trans }} + {% endif %} + + + {% endfor %} diff --git a/src/Bundle/ChillActivityBundle/Resources/views/ActivityReasonCategory/index.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/ActivityReasonCategory/index.html.twig index 473059124..5f48180b3 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/ActivityReasonCategory/index.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/ActivityReasonCategory/index.html.twig @@ -7,6 +7,7 @@ {{ 'Name'|trans }} + {{ 'Active'|trans }} {{ 'Actions'|trans }} @@ -14,7 +15,11 @@ {% for entity in entities %} - {{ entity.name|localize_translatable_string }} + {{ entity.name|localize_translatable_string }} + + + +