From c3f9ce1ea6212c8e6e99fe632f9868cb53f40566 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 17:39:13 +0200 Subject: [PATCH 01/13] issue641: create 2 new Select2 formType (for actions and issues) --- .../ACPFilters/BySocialActionFilter.php | 12 ++--- .../Filter/ACPFilters/BySocialIssueFilter.php | 12 ++--- .../Form/Type/Select2SocialActionType.php | 51 +++++++++++++++++++ .../Form/Type/Select2SocialIssueType.php | 51 +++++++++++++++++++ .../config/services/form.yaml | 8 +++ .../translations/messages.fr.yml | 2 + 6 files changed, 118 insertions(+), 18 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php create mode 100644 src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php index e3ce8b287..4502450cb 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php @@ -13,11 +13,10 @@ namespace Chill\ActivityBundle\Export\Filter\ACPFilters; use Chill\ActivityBundle\Export\Declarations; use Chill\MainBundle\Export\FilterInterface; -use Chill\PersonBundle\Entity\SocialWork\SocialAction; +use Chill\PersonBundle\Form\Type\Select2SocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -62,13 +61,8 @@ class BySocialActionFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialactions', EntityType::class, [ - 'class' => SocialAction::class, - 'choice_label' => function (SocialAction $sa) { - return $this->actionRender->renderString($sa, []); - }, - 'multiple' => true, - 'expanded' => true, + $builder->add('accepted_socialactions', Select2SocialActionType::class, [ + 'multiple' => true ]); } diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php index f5d552011..59d79d21d 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php @@ -13,11 +13,10 @@ namespace Chill\ActivityBundle\Export\Filter\ACPFilters; use Chill\ActivityBundle\Export\Declarations; use Chill\MainBundle\Export\FilterInterface; -use Chill\PersonBundle\Entity\SocialWork\SocialIssue; +use Chill\PersonBundle\Form\Type\Select2SocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -62,13 +61,8 @@ class BySocialIssueFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialissues', EntityType::class, [ - 'class' => SocialIssue::class, - 'choice_label' => function (SocialIssue $si) { - return $this->issueRender->renderString($si, []); - }, - 'multiple' => true, - 'expanded' => true, + $builder->add('accepted_socialissues', Select2SocialIssueType::class, [ + 'multiple' => true ]); } diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php new file mode 100644 index 000000000..f1e8cd913 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php @@ -0,0 +1,51 @@ +actionRender = $actionRender; + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->add('social_actions', EntityType::class, [ + 'class' => SocialAction::class, + 'choice_label' => function (SocialAction $sa) { + return $this->actionRender->renderString($sa, []); + }, + 'placeholder' => 'Pick a social action', + 'required' => false, + 'label' => $options['label'], + 'label_attr' => $options['label_attr'], + 'multiple' => $options['multiple'], + 'attr' => ['class' => 'select2'], + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver + ->setDefault('label', 'Social actions') + ->setDefault('label_attr', []) + ->setDefault('multiple', false) + ->setAllowedTypes('multiple', ['bool']) + ; + } + + public function getBlockPrefix(): string + { + return 'select2_social_action_type'; + } +} diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php new file mode 100644 index 000000000..49ee6d574 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php @@ -0,0 +1,51 @@ +issueRender = $issueRender; + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder->add('social_issues', EntityType::class, [ + 'class' => SocialIssue::class, + 'choice_label' => function (SocialIssue $si) { + return $this->issueRender->renderString($si, []); + }, + 'placeholder' => 'Pick a social issue', + 'required' => false, + 'label' => $options['label'], + 'label_attr' => $options['label_attr'], + 'multiple' => $options['multiple'], + 'attr' => ['class' => 'select2'], + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver + ->setDefault('label', 'Social issues') + ->setDefault('label_attr', []) + ->setDefault('multiple', false) + ->setAllowedTypes('multiple', ['bool']) + ; + } + + public function getBlockPrefix(): string + { + return 'select2_social_issue_type'; + } +} diff --git a/src/Bundle/ChillPersonBundle/config/services/form.yaml b/src/Bundle/ChillPersonBundle/config/services/form.yaml index 52bf1f494..85b97c86b 100644 --- a/src/Bundle/ChillPersonBundle/config/services/form.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/form.yaml @@ -27,3 +27,11 @@ services: $config: "%chill_person.accompanying_period_fields%" tags: - { name: form.type } + + Chill\PersonBundle\Form\Type\Select2SocialActionType: + autowire: true + autoconfigure: true + + Chill\PersonBundle\Form\Type\Select2SocialIssueType: + autowire: true + autoconfigure: true diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index a5040e6df..a755ce0f0 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -205,9 +205,11 @@ Resources: Interlocuteurs privilégiés Any requestor to this accompanying course: Aucun demandeur pour ce parcours Social action: Action d'accompagnement Social actions: Actions d'accompagnement +Pick a social action: Choisir une action d'accompagnement Last social actions: Les dernières actions d'accompagnement Social issue: Problématique sociale Social issues: Problématiques sociales +Pick a social issue: Choisir une problématique sociale Last events on accompanying course: Dernières actions de suivi Edit & activate accompanying course: Modifier et valider See accompanying periods: Voir toutes les périodes d'accompagnement From 9c709d4388a34658fde0d4aedb41644ff8df000a Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 18:17:09 +0200 Subject: [PATCH 02/13] issue641: others filters use these new Select2...Type --- .../AccompanyingCourseFilters/SocialActionFilter.php | 10 ++-------- .../AccompanyingCourseFilters/SocialIssueFilter.php | 9 ++------- 2 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php index c5c37ce21..6c78d703f 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php @@ -13,12 +13,11 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; -use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Export\Declarations; +use Chill\PersonBundle\Form\Type\Select2SocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -71,13 +70,8 @@ class SocialActionFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialactions', EntityType::class, [ - 'class' => SocialAction::class, - 'choice_label' => function (SocialAction $sa) { - return $this->actionRender->renderString($sa, []); - }, + $builder->add('accepted_socialactions', Select2SocialActionType::class, [ 'multiple' => true, - 'expanded' => true, ]); } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index d896d6395..b5f52b1fb 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -15,10 +15,10 @@ use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Export\Declarations; +use Chill\PersonBundle\Form\Type\Select2SocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; -use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Contracts\Translation\TranslatorInterface; use function in_array; @@ -78,13 +78,8 @@ class SocialIssueFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialissues', EntityType::class, [ - 'class' => SocialIssue::class, - 'choice_label' => function ($socialIssue) { - return $this->socialIssueRender->renderString($socialIssue, []); - }, + $builder->add('accepted_socialissues', Select2SocialIssueType::class, [ 'multiple' => true, - 'expanded' => true, ]); } From 5c6068e8a5a08145f0a1c77793e4532e91cbad0e Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 10 Oct 2022 18:52:38 +0200 Subject: [PATCH 03/13] fix cs --- .../Form/Type/Select2LocationTypeType.php | 12 +++- .../Form/Type/Select2UserLocationType.php | 3 +- .../Test/Export/AbstractAggregatorTest.php | 72 +++++++++---------- .../Test/Export/AbstractFilterTest.php | 72 +++++++++---------- .../AdministrativeLocationFilter.php | 2 - .../Form/Type/Select2SocialActionType.php | 12 +++- .../Form/Type/Select2SocialIssueType.php | 12 +++- 7 files changed, 103 insertions(+), 82 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php index e680b06ca..783ec4e5c 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php @@ -1,5 +1,14 @@ setDefault('label', 'Location type') ->setDefault('label_attr', []) ->setDefault('multiple', false) - ->setAllowedTypes('multiple', ['bool']) - ; + ->setAllowedTypes('multiple', ['bool']); } public function getBlockPrefix(): string diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php index de4a8537d..8fb100441 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php @@ -57,8 +57,7 @@ class Select2UserLocationType extends AbstractType ->setDefault('label', 'Current location') ->setDefault('label_attr', []) ->setDefault('multiple', false) - ->setAllowedTypes('multiple', ['bool']) - ; + ->setAllowedTypes('multiple', ['bool']); } public function getBlockPrefix(): string diff --git a/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php b/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php index d3f2849b7..115c364ad 100644 --- a/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php +++ b/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php @@ -29,6 +29,18 @@ use function is_string; */ abstract class AbstractAggregatorTest extends KernelTestCase { + /** + * provide data for `testAliasDidNotDisappears`. + */ + public function dataProviderAliasDidNotDisappears() + { + foreach ($this->getQueryBuilders() as $qb) { + foreach ($this->getFormData() as $data) { + yield [clone $qb, $data]; + } + } + } + /** * provide data for `testAlterQuery`. */ @@ -63,18 +75,6 @@ abstract class AbstractAggregatorTest extends KernelTestCase } } - /** - * provide data for `testAliasDidNotDisappears`. - */ - public function dataProviderAliasDidNotDisappears() - { - foreach ($this->getQueryBuilders() as $qb) { - foreach ($this->getFormData() as $data) { - yield [clone $qb, $data]; - } - } - } - /** * Create an aggregator instance which will be used in tests. * @@ -107,6 +107,30 @@ abstract class AbstractAggregatorTest extends KernelTestCase */ abstract public function getQueryBuilders(); + /** + * Compare aliases array before and after that aggregator alter query + * + * @dataProvider dataProviderAliasDidNotDisappears + * + * @param QueryBuilder $qb + * @param array $data + * @return void + */ + public function testAliasDidNotDisappears(QueryBuilder $qb, array $data) + { + $aliases = $qb->getAllAliases(); + + $this->getAggregator()->alterQuery($qb, $data); + + $alteredQuery = $qb->getAllAliases(); + + $this->assertGreaterThanOrEqual(count($aliases), count($alteredQuery)); + + foreach ($aliases as $alias) { + $this->assertContains($alias, $alteredQuery); + } + } + /** * test the alteration of query by the filter. * @@ -270,28 +294,4 @@ abstract class AbstractAggregatorTest extends KernelTestCase ); } - /** - * Compare aliases array before and after that aggregator alter query - * - * @dataProvider dataProviderAliasDidNotDisappears - * - * @param QueryBuilder $qb - * @param array $data - * @return void - */ - public function testAliasDidNotDisappears(QueryBuilder $qb, array $data) - { - $aliases = $qb->getAllAliases(); - - $this->getAggregator()->alterQuery($qb, $data); - - $alteredQuery = $qb->getAllAliases(); - - $this->assertGreaterThanOrEqual(count($aliases), count($alteredQuery)); - - foreach ($aliases as $alias) { - $this->assertContains($alias, $alteredQuery); - } - } - } diff --git a/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php b/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php index 8e8220b89..1525385aa 100644 --- a/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php +++ b/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php @@ -40,6 +40,18 @@ abstract class AbstractFilterTest extends KernelTestCase $this->prophet = $this->getProphet(); } + /** + * provide data for `testAliasDidNotDisappears`. + */ + public function dataProviderAliasDidNotDisappears() + { + foreach ($this->getQueryBuilders() as $qb) { + foreach ($this->getFormData() as $data) { + yield [clone $qb, $data]; + } + } + } + public function dataProviderAlterQuery() { foreach ($this->getQueryBuilders() as $qb) { @@ -56,18 +68,6 @@ abstract class AbstractFilterTest extends KernelTestCase } } - /** - * provide data for `testAliasDidNotDisappears`. - */ - public function dataProviderAliasDidNotDisappears() - { - foreach ($this->getQueryBuilders() as $qb) { - foreach ($this->getFormData() as $data) { - yield [clone $qb, $data]; - } - } - } - /** * Create a filter which will be used in tests. * @@ -99,6 +99,30 @@ abstract class AbstractFilterTest extends KernelTestCase */ abstract public function getQueryBuilders(); + /** + * Compare aliases array before and after that filter alter query + * + * @dataProvider dataProviderAliasDidNotDisappears + * + * @param QueryBuilder $qb + * @param array $data + * @return void + */ + public function testAliasDidNotDisappears(QueryBuilder $qb, array $data) + { + $aliases = $qb->getAllAliases(); + + $this->getFilter()->alterQuery($qb, $data); + + $alteredQuery = $qb->getAllAliases(); + + $this->assertGreaterThanOrEqual(count($aliases), count($alteredQuery)); + + foreach ($aliases as $alias) { + $this->assertContains($alias, $alteredQuery); + } + } + /** * test the alteration of query by the filter. * @@ -211,28 +235,4 @@ abstract class AbstractFilterTest extends KernelTestCase 'test that the title is not empty' ); } - - /** - * Compare aliases array before and after that filter alter query - * - * @dataProvider dataProviderAliasDidNotDisappears - * - * @param QueryBuilder $qb - * @param array $data - * @return void - */ - public function testAliasDidNotDisappears(QueryBuilder $qb, array $data) - { - $aliases = $qb->getAllAliases(); - - $this->getFilter()->alterQuery($qb, $data); - - $alteredQuery = $qb->getAllAliases(); - - $this->assertGreaterThanOrEqual(count($aliases), count($alteredQuery)); - - foreach ($aliases as $alias) { - $this->assertContains($alias, $alteredQuery); - } - } } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php index 9e54a2272..d289220af 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php @@ -20,7 +20,6 @@ use Symfony\Component\Form\FormBuilderInterface; class AdministrativeLocationFilter implements FilterInterface { - private TranslatableStringHelper $translatableStringHelper; public function __construct( @@ -49,7 +48,6 @@ class AdministrativeLocationFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_locations', Select2UserLocationType::class, [ 'label' => 'Accepted locations', 'label_attr' => [ diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php index f1e8cd913..1365757c7 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php @@ -1,5 +1,14 @@ setDefault('label', 'Social actions') ->setDefault('label_attr', []) ->setDefault('multiple', false) - ->setAllowedTypes('multiple', ['bool']) - ; + ->setAllowedTypes('multiple', ['bool']); } public function getBlockPrefix(): string diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php index 49ee6d574..04b2f723d 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php @@ -1,5 +1,14 @@ setDefault('label', 'Social issues') ->setDefault('label_attr', []) ->setDefault('multiple', false) - ->setAllowedTypes('multiple', ['bool']) - ; + ->setAllowedTypes('multiple', ['bool']); } public function getBlockPrefix(): string From a46c85d66cb5b13ddda1cb6b4698ad7e5f2b507d Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 13:21:03 +0200 Subject: [PATCH 04/13] Fix nested forms problems with select2 FormTypes in issue 641 and 649 Select2UserLocationType.php is called in another context: UserController call UserCurrentLocationType which instanciate new Select2UserLocationType.php FormType --- .../Controller/UserController.php | 4 +-- .../Form/Type/Select2LocationTypeType.php | 35 +++++++------------ .../Form/Type/Select2UserLocationType.php | 26 +++++--------- .../Form/UserCurrentLocationType.php | 26 ++++++++++++++ .../Form/Type/Select2SocialActionType.php | 35 +++++++------------ .../Form/Type/Select2SocialIssueType.php | 35 +++++++------------ 6 files changed, 75 insertions(+), 86 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php diff --git a/src/Bundle/ChillMainBundle/Controller/UserController.php b/src/Bundle/ChillMainBundle/Controller/UserController.php index 03d8d2692..9d3941411 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserController.php @@ -15,7 +15,7 @@ use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Entity\GroupCenter; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Form\Type\ComposedGroupCenterType; -use Chill\MainBundle\Form\Type\Select2UserLocationType; +use Chill\MainBundle\Form\UserCurrentLocationType; use Chill\MainBundle\Form\UserPasswordType; use Chill\MainBundle\Form\UserType; use Chill\MainBundle\Pagination\PaginatorInterface; @@ -234,7 +234,7 @@ class UserController extends CRUDController public function editCurrentLocationAction(Request $request) { $user = $this->getUser(); - $form = $this->createForm(Select2UserLocationType::class, $user) + $form = $this->createForm(UserCurrentLocationType::class, $user) ->add('submit', SubmitType::class, ['label' => 'Save']) ->handleRequest($request); diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php index 783ec4e5c..33121f8bb 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php @@ -15,7 +15,6 @@ use Chill\MainBundle\Entity\LocationType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2LocationTypeType extends AbstractType @@ -27,33 +26,25 @@ class Select2LocationTypeType extends AbstractType $this->translatableStringHelper = $translatableStringHelper; } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder->add('locationtype', EntityType::class, [ - 'class' => LocationType::class, - 'choice_label' => function (LocationType $type) { - return $this->translatableStringHelper->localize($type->getTitle()); - }, - 'placeholder' => 'Pick a location type', - 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], - 'attr' => ['class' => 'select2'], - ]); - } - public function configureOptions(OptionsResolver $resolver) { $resolver - ->setDefault('label', 'Location type') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + ->setDefaults([ + 'class' => LocationType::class, + 'choice_label' => function (LocationType $type) { + return $this->translatableStringHelper->localize($type->getTitle()); + }, + 'placeholder' => 'Pick a location type', + 'required' => false, + 'attr' => ['class' => 'select2'], + 'label' => 'Location type', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_location_type_type'; + return EntityType::class; } } diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php index 8fb100441..3c1e51e68 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php @@ -16,7 +16,6 @@ use Chill\MainBundle\Repository\LocationRepository; use Chill\MainBundle\Templating\TranslatableStringHelper; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2UserLocationType extends AbstractType @@ -31,10 +30,10 @@ class Select2UserLocationType extends AbstractType $this->locationRepository = $locationRepository; } - public function buildForm(FormBuilderInterface $builder, array $options) + public function configureOptions(OptionsResolver $resolver) { - $builder - ->add('currentLocation', EntityType::class, [ + $resolver + ->setDefaults([ 'class' => Location::class, 'choices' => $this->locationRepository->findByPublicLocations(), 'choice_label' => function (Location $entity) { @@ -44,24 +43,15 @@ class Select2UserLocationType extends AbstractType }, 'placeholder' => 'Pick a location', 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], 'attr' => ['class' => 'select2'], - ]); - } - - public function configureOptions(OptionsResolver $resolver) - { - $resolver - ->setDefault('label', 'Current location') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + 'label' => 'Current location', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_user_location_type'; + return EntityType::class; } } diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php new file mode 100644 index 000000000..f1ef83a9f --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php @@ -0,0 +1,26 @@ +add('currentLocation', Select2UserLocationType::class); + } +} + diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php index 1365757c7..b53960cad 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php @@ -15,7 +15,6 @@ use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2SocialActionType extends AbstractType @@ -27,33 +26,25 @@ class Select2SocialActionType extends AbstractType $this->actionRender = $actionRender; } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder->add('social_actions', EntityType::class, [ - 'class' => SocialAction::class, - 'choice_label' => function (SocialAction $sa) { - return $this->actionRender->renderString($sa, []); - }, - 'placeholder' => 'Pick a social action', - 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], - 'attr' => ['class' => 'select2'], - ]); - } - public function configureOptions(OptionsResolver $resolver) { $resolver - ->setDefault('label', 'Social actions') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + ->setDefaults([ + 'class' => SocialAction::class, + 'choice_label' => function (SocialAction $sa) { + return $this->actionRender->renderString($sa, []); + }, + 'placeholder' => 'Pick a social action', + 'required' => false, + 'attr' => ['class' => 'select2'], + 'label' => 'Social actions', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_social_action_type'; + return EntityType::class; } } diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php index 04b2f723d..aeaeafe34 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php @@ -15,7 +15,6 @@ use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class Select2SocialIssueType extends AbstractType @@ -27,33 +26,25 @@ class Select2SocialIssueType extends AbstractType $this->issueRender = $issueRender; } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder->add('social_issues', EntityType::class, [ - 'class' => SocialIssue::class, - 'choice_label' => function (SocialIssue $si) { - return $this->issueRender->renderString($si, []); - }, - 'placeholder' => 'Pick a social issue', - 'required' => false, - 'label' => $options['label'], - 'label_attr' => $options['label_attr'], - 'multiple' => $options['multiple'], - 'attr' => ['class' => 'select2'], - ]); - } - public function configureOptions(OptionsResolver $resolver) { $resolver - ->setDefault('label', 'Social issues') - ->setDefault('label_attr', []) - ->setDefault('multiple', false) + ->setDefaults([ + 'class' => SocialIssue::class, + 'choice_label' => function (SocialIssue $si) { + return $this->issueRender->renderString($si, []); + }, + 'placeholder' => 'Pick a social issue', + 'required' => false, + 'attr' => ['class' => 'select2'], + 'label' => 'Social issues', + 'multiple' => false, + ]) ->setAllowedTypes('multiple', ['bool']); } - public function getBlockPrefix(): string + public function getParent(): string { - return 'select2_social_issue_type'; + return EntityType::class; } } From 6d40ef279f2425c21510964107bb8af4b0c374af Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 13:53:05 +0200 Subject: [PATCH 05/13] issue641: simplify alterQuery where clause in concerned filters --- .../Filter/ACPFilters/BySocialActionFilter.php | 13 ++----------- .../Filter/ACPFilters/BySocialIssueFilter.php | 13 ++----------- .../SocialActionFilter.php | 12 ++---------- .../SocialIssueFilter.php | 15 ++------------- 4 files changed, 8 insertions(+), 45 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php index 4502450cb..f15d88bb0 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php @@ -15,7 +15,6 @@ use Chill\ActivityBundle\Export\Declarations; use Chill\MainBundle\Export\FilterInterface; use Chill\PersonBundle\Form\Type\Select2SocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; -use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -36,22 +35,14 @@ class BySocialActionFilter implements FilterInterface public function alterQuery(QueryBuilder $qb, $data) { - $where = $qb->getDQLPart('where'); - if (!in_array('actsocialaction', $qb->getAllAliases(), true)) { $qb->join('activity.socialActions', 'actsocialaction'); } $clause = $qb->expr()->in('actsocialaction.id', ':socialactions'); - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter('socialactions', $data['accepted_socialactions']); + $qb ->andWhere($clause) + ->setParameter('socialactions', $data['accepted_socialactions']); } public function applyOn(): string diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php index 59d79d21d..b9d1856c8 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php @@ -15,7 +15,6 @@ use Chill\ActivityBundle\Export\Declarations; use Chill\MainBundle\Export\FilterInterface; use Chill\PersonBundle\Form\Type\Select2SocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; -use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -36,22 +35,14 @@ class BySocialIssueFilter implements FilterInterface public function alterQuery(QueryBuilder $qb, $data) { - $where = $qb->getDQLPart('where'); - if (!in_array('actsocialissue', $qb->getAllAliases(), true)) { $qb->join('activity.socialIssues', 'actsocialissue'); } $clause = $qb->expr()->in('actsocialissue.id', ':socialissues'); - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter('socialissues', $data['accepted_socialissues']); + $qb ->andWhere($clause) + ->setParameter('socialissues', $data['accepted_socialissues']); } public function applyOn(): string diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php index 6c78d703f..7fb8b3b59 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php @@ -16,7 +16,6 @@ use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Form\Type\Select2SocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; -use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; use function in_array; @@ -50,17 +49,10 @@ class SocialActionFilter implements FilterInterface $qb->join('acpw.socialAction', 'acpwsocialaction'); } - $where = $qb->getDQLPart('where'); $clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions'); - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter('socialactions', $data['accepted_socialactions']); + $qb ->andWhere($clause) + ->setParameter('socialactions', $data['accepted_socialactions']); } public function applyOn(): string diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index b5f52b1fb..06215de55 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -17,7 +17,6 @@ use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Form\Type\Select2SocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; -use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -55,20 +54,10 @@ class SocialIssueFilter implements FilterInterface $qb->join('acp.socialIssues', 'acpsocialissue'); } - $where = $qb->getDQLPart('where'); $clause = $qb->expr()->in('acpsocialissue.id', ':socialissues'); - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter( - 'socialissues', - $this->addParentIssues($data['accepted_socialissues']) - ); + $qb ->andWhere($clause) + ->setParameter('socialissues', $this->addParentIssues($data['accepted_socialissues'])); } public function applyOn() From 8d7b6fea71dffcab2ff214ee9bd00e618df3ac9f Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 15:34:11 +0200 Subject: [PATCH 06/13] issue641: remove method to add parents socialissues (bad understanding) --- .../SocialIssueFilter.php | 41 +------------------ 1 file changed, 2 insertions(+), 39 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index 06215de55..a64dda2b8 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -13,7 +13,6 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; -use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Form\Type\Select2SocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; @@ -57,7 +56,7 @@ class SocialIssueFilter implements FilterInterface $clause = $qb->expr()->in('acpsocialissue.id', ':socialissues'); $qb ->andWhere($clause) - ->setParameter('socialissues', $this->addParentIssues($data['accepted_socialissues'])); + ->setParameter('socialissues', $data['accepted_socialissues']); } public function applyOn() @@ -76,7 +75,7 @@ class SocialIssueFilter implements FilterInterface { $issues = []; - $socialissues = $this->addParentIssues($data['accepted_socialissues']); + $socialissues = $data['accepted_socialissues']; foreach ($socialissues as $i) { if ('null' === $i) { @@ -96,40 +95,4 @@ class SocialIssueFilter implements FilterInterface { return 'Filter by social issue'; } - - /** - * "Le filtre retiendra les parcours qui comportent cette problématique, - * ou une problématique parente à celles choisies.". - * - * Add parent of each socialissue selected, and remove duplicates - * - * @param $accepted_issues - */ - private function addParentIssues($accepted_issues): array - { - $array = []; - - foreach ($accepted_issues as $i) { - /** @var SocialIssue $i */ - if ($i->hasParent()) { - $array[] = $i->getParent(); - } - $array[] = $i; - } - - return $this->removeDuplicate($array); - } - - private function removeDuplicate(array $array): array - { - $ids = array_map(static function ($item) { - return $item->getId(); - }, $array); - - $unique_ids = array_unique($ids); - - return array_values( - array_intersect_key($array, $unique_ids) - ); - } } From de7ba29853544aa15c5d30827564457349397ef2 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 18:38:00 +0200 Subject: [PATCH 07/13] issue641: form select2 list activated socialissues and sort by ordering --- .../Form/Type/Select2SocialIssueType.php | 11 +++++++++-- .../SocialWork/SocialIssueRepository.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php index aeaeafe34..615c2830d 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Form\Type; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; +use Chill\PersonBundle\Repository\SocialWork\SocialIssueRepository; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -21,9 +22,14 @@ class Select2SocialIssueType extends AbstractType { private SocialIssueRender $issueRender; - public function __construct(SocialIssueRender $issueRender) - { + private SocialIssueRepository $issueRepository; + + public function __construct( + SocialIssueRender $issueRender, + SocialIssueRepository $issueRepository + ) { $this->issueRender = $issueRender; + $this->issueRepository = $issueRepository; } public function configureOptions(OptionsResolver $resolver) @@ -31,6 +37,7 @@ class Select2SocialIssueType extends AbstractType $resolver ->setDefaults([ 'class' => SocialIssue::class, + 'choices' => $this->issueRepository->findIssuesNotDesactivated(), 'choice_label' => function (SocialIssue $si) { return $this->issueRender->renderString($si, []); }, diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php index 27b4ed4e9..0c18cd970 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php @@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Repository\SocialWork; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ObjectRepository; final class SocialIssueRepository implements ObjectRepository @@ -61,4 +62,21 @@ final class SocialIssueRepository implements ObjectRepository { return SocialIssue::class; } + + public function findIssuesNotDesactivated(): array + { + return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); + } + + private function buildQueryWithDesactivatedDateCriteria(): QueryBuilder + { + $qb = $this->repository->createQueryBuilder('si'); + + $qb ->where('si.desactivationDate is null') + ->orWhere('si.desactivationDate > :now') + ->orderBy('si.ordering', 'ASC') + ->setParameter('now', new \DateTime('now')); + + return $qb; + } } From ccea6dd95cceef084b4c215024042ac02397f41b Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 19:22:19 +0200 Subject: [PATCH 08/13] issue641: add descendants to selected in alterQuery --- .../SocialIssueFilter.php | 44 ++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index a64dda2b8..aa689165f 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Form\Type\Select2SocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; @@ -56,7 +57,9 @@ class SocialIssueFilter implements FilterInterface $clause = $qb->expr()->in('acpsocialissue.id', ':socialissues'); $qb ->andWhere($clause) - ->setParameter('socialissues', $data['accepted_socialissues']); + ->setParameter('socialissues', + $this->addDescendantsIssues($data['accepted_socialissues']) + ); } public function applyOn() @@ -71,17 +74,18 @@ class SocialIssueFilter implements FilterInterface ]); } - public function describeAction($data, $format = 'string') + public function describeAction($data, $format = 'string'): array { $issues = []; $socialissues = $data['accepted_socialissues']; - foreach ($socialissues as $i) { - if ('null' === $i) { + foreach ($socialissues as $si) { + /** @var SocialIssue $si */ + if (null === $si) { $issues[] = $this->translator->trans('Not given'); } else { - $issues[] = $this->socialIssueRender->renderString($i, []); + $issues[] = $this->socialIssueRender->renderString($si, []); } } @@ -91,8 +95,36 @@ class SocialIssueFilter implements FilterInterface ], ]; } - public function getTitle() + public function getTitle(): string { return 'Filter by social issue'; } + + private function addDescendantsIssues($accepted_socialissues): array + { + $array = []; + + foreach ($accepted_socialissues as $si) { + /** @var SocialIssue $si */ + $array[] = $si; + if (!$si->hasParent()) { + $array = array_merge($array, $si->getDescendants()->toArray()); + } + } + + return $this->removeDuplicate($array); + } + + private function removeDuplicate(array $array): array + { + $ids = array_map(static function ($item) { + return $item->getId(); + }, $array); + + $unique_ids = array_unique($ids); + + return array_values( + array_intersect_key($array, $unique_ids) + ); + } } From 6b1155b9d8a6bb263737456768314ef0e0b038f2 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 19:35:18 +0200 Subject: [PATCH 09/13] issue641: don't list all children if parent is chosen add mechanism to differenciate string in export --- .../SocialIssueFilter.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index aa689165f..c72c78aa5 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -85,7 +85,8 @@ class SocialIssueFilter implements FilterInterface if (null === $si) { $issues[] = $this->translator->trans('Not given'); } else { - $issues[] = $this->socialIssueRender->renderString($si, []); + $issues[] = $this->renderIssue($si); + } } @@ -127,4 +128,17 @@ class SocialIssueFilter implements FilterInterface array_intersect_key($array, $unique_ids) ); } + + private function renderIssue(SocialIssue $si): string + { + $render_str = $this->socialIssueRender->renderString($si, []); + + if (!$si->hasParent()) { + if (count($si->getDescendants()) > 0) { + return $render_str . " (et dérivés)"; + } + } + + return $render_str; + } } From 71f989f00e05694173b08ae13c734276836bfaaa Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Sun, 16 Oct 2022 20:06:18 +0200 Subject: [PATCH 10/13] issue641: same for actions: take children, propose only associated and sort them by ordering --- .../SocialActionFilter.php | 56 +++++++++++++++++-- .../Form/Type/Select2SocialActionType.php | 11 +++- .../SocialWork/SocialActionRepository.php | 17 ++++++ 3 files changed, 77 insertions(+), 7 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php index 7fb8b3b59..138583ef2 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php @@ -13,6 +13,7 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; +use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Form\Type\Select2SocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; @@ -52,7 +53,9 @@ class SocialActionFilter implements FilterInterface $clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions'); $qb ->andWhere($clause) - ->setParameter('socialactions', $data['accepted_socialactions']); + ->setParameter('socialactions', + $this->addDescendantsActions($data['accepted_socialactions']) + ); } public function applyOn(): string @@ -69,14 +72,16 @@ class SocialActionFilter implements FilterInterface public function describeAction($data, $format = 'string'): array { - $socialactions = []; + $actions = []; - foreach ($data['accepted_socialactions'] as $sa) { - $socialactions[] = $this->actionRender->renderString($sa, []); + $socialactions = $data['accepted_socialactions']; + + foreach ($socialactions as $action) { + $actions[] = $this->renderAction($action); } return ['Filtered by socialactions: only %socialactions%', [ - '%socialactions%' => implode(', ou ', $socialactions), + '%socialactions%' => implode(', ou ', $actions), ]]; } @@ -84,4 +89,45 @@ class SocialActionFilter implements FilterInterface { return 'Filter by socialaction'; } + + private function addDescendantsActions($accepted_socialactions): array + { + $array = []; + + foreach ($accepted_socialactions as $action) { + /** @var SocialAction $action */ + $array[] = $action; + if (!$action->hasParent()) { + $array = array_merge($array, $action->getDescendants()->toArray()); + } + } + + return $this->removeDuplicate($array); + } + + private function removeDuplicate(array $array): array + { + $ids = array_map(static function ($item) { + return $item->getId(); + }, $array); + + $unique_ids = array_unique($ids); + + return array_values( + array_intersect_key($array, $unique_ids) + ); + } + + private function renderAction(SocialAction $action): string + { + $render_str = $this->actionRender->renderString($action, []); + + if (!$action->hasParent()) { + if (count($action->getDescendants()) > 0) { + return $render_str . " (et dérivés)"; + } + } + + return $render_str; + } } diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php index b53960cad..ab22f2972 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Form\Type; use Chill\PersonBundle\Entity\SocialWork\SocialAction; +use Chill\PersonBundle\Repository\SocialWork\SocialActionRepository; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; @@ -21,9 +22,14 @@ class Select2SocialActionType extends AbstractType { private SocialActionRender $actionRender; - public function __construct(SocialActionRender $actionRender) - { + private SocialActionRepository $actionRepository; + + public function __construct( + SocialActionRender $actionRender, + SocialActionRepository $actionRepository + ) { $this->actionRender = $actionRender; + $this->actionRepository = $actionRepository; } public function configureOptions(OptionsResolver $resolver) @@ -31,6 +37,7 @@ class Select2SocialActionType extends AbstractType $resolver ->setDefaults([ 'class' => SocialAction::class, + 'choices' => $this->actionRepository->findActionsNotDesactivated(), 'choice_label' => function (SocialAction $sa) { return $this->actionRender->renderString($sa, []); }, diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php index 052432741..3b8715d7e 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php @@ -67,4 +67,21 @@ final class SocialActionRepository implements ObjectRepository { return SocialAction::class; } + + public function findActionsNotDesactivated(): array + { + return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); + } + + private function buildQueryWithDesactivatedDateCriteria(): QueryBuilder + { + $qb = $this->repository->createQueryBuilder('sa'); + + $qb ->where('sa.desactivationDate is null') + ->orWhere('sa.desactivationDate > :now') + ->orderBy('sa.ordering', 'ASC') + ->setParameter('now', new \DateTime('now')); + + return $qb; + } } From 32ddc5465c02e97ffe078765a7d9c04e053aad03 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 17 Oct 2022 09:52:29 +0200 Subject: [PATCH 11/13] review: fix stuffs on socialAction and socialIssue filters: * filters call static entity method * add renderString option to manage add_children * add tests on new entity method getDescendantsWithThisFor..() * rename function in repository * rename 'Select2..' classes by 'Pick..' and replace all occurences --- .../ACPFilters/BySocialActionFilter.php | 17 ++++-- .../Filter/ACPFilters/BySocialIssueFilter.php | 17 ++++-- .../Filter/ACPFilters/LocationTypeFilter.php | 4 +- ...nTypeType.php => PickLocationTypeType.php} | 2 +- ...ationType.php => PickUserLocationType.php} | 2 +- .../Form/UserCurrentLocationType.php | 4 +- .../ChillMainBundle/config/services/form.yaml | 8 --- .../Entity/SocialWork/SocialAction.php | 22 +++++++ .../Entity/SocialWork/SocialIssue.php | 22 +++++++ .../AdministrativeLocationFilter.php | 4 +- .../SocialActionFilter.php | 57 +++-------------- .../SocialIssueFilter.php | 61 +++---------------- ...ctionType.php => PickSocialActionType.php} | 4 +- ...lIssueType.php => PickSocialIssueType.php} | 4 +- .../SocialWork/SocialActionRepository.php | 5 +- .../SocialWork/SocialIssueRepository.php | 5 +- .../Templating/Entity/SocialActionRender.php | 30 +++++++-- .../Templating/Entity/SocialIssueRender.php | 30 +++++++-- .../Entity/SocialWork/SocialActionTest.php | 53 ++++++++++++++++ .../Entity/SocialWork/SocialIssueTest.php | 29 +++++++++ .../config/services/form.yaml | 8 --- .../translations/messages.fr.yml | 7 +++ 22 files changed, 244 insertions(+), 151 deletions(-) rename src/Bundle/ChillMainBundle/Form/Type/{Select2LocationTypeType.php => PickLocationTypeType.php} (96%) rename src/Bundle/ChillMainBundle/Form/Type/{Select2UserLocationType.php => PickUserLocationType.php} (97%) rename src/Bundle/ChillPersonBundle/Form/Type/{Select2SocialActionType.php => PickSocialActionType.php} (92%) rename src/Bundle/ChillPersonBundle/Form/Type/{Select2SocialIssueType.php => PickSocialIssueType.php} (92%) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php index f15d88bb0..a7d6e5e3a 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php @@ -13,7 +13,8 @@ namespace Chill\ActivityBundle\Export\Filter\ACPFilters; use Chill\ActivityBundle\Export\Declarations; use Chill\MainBundle\Export\FilterInterface; -use Chill\PersonBundle\Form\Type\Select2SocialActionType; +use Chill\PersonBundle\Entity\SocialWork\SocialAction; +use Chill\PersonBundle\Form\Type\PickSocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; @@ -41,8 +42,10 @@ class BySocialActionFilter implements FilterInterface $clause = $qb->expr()->in('actsocialaction.id', ':socialactions'); - $qb ->andWhere($clause) - ->setParameter('socialactions', $data['accepted_socialactions']); + $qb->andWhere($clause) + ->setParameter('socialactions', + SocialAction::getDescendantsWithThisForActions($data['accepted_socialactions']) + ); } public function applyOn(): string @@ -52,7 +55,7 @@ class BySocialActionFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialactions', Select2SocialActionType::class, [ + $builder->add('accepted_socialactions', PickSocialActionType::class, [ 'multiple' => true ]); } @@ -61,8 +64,10 @@ class BySocialActionFilter implements FilterInterface { $actions = []; - foreach ($data['accepted_socialactions'] as $sa) { - $actions[] = $this->actionRender->renderString($sa, []); + foreach ($data['accepted_socialactions'] as $action) { + $actions[] = $this->actionRender->renderString($action, [ + 'show_and_children' => true, + ]); } return ['Filtered activity by linked socialaction: only %actions%', [ diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php index b9d1856c8..94189169d 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php @@ -13,7 +13,8 @@ namespace Chill\ActivityBundle\Export\Filter\ACPFilters; use Chill\ActivityBundle\Export\Declarations; use Chill\MainBundle\Export\FilterInterface; -use Chill\PersonBundle\Form\Type\Select2SocialIssueType; +use Chill\PersonBundle\Entity\SocialWork\SocialIssue; +use Chill\PersonBundle\Form\Type\PickSocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; @@ -41,8 +42,10 @@ class BySocialIssueFilter implements FilterInterface $clause = $qb->expr()->in('actsocialissue.id', ':socialissues'); - $qb ->andWhere($clause) - ->setParameter('socialissues', $data['accepted_socialissues']); + $qb->andWhere($clause) + ->setParameter('socialissues', + SocialIssue::getDescendantsWithThisForIssues($data['accepted_socialissues']) + ); } public function applyOn(): string @@ -52,7 +55,7 @@ class BySocialIssueFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialissues', Select2SocialIssueType::class, [ + $builder->add('accepted_socialissues', PickSocialIssueType::class, [ 'multiple' => true ]); } @@ -61,8 +64,10 @@ class BySocialIssueFilter implements FilterInterface { $issues = []; - foreach ($data['accepted_socialissues'] as $si) { - $issues[] = $this->issueRender->renderString($si, []); + foreach ($data['accepted_socialissues'] as $issue) { + $issues[] = $this->issueRender->renderString($issue, [ + 'show_and_children' => true, + ]); } return ['Filtered activity by linked socialissue: only %issues%', [ diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php index 023882cf9..2c9244022 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/LocationTypeFilter.php @@ -13,7 +13,7 @@ namespace Chill\ActivityBundle\Export\Filter\ACPFilters; use Chill\ActivityBundle\Export\Declarations; use Chill\MainBundle\Export\FilterInterface; -use Chill\MainBundle\Form\Type\Select2LocationTypeType; +use Chill\MainBundle\Form\Type\PickLocationTypeType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; @@ -60,7 +60,7 @@ class LocationTypeFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_locationtype', Select2LocationTypeType::class, [ + $builder->add('accepted_locationtype', PickLocationTypeType::class, [ 'multiple' => true, //'label' => false, ]); diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php b/src/Bundle/ChillMainBundle/Form/Type/PickLocationTypeType.php similarity index 96% rename from src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php rename to src/Bundle/ChillMainBundle/Form/Type/PickLocationTypeType.php index 33121f8bb..6774e0941 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2LocationTypeType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickLocationTypeType.php @@ -17,7 +17,7 @@ use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; -class Select2LocationTypeType extends AbstractType +class PickLocationTypeType extends AbstractType { private TranslatableStringHelper $translatableStringHelper; diff --git a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php b/src/Bundle/ChillMainBundle/Form/Type/PickUserLocationType.php similarity index 97% rename from src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php rename to src/Bundle/ChillMainBundle/Form/Type/PickUserLocationType.php index 3c1e51e68..792daa39e 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/Select2UserLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PickUserLocationType.php @@ -18,7 +18,7 @@ use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; -class Select2UserLocationType extends AbstractType +class PickUserLocationType extends AbstractType { private LocationRepository $locationRepository; diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php index f1ef83a9f..657ba8942 100644 --- a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php @@ -11,7 +11,7 @@ declare(strict_types=1); namespace Chill\MainBundle\Form; -use Chill\MainBundle\Form\Type\Select2UserLocationType; +use Chill\MainBundle\Form\Type\PickUserLocationType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; @@ -20,7 +20,7 @@ class UserCurrentLocationType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { - $builder->add('currentLocation', Select2UserLocationType::class); + $builder->add('currentLocation', PickUserLocationType::class); } } diff --git a/src/Bundle/ChillMainBundle/config/services/form.yaml b/src/Bundle/ChillMainBundle/config/services/form.yaml index 34f5d1802..4408a7223 100644 --- a/src/Bundle/ChillMainBundle/config/services/form.yaml +++ b/src/Bundle/ChillMainBundle/config/services/form.yaml @@ -130,14 +130,6 @@ services: autowire: true autoconfigure: true - Chill\MainBundle\Form\Type\Select2UserLocationType: - autowire: true - autoconfigure: true - - Chill\MainBundle\Form\Type\Select2LocationTypeType: - autowire: true - autoconfigure: true - Chill\MainBundle\Form\Type\LocationFormType: ~ Chill\MainBundle\Form\WorkflowStepType: ~ diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php index bc81bc180..ea5df8fe0 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php @@ -227,6 +227,23 @@ class SocialAction return $descendants; } + /** + * @param Collection|SocialAction[] $socialActions + * @return Collection + */ + public static function getDescendantsWithThisForActions($socialActions): Collection + { + $unique = []; + + foreach ($socialActions as $action) { + foreach ($action->getDescendantsWithThis() as $child) { + $unique[spl_object_hash($child)] = $child; + } + } + + return new ArrayCollection(array_values($unique)); + } + public function getEvaluations(): Collection { return $this->evaluations; @@ -278,6 +295,11 @@ class SocialAction return $this->getParent() instanceof self; } + public function hasChildren(): bool + { + return 0 < $this->getChildren()->count(); + } + /** * Recursive method which return true if the current $action * is a descendant of the $action given in parameter. diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php index 06f05c91f..d36642977 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php @@ -215,6 +215,23 @@ class SocialIssue return $descendants; } + /** + * @param Collection|SocialIssue[] $socialIssues + * @return Collection + */ + public static function getDescendantsWithThisForIssues(Collection $socialIssues): Collection + { + $unique = []; + + foreach ($socialIssues as $issue) { + foreach ($issue->getDescendantsWithThis() as $child) { + $unique[spl_object_hash($child)] = $child; + } + } + + return new ArrayCollection(array_values($unique)); + } + public function getId(): ?int { return $this->id; @@ -267,6 +284,11 @@ class SocialIssue return null !== $this->parent; } + public function hasChildren(): bool + { + return 0 < $this->getChildren()->count(); + } + /** * Recursive method which return true if the current $issue is a descendant * of the $issue given in parameter. diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php index d289220af..19f1a6090 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/AdministrativeLocationFilter.php @@ -12,7 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Export\FilterInterface; -use Chill\MainBundle\Form\Type\Select2UserLocationType; +use Chill\MainBundle\Form\Type\PickUserLocationType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; use Doctrine\ORM\QueryBuilder; @@ -48,7 +48,7 @@ class AdministrativeLocationFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_locations', Select2UserLocationType::class, [ + $builder->add('accepted_locations', PickUserLocationType::class, [ 'label' => 'Accepted locations', 'label_attr' => [ //'class' => 'd-none' diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php index 138583ef2..a1834aa0e 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php @@ -15,7 +15,7 @@ use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Chill\PersonBundle\Export\Declarations; -use Chill\PersonBundle\Form\Type\Select2SocialActionType; +use Chill\PersonBundle\Form\Type\PickSocialActionType; use Chill\PersonBundle\Templating\Entity\SocialActionRender; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; @@ -23,10 +23,10 @@ use function in_array; class SocialActionFilter implements FilterInterface { - private SocialActionRender $actionRender; - private TranslatableStringHelper $translatableStringHelper; + private SocialActionRender $actionRender; + public function __construct( TranslatableStringHelper $translatableStringHelper, SocialActionRender $actionRender @@ -52,9 +52,9 @@ class SocialActionFilter implements FilterInterface $clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions'); - $qb ->andWhere($clause) + $qb->andWhere($clause) ->setParameter('socialactions', - $this->addDescendantsActions($data['accepted_socialactions']) + SocialAction::getDescendantsWithThisForActions($data['accepted_socialactions'])->toArray() ); } @@ -65,7 +65,7 @@ class SocialActionFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialactions', Select2SocialActionType::class, [ + $builder->add('accepted_socialactions', PickSocialActionType::class, [ 'multiple' => true, ]); } @@ -77,7 +77,9 @@ class SocialActionFilter implements FilterInterface $socialactions = $data['accepted_socialactions']; foreach ($socialactions as $action) { - $actions[] = $this->renderAction($action); + $actions[] = $this->actionRender->renderString($action, [ + 'show_and_children' => true, + ]); } return ['Filtered by socialactions: only %socialactions%', [ @@ -89,45 +91,4 @@ class SocialActionFilter implements FilterInterface { return 'Filter by socialaction'; } - - private function addDescendantsActions($accepted_socialactions): array - { - $array = []; - - foreach ($accepted_socialactions as $action) { - /** @var SocialAction $action */ - $array[] = $action; - if (!$action->hasParent()) { - $array = array_merge($array, $action->getDescendants()->toArray()); - } - } - - return $this->removeDuplicate($array); - } - - private function removeDuplicate(array $array): array - { - $ids = array_map(static function ($item) { - return $item->getId(); - }, $array); - - $unique_ids = array_unique($ids); - - return array_values( - array_intersect_key($array, $unique_ids) - ); - } - - private function renderAction(SocialAction $action): string - { - $render_str = $this->actionRender->renderString($action, []); - - if (!$action->hasParent()) { - if (count($action->getDescendants()) > 0) { - return $render_str . " (et dérivés)"; - } - } - - return $render_str; - } } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index c72c78aa5..08ab7fca2 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -15,7 +15,7 @@ use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Chill\PersonBundle\Export\Declarations; -use Chill\PersonBundle\Form\Type\Select2SocialIssueType; +use Chill\PersonBundle\Form\Type\PickSocialIssueType; use Chill\PersonBundle\Templating\Entity\SocialIssueRender; use Doctrine\ORM\QueryBuilder; use Symfony\Component\Form\FormBuilderInterface; @@ -56,9 +56,9 @@ class SocialIssueFilter implements FilterInterface $clause = $qb->expr()->in('acpsocialissue.id', ':socialissues'); - $qb ->andWhere($clause) + $qb->andWhere($clause) ->setParameter('socialissues', - $this->addDescendantsIssues($data['accepted_socialissues']) + SocialIssue::getDescendantsWithThisForIssues($data['accepted_socialissues']) ); } @@ -69,7 +69,7 @@ class SocialIssueFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add('accepted_socialissues', Select2SocialIssueType::class, [ + $builder->add('accepted_socialissues', PickSocialIssueType::class, [ 'multiple' => true, ]); } @@ -80,14 +80,10 @@ class SocialIssueFilter implements FilterInterface $socialissues = $data['accepted_socialissues']; - foreach ($socialissues as $si) { - /** @var SocialIssue $si */ - if (null === $si) { - $issues[] = $this->translator->trans('Not given'); - } else { - $issues[] = $this->renderIssue($si); - - } + foreach ($socialissues as $issue) { + $issues[] = $this->socialIssueRender->renderString($issue, [ + 'show_and_children' => true, + ]); } return [ @@ -100,45 +96,4 @@ class SocialIssueFilter implements FilterInterface { return 'Filter by social issue'; } - - private function addDescendantsIssues($accepted_socialissues): array - { - $array = []; - - foreach ($accepted_socialissues as $si) { - /** @var SocialIssue $si */ - $array[] = $si; - if (!$si->hasParent()) { - $array = array_merge($array, $si->getDescendants()->toArray()); - } - } - - return $this->removeDuplicate($array); - } - - private function removeDuplicate(array $array): array - { - $ids = array_map(static function ($item) { - return $item->getId(); - }, $array); - - $unique_ids = array_unique($ids); - - return array_values( - array_intersect_key($array, $unique_ids) - ); - } - - private function renderIssue(SocialIssue $si): string - { - $render_str = $this->socialIssueRender->renderString($si, []); - - if (!$si->hasParent()) { - if (count($si->getDescendants()) > 0) { - return $render_str . " (et dérivés)"; - } - } - - return $render_str; - } } diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php b/src/Bundle/ChillPersonBundle/Form/Type/PickSocialActionType.php similarity index 92% rename from src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php rename to src/Bundle/ChillPersonBundle/Form/Type/PickSocialActionType.php index ab22f2972..7c0da43b1 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialActionType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/PickSocialActionType.php @@ -18,7 +18,7 @@ use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; -class Select2SocialActionType extends AbstractType +class PickSocialActionType extends AbstractType { private SocialActionRender $actionRender; @@ -37,7 +37,7 @@ class Select2SocialActionType extends AbstractType $resolver ->setDefaults([ 'class' => SocialAction::class, - 'choices' => $this->actionRepository->findActionsNotDesactivated(), + 'choices' => $this->actionRepository->findAllActive(), 'choice_label' => function (SocialAction $sa) { return $this->actionRender->renderString($sa, []); }, diff --git a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php b/src/Bundle/ChillPersonBundle/Form/Type/PickSocialIssueType.php similarity index 92% rename from src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php rename to src/Bundle/ChillPersonBundle/Form/Type/PickSocialIssueType.php index 615c2830d..e79c17191 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/Select2SocialIssueType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/PickSocialIssueType.php @@ -18,7 +18,7 @@ use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; use Symfony\Component\OptionsResolver\OptionsResolver; -class Select2SocialIssueType extends AbstractType +class PickSocialIssueType extends AbstractType { private SocialIssueRender $issueRender; @@ -37,7 +37,7 @@ class Select2SocialIssueType extends AbstractType $resolver ->setDefaults([ 'class' => SocialIssue::class, - 'choices' => $this->issueRepository->findIssuesNotDesactivated(), + 'choices' => $this->issueRepository->findAllActive(), 'choice_label' => function (SocialIssue $si) { return $this->issueRender->renderString($si, []); }, diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php index 3b8715d7e..86e8e41a8 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php @@ -68,7 +68,10 @@ final class SocialActionRepository implements ObjectRepository return SocialAction::class; } - public function findActionsNotDesactivated(): array + /** + * @return array|SocialAction[] + */ + public function findAllActive(): array { return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); } diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php index 0c18cd970..21831b432 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php @@ -63,7 +63,10 @@ final class SocialIssueRepository implements ObjectRepository return SocialIssue::class; } - public function findIssuesNotDesactivated(): array + /** + * @return array|SocialIssue[] + */ + public function findAllActive(): array { return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); } diff --git a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php index f2ee13fc1..8f1bfaccb 100644 --- a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php +++ b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php @@ -15,7 +15,7 @@ use Chill\MainBundle\Templating\Entity\ChillEntityRenderInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\SocialAction; use Symfony\Component\Templating\EngineInterface; - +use Symfony\Contracts\Translation\TranslatorInterface; use function array_merge; use function array_reverse; use function implode; @@ -25,6 +25,8 @@ class SocialActionRender implements ChillEntityRenderInterface public const DEFAULT_ARGS = [ self::SEPARATOR_KEY => ' > ', self::NO_BADGE => false, + self::SHOW_AND_CHILDREN => false, + self::AND_CHILDREN_MENTION => 'social_action.and children', ]; /** @@ -34,14 +36,28 @@ class SocialActionRender implements ChillEntityRenderInterface public const SEPARATOR_KEY = 'default.separator'; + /** + * Show a mention "and children" on each SocialAction, if the social action + * has at least one child + */ + public const SHOW_AND_CHILDREN = 'show_and_children'; + + public const AND_CHILDREN_MENTION = 'show_and_children_mention'; + private EngineInterface $engine; private TranslatableStringHelper $translatableStringHelper; - public function __construct(TranslatableStringHelper $translatableStringHelper, EngineInterface $engine) - { + private TranslatorInterface $translator; + + public function __construct( + TranslatableStringHelper $translatableStringHelper, + EngineInterface $engine, + TranslatorInterface $translator + ) { $this->translatableStringHelper = $translatableStringHelper; $this->engine = $engine; + $this->translator = $translator; } public function renderBox($socialAction, array $options): string @@ -72,7 +88,13 @@ class SocialActionRender implements ChillEntityRenderInterface $titles = array_reverse($titles); - return implode($options[self::SEPARATOR_KEY], $titles); + $title = implode($options[self::SEPARATOR_KEY], $titles); + + if ($options[self::SHOW_AND_CHILDREN] && $socialAction->hasChildren()) { + $title .= ' (' . $this->translator->trans($options[self::AND_CHILDREN_MENTION]) . ')'; + } + + return $title; } public function supports($entity, array $options): bool diff --git a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php index 014b23c8e..20a9c9fd2 100644 --- a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php +++ b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php @@ -15,7 +15,7 @@ use Chill\MainBundle\Templating\Entity\ChillEntityRenderInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; use Symfony\Component\Templating\EngineInterface; - +use Symfony\Contracts\Translation\TranslatorInterface; use function array_reverse; use function implode; @@ -23,18 +23,34 @@ final class SocialIssueRender implements ChillEntityRenderInterface { public const DEFAULT_ARGS = [ self::SEPARATOR_KEY => ' > ', + self::SHOW_AND_CHILDREN => false, + self::AND_CHILDREN_MENTION => 'social_issue.and children', ]; public const SEPARATOR_KEY = 'default.separator'; + /** + * Show a mention "and children" on each SocialIssue, if the social issue + * has at least one child + */ + public const SHOW_AND_CHILDREN = 'show_and_children'; + + public const AND_CHILDREN_MENTION = 'show_and_children_mention'; + private EngineInterface $engine; private TranslatableStringHelper $translatableStringHelper; - public function __construct(TranslatableStringHelper $translatableStringHelper, EngineInterface $engine) - { + private TranslatorInterface $translator; + + public function __construct( + TranslatableStringHelper $translatableStringHelper, + EngineInterface $engine, + TranslatorInterface $translator + ) { $this->translatableStringHelper = $translatableStringHelper; $this->engine = $engine; + $this->translator = $translator; } /** @@ -78,7 +94,13 @@ final class SocialIssueRender implements ChillEntityRenderInterface $titles = array_reverse($titles); - return implode($options[self::SEPARATOR_KEY], $titles); + $title = implode($options[self::SEPARATOR_KEY], $titles); + + if ($options[self::SHOW_AND_CHILDREN] && $socialIssue->hasChildren()) { + $title .= ' (' . $this->translator->trans($options[self::AND_CHILDREN_MENTION]) . ')'; + } + + return $title; } public function supports($entity, array $options): bool diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php new file mode 100644 index 000000000..2e0b64d6d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php @@ -0,0 +1,53 @@ +setParent($parentA); + $grandChildA = (new SocialAction())->setParent($childA); + $grandGrandChildA = (new SocialAction())->setParent($grandChildA); + $unrelatedA = new SocialAction(); + + $parentB = new SocialAction(); + $childB = (new SocialAction())->setParent($parentB); + $grandChildB = (new SocialAction())->setParent($childB); + $grandGrandChildB = (new SocialAction())->setParent($grandChildB); + $unrelatedB = new SocialAction(); + + $actual = SocialAction::getDescendantsWithThisForActions([$parentA, $parentB]); + + $this->assertContains($parentA, $actual); + $this->assertContains($parentB, $actual); + $this->assertContains($childA, $actual); + $this->assertContains($childB, $actual); + $this->assertContains($grandChildA, $actual); + $this->assertContains($grandChildB, $actual); + $this->assertContains($grandGrandChildA, $actual); + $this->assertContains($grandGrandChildB, $actual); + $this->assertCount(8, $actual); + $this->assertNotContains($unrelatedA, $actual); + $this->assertNotContains($unrelatedB, $actual); + } + +} \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php index edc1e5474..aee4bec07 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php @@ -77,4 +77,33 @@ final class SocialIssueTest extends TestCase $this->assertFalse($child->isDescendantOf($grandChild)); } + + public function testGetDescendantsWithThisForIssues() + { + $parentA = new SocialIssue(); + $childA = (new SocialIssue())->setParent($parentA); + $grandChildA = (new SocialIssue())->setParent($childA); + $grandGrandChildA = (new SocialIssue())->setParent($grandChildA); + $unrelatedA = new SocialIssue(); + + $parentB = new SocialIssue(); + $childB = (new SocialIssue())->setParent($parentB); + $grandChildB = (new SocialIssue())->setParent($childB); + $grandGrandChildB = (new SocialIssue())->setParent($grandChildB); + $unrelatedB = new SocialIssue(); + + $actual = SocialIssue::getDescendantsWithThisForIssues([$parentA, $parentB]); + + $this->assertContains($parentA, $actual); + $this->assertContains($parentB, $actual); + $this->assertContains($childA, $actual); + $this->assertContains($childB, $actual); + $this->assertContains($grandChildA, $actual); + $this->assertContains($grandChildB, $actual); + $this->assertContains($grandGrandChildA, $actual); + $this->assertContains($grandGrandChildB, $actual); + $this->assertCount(8, $actual); + $this->assertNotContains($unrelatedA, $actual); + $this->assertNotContains($unrelatedB, $actual); + } } diff --git a/src/Bundle/ChillPersonBundle/config/services/form.yaml b/src/Bundle/ChillPersonBundle/config/services/form.yaml index 85b97c86b..52bf1f494 100644 --- a/src/Bundle/ChillPersonBundle/config/services/form.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/form.yaml @@ -27,11 +27,3 @@ services: $config: "%chill_person.accompanying_period_fields%" tags: - { name: form.type } - - Chill\PersonBundle\Form\Type\Select2SocialActionType: - autowire: true - autoconfigure: true - - Chill\PersonBundle\Form\Type\Select2SocialIssueType: - autowire: true - autoconfigure: true diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index a755ce0f0..5e4d9aada 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -951,3 +951,10 @@ export: Group course by referrer's scope: Grouper les parcours par service du référent Computation date for referrer: Date à laquelle le référent était actif Referrer's scope: Service du référent de parcours + + +social_action: + and children: et dérivés + +social_issue: + and children: et dérivés \ No newline at end of file From 531e05fc3abd8cb8b5f7a5b1e76a6854988a31e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 17 Oct 2022 17:54:50 +0200 Subject: [PATCH 12/13] DX: fix tests to find ancestor to all SocialAction and SocialIssue --- .../Entity/SocialWork/SocialAction.php | 2 ++ .../Entity/SocialWork/SocialIssue.php | 12 +++++++++--- .../Tests/Entity/SocialWork/SocialActionTest.php | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php index ea5df8fe0..bb393a3b5 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php @@ -389,6 +389,8 @@ class SocialAction { $this->parent = $parent; + $parent->addChild($this); + return $this; } diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php index d36642977..8708e34f3 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php @@ -71,11 +71,15 @@ class SocialIssue $this->socialActions = new ArrayCollection(); } + /** + * @internal use @see{SocialIssue::setParent} instead + * @param SocialIssue $child + * @return $this + */ public function addChild(self $child): self { if (!$this->children->contains($child)) { $this->children[] = $child; - $child->setParent($this); } return $this; @@ -216,10 +220,10 @@ class SocialIssue } /** - * @param Collection|SocialIssue[] $socialIssues + * @param array|SocialIssue[] $socialIssues * @return Collection */ - public static function getDescendantsWithThisForIssues(Collection $socialIssues): Collection + public static function getDescendantsWithThisForIssues(array $socialIssues): Collection { $unique = []; @@ -351,6 +355,8 @@ class SocialIssue { $this->parent = $parent; + $parent->addChild($this); + return $this; } diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php index 2e0b64d6d..804ea2b1b 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php @@ -50,4 +50,4 @@ final class SocialActionTest extends TestCase $this->assertNotContains($unrelatedB, $actual); } -} \ No newline at end of file +} From 9eb451e359e8eb10727e2acce2d7bff1dfc57c2c Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Tue, 18 Oct 2022 14:37:33 +0200 Subject: [PATCH 13/13] cs fixer --- .../ACPFilters/BySocialActionFilter.php | 5 +- .../Filter/ACPFilters/BySocialIssueFilter.php | 5 +- .../Form/UserCurrentLocationType.php | 2 - .../Test/Export/AbstractAggregatorTest.php | 2 +- .../Test/Export/AbstractFilterTest.php | 1 + .../Entity/SocialWork/SocialAction.php | 11 ++--- .../Entity/SocialWork/SocialIssue.php | 13 +++--- .../SocialActionFilter.php | 7 +-- .../SocialIssueFilter.php | 3 +- .../SocialWork/SocialActionRepository.php | 23 +++++----- .../SocialWork/SocialIssueRepository.php | 21 +++++---- .../Templating/Entity/SocialActionRender.php | 6 +-- .../Templating/Entity/SocialIssueRender.php | 8 ++-- .../Entity/SocialWork/SocialActionTest.php | 2 - .../Entity/SocialWork/SocialIssueTest.php | 46 +++++++++---------- 15 files changed, 79 insertions(+), 76 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php index a7d6e5e3a..b897cde8c 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialActionFilter.php @@ -43,7 +43,8 @@ class BySocialActionFilter implements FilterInterface $clause = $qb->expr()->in('actsocialaction.id', ':socialactions'); $qb->andWhere($clause) - ->setParameter('socialactions', + ->setParameter( + 'socialactions', SocialAction::getDescendantsWithThisForActions($data['accepted_socialactions']) ); } @@ -56,7 +57,7 @@ class BySocialActionFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { $builder->add('accepted_socialactions', PickSocialActionType::class, [ - 'multiple' => true + 'multiple' => true, ]); } diff --git a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php index 94189169d..f827c8c56 100644 --- a/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php +++ b/src/Bundle/ChillActivityBundle/Export/Filter/ACPFilters/BySocialIssueFilter.php @@ -43,7 +43,8 @@ class BySocialIssueFilter implements FilterInterface $clause = $qb->expr()->in('actsocialissue.id', ':socialissues'); $qb->andWhere($clause) - ->setParameter('socialissues', + ->setParameter( + 'socialissues', SocialIssue::getDescendantsWithThisForIssues($data['accepted_socialissues']) ); } @@ -56,7 +57,7 @@ class BySocialIssueFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { $builder->add('accepted_socialissues', PickSocialIssueType::class, [ - 'multiple' => true + 'multiple' => true, ]); } diff --git a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php index 657ba8942..bf6a5d172 100644 --- a/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php +++ b/src/Bundle/ChillMainBundle/Form/UserCurrentLocationType.php @@ -17,10 +17,8 @@ use Symfony\Component\Form\FormBuilderInterface; class UserCurrentLocationType extends AbstractType { - public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('currentLocation', PickUserLocationType::class); } } - diff --git a/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php b/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php index ffb7e4380..af0e3843a 100644 --- a/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php +++ b/src/Bundle/ChillMainBundle/Test/Export/AbstractAggregatorTest.php @@ -111,6 +111,7 @@ abstract class AbstractAggregatorTest extends KernelTestCase * Compare aliases array before and after that aggregator alter query. * * @dataProvider dataProviderAliasDidNotDisappears + * * @return void */ public function testAliasDidNotDisappears(QueryBuilder $qb, array $data) @@ -290,5 +291,4 @@ abstract class AbstractAggregatorTest extends KernelTestCase 'test that the title is not empty' ); } - } diff --git a/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php b/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php index 5632ee022..e1c8665ec 100644 --- a/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php +++ b/src/Bundle/ChillMainBundle/Test/Export/AbstractFilterTest.php @@ -103,6 +103,7 @@ abstract class AbstractFilterTest extends KernelTestCase * Compare aliases array before and after that filter alter query. * * @dataProvider dataProviderAliasDidNotDisappears + * * @return void */ public function testAliasDidNotDisappears(QueryBuilder $qb, array $data) diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php index 08fc4bf9e..29e8a413d 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialAction.php @@ -230,7 +230,6 @@ class SocialAction /** * @param Collection|SocialAction[] $socialActions - * @return Collection */ public static function getDescendantsWithThisForActions($socialActions): Collection { @@ -291,16 +290,16 @@ class SocialAction return $this->title; } - public function hasParent(): bool - { - return $this->getParent() instanceof self; - } - public function hasChildren(): bool { return 0 < $this->getChildren()->count(); } + public function hasParent(): bool + { + return $this->getParent() instanceof self; + } + /** * Recursive method which return true if the current $action * is a descendant of the $action given in parameter. diff --git a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php index 8708e34f3..42c8442c1 100644 --- a/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php +++ b/src/Bundle/ChillPersonBundle/Entity/SocialWork/SocialIssue.php @@ -73,7 +73,9 @@ class SocialIssue /** * @internal use @see{SocialIssue::setParent} instead + * * @param SocialIssue $child + * * @return $this */ public function addChild(self $child): self @@ -221,7 +223,6 @@ class SocialIssue /** * @param array|SocialIssue[] $socialIssues - * @return Collection */ public static function getDescendantsWithThisForIssues(array $socialIssues): Collection { @@ -283,16 +284,16 @@ class SocialIssue return $this->title; } - public function hasParent(): bool - { - return null !== $this->parent; - } - public function hasChildren(): bool { return 0 < $this->getChildren()->count(); } + public function hasParent(): bool + { + return null !== $this->parent; + } + /** * Recursive method which return true if the current $issue is a descendant * of the $issue given in parameter. diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php index a1834aa0e..e3bf77af2 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialActionFilter.php @@ -23,10 +23,10 @@ use function in_array; class SocialActionFilter implements FilterInterface { - private TranslatableStringHelper $translatableStringHelper; - private SocialActionRender $actionRender; + private TranslatableStringHelper $translatableStringHelper; + public function __construct( TranslatableStringHelper $translatableStringHelper, SocialActionRender $actionRender @@ -53,7 +53,8 @@ class SocialActionFilter implements FilterInterface $clause = $qb->expr()->in('acpwsocialaction.id', ':socialactions'); $qb->andWhere($clause) - ->setParameter('socialactions', + ->setParameter( + 'socialactions', SocialAction::getDescendantsWithThisForActions($data['accepted_socialactions'])->toArray() ); } diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php index 08ab7fca2..083c15f91 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/SocialIssueFilter.php @@ -57,7 +57,8 @@ class SocialIssueFilter implements FilterInterface $clause = $qb->expr()->in('acpsocialissue.id', ':socialissues'); $qb->andWhere($clause) - ->setParameter('socialissues', + ->setParameter( + 'socialissues', SocialIssue::getDescendantsWithThisForIssues($data['accepted_socialissues']) ); } diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php index 86e8e41a8..370704aed 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialActionRepository.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Repository\SocialWork; use Chill\PersonBundle\Entity\SocialWork\SocialAction; +use DateTime; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; @@ -44,6 +45,14 @@ final class SocialActionRepository implements ObjectRepository return $this->repository->findAll(); } + /** + * @return array|SocialAction[] + */ + public function findAllActive(): array + { + return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); + } + /** * @param mixed|null $limit * @param mixed|null $offset @@ -68,22 +77,14 @@ final class SocialActionRepository implements ObjectRepository return SocialAction::class; } - /** - * @return array|SocialAction[] - */ - public function findAllActive(): array - { - return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); - } - private function buildQueryWithDesactivatedDateCriteria(): QueryBuilder { $qb = $this->repository->createQueryBuilder('sa'); - - $qb ->where('sa.desactivationDate is null') + + $qb->where('sa.desactivationDate is null') ->orWhere('sa.desactivationDate > :now') ->orderBy('sa.ordering', 'ASC') - ->setParameter('now', new \DateTime('now')); + ->setParameter('now', new DateTime('now')); return $qb; } diff --git a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php index 21831b432..8dd8f6a62 100644 --- a/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/SocialWork/SocialIssueRepository.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Repository\SocialWork; use Chill\PersonBundle\Entity\SocialWork\SocialIssue; +use DateTime; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; @@ -39,6 +40,14 @@ final class SocialIssueRepository implements ObjectRepository return $this->repository->findAll(); } + /** + * @return array|SocialIssue[] + */ + public function findAllActive(): array + { + return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); + } + /** * @param mixed|null $limit * @param mixed|null $offset @@ -63,22 +72,14 @@ final class SocialIssueRepository implements ObjectRepository return SocialIssue::class; } - /** - * @return array|SocialIssue[] - */ - public function findAllActive(): array - { - return $this->buildQueryWithDesactivatedDateCriteria()->getQuery()->getResult(); - } - private function buildQueryWithDesactivatedDateCriteria(): QueryBuilder { $qb = $this->repository->createQueryBuilder('si'); - $qb ->where('si.desactivationDate is null') + $qb->where('si.desactivationDate is null') ->orWhere('si.desactivationDate > :now') ->orderBy('si.ordering', 'ASC') - ->setParameter('now', new \DateTime('now')); + ->setParameter('now', new DateTime('now')); return $qb; } diff --git a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php index 8f1bfaccb..bfe49c0a4 100644 --- a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php +++ b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialActionRender.php @@ -22,6 +22,8 @@ use function implode; class SocialActionRender implements ChillEntityRenderInterface { + public const AND_CHILDREN_MENTION = 'show_and_children_mention'; + public const DEFAULT_ARGS = [ self::SEPARATOR_KEY => ' > ', self::NO_BADGE => false, @@ -38,12 +40,10 @@ class SocialActionRender implements ChillEntityRenderInterface /** * Show a mention "and children" on each SocialAction, if the social action - * has at least one child + * has at least one child. */ public const SHOW_AND_CHILDREN = 'show_and_children'; - public const AND_CHILDREN_MENTION = 'show_and_children_mention'; - private EngineInterface $engine; private TranslatableStringHelper $translatableStringHelper; diff --git a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php index 20a9c9fd2..62d17f47e 100644 --- a/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php +++ b/src/Bundle/ChillPersonBundle/Templating/Entity/SocialIssueRender.php @@ -21,6 +21,8 @@ use function implode; final class SocialIssueRender implements ChillEntityRenderInterface { + public const AND_CHILDREN_MENTION = 'show_and_children_mention'; + public const DEFAULT_ARGS = [ self::SEPARATOR_KEY => ' > ', self::SHOW_AND_CHILDREN => false, @@ -31,11 +33,9 @@ final class SocialIssueRender implements ChillEntityRenderInterface /** * Show a mention "and children" on each SocialIssue, if the social issue - * has at least one child + * has at least one child. */ - public const SHOW_AND_CHILDREN = 'show_and_children'; - - public const AND_CHILDREN_MENTION = 'show_and_children_mention'; + public const SHOW_AND_CHILDREN = 'show_and_children'; private EngineInterface $engine; diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php index 804ea2b1b..2dcfa6b11 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialActionTest.php @@ -12,7 +12,6 @@ declare(strict_types=1); namespace Chill\PersonBundle\Tests\Entity\SocialWork; use Chill\PersonBundle\Entity\SocialWork\SocialAction; -use Doctrine\Common\Collections\ArrayCollection; use PHPUnit\Framework\TestCase; /** @@ -49,5 +48,4 @@ final class SocialActionTest extends TestCase $this->assertNotContains($unrelatedA, $actual); $this->assertNotContains($unrelatedB, $actual); } - } diff --git a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php index aee4bec07..b846ae5f4 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Entity/SocialWork/SocialIssueTest.php @@ -55,29 +55,6 @@ final class SocialIssueTest extends TestCase $this->assertCount(0, $unrelated->getAncestors(false)); } - public function testIsDescendantOf() - { - $parent = new SocialIssue(); - $child = (new SocialIssue())->setParent($parent); - $grandChild = (new SocialIssue())->setParent($child); - $grandGrandChild = (new SocialIssue())->setParent($grandChild); - $unrelated = new SocialIssue(); - - $this->assertTrue($grandGrandChild->isDescendantOf($parent)); - $this->assertTrue($grandGrandChild->isDescendantOf($grandChild)); - $this->assertTrue($grandGrandChild->isDescendantOf($child)); - $this->assertFalse($grandGrandChild->isDescendantOf($unrelated)); - - $this->assertTrue($grandChild->isDescendantOf($parent)); - $this->assertTrue($grandChild->isDescendantOf($child)); - $this->assertFalse($grandChild->isDescendantOf($unrelated)); - $this->assertFalse($grandChild->isDescendantOf($grandChild)); - - $this->assertFalse($unrelated->isDescendantOf($parent)); - - $this->assertFalse($child->isDescendantOf($grandChild)); - } - public function testGetDescendantsWithThisForIssues() { $parentA = new SocialIssue(); @@ -106,4 +83,27 @@ final class SocialIssueTest extends TestCase $this->assertNotContains($unrelatedA, $actual); $this->assertNotContains($unrelatedB, $actual); } + + public function testIsDescendantOf() + { + $parent = new SocialIssue(); + $child = (new SocialIssue())->setParent($parent); + $grandChild = (new SocialIssue())->setParent($child); + $grandGrandChild = (new SocialIssue())->setParent($grandChild); + $unrelated = new SocialIssue(); + + $this->assertTrue($grandGrandChild->isDescendantOf($parent)); + $this->assertTrue($grandGrandChild->isDescendantOf($grandChild)); + $this->assertTrue($grandGrandChild->isDescendantOf($child)); + $this->assertFalse($grandGrandChild->isDescendantOf($unrelated)); + + $this->assertTrue($grandChild->isDescendantOf($parent)); + $this->assertTrue($grandChild->isDescendantOf($child)); + $this->assertFalse($grandChild->isDescendantOf($unrelated)); + $this->assertFalse($grandChild->isDescendantOf($grandChild)); + + $this->assertFalse($unrelated->isDescendantOf($parent)); + + $this->assertFalse($child->isDescendantOf($grandChild)); + } }