From c76e4010211bec2f0641e55d98a5cee1e00f8b64 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 3 Feb 2021 18:50:34 +0100 Subject: [PATCH] fix https://framagit.org/Chill-project/Chill-Person/-/issues/22 bug --- Controller/AccompanyingPeriodController.php | 61 +++++++++++---- Form/AccompanyingPeriodType.php | 82 ++++++++++++--------- Form/ClosingMotiveType.php | 13 +++- Form/Type/ClosingMotivePickerType.php | 63 ++++++++++------ Repository/ClosingMotiveRepository.php | 9 ++- config/services/repository.yaml | 2 - 6 files changed, 152 insertions(+), 78 deletions(-) diff --git a/Controller/AccompanyingPeriodController.php b/Controller/AccompanyingPeriodController.php index 566bb1eae..2f6925373 100644 --- a/Controller/AccompanyingPeriodController.php +++ b/Controller/AccompanyingPeriodController.php @@ -55,8 +55,10 @@ class AccompanyingPeriodController extends AbstractController $this->eventDispatcher = $eventDispatcher; } - - + /** + * @param $person_id + * @return Response + */ public function listAction($person_id){ $person = $this->_getPerson($person_id); @@ -71,8 +73,15 @@ class AccompanyingPeriodController extends AbstractController array('accompanying_periods' => $person->getAccompanyingPeriodsOrdered(), 'person' => $person)); } - - public function createAction($person_id, Request $request) { + + /** + * @param $person_id + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response + */ + public function createAction($person_id, Request $request) + { + $person = $this->_getPerson($person_id); $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, @@ -91,7 +100,7 @@ class AccompanyingPeriodController extends AbstractController 'period_action' => 'create', 'center' => $person->getCenter() ]); - + if ($request->getMethod() === 'POST') { $form->handleRequest($request); $errors = $this->_validatePerson($person); @@ -127,7 +136,13 @@ class AccompanyingPeriodController extends AbstractController ) ); } - + + /** + * @param $person_id + * @param $period_id + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response|\Symfony\Component\HttpKernel\Exception\NotFoundHttpException + */ public function updateAction($person_id, $period_id, Request $request){ $em = $this->getDoctrine()->getManager(); @@ -180,8 +195,16 @@ class AccompanyingPeriodController extends AbstractController 'accompanying_period' => $accompanyingPeriod ) ); } - - public function closeAction($person_id, Request $request) { + + /** + * @param $person_id + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response + * @throws \Exception + */ + public function closeAction($person_id, Request $request) + { + $person = $this->_getPerson($person_id); $this->denyAccessUnlessGranted(PersonVoter::UPDATE, $person, @@ -200,6 +223,7 @@ class AccompanyingPeriodController extends AbstractController } $current = $person->getCurrentAccompanyingPeriod(); + $form = $this->createForm(AccompanyingPeriodType::class, $current, array( 'period_action' => 'close', 'center' => $person->getCenter() @@ -256,10 +280,9 @@ class AccompanyingPeriodController extends AbstractController 'accompanying_period' => $current )); } - + /** - * - * @param Chill\PersonBundle\Entity\Person $person + * @param Person $person * @return \Symfony\Component\Validator\ConstraintViolationListInterface */ private function _validatePerson(Person $person) { @@ -274,8 +297,12 @@ class AccompanyingPeriodController extends AbstractController return $errors; } - - + + /** + * @param $person_id + * @param Request $request + * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response + */ public function openAction($person_id, Request $request) { $person = $this->_getPerson($person_id); @@ -343,7 +370,13 @@ class AccompanyingPeriodController extends AbstractController 'person' => $person, 'accompanying_period' => $accompanyingPeriod)); } - + + /** + * @param $person_id + * @param $period_id + * @param Request $request + * @return object|\Symfony\Component\HttpFoundation\RedirectResponse|Response + */ public function reOpenAction($person_id, $period_id, Request $request) { $person = $this->_getPerson($person_id); diff --git a/Form/AccompanyingPeriodType.php b/Form/AccompanyingPeriodType.php index eb29ffcc5..2de2011a1 100644 --- a/Form/AccompanyingPeriodType.php +++ b/Form/AccompanyingPeriodType.php @@ -2,6 +2,7 @@ namespace Chill\PersonBundle\Form; +use Chill\MainBundle\Entity\Center; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -16,6 +17,11 @@ use Chill\MainBundle\Form\Type\UserPickerType; use Symfony\Component\Security\Core\Role\Role; use Chill\PersonBundle\Form\Type\ClosingMotivePickerType; +/** + * Class AccompanyingPeriodType + * + * @package Chill\PersonBundle\Form + */ class AccompanyingPeriodType extends AbstractType { /** @@ -26,7 +32,7 @@ class AccompanyingPeriodType extends AbstractType * * @var string[] */ - protected $config = array(); + protected $config = []; /** * @@ -46,35 +52,36 @@ class AccompanyingPeriodType extends AbstractType //if the period_action is close, date opening should not be seen if ($options['period_action'] !== 'close') { $builder - ->add('openingDate', DateType::class, array( + ->add('openingDate', DateType::class, [ "required" => true, 'widget' => 'single_text', 'format' => 'dd-MM-yyyy' - )); + ]) + ; } - - // the closingDate should be seen only if period_action = close - // or period_action = update AND accopanying period is already closed - $builder->addEventListener( - FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) { - $accompanyingPeriod = $event->getData(); - $form = $event->getForm(); - - //add date opening - if ( - //if the period_action is "close, should not be shown" - ($options['period_action'] === 'close') - OR - ($options['period_action'] === 'create') - OR - ($options['period_action'] === 'update' AND !$accompanyingPeriod->isOpen()) - ) { - $form->add('closingDate', DateType::class, array('required' => true, - 'widget' => 'single_text', 'format' => 'dd-MM-yyyy')); - $form->add('closingMotive', ClosingMotivePickerType::class); - } - }); + // closingDate should be seen only if + // period_action = close + // OR ( period_action = update AND accompanying period is already closed ) + $accompanyingPeriod = $options['data']; + + if ( + ($options['period_action'] === 'close') + OR + ($options['period_action'] === 'create') + OR + ($options['period_action'] === 'update' AND !$accompanyingPeriod->isOpen()) + ) { + + $builder->add('closingDate', DateType::class, [ + 'required' => true, + 'widget' => 'single_text', + 'format' => 'dd-MM-yyyy' + ]); + + $builder->add('closingMotive', ClosingMotivePickerType::class); + } + if ($this->config['user'] === 'visible') { $builder->add('user', UserPickerType::class, [ 'center' => $options['center'], @@ -82,10 +89,9 @@ class AccompanyingPeriodType extends AbstractType ]); } - $builder->add('remark', TextareaType::class, array( - 'required' => false - )) - ; + $builder->add('remark', TextareaType::class, [ + 'required' => false + ]); } /** @@ -93,20 +99,24 @@ class AccompanyingPeriodType extends AbstractType */ public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(array( + $resolver->setDefaults([ 'data_class' => 'Chill\PersonBundle\Entity\AccompanyingPeriod' - )); + ]); $resolver - ->setRequired(array('period_action')) + ->setRequired(['period_action']) ->addAllowedTypes('period_action', 'string') - ->addAllowedValues('period_action', array( - 'update', 'open', 'close', 'create')) + ->addAllowedValues('period_action', ['update', 'open', 'close', 'create']) ->setRequired('center') - ->setAllowedTypes('center', \Chill\MainBundle\Entity\Center::class) + ->setAllowedTypes('center', Center::class) ; } - + + /** + * @param FormView $view + * @param FormInterface $form + * @param array $options + */ public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['action'] = $options['period_action']; diff --git a/Form/ClosingMotiveType.php b/Form/ClosingMotiveType.php index 10bff895f..672fc4f19 100644 --- a/Form/ClosingMotiveType.php +++ b/Form/ClosingMotiveType.php @@ -21,17 +21,23 @@ namespace Chill\PersonBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Chill\PersonBundle\Form\Type\ClosingMotivePickerType; use Chill\MainBundle\Form\Type\TranslatableStringFormType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\NumberType; /** - * + * Class ClosingMotiveType * + * @package Chill\PersonBundle\Form */ class ClosingMotiveType extends AbstractType { + /** + * @param FormBuilderInterface $builder + * @param array $options + */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder @@ -57,10 +63,13 @@ class ClosingMotiveType extends AbstractType ; } + /** + * @param OptionsResolver $resolver + */ public function configureOptions(OptionsResolver $resolver) { $resolver - ->setDefault('class', \Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive::class) + ->setDefault('class', ClosingMotive::class) ; } } diff --git a/Form/Type/ClosingMotivePickerType.php b/Form/Type/ClosingMotivePickerType.php index 209153d46..aa7e47583 100644 --- a/Form/Type/ClosingMotivePickerType.php +++ b/Form/Type/ClosingMotivePickerType.php @@ -14,30 +14,36 @@ use Symfony\Component\OptionsResolver\Options; use Chill\PersonBundle\Repository\ClosingMotiveRepository; /** + * Class ClosingMotivePickerType * A type to add a closing motive * + * @package Chill\PersonBundle\Form\Type */ class ClosingMotivePickerType extends AbstractType { /** - * * @var TranslatableStringHelper */ protected $translatableStringHelper; - + /** - * * @var ChillEntityRenderExtension */ protected $entityRenderExtension; - + /** - * * @var ClosingMotiveRepository */ protected $repository; - + + /** + * ClosingMotivePickerType constructor. + * + * @param TranslatableStringHelper $translatableStringHelper + * @param ChillEntityRenderExtension $chillEntityRenderExtension + * @param ClosingMotiveRepository $closingMotiveRepository + */ public function __construct( TranslatableStringHelper $translatableStringHelper, ChillEntityRenderExtension $chillEntityRenderExtension, @@ -47,35 +53,46 @@ class ClosingMotivePickerType extends AbstractType $this->entityRenderExtension = $chillEntityRenderExtension; $this->repository = $closingMotiveRepository; } - - public function getBlockPrefix() + + /** + * @return string + */ + public function getBlockPrefix() { return 'closing_motive'; } - + + /** + * @return null|string + */ public function getParent() { return EntityType::class; } - + + /** + * @param OptionsResolver $resolver + */ public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(array( - 'class' => ClosingMotive::class, - 'empty_data' => null, - 'placeholder' => 'Choose a motive', - 'choice_label' => function(ClosingMotive $cm) { - return $this->entityRenderExtension->renderString($cm); - }, - 'only_leaf' => true - ) - ); - + + $resolver->setDefaults([ + 'class' => ClosingMotive::class, + 'empty_data' => null, + 'placeholder' => 'Choose a motive', + 'choice_label' => function(ClosingMotive $cm) { + return $this->entityRenderExtension->renderString($cm); + }, + 'only_leaf' => true + ]); + $resolver ->setAllowedTypes('only_leaf', 'bool') ->setNormalizer('choices', function (Options $options) { - return $this->repository->getActiveClosingMotive($options['only_leaf']); - }); + return $this->repository + ->getActiveClosingMotive($options['only_leaf']); + }) + ; } } diff --git a/Repository/ClosingMotiveRepository.php b/Repository/ClosingMotiveRepository.php index 279935da9..c3519796d 100644 --- a/Repository/ClosingMotiveRepository.php +++ b/Repository/ClosingMotiveRepository.php @@ -17,15 +17,22 @@ */ namespace Chill\PersonBundle\Repository; +use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Query\ResultSetMappingBuilder; /** + * Class ClosingMotiveRepository * Entity repository for closing motives * + * @package Chill\PersonBundle\Repository */ -class ClosingMotiveRepository extends \Doctrine\ORM\EntityRepository +class ClosingMotiveRepository extends EntityRepository { + /** + * @param bool $onlyLeaf + * @return mixed + */ public function getActiveClosingMotive(bool $onlyLeaf = true) { $rsm = new ResultSetMappingBuilder($this->getEntityManager()); diff --git a/config/services/repository.yaml b/config/services/repository.yaml index 83fde0e4e..ee945def7 100644 --- a/config/services/repository.yaml +++ b/config/services/repository.yaml @@ -12,5 +12,3 @@ services: factory: ['@doctrine.orm.entity_manager', getRepository] arguments: - 'Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive' - tags: - - { name: doctrine.repository_service }