From 6ccc8d4cb8027e1ff5a48abaf856e6dde97f09e7 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Thu, 18 Nov 2021 11:46:30 +0100 Subject: [PATCH] fix: Let Symfony inject dependencies automatically. --- .../Export/Filter/ActivityDateFilter.php | 104 ++++++++---------- .../PersonHavingActivityBetweenDateFilter.php | 8 +- 2 files changed, 49 insertions(+), 63 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ActivityDateFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ActivityDateFilter.php index dca4cec30..2e0149a38 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ActivityDateFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ActivityDateFilter.php @@ -1,25 +1,12 @@ - * - * 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\MainBundle\Export\FilterInterface; +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; @@ -28,34 +15,24 @@ use Chill\MainBundle\Form\Type\Export\FilterType; use Doctrine\ORM\Query\Expr; use Symfony\Component\Translation\TranslatorInterface; -/** - * - * - * @author Julien Fastré - */ class ActivityDateFilter implements FilterInterface { - /** - * - * @var TranslatorInterface - */ - protected $translator; - + protected TranslatorInterface $translator; + function __construct(TranslatorInterface $translator) { $this->translator = $translator; } - public function addRole() { return null; } - public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data) + public function alterQuery(QueryBuilder $qb, $data) { $where = $qb->getDQLPart('where'); - $clause = $qb->expr()->between('activity.date', ':date_from', + $clause = $qb->expr()->between('activity.date', ':date_from', ':date_to'); if ($where instanceof Expr\Andx) { @@ -63,7 +40,7 @@ class ActivityDateFilter implements FilterInterface } else { $where = $qb->expr()->andX($clause); } - + $qb->add('where', $where); $qb->setParameter('date_from', $data['date_from']); $qb->setParameter('date_to', $data['date_to']); @@ -74,35 +51,43 @@ class ActivityDateFilter implements FilterInterface return 'activity'; } - public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder) + public function buildForm(FormBuilderInterface $builder) { - $builder->add('date_from', DateType::class, array( - 'label' => "Activities after this date", - 'data' => new \DateTime(), - 'attr' => array('class' => 'datepicker'), - 'widget'=> 'single_text', - 'format' => 'dd-MM-yyyy', - )); - - $builder->add('date_to', DateType::class, array( - 'label' => "Activities before this date", - 'data' => new \DateTime(), - 'attr' => array('class' => 'datepicker'), - 'widget'=> 'single_text', - 'format' => 'dd-MM-yyyy', - )); - + $builder->add( + 'date_from', + DateType::class, + [ + 'label' => 'Activities after this date', + 'data' => new \DateTime(), + 'attr' => ['class' => 'datepicker'], + 'widget'=> 'single_text', + 'format' => 'dd-MM-yyyy', + ] + ); + + $builder->add( + 'date_to', + DateType::class, + [ + 'label' => 'Activities before this date', + 'data' => new \DateTime(), + 'attr' => ['class' => 'datepicker'], + 'widget'=> 'single_text', + 'format' => 'dd-MM-yyyy', + ] + ); + $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) { /* @var $filterForm \Symfony\Component\Form\FormInterface */ $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( @@ -113,8 +98,8 @@ class ActivityDateFilter 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) @@ -132,17 +117,18 @@ class ActivityDateFilter implements FilterInterface public function describeAction($data, $format = 'string') { - return array( - "Filtered by date of activity: only between %date_from% and %date_to%", - array( - "%date_from%" => $data['date_from']->format('d-m-Y'), - '%date_to%' => $data['date_to']->format('d-m-Y') - )); + return [ + 'Filtered by date of activity: only between %date_from% and %date_to%', + [ + '%date_from%' => $data['date_from']->format('d-m-Y'), + '%date_to%' => $data['date_to']->format('d-m-Y') + ] + ]; } public function getTitle() { - return "Filtered by date activity"; + return 'Filtered by date activity'; } } diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php index 8773c95a0..e197e1220 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/PersonHavingActivityBetweenDateFilter.php @@ -33,9 +33,9 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, ExportEl protected TranslatorInterface $translator; public function __construct( - TranslatableStringHelper $translatableStringHelper, - ActivityReasonRepository $activityReasonRepository, - TranslatorInterface $translator + TranslatableStringHelper $translatableStringHelper, + ActivityReasonRepository $activityReasonRepository, + TranslatorInterface $translator ) { $this->translatableStringHelper = $translatableStringHelper; $this->activityReasonRepository = $activityReasonRepository; @@ -187,7 +187,7 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, ExportEl public function getTitle() { - return "Filtered by person having an activity in a period"; + return 'Filtered by person having an activity in a period'; } }