mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
fix: Let Symfony inject dependencies automatically.
This commit is contained in:
parent
d9b862925f
commit
6ccc8d4cb8
@ -1,25 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
declare(strict_types=1);
|
||||||
* Copyright (C) 2017 Champs-Libres <info@champs-libres.coop>
|
|
||||||
*
|
|
||||||
* 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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Chill\ActivityBundle\Export\Filter;
|
namespace Chill\ActivityBundle\Export\Filter;
|
||||||
|
|
||||||
use Chill\MainBundle\Export\FilterInterface;
|
use Chill\MainBundle\Export\FilterInterface;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\Form\FormEvent;
|
use Symfony\Component\Form\FormEvent;
|
||||||
use Symfony\Component\Form\FormEvents;
|
use Symfony\Component\Form\FormEvents;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
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 Doctrine\ORM\Query\Expr;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
|
||||||
*/
|
|
||||||
class ActivityDateFilter implements FilterInterface
|
class ActivityDateFilter implements FilterInterface
|
||||||
{
|
{
|
||||||
/**
|
protected TranslatorInterface $translator;
|
||||||
*
|
|
||||||
* @var TranslatorInterface
|
|
||||||
*/
|
|
||||||
protected $translator;
|
|
||||||
|
|
||||||
function __construct(TranslatorInterface $translator)
|
function __construct(TranslatorInterface $translator)
|
||||||
{
|
{
|
||||||
$this->translator = $translator;
|
$this->translator = $translator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function addRole()
|
public function addRole()
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function alterQuery(\Doctrine\ORM\QueryBuilder $qb, $data)
|
public function alterQuery(QueryBuilder $qb, $data)
|
||||||
{
|
{
|
||||||
$where = $qb->getDQLPart('where');
|
$where = $qb->getDQLPart('where');
|
||||||
$clause = $qb->expr()->between('activity.date', ':date_from',
|
$clause = $qb->expr()->between('activity.date', ':date_from',
|
||||||
':date_to');
|
':date_to');
|
||||||
|
|
||||||
if ($where instanceof Expr\Andx) {
|
if ($where instanceof Expr\Andx) {
|
||||||
@ -63,7 +40,7 @@ class ActivityDateFilter implements FilterInterface
|
|||||||
} else {
|
} else {
|
||||||
$where = $qb->expr()->andX($clause);
|
$where = $qb->expr()->andX($clause);
|
||||||
}
|
}
|
||||||
|
|
||||||
$qb->add('where', $where);
|
$qb->add('where', $where);
|
||||||
$qb->setParameter('date_from', $data['date_from']);
|
$qb->setParameter('date_from', $data['date_from']);
|
||||||
$qb->setParameter('date_to', $data['date_to']);
|
$qb->setParameter('date_to', $data['date_to']);
|
||||||
@ -74,35 +51,43 @@ class ActivityDateFilter implements FilterInterface
|
|||||||
return 'activity';
|
return 'activity';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder)
|
public function buildForm(FormBuilderInterface $builder)
|
||||||
{
|
{
|
||||||
$builder->add('date_from', DateType::class, array(
|
$builder->add(
|
||||||
'label' => "Activities after this date",
|
'date_from',
|
||||||
'data' => new \DateTime(),
|
DateType::class,
|
||||||
'attr' => array('class' => 'datepicker'),
|
[
|
||||||
'widget'=> 'single_text',
|
'label' => 'Activities after this date',
|
||||||
'format' => 'dd-MM-yyyy',
|
'data' => new \DateTime(),
|
||||||
));
|
'attr' => ['class' => 'datepicker'],
|
||||||
|
'widget'=> 'single_text',
|
||||||
$builder->add('date_to', DateType::class, array(
|
'format' => 'dd-MM-yyyy',
|
||||||
'label' => "Activities before this date",
|
]
|
||||||
'data' => new \DateTime(),
|
);
|
||||||
'attr' => array('class' => 'datepicker'),
|
|
||||||
'widget'=> 'single_text',
|
$builder->add(
|
||||||
'format' => 'dd-MM-yyyy',
|
'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) {
|
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
|
||||||
/* @var $filterForm \Symfony\Component\Form\FormInterface */
|
/* @var $filterForm \Symfony\Component\Form\FormInterface */
|
||||||
$filterForm = $event->getForm()->getParent();
|
$filterForm = $event->getForm()->getParent();
|
||||||
$enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData();
|
$enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData();
|
||||||
|
|
||||||
if ($enabled === true) {
|
if ($enabled === true) {
|
||||||
// if the filter is enabled, add some validation
|
// if the filter is enabled, add some validation
|
||||||
$form = $event->getForm();
|
$form = $event->getForm();
|
||||||
$date_from = $form->get('date_from')->getData();
|
$date_from = $form->get('date_from')->getData();
|
||||||
$date_to = $form->get('date_to')->getData();
|
$date_to = $form->get('date_to')->getData();
|
||||||
|
|
||||||
// check that fields are not empty
|
// check that fields are not empty
|
||||||
if ($date_from === null) {
|
if ($date_from === null) {
|
||||||
$form->get('date_from')->addError(new FormError(
|
$form->get('date_from')->addError(new FormError(
|
||||||
@ -113,8 +98,8 @@ class ActivityDateFilter implements FilterInterface
|
|||||||
$form->get('date_to')->addError(new FormError(
|
$form->get('date_to')->addError(new FormError(
|
||||||
$this->translator->trans('This field '
|
$this->translator->trans('This field '
|
||||||
. 'should not be empty')));
|
. 'should not be empty')));
|
||||||
}
|
}
|
||||||
|
|
||||||
// check that date_from is before date_to
|
// check that date_from is before date_to
|
||||||
if (
|
if (
|
||||||
($date_from !== null && $date_to !== null)
|
($date_from !== null && $date_to !== null)
|
||||||
@ -132,17 +117,18 @@ class ActivityDateFilter implements FilterInterface
|
|||||||
|
|
||||||
public function describeAction($data, $format = 'string')
|
public function describeAction($data, $format = 'string')
|
||||||
{
|
{
|
||||||
return array(
|
return [
|
||||||
"Filtered by date of activity: only between %date_from% and %date_to%",
|
'Filtered by date of activity: only between %date_from% and %date_to%',
|
||||||
array(
|
[
|
||||||
"%date_from%" => $data['date_from']->format('d-m-Y'),
|
'%date_from%' => $data['date_from']->format('d-m-Y'),
|
||||||
'%date_to%' => $data['date_to']->format('d-m-Y')
|
'%date_to%' => $data['date_to']->format('d-m-Y')
|
||||||
));
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTitle()
|
public function getTitle()
|
||||||
{
|
{
|
||||||
return "Filtered by date activity";
|
return 'Filtered by date activity';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,9 +33,9 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, ExportEl
|
|||||||
protected TranslatorInterface $translator;
|
protected TranslatorInterface $translator;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
TranslatableStringHelper $translatableStringHelper,
|
TranslatableStringHelper $translatableStringHelper,
|
||||||
ActivityReasonRepository $activityReasonRepository,
|
ActivityReasonRepository $activityReasonRepository,
|
||||||
TranslatorInterface $translator
|
TranslatorInterface $translator
|
||||||
) {
|
) {
|
||||||
$this->translatableStringHelper = $translatableStringHelper;
|
$this->translatableStringHelper = $translatableStringHelper;
|
||||||
$this->activityReasonRepository = $activityReasonRepository;
|
$this->activityReasonRepository = $activityReasonRepository;
|
||||||
@ -187,7 +187,7 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface, ExportEl
|
|||||||
|
|
||||||
public function getTitle()
|
public function getTitle()
|
||||||
{
|
{
|
||||||
return "Filtered by person having an activity in a period";
|
return 'Filtered by person having an activity in a period';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user