PrivateCommentEmbeddable created and added to entities + datamapper

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

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');
}
}