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

@ -7,7 +7,7 @@ services:
- "@translator.default"
tags:
- { name: form.type, alias: translatable_string }
chill.main.form.type.select2choice:
class: Chill\MainBundle\Form\Type\Select2ChoiceType
tags:
@ -33,7 +33,7 @@ services:
- "@doctrine.orm.entity_manager"
tags:
- { name: form.type, alias: select2_chill_language }
chill.main.form.type.center:
class: Chill\MainBundle\Form\Type\CenterType
arguments:
@ -41,7 +41,7 @@ services:
- "@chill.main.form.data_transformer.center_transformer"
tags:
- { name: form.type, alias: center }
chill.main.form.type.composed_role_scope:
class: Chill\MainBundle\Form\Type\ComposedRoleScopeType
arguments:
@ -49,8 +49,8 @@ services:
- "@chill.main.role_provider"
tags:
- { name: form.type, alias: composed_role_scope }
chill.main.form.type.postal_code_type:
class: Chill\MainBundle\Form\Type\PostalCodeType
arguments:
@ -60,26 +60,26 @@ services:
- '@Symfony\Component\Translation\TranslatorInterface'
tags:
- { name: form.type }
chill.main.form.choice_loader.postal_code:
class: Chill\MainBundle\Form\ChoiceLoader\PostalCodeChoiceLoader
arguments:
- '@Chill\MainBundle\Repository\PostalCodeRepository'
chill.main.form.type.export:
class: Chill\MainBundle\Form\Type\Export\ExportType
tags:
- { name: form.type }
arguments:
- '@Chill\MainBundle\Export\ExportManager'
chill.main.form.pick_formatter_type:
class: Chill\MainBundle\Form\Type\Export\PickFormatterType
tags:
- { name: form.type }
arguments:
- '@Chill\MainBundle\Export\ExportManager'
chill.main.form.pick_centers_type:
class: Chill\MainBundle\Form\Type\Export\PickCenterType
arguments:
@ -88,19 +88,19 @@ services:
- "@chill.main.security.authorization.helper"
tags:
- { name: form.type }
chill.main.form.formatter_type:
class: Chill\MainBundle\Form\Type\Export\FormatterType
tags:
- { name: form.type }
arguments:
- '@Chill\MainBundle\Export\ExportManager'
chill.main.form.date_type:
class: Chill\MainBundle\Form\Type\ChillDateType
tags:
- { name: form.type }
chill.main.form.pick_user_type:
class: Chill\MainBundle\Form\Type\UserPickerType
arguments:
@ -109,7 +109,7 @@ services:
- "@chill.main.user_repository"
tags:
- { name: form.type }
chill.main.form.pick_scope_type:
class: Chill\MainBundle\Form\Type\ScopePickerType
arguments:
@ -119,21 +119,21 @@ services:
- "@chill.main.helper.translatable_string"
tags:
- { name: form.type }
chill.main.form.advanced_search_type:
class: Chill\MainBundle\Form\AdvancedSearchType
arguments:
- "@chill_main.search_provider"
tags:
- { name: form.type }
Chill\MainBundle\Form\UserPasswordType:
arguments:
$chillLogger: '@monolog.logger.chill'
$passwordEncoder: '@Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface'
tags:
- { name: form.type }
Chill\MainBundle\Form\PermissionsGroupType:
tags:
- { name: form.type }
@ -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

@ -15,18 +15,24 @@ services:
# class: Twig_Extensions_Extension_Text
# tags:
# - { name: twig.extension }
Chill\MainBundle\Templating\ChillTwigHelper:
tags:
- { name: twig.extension }
Chill\MainBundle\Templating\ChillTwigRoutingHelper:
arguments:
$requestStack: '@Symfony\Component\HttpFoundation\RequestStack'
$originalExtension: '@twig.extension.routing'
tags:
- { name: twig.extension }
Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension:
tags:
- { name: twig.extension }
- { name: twig.extension }
Chill\MainBundle\Templating\Entity\CommentRender:
arguments:
- '@chill.main.user_repository'
tags:
- { name: 'chill.render_entity' }