PrivateCommentEmbeddable created and added to entities + datamapper

This commit is contained in:
Julie Lenaerts 2022-04-26 20:21:33 +02:00
parent 64b5de2c03
commit 37a198b860
8 changed files with 167 additions and 32 deletions

View File

@ -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;

View File

@ -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;

View File

@ -0,0 +1,44 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Entity\Embeddable;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Embeddable
*/
class PrivateCommentEmbeddable
{
/**
* @ORM\Column(type="json", nullable=true)
*/
private ?array $comments = [];
public function getComments(): ?array
{
return $this->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;
}
}

View File

@ -0,0 +1,54 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Form\DataMapper;
use Chill\MainBundle\Entity\Embeddable\PrivateCommentEmbeddable;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Security\Core\Security;
final class PrivateCommentDataMapper extends AbstractType implements DataMapperInterface
{
private Security $security;
public function __construct(Security $security)
{
$this->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());
}
}

View File

@ -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,
]);
}

View File

@ -143,3 +143,7 @@ services:
Chill\MainBundle\Form\Type\LocationFormType: ~
Chill\MainBundle\Form\WorkflowStepType: ~
Chill\MainBundle\Form\DataMapper\PrivateCommentDataMapper:
autowire: true
autoconfigure: true

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20220426133048 extends AbstractMigration
{
public function getDescription(): string
{
return 'add private embeddable comment to activity, calendar and accompanyingperiod work';
}
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->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');
}
}

View File

@ -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;