From cc69a3e86b89d1277d82628766f142f6aee5ec43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 6 Apr 2023 17:29:05 +0200 Subject: [PATCH] Fixed: [Activity] allow to set a user null Fix https://gitlab.com/Chill-Projet/chill-bundles/-/issues/74 --- .../ChillActivityBundle/Entity/Activity.php | 8 ++++---- .../ChillActivityBundle/Form/ActivityType.php | 1 + .../views/Activity/_list_item.html.twig | 2 +- .../Resources/views/Activity/show.html.twig | 10 ++++++---- .../public/module/pick-entity/index.js | 19 ++++++++++--------- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index 2a5ae6acb..9adfa057f 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -195,7 +195,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User") * @Groups({"docgen:read"}) */ - private User $user; + private ?User $user; /** * @ORM\ManyToMany(targetEntity="Chill\MainBundle\Entity\User") @@ -494,7 +494,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac return $this->activityType; } - public function getUser(): User + public function getUser(): ?User { return $this->user; } @@ -681,14 +681,14 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac return $this; } - public function setUser(UserInterface $user): self + public function setUser(?User $user): self { $this->user = $user; return $this; } - public function setUsers(?Collection $users): self + public function setUsers(Collection $users): self { $this->users = $users; diff --git a/src/Bundle/ChillActivityBundle/Form/ActivityType.php b/src/Bundle/ChillActivityBundle/Form/ActivityType.php index 6d198c5fa..5dfd756f9 100644 --- a/src/Bundle/ChillActivityBundle/Form/ActivityType.php +++ b/src/Bundle/ChillActivityBundle/Form/ActivityType.php @@ -225,6 +225,7 @@ class ActivityType extends AbstractType $builder->add('user', PickUserDynamicType::class, [ 'label' => $activityType->getLabel('user'), 'required' => $activityType->isRequired('user'), + 'multiple' => false, ]); } diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/_list_item.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/_list_item.html.twig index 885125aa6..8d1aba2b3 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/_list_item.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/_list_item.html.twig @@ -63,7 +63,7 @@ {% endif %} - {% if activity.user and t.userVisible %} + {% if activity.user is not null and t.userVisible %}

{{ 'Referrer'|trans }}

diff --git a/src/Bundle/ChillActivityBundle/Resources/views/Activity/show.html.twig b/src/Bundle/ChillActivityBundle/Resources/views/Activity/show.html.twig index acda43b97..fca6a7658 100644 --- a/src/Bundle/ChillActivityBundle/Resources/views/Activity/show.html.twig +++ b/src/Bundle/ChillActivityBundle/Resources/views/Activity/show.html.twig @@ -34,10 +34,12 @@
-
{{ 'Referrer'|trans|capitalize }}
-
- {{ entity.user|chill_entity_render_box }} -
+ {%- if entity.user is not null %} +
{{ 'Referrer'|trans|capitalize }}
+
+ {{ entity.user|chill_entity_render_box }} +
+ {% endif %} {%- if entity.scope -%}
{{ 'Scope'|trans }}
diff --git a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js index 6b143a11d..99f83e1e3 100644 --- a/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js +++ b/src/Bundle/ChillMainBundle/Resources/public/module/pick-entity/index.js @@ -18,6 +18,7 @@ function loadDynamicPicker(element) { isMultiple = parseInt(el.dataset.multiple) === 1, uniqId = el.dataset.uniqid, input = element.querySelector('[data-input-uniqid="'+ el.dataset.uniqid +'"]'), + // the "picked" will always be an array, even if multiple is false picked = isMultiple ? JSON.parse(input.value) : ( (input.value === '[]' || input.value === '') ? @@ -54,15 +55,11 @@ function loadDynamicPicker(element) { }, computed: { notPickedSuggested() { - if (this.multiple) { - const pickedIds = new Set(); - for (const p of this.picked) { - pickedIds.add(`${p.type}${p.id}`); - } - return this.suggested.filter(e => !pickedIds.has(`${e.type}${e.id}`)) + const pickedIds = new Set(); + for (const p of this.picked) { + pickedIds.add(`${p.type}${p.id}`); } - - return this.suggested.filter(e => e.type !== this.picked.type && e.id !== e.picked.id); + return this.suggested.filter(e => !pickedIds.has(`${e.type}${e.id}`)) } }, methods: { @@ -90,7 +87,11 @@ function loadDynamicPicker(element) { this.suggested.push(entity); } this.picked = this.picked.filter(e => !(e.type === entity.type && e.id === entity.id)); - input.value = JSON.stringify(this.picked); + if (this.multiple) { + input.value = JSON.stringify(this.picked); + } else { + input.value = ""; + } }, } })