Add CommentEmbeddable to Activity (Ref #21)

This commit is contained in:
Jean-Francois Monfort 2021-03-11 12:35:07 +01:00
parent 21d451626b
commit 63dedb90f9
5 changed files with 282 additions and 20 deletions

View File

@ -0,0 +1,79 @@
<?php
namespace Chill\MainBundle\Entity\Embeddable;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Embeddable()
*/
class CommentEmbeddable
{
/**
* @var string
* @ORM\Column(type="text", nullable=true)
*/
private $comment;
/**
* Embeddable does not support associations
* @var integer
* @ORM\Column(type="integer", nullable=true)
*/
private $userId;
/**
* @var \DateTime
* @ORM\Column(type="datetime", nullable=true)
*/
private $date;
/**
* @return string
*/
public function getComment()
{
return $this->comment;
}
/**
* @param string $comment
*/
public function setComment(?string $comment)
{
$this->comment = $comment;
}
/**
* @return interger $userId
*/
public function getUserId()
{
return $this->userId;
}
/**
* @param integer $userId
*/
public function setUserId($userId)
{
$this->userId = $userId;
}
/**
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* @param \DateTime $date
*/
public function setDate(?\DateTime $date)
{
$this->date = $date;
}
}

79
Form/Type/CommentType.php Normal file
View File

@ -0,0 +1,79 @@
<?php
/*
* Copyright (C) 2017 Champs Libres Cooperative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Form\Type;
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class CommentType extends AbstractType
{
/**
* @var \Symfony\Component\Security\Core\User\UserInterface
*/
protected $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->user = $tokenStorage->getToken()->getUser();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('comment', TextareaType::class, [
'label' => false,
])
;
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getForm()->getData();
$comment = $event->getData();
if ($data->getComment() !== $comment['comment']) {
$data->setDate(new \DateTime());
$data->setUserId($this->user->getId());
$event->getForm()->setData($data);
}
});
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars = array_replace(
$view->vars, [
'hideLabel' => true,
]
);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => CommentEmbeddable::class,
]);
}
}

View File

@ -0,0 +1,91 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2020, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Templating\Entity;
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Templating\Entity\AbstractChillEntityRender;
class CommentRender extends AbstractChillEntityRender
{
/**
* @var \Chill\MainBundle\Repository\UserRepository
*/
private $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* @param CommentEmbeddable $entity
* @param array $options
*
* @return string
*/
public function renderBox($entity, array $options): string
{
$username = '';
$user = $this->userRepository->find($entity->getUserId());
if ($user instanceof User) {
$username = $user->getUsername();
}
$str = $this->getDefaultOpeningBox('comment-embeddable').
'<span class="comment_comment">'.
nl2br($entity->getComment()).
'</span>'.
'<span class="comment_date">'.
$entity->getDate()->format('d/m/Y h:i');
'</span>';
if (strlen($username) > 0) {
$str .= '<span class="comment_user">'.
$username.
'</span>'
;
}
$str .= $this->getDefaultClosingBox();
return $str;
}
/**
* @param CommentEmbeddable $entity
* @param array $options
*
* @return string
*/
public function renderString($entity, array $options): string
{
return $entity->getComment();
}
public function supports($entity, array $options): bool
{
return $entity instanceof CommentEmbeddable;
}
}

View File

@ -141,3 +141,10 @@ services:
Chill\MainBundle\Form\Extension\CKEditorExtension:
tags:
- { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\TextareaType }
chill.main.form.type.comment:
class: Chill\MainBundle\Form\Type\CommentType
arguments:
- "@security.token_storage"
tags:
- { name: form.type }

View File

@ -30,3 +30,9 @@ services:
Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension:
tags:
- { name: twig.extension }
Chill\MainBundle\Templating\Entity\CommentRender:
arguments:
- '@chill.main.user_repository'
tags:
- { name: 'chill.render_entity' }