mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
fix: Inject newly created repository instead of having to deal with the container definitions.
This commit is contained in:
parent
97ab71b63d
commit
7dcf0f8dcf
@ -1,25 +1,14 @@
|
|||||||
<?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\ActivityBundle\Repository\ActivityReasonRepository;
|
||||||
use Chill\MainBundle\Export\FilterInterface;
|
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\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;
|
||||||
@ -29,43 +18,23 @@ use Doctrine\ORM\Query\Expr;
|
|||||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
use Chill\ActivityBundle\Entity\ActivityReason;
|
use Chill\ActivityBundle\Entity\ActivityReason;
|
||||||
use Doctrine\ORM\EntityRepository;
|
|
||||||
use Doctrine\ORM\EntityManager;
|
|
||||||
use Chill\PersonBundle\Export\Declarations;
|
use Chill\PersonBundle\Export\Declarations;
|
||||||
|
use Symfony\Component\Form\FormInterface;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||||
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
||||||
|
|
||||||
/**
|
class PersonHavingActivityBetweenDateFilter implements FilterInterface, ExportElementValidatedInterface
|
||||||
*
|
|
||||||
*
|
|
||||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
|
||||||
*/
|
|
||||||
class PersonHavingActivityBetweenDateFilter implements FilterInterface,
|
|
||||||
ExportElementValidatedInterface
|
|
||||||
{
|
{
|
||||||
|
protected TranslatableStringHelperInterface $translatableStringHelper;
|
||||||
|
|
||||||
/**
|
protected ActivityReasonRepository $activityReasonRepository;
|
||||||
*
|
|
||||||
* @var TranslatableStringHelper
|
|
||||||
*/
|
|
||||||
protected $translatableStringHelper;
|
|
||||||
|
|
||||||
/**
|
protected TranslatorInterface $translator;
|
||||||
*
|
|
||||||
* @var EntityRepository
|
|
||||||
*/
|
|
||||||
protected $activityReasonRepository;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @var TranslatorInterface
|
|
||||||
*/
|
|
||||||
protected $translator;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
TranslatableStringHelper $translatableStringHelper,
|
TranslatableStringHelper $translatableStringHelper,
|
||||||
EntityRepository $activityReasonRepository,
|
ActivityReasonRepository $activityReasonRepository,
|
||||||
TranslatorInterface $translator
|
TranslatorInterface $translator
|
||||||
) {
|
) {
|
||||||
$this->translatableStringHelper = $translatableStringHelper;
|
$this->translatableStringHelper = $translatableStringHelper;
|
||||||
@ -73,32 +42,32 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface,
|
|||||||
$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)
|
||||||
{
|
{
|
||||||
// create a query for activity
|
// create a query for activity
|
||||||
$sqb = $qb->getEntityManager()->createQueryBuilder();
|
$sqb = $qb->getEntityManager()->createQueryBuilder();
|
||||||
$sqb->select("person_person_having_activity.id")
|
$sqb->select('person_person_having_activity.id')
|
||||||
->from("ChillActivityBundle:Activity", "activity_person_having_activity")
|
->from('ChillActivityBundle:Activity', 'activity_person_having_activity')
|
||||||
->join("activity_person_having_activity.person", "person_person_having_activity")
|
->join('activity_person_having_activity.person', 'person_person_having_activity');
|
||||||
;
|
|
||||||
// add clause between date
|
// add clause between date
|
||||||
$sqb->where("activity_person_having_activity.date BETWEEN "
|
$sqb->where('activity_person_having_activity.date BETWEEN '
|
||||||
. ":person_having_activity_between_date_from"
|
. ':person_having_activity_between_date_from'
|
||||||
. " AND "
|
. ' AND '
|
||||||
. ":person_having_activity_between_date_to");
|
. ':person_having_activity_between_date_to');
|
||||||
|
|
||||||
// add clause activity reason
|
// add clause activity reason
|
||||||
$sqb->join('activity_person_having_activity.reasons',
|
$sqb->join('activity_person_having_activity.reasons', 'reasons_person_having_activity');
|
||||||
'reasons_person_having_activity');
|
|
||||||
$sqb->andWhere(
|
$sqb->andWhere(
|
||||||
$sqb->expr()->in(
|
$sqb->expr()->in(
|
||||||
'reasons_person_having_activity',
|
'reasons_person_having_activity', ':person_having_activity_reasons'
|
||||||
":person_having_activity_reasons")
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
$where = $qb->getDQLPart('where');
|
$where = $qb->getDQLPart('where');
|
||||||
@ -123,42 +92,36 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface,
|
|||||||
return Declarations::PERSON_IMPLIED_IN;
|
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(
|
$builder->add('date_from', DateType::class, [
|
||||||
'label' => "Implied in an activity after this date",
|
'label' => 'Implied in an activity after this date',
|
||||||
'data' => new \DateTime(),
|
'data' => new \DateTime(),
|
||||||
'attr' => array('class' => 'datepicker'),
|
'attr' => ['class' => 'datepicker'],
|
||||||
'widget'=> 'single_text',
|
'widget'=> 'single_text',
|
||||||
'format' => 'dd-MM-yyyy',
|
'format' => 'dd-MM-yyyy',
|
||||||
));
|
]);
|
||||||
|
|
||||||
$builder->add('date_to', DateType::class, array(
|
$builder->add('date_to', DateType::class, [
|
||||||
'label' => "Implied in an activity before this date",
|
'label' => 'Implied in an activity before this date',
|
||||||
'data' => new \DateTime(),
|
'data' => new \DateTime(),
|
||||||
'attr' => array('class' => 'datepicker'),
|
'attr' => ['class' => 'datepicker'],
|
||||||
'widget'=> 'single_text',
|
'widget'=> 'single_text',
|
||||||
'format' => 'dd-MM-yyyy',
|
'format' => 'dd-MM-yyyy',
|
||||||
));
|
]);
|
||||||
|
|
||||||
$builder->add('reasons', EntityType::class, array(
|
$builder->add('reasons', EntityType::class, [
|
||||||
'class' => 'ChillActivityBundle:ActivityReason',
|
'class' => ActivityReason::class,
|
||||||
'choice_label' => function (ActivityReason $reason) {
|
'choice_label' => static fn (ActivityReason $reason): ?string => $this->translatableStringHelper->localize($reason->getName()),
|
||||||
return $this->translatableStringHelper
|
'group_by' => static fn(ActivityReason $reason): ?string => $this->translatableStringHelper->localize($reason->getCategory()->getName()),
|
||||||
->localize($reason->getName());
|
|
||||||
},
|
|
||||||
'group_by' => function(ActivityReason $reason) {
|
|
||||||
return $this->translatableStringHelper
|
|
||||||
->localize($reason->getCategory()->getName());
|
|
||||||
},
|
|
||||||
'data' => $this->activityReasonRepository->findAll(),
|
'data' => $this->activityReasonRepository->findAll(),
|
||||||
'multiple' => true,
|
'multiple' => true,
|
||||||
'expanded' => false,
|
'expanded' => false,
|
||||||
'label' => "Activity reasons for those activities"
|
'label' => 'Activity reasons for those activities'
|
||||||
));
|
]);
|
||||||
|
|
||||||
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
|
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
|
||||||
/* @var $filterForm \Symfony\Component\Form\FormInterface */
|
/* @var FormInterface $filterForm */
|
||||||
$filterForm = $event->getForm()->getParent();
|
$filterForm = $event->getForm()->getParent();
|
||||||
$enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData();
|
$enabled = $filterForm->get(FilterType::ENABLED_FIELD)->getData();
|
||||||
|
|
||||||
@ -198,26 +161,28 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface,
|
|||||||
public function validateForm($data, ExecutionContextInterface $context)
|
public function validateForm($data, ExecutionContextInterface $context)
|
||||||
{
|
{
|
||||||
if ($data['reasons'] === null || count($data['reasons']) === 0) {
|
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();
|
->addViolation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function describeAction($data, $format = 'string')
|
public function describeAction($data, $format = 'string')
|
||||||
{
|
{
|
||||||
return array(
|
return [
|
||||||
"Filtered by person having an activity between %date_from% and "
|
'Filtered by person having an activity between %date_from% and '
|
||||||
. "%date_to% with reasons %reasons_name%",
|
. '%date_to% with reasons %reasons_name%',
|
||||||
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'),
|
||||||
"%reasons_name%" => implode(", ", array_map(
|
'%reasons_name%' => implode(
|
||||||
function (ActivityReason $r) {
|
", ",
|
||||||
return '"'.$this->translatableStringHelper->
|
array_map(
|
||||||
localize($r->getName()).'"';
|
static fn(ActivityReason $r): string => '"' . $this->translatableStringHelper->localize($r->getName()) . '"',
|
||||||
},
|
$data['reasons']
|
||||||
$data['reasons']))
|
)
|
||||||
));
|
)
|
||||||
|
]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTitle()
|
public function getTitle()
|
||||||
|
@ -1,56 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
declare(strict_types=1);
|
||||||
* Chill is a software for social workers
|
|
||||||
*
|
|
||||||
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
|
||||||
* <http://www.champs-libres.coop>, <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\Form\Type;
|
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\AbstractType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
|
||||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
|
||||||
use Doctrine\ORM\EntityRepository;
|
|
||||||
use Chill\ActivityBundle\Entity\ActivityType;
|
use Chill\ActivityBundle\Entity\ActivityType;
|
||||||
|
|
||||||
/**
|
|
||||||
* Description of TranslatableActivityType
|
|
||||||
*
|
|
||||||
* @author Champs-Libres Coop
|
|
||||||
*/
|
|
||||||
class TranslatableActivityType extends AbstractType
|
class TranslatableActivityType extends AbstractType
|
||||||
{
|
{
|
||||||
|
protected TranslatableStringHelperInterface $translatableStringHelper;
|
||||||
|
|
||||||
/**
|
protected ActivityTypeRepository $activityTypeRepository;
|
||||||
*
|
|
||||||
* @var TranslatableStringHelper
|
|
||||||
*/
|
|
||||||
protected $translatableStringHelper;
|
|
||||||
|
|
||||||
protected $activityTypeRepository;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
TranslatableStringHelper $helper,
|
TranslatableStringHelperInterface $helper,
|
||||||
EntityRepository $activityTypeRepository
|
ActivityTypeRepository $activityTypeRepository
|
||||||
)
|
) {
|
||||||
{
|
|
||||||
$this->translatableStringHelper = $helper;
|
$this->translatableStringHelper = $helper;
|
||||||
$this->activityTypeRepository = $activityTypeRepository;
|
$this->activityTypeRepository = $activityTypeRepository;
|
||||||
}
|
}
|
||||||
@ -65,22 +38,21 @@ class TranslatableActivityType extends AbstractType
|
|||||||
return EntityType::class;
|
return EntityType::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options) {
|
public function buildForm(FormBuilderInterface $builder, array $options) {
|
||||||
/* @var $qb \Doctrine\ORM\QueryBuilder */
|
/* @var QueryBuilder $qb */
|
||||||
$qb = $options['query_builder'];
|
$qb = $options['query_builder'];
|
||||||
|
|
||||||
if ($options['active_only'] === true) {
|
if ($options['active_only'] === true) {
|
||||||
$qb->where($qb->expr()->eq('at.active', ':active'));
|
$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)
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
{
|
{
|
||||||
|
|
||||||
$resolver->setDefaults(
|
$resolver->setDefaults(
|
||||||
array(
|
array(
|
||||||
'class' => 'ChillActivityBundle:ActivityType',
|
'class' => ActivityType::class,
|
||||||
'active_only' => true,
|
'active_only' => true,
|
||||||
'query_builder' => $this->activityTypeRepository
|
'query_builder' => $this->activityTypeRepository
|
||||||
->createQueryBuilder('at'),
|
->createQueryBuilder('at'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user