add validation and translations

This commit is contained in:
Julien Fastré 2017-02-26 01:04:02 +01:00
parent 7a76868f6a
commit 13f3a1f55f
6 changed files with 76 additions and 17 deletions

View File

@ -26,6 +26,7 @@ use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormError;
use Chill\MainBundle\Form\Type\Export\FilterType; use Chill\MainBundle\Form\Type\Export\FilterType;
use Doctrine\ORM\Query\Expr; use Doctrine\ORM\Query\Expr;
use Symfony\Component\Translation\TranslatorInterface;
/** /**
* *
@ -34,6 +35,17 @@ use Doctrine\ORM\Query\Expr;
*/ */
class ActivityDateFilter implements FilterInterface class ActivityDateFilter implements FilterInterface
{ {
/**
*
* @var TranslatorInterface
*/
protected $translator;
function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function addRole() public function addRole()
{ {
@ -93,12 +105,14 @@ class ActivityDateFilter implements FilterInterface
// 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('This field ' $form->get('date_from')->addError(new FormError(
. 'should not be empty')); $this->translator->trans('This field '
. 'should not be empty')));
} }
if ($date_to === null) { if ($date_to === null) {
$form->get('date_to')->addError(new FormError('This field ' $form->get('date_to')->addError(new FormError(
. 'should not be empty')); $this->translator->trans('This field '
. 'should not be empty')));
} }
// check that date_from is before date_to // check that date_from is before date_to
@ -107,8 +121,10 @@ class ActivityDateFilter implements FilterInterface
&& &&
$date_from >= $date_to $date_from >= $date_to
) { ) {
$form->get('date_to')->addError(new FormError('This date ' $form->get('date_to')->addError(new FormError(
. 'should be after the date given in "activities after" field')); $this->translator->trans('This date should be after '
. 'the date given in "Implied in an activity after '
. 'this date" field')));
} }
} }
}); });

View File

@ -30,13 +30,16 @@ use Symfony\Component\Security\Core\Role\Role;
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter; use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr\Join; use Doctrine\ORM\Query\Expr\Join;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
/** /**
* *
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
*/ */
class ActivityReasonFilter implements FilterInterface class ActivityReasonFilter implements FilterInterface,
ExportElementValidatedInterface
{ {
/** /**
* *
@ -131,8 +134,16 @@ class ActivityReasonFilter implements FilterInterface
'expanded' => false 'expanded' => false
)); ));
} }
public function validateForm($data, ExecutionContextInterface $context)
{
if ($data['reasons'] === null || count($data['reasons']) === 0) {
$context->buildViolation("At least one reason must be choosen")
->addViolation();
}
}
public function getTitle() public function getTitle()
{ {
return 'Filter by reason'; return 'Filter by reason';
} }

View File

@ -32,13 +32,17 @@ use Chill\ActivityBundle\Entity\ActivityReason;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\MainBundle\Export\ExportElementValidatedInterface;
/** /**
* *
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
*/ */
class PersonHavingActivityBetweenDateFilter implements FilterInterface class PersonHavingActivityBetweenDateFilter implements FilterInterface,
ExportElementValidatedInterface
{ {
/** /**
@ -53,12 +57,20 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface
*/ */
protected $activityReasonRepository; protected $activityReasonRepository;
/**
*
* @var TranslatorInterface
*/
protected $translator;
public function __construct( public function __construct(
TranslatableStringHelper $translatableStringHelper, TranslatableStringHelper $translatableStringHelper,
EntityRepository $activityReasonRepository EntityRepository $activityReasonRepository,
TranslatorInterface $translator
) { ) {
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->activityReasonRepository = $activityReasonRepository; $this->activityReasonRepository = $activityReasonRepository;
$this->translator = $translator;
} }
@ -158,12 +170,14 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface
// 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('This field ' $form->get('date_from')->addError(new FormError(
. 'should not be empty')); $this->translator->trans('This field '
. 'should not be empty')));
} }
if ($date_to === null) { if ($date_to === null) {
$form->get('date_to')->addError(new FormError('This field ' $form->get('date_to')->addError(new FormError(
. 'should not be empty')); $this->translator->trans('This field '
. 'should not be empty')));
} }
// check that date_from is before date_to // check that date_from is before date_to
@ -172,13 +186,22 @@ class PersonHavingActivityBetweenDateFilter implements FilterInterface
&& &&
$date_from >= $date_to $date_from >= $date_to
) { ) {
$form->get('date_to')->addError(new FormError('This date ' $form->get('date_to')->addError(new FormError(
$this->translator->trans('This date '
. 'should be after the date given in "Implied in an ' . 'should be after the date given in "Implied in an '
. 'activity after this date" field')); . 'activity after this date" field')));
} }
} }
}); });
} }
public function validateForm($data, ExecutionContextInterface $context)
{
if ($data['reasons'] === null || count($data['reasons']) === 0) {
$context->buildViolation("At least one reason must be choosen")
->addViolation();
}
}
public function describeAction($data, $format = 'string') public function describeAction($data, $format = 'string')
{ {

View File

@ -33,6 +33,8 @@ services:
chill.activity.export.date_filter: chill.activity.export.date_filter:
class: Chill\ActivityBundle\Export\Filter\ActivityDateFilter class: Chill\ActivityBundle\Export\Filter\ActivityDateFilter
arguments:
- "@translator"
tags: tags:
- { name: chill.export_filter, alias: 'activity_date_filter' } - { name: chill.export_filter, alias: 'activity_date_filter' }
@ -41,6 +43,7 @@ services:
arguments: arguments:
- "@chill.main.helper.translatable_string" - "@chill.main.helper.translatable_string"
- "@chill_activity.repository.reason" - "@chill_activity.repository.reason"
- "@translator"
tags: tags:
- #0 register as a filter - #0 register as a filter
name: chill.export_filter name: chill.export_filter

View File

@ -103,8 +103,11 @@ Count activities by various parameters.: Compte le nombre d'activités enregistr
Filter by reason: Filtrer par sujet d'activité Filter by reason: Filtrer par sujet d'activité
'Filtered by reasons: only %list%': 'Filtré par sujet: seulement %list%' 'Filtered by reasons: only %list%': 'Filtré par sujet: seulement %list%'
Filtered by date activity: Filtrer par date d'activité
Activities after this date: Activités après cette date
Activities before this date: Activités avant cette date
"Filtered by date of activity: only between %date_from% and %date_to%": "Filtré par date de l'activité: uniquement entre %date_from% et %date_to%" "Filtered by date of activity: only between %date_from% and %date_to%": "Filtré par date de l'activité: uniquement entre %date_from% et %date_to%"
This date should be after the date given in "Implied in an activity after this date" field: Cette date devrait être postérieure à la date donnée dans le champ "activités après cette date"
Filtered by person having an activity in a period: Uniquement les personnes ayant eu une activité dans la période donnée Filtered by person having an activity in a period: Uniquement les personnes ayant eu une activité dans la période donnée
Implied in an activity after this date: Impliqué dans une activité après cette date Implied in an activity after this date: Impliqué dans une activité après cette date
@ -112,8 +115,10 @@ Implied in an activity before this date: Impliqué dans une activité avant cett
Filtered by person having an activity between %date_from% and %date_to% with reasons %reasons_name%: Filtré par personnes associées à une activité entre %date_from% et %date_to% avec les sujets %reasons_name% Filtered by person having an activity between %date_from% and %date_to% with reasons %reasons_name%: Filtré par personnes associées à une activité entre %date_from% et %date_to% avec les sujets %reasons_name%
Activity reasons for those activities: Sujets de ces activités Activity reasons for those activities: Sujets de ces activités
#aggregators #aggregators
Aggregate by activity reason: Aggréger par sujet de l'activité Aggregate by activity reason: Aggréger par sujet de l'activité
By reason: Par sujet By reason: Par sujet
By category of reason: Par catégorie de sujet By category of reason: Par catégorie de sujet
Reason's level: Niveau du sujet Reason's level: Niveau du sujet
Aggregate by activity type: Aggréger par date d'activité

View File

@ -1 +1,2 @@
The reasons's level should not be empty: Le niveau du sujet ne peut pas être vide The reasons's level should not be empty: Le niveau du sujet ne peut pas être vide
At least one reason must be choosen: Au moins un sujet doit être choisi