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