From 7dcf0f8dcfb40f769e4abdb962d5a68826c2674c Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 18 Nov 2021 10:15:27 +0100 Subject: [PATCH] fix: Inject newly created repository instead of having to deal with the container definitions. --- .../PersonHavingActivityBetweenDateFilter.php | 183 +++++++----------- .../Form/Type/TranslatableActivityType.php | 58 ++---- 2 files changed, 89 insertions(+), 152 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php index cf444bd0a..8773c95a0 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php @@ -1,25 +1,14 @@ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ +declare(strict_types=1); namespace Chill\ActivityBundle\Export\Filter; +use Chill\ActivityBundle\Repository\ActivityReasonRepository; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; +use Doctrine\ORM\QueryBuilder; +use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\Extension\Core\Type\DateType; @@ -29,78 +18,58 @@ use Doctrine\ORM\Query\Expr; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Chill\ActivityBundle\Entity\ActivityReason; -use Doctrine\ORM\EntityRepository; -use Doctrine\ORM\EntityManager; use Chill\PersonBundle\Export\Declarations; +use Symfony\Component\Form\FormInterface; use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Chill\MainBundle\Export\ExportElementValidatedInterface; -/** - * - * - * @author Julien Fastré - */ -class PersonHavingActivityBetweenDateFilter implements FilterInterface, - ExportElementValidatedInterface +class PersonHavingActivityBetweenDateFilter implements FilterInterface, ExportElementValidatedInterface { - - /** - * - * @var TranslatableStringHelper - */ - protected $translatableStringHelper; - - /** - * - * @var EntityRepository - */ - protected $activityReasonRepository; - - /** - * - * @var TranslatorInterface - */ - protected $translator; - + protected TranslatableStringHelperInterface $translatableStringHelper; + + protected ActivityReasonRepository $activityReasonRepository; + + protected TranslatorInterface $translator; + public function __construct( TranslatableStringHelper $translatableStringHelper, - EntityRepository $activityReasonRepository, + ActivityReasonRepository $activityReasonRepository, TranslatorInterface $translator ) { $this->translatableStringHelper = $translatableStringHelper; $this->activityReasonRepository = $activityReasonRepository; - $this->translator = $translator; + $this->translator = $translator; } - public function addRole() { return null; } - public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data) + public function alterQuery(QueryBuilder $qb, $data) { // create a query for activity $sqb = $qb->getEntityManager()->createQueryBuilder(); - $sqb->select("person_person_having_activity.id") - ->from("ChillActivityBundle:Activity", "activity_person_having_activity") - ->join("activity_person_having_activity.person", "person_person_having_activity") - ; + $sqb->select('person_person_having_activity.id') + ->from('ChillActivityBundle:Activity', 'activity_person_having_activity') + ->join('activity_person_having_activity.person', 'person_person_having_activity'); + // add clause between date - $sqb->where("activity_person_having_activity.date BETWEEN " - . ":person_having_activity_between_date_from" - . " AND " - . ":person_having_activity_between_date_to"); + $sqb->where('activity_person_having_activity.date BETWEEN ' + . ':person_having_activity_between_date_from' + . ' AND ' + . ':person_having_activity_between_date_to'); + // add clause activity reason - $sqb->join('activity_person_having_activity.reasons', - 'reasons_person_having_activity'); + $sqb->join('activity_person_having_activity.reasons', 'reasons_person_having_activity'); + $sqb->andWhere( - $sqb->expr()->in( - 'reasons_person_having_activity', - ":person_having_activity_reasons") - ); - + $sqb->expr()->in( + 'reasons_person_having_activity', ':person_having_activity_reasons' + ) + ); + $where = $qb->getDQLPart('where'); $clause = $qb->expr()->in('person.id', $sqb->getDQL()); @@ -109,11 +78,11 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, } else { $where = $qb->expr()->andX($clause); } - + $qb->add('where', $where); - $qb->setParameter('person_having_activity_between_date_from', + $qb->setParameter('person_having_activity_between_date_from', $data['date_from']); - $qb->setParameter('person_having_activity_between_date_to', + $qb->setParameter('person_having_activity_between_date_to', $data['date_to']); $qb->setParameter('person_having_activity_reasons', $data['reasons']); } @@ -123,51 +92,45 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, return Declarations::PERSON_IMPLIED_IN; } - public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder) + public function buildForm(FormBuilderInterface $builder) { - $builder->add('date_from', DateType::class, array( - 'label' => "Implied in an activity after this date", + $builder->add('date_from', DateType::class, [ + 'label' => 'Implied in an activity after this date', 'data' => new \DateTime(), - 'attr' => array('class' => 'datepicker'), + 'attr' => ['class' => 'datepicker'], 'widget'=> 'single_text', 'format' => 'dd-MM-yyyy', - )); - - $builder->add('date_to', DateType::class, array( - 'label' => "Implied in an activity before this date", + ]); + + $builder->add('date_to', DateType::class, [ + 'label' => 'Implied in an activity before this date', 'data' => new \DateTime(), - 'attr' => array('class' => 'datepicker'), + 'attr' => ['class' => 'datepicker'], 'widget'=> 'single_text', 'format' => 'dd-MM-yyyy', - )); - - $builder->add('reasons', EntityType::class, array( - 'class' => 'ChillActivityBundle:ActivityReason', - 'choice_label' => function (ActivityReason $reason) { - return $this->translatableStringHelper - ->localize($reason->getName()); - }, - 'group_by' => function(ActivityReason $reason) { - return $this->translatableStringHelper - ->localize($reason->getCategory()->getName()); - }, + ]); + + $builder->add('reasons', EntityType::class, [ + 'class' => ActivityReason::class, + 'choice_label' => static fn (ActivityReason $reason): ?string => $this->translatableStringHelper->localize($reason->getName()), + 'group_by' => static fn(ActivityReason $reason): ?string => $this->translatableStringHelper->localize($reason->getCategory()->getName()), 'data' => $this->activityReasonRepository->findAll(), 'multiple' => true, 'expanded' => false, - 'label' => "Activity reasons for those activities" - )); - + 'label' => 'Activity reasons for those activities' + ]); + $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) { - /* @var $filterForm \Symfony\Component\Form\FormInterface */ + /* @var FormInterface $filterForm */ $filterForm = $event->getForm()->getParent(); $enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData(); - + if ($enabled === true) { // if the filter is enabled, add some validation $form = $event->getForm(); $date_from = $form->get('date_from')->getData(); $date_to = $form->get('date_to')->getData(); - + // check that fields are not empty if ($date_from === null) { $form->get('date_from')->addError(new FormError( @@ -178,8 +141,8 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, $form->get('date_to')->addError(new FormError( $this->translator->trans('This field ' . 'should not be empty'))); - } - + } + // check that date_from is before date_to if ( ($date_from !== null && $date_to !== null) @@ -194,30 +157,32 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, } }); } - + public function validateForm($data, ExecutionContextInterface $context) { if ($data['reasons'] === null || count($data['reasons']) === 0) { - $context->buildViolation("At least one reason must be choosen") + $context->buildViolation('At least one reason must be chosen') ->addViolation(); } } public function describeAction($data, $format = 'string') { - return array( - "Filtered by person having an activity between %date_from% and " - . "%date_to% with reasons %reasons_name%", - array( - "%date_from%" => $data['date_from']->format('d-m-Y'), + return [ + 'Filtered by person having an activity between %date_from% and ' + . '%date_to% with reasons %reasons_name%', + [ + '%date_from%' => $data['date_from']->format('d-m-Y'), '%date_to%' => $data['date_to']->format('d-m-Y'), - "%reasons_name%" => implode(", ", array_map( - function (ActivityReason $r) { - return '"'.$this->translatableStringHelper-> - localize($r->getName()).'"'; - }, - $data['reasons'])) - )); + '%reasons_name%' => implode( + ", ", + array_map( + static fn(ActivityReason $r): string => '"' . $this->translatableStringHelper->localize($r->getName()) . '"', + $data['reasons'] + ) + ) + ] + ]; } public function getTitle() diff --git a/src/Bundle/ChillActivityBundle/Form/Type/TranslatableActivityType.php b/src/Bundle/ChillActivityBundle/Form/Type/TranslatableActivityType.php index fa4b23212..f930d3c1a 100644 --- a/src/Bundle/ChillActivityBundle/Form/Type/TranslatableActivityType.php +++ b/src/Bundle/ChillActivityBundle/Form/Type/TranslatableActivityType.php @@ -1,56 +1,29 @@ , - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ +declare(strict_types=1); namespace Chill\ActivityBundle\Form\Type; +use Chill\ActivityBundle\Repository\ActivityTypeRepository; +use Chill\MainBundle\Templating\TranslatableStringHelperInterface; +use Doctrine\DBAL\Types\Types; +use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; -use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Bridge\Doctrine\Form\Type\EntityType; -use Chill\MainBundle\Templating\TranslatableStringHelper; -use Doctrine\ORM\EntityRepository; use Chill\ActivityBundle\Entity\ActivityType; -/** - * Description of TranslatableActivityType - * - * @author Champs-Libres Coop - */ class TranslatableActivityType extends AbstractType { + protected TranslatableStringHelperInterface $translatableStringHelper; - /** - * - * @var TranslatableStringHelper - */ - protected $translatableStringHelper; - - protected $activityTypeRepository; + protected ActivityTypeRepository $activityTypeRepository; public function __construct( - TranslatableStringHelper $helper, - EntityRepository $activityTypeRepository - ) - { + TranslatableStringHelperInterface $helper, + ActivityTypeRepository $activityTypeRepository + ) { $this->translatableStringHelper = $helper; $this->activityTypeRepository = $activityTypeRepository; } @@ -65,22 +38,21 @@ class TranslatableActivityType extends AbstractType return EntityType::class; } - public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options) { - /* @var $qb \Doctrine\ORM\QueryBuilder */ + public function buildForm(FormBuilderInterface $builder, array $options) { + /* @var QueryBuilder $qb */ $qb = $options['query_builder']; if ($options['active_only'] === true) { $qb->where($qb->expr()->eq('at.active', ':active')); - $qb->setParameter('active', true, \Doctrine\DBAL\Types\Types::BOOLEAN); + $qb->setParameter('active', true, Types::BOOLEAN); } } public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults( array( - 'class' => 'ChillActivityBundle:ActivityType', + 'class' => ActivityType::class, 'active_only' => true, 'query_builder' => $this->activityTypeRepository ->createQueryBuilder('at'),