From b705c5910f07bdbf96c5f63aec4a6fd544a95308 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Mon, 28 Jun 2021 20:46:30 +0200 Subject: [PATCH] dispatch action change value of hidden fields, persisted when submited --- .../ChillActivityBundle/Form/ActivityType.php | 68 ++++++++++--------- .../Activity/components/SocialIssuesAcc.vue | 1 + .../Resources/public/vuejs/Activity/store.js | 12 ++++ .../Resources/views/Activity/edit.html.twig | 3 +- 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Form/ActivityType.php b/src/Bundle/ChillActivityBundle/Form/ActivityType.php index 221de2e5d..80d0991e7 100644 --- a/src/Bundle/ChillActivityBundle/Form/ActivityType.php +++ b/src/Bundle/ChillActivityBundle/Form/ActivityType.php @@ -112,41 +112,45 @@ class ActivityType extends AbstractType } if ($activityType->isVisible('socialIssues') && $accompanyingPeriod) { - $builder->add('socialIssues', - /* - HiddenType::class - */ - EntityType::class, [ - 'label' => $activityType->getLabel('socialIssues'), - 'required' => $activityType->isRequired('socialIssues'), - 'class' => SocialIssue::class, - 'choice_label' => function (SocialIssue $socialIssue) { - return $this->socialIssueRender->renderString($socialIssue, []); - }, - 'multiple' => true, - 'choices' => $accompanyingPeriod->getRecursiveSocialIssues(), - 'expanded' => true, - ] - ); + $builder->add('socialIssues', HiddenType::class); + $builder->get('socialIssues') + ->addModelTransformer(new CallbackTransformer( + function (iterable $socialIssuesAsIterable): string { + $socialIssueIds = []; + foreach ($socialIssuesAsIterable as $value) { + $socialIssueIds[] = $value->getId(); + } + return implode(',', $socialIssueIds); + }, + function (?string $socialIssuesAsString): array { + return array_map( + fn(string $id): ?SocialIssue => $this->om->getRepository(SocialIssue::class)->findOneBy(['id' => (int) $id]), + explode(',', $socialIssuesAsString) + ); + } + )) + ; } if ($activityType->isVisible('socialActions') && $accompanyingPeriod) { - $builder->add('socialActions', - /* - HiddenType::class - */ - EntityType::class, [ - 'label' => $activityType->getLabel('socialActions'), - 'required' => $activityType->isRequired('socialActions'), - 'class' => SocialAction::class, - 'choice_label' => function (SocialAction $socialAction) { - return $this->socialActionRender->renderString($socialAction, []); - }, - 'multiple' => true, - 'choices' => $accompanyingPeriod->getRecursiveSocialActions(), - 'expanded' => true, - ] - ); + $builder->add('socialActions', HiddenType::class); + $builder->get('socialActions') + ->addModelTransformer(new CallbackTransformer( + function (iterable $socialActionsAsIterable): string { + $socialActionIds = []; + foreach ($socialActionsAsIterable as $value) { + $socialActionIds[] = $value->getId(); + } + return implode(',', $socialActionIds); + }, + function (?string $socialActionsAsString): array { + return array_map( + fn(string $id): ?SocialAction => $this->om->getRepository(SocialAction::class)->findOneBy(['id' => (int) $id]), + explode(',', $socialActionsAsString) + ); + } + )) + ; } if ($activityType->isVisible('date')) { diff --git a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/SocialIssuesAcc.vue b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/SocialIssuesAcc.vue index 304a119bc..4ac1b1957 100644 --- a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/SocialIssuesAcc.vue +++ b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/components/SocialIssuesAcc.vue @@ -69,6 +69,7 @@ {{ $t('activity.select_first_a_social_issue') }} + diff --git a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/store.js b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/store.js index 22b186b99..9684ab375 100644 --- a/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/store.js +++ b/src/Bundle/ChillActivityBundle/Resources/public/vuejs/Activity/store.js @@ -111,12 +111,24 @@ const store = createStore({ }, actions: { addIssueSelected({ commit }, issue) { + let aSocialIssues = document.getElementById("chill_activitybundle_activity_socialIssues"); + aSocialIssues.value = addIdToValue(aSocialIssues.value, issue.id); commit('addIssueSelected', issue); }, updateIssuesSelected({ commit }, payload) { + let aSocialIssues = document.getElementById("chill_activitybundle_activity_socialIssues"); + aSocialIssues.value = ''; + payload.forEach(item => { + aSocialIssues.value = addIdToValue(aSocialIssues.value, item.id); + }); commit('updateIssuesSelected', payload); }, updateActionsSelected({ commit }, payload) { + let aSocialActions = document.getElementById("chill_activitybundle_activity_socialActions"); + aSocialActions.value = ''; + payload.forEach(item => { + aSocialActions.value = addIdToValue(aSocialActions.value, item.id); + }); commit('updateActionsSelected', payload); }, addPersonsInvolved({ commit }, payload) { diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig index e588d7a62..56a7c96b4 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/edit.html.twig @@ -23,7 +23,6 @@ {{ form_row(edit_form.scope) }} {% endif %} - +
{%- if edit_form.reasons is defined -%}