diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index f7e429476..b5a58c900 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -15,6 +15,7 @@ use Chill\ActivityBundle\Validator\Constraints as ActivityValidator; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable; +use Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable; use Chill\MainBundle\Entity\HasCenterInterface; use Chill\MainBundle\Entity\HasScopeInterface; use Chill\MainBundle\Entity\Location; @@ -86,10 +87,10 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac private CommentEmbeddable $comment; /** - * @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\CommentEmbeddable", columnPrefix="prcomment_") + * @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable", columnPrefix="privateComment_") * @Groups({"docgen:read"}) */ - private CommentEmbeddable $privateComment; + private PrivateCommentEmbeddable $privateComment; /** * @ORM\Column(type="datetime") @@ -197,7 +198,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac { $this->reasons = new ArrayCollection(); $this->comment = new CommentEmbeddable(); - $this->privateComment = new CommentEmbeddable(); + $this->privateComment = new PrivateCommentEmbeddable(); $this->persons = new ArrayCollection(); $this->thirdParties = new ArrayCollection(); $this->documents = new ArrayCollection(); @@ -307,7 +308,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac return $this->comment; } - public function getPrivateComment(): CommentEmbeddable + public function getPrivateComment(): PrivateCommentEmbeddable { return $this->privateComment; } @@ -536,7 +537,7 @@ class Activity implements AccompanyingPeriodLinkedWithSocialIssuesEntityInterfac return $this; } - public function setPrivateComment(CommentEmbeddable $privateComment): self + public function setPrivateComment(PrivateCommentEmbeddable $privateComment): self { $this->privateComment = $privateComment; diff --git a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php index bf425bd2d..4783e70b6 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php +++ b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php @@ -14,6 +14,7 @@ namespace Chill\CalendarBundle\Entity; use Chill\ActivityBundle\Entity\Activity; use Chill\CalendarBundle\Repository\CalendarRepository; use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable; +use Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable; use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Entity\User; use Chill\PersonBundle\Entity\AccompanyingPeriod; @@ -71,10 +72,10 @@ class Calendar private CommentEmbeddable $comment; /** - * @ORM\Embedded(class=CommentEmbeddable::class, columnPrefix="prcomment_") + * @ORM\Embedded(class=PrivateCommentEmbeddable::class, columnPrefix="privateComment_") * @Serializer\Groups({"calendar:read"}) */ - private CommentEmbeddable $privateComment; + private PrivateCommentEmbeddable $privateComment; /** * @ORM\Column(type="datetimetz_immutable") @@ -157,7 +158,7 @@ class Calendar public function __construct() { $this->comment = new CommentEmbeddable(); - $this->privateComment = new CommentEmbeddable(); + $this->privateComment = new PrivateCommentEmbeddable(); $this->persons = new ArrayCollection(); $this->professionals = new ArrayCollection(); $this->invites = new ArrayCollection(); @@ -215,7 +216,7 @@ class Calendar return $this->comment; } - public function getPrivateComment(): CommentEmbeddable + public function getPrivateComment(): PrivateCommentEmbeddable { return $this->privateComment; } @@ -398,7 +399,7 @@ class Calendar return $this; } - public function setPrivateComment(CommentEmbeddable $privateComment): self + public function setPrivateComment(PrivateCommentEmbeddable $privateComment): self { $this->privateComment = $privateComment; diff --git a/src/Bundle/ChillMainBundle/Entity/Embeddable/PrivateCommentEmbeddable.php b/src/Bundle/ChillMainBundle/Entity/Embeddable/PrivateCommentEmbeddable.php new file mode 100644 index 000000000..70374749c --- /dev/null +++ b/src/Bundle/ChillMainBundle/Entity/Embeddable/PrivateCommentEmbeddable.php @@ -0,0 +1,44 @@ +comments; + } + + + public function getCommentForUser(User $user): string + { + return $this->comments[$user->getId()] ?? ''; + } + + public function setCommentForUser(User $user, string $content): self + { + $this->comments[$user->getId()] = $content; + + return $this; + } +} diff --git a/src/Bundle/ChillMainBundle/Form/DataMapper/PrivateCommentDataMapper.php b/src/Bundle/ChillMainBundle/Form/DataMapper/PrivateCommentDataMapper.php new file mode 100644 index 000000000..5d4cd08cd --- /dev/null +++ b/src/Bundle/ChillMainBundle/Form/DataMapper/PrivateCommentDataMapper.php @@ -0,0 +1,54 @@ +security = $security; + } + + public function mapDataToForms($viewData, $forms) + { + if (null === $viewData) { + return null; + } + + if (!$viewData instanceof PrivateCommentEmbeddable) { + throw new UnexpectedTypeException($viewData, PrivateCommentEmbeddable::class); + } + + $forms = iterator_to_array($forms); + + $forms['comments']->setData($viewData->getCommentForUser($this->security->getUser())); + + + } + + public function mapFormsToData($forms, &$viewData) + { + $forms = iterator_to_array($forms); + + $viewData->setCommentForUser($this->security->getUser(), $forms['comments']->getData()); + } + +} \ No newline at end of file diff --git a/src/Bundle/ChillMainBundle/Form/Type/PrivateCommentType.php b/src/Bundle/ChillMainBundle/Form/Type/PrivateCommentType.php index 77e1a1902..174326d86 100644 --- a/src/Bundle/ChillMainBundle/Form/Type/PrivateCommentType.php +++ b/src/Bundle/ChillMainBundle/Form/Type/PrivateCommentType.php @@ -12,8 +12,13 @@ declare(strict_types=1); namespace Chill\MainBundle\Form\Type; use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable; +use Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable; +use Chill\MainBundle\Form\DataMapper\PrivateCommentDataMapper; use DateTime; + use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\CollectionType; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; @@ -27,29 +32,19 @@ class PrivateCommentType extends AbstractType { protected UserInterface $user; - public function __construct(TokenStorageInterface $tokenStorage) + protected PrivateCommentDataMapper $dataMapper; + + public function __construct(TokenStorageInterface $tokenStorage, PrivateCommentDataMapper $dataMapper) { $this->user = $tokenStorage->getToken()->getUser(); + $this->dataMapper = $dataMapper; } public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('comment', ChillTextareaType::class, [ - 'disable_editor' => $options['disable_editor'], - 'label' => $options['label'], - ]); - - $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { - $data = $event->getForm()->getData(); - $privateComment = $event->getData() ?? ['privateComment' => '']; - - if (null !== $data && $data->getComment() !== $privateComment['comment']) { - $data->setDate(new DateTime()); - $data->setUserId($this->user->getId()); - $event->getForm()->setData($data); - } - }); + ->add('comments', ChillTextareaType::class) + ->setDataMapper($this->dataMapper); } public function buildView(FormView $view, FormInterface $form, array $options) @@ -68,7 +63,7 @@ class PrivateCommentType extends AbstractType ->setDefined('disable_editor') ->setAllowedTypes('disable_editor', 'bool') ->setDefaults([ - 'data_class' => CommentEmbeddable::class, + 'data_class' => PrivateCommentEmbeddable::class, 'disable_editor' => false, ]); } diff --git a/src/Bundle/ChillMainBundle/config/services/form.yaml b/src/Bundle/ChillMainBundle/config/services/form.yaml index f2dc7803a..407a1b6af 100644 --- a/src/Bundle/ChillMainBundle/config/services/form.yaml +++ b/src/Bundle/ChillMainBundle/config/services/form.yaml @@ -143,3 +143,7 @@ services: Chill\MainBundle\Form\Type\LocationFormType: ~ Chill\MainBundle\Form\WorkflowStepType: ~ + + Chill\MainBundle\Form\DataMapper\PrivateCommentDataMapper: + autowire: true + autoconfigure: true diff --git a/src/Bundle/ChillMainBundle/migrations/Version20220426133048.php b/src/Bundle/ChillMainBundle/migrations/Version20220426133048.php new file mode 100644 index 000000000..2f1fbab1a --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20220426133048.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE activity ADD privateComment_comments JSON DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_calendar.calendar ADD privateComment_comments JSON DEFAULT NULL'); + $this->addSql('ALTER TABLE chill_person_accompanying_period_work ADD privateComment_comments JSON DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE chill_person_accompanying_period_work DROP privateComment_comments'); + $this->addSql('ALTER TABLE activity DROP privateComment_comments'); + $this->addSql('ALTER TABLE chill_calendar.calendar DROP privateComment_comments'); + } +} diff --git a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php index 91d85883a..fd1156e01 100644 --- a/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php +++ b/src/Bundle/ChillPersonBundle/Entity/AccompanyingPeriod/AccompanyingPeriodWork.php @@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\MainBundle\Doctrine\Model\TrackCreationInterface; use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface; use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable; +use Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable; use Chill\MainBundle\Entity\User; use Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodLinkedWithSocialIssuesEntityInterface; use Chill\PersonBundle\Entity\AccompanyingPeriod; @@ -136,10 +137,10 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues private string $note = ''; /** - * @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\CommentEmbeddable", columnPrefix="prcomment_") + * @ORM\Embedded(class="Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable", columnPrefix="privateComment_") * @Serializer\Groups({"read", "accompanying_period_work:edit", "docgen:read"}) */ - private CommentEmbeddable $privateComment; + private PrivateCommentEmbeddable $privateComment; /** * @ORM\ManyToMany(targetEntity=Person::class) @@ -209,7 +210,7 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues public function __construct() { $this->goals = new ArrayCollection(); - $this->privateComment = new CommentEmbeddable(); + $this->privateComment = new PrivateCommentEmbeddable(); $this->results = new ArrayCollection(); $this->thirdParties = new ArrayCollection(); $this->persons = new ArrayCollection(); @@ -331,7 +332,7 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues return $this->note; } - public function getPrivateComment(): CommentEmbeddable + public function getPrivateComment(): PrivateCommentEmbeddable { return $this->privateComment; } @@ -518,7 +519,7 @@ class AccompanyingPeriodWork implements AccompanyingPeriodLinkedWithSocialIssues return $this; } - public function setPrivateComment(CommentEmbeddable $privateComment): self + public function setPrivateComment(PrivateCommentEmbeddable $privateComment): self { $this->privateComment = $privateComment;