From 63dedb90f95b4c106c876f5df623bbebb2d40b56 Mon Sep 17 00:00:00 2001 From: Jean-Francois Monfort Date: Thu, 11 Mar 2021 12:35:07 +0100 Subject: [PATCH] Add CommentEmbeddable to Activity (Ref #21) --- Entity/Embeddable/CommentEmbeddable.php | 79 +++++++++++++++++++++ Form/Type/CommentType.php | 79 +++++++++++++++++++++ Templating/Entity/CommentRender.php | 91 +++++++++++++++++++++++++ config/services/form.yaml | 39 ++++++----- config/services/templating.yaml | 14 ++-- 5 files changed, 282 insertions(+), 20 deletions(-) create mode 100644 Entity/Embeddable/CommentEmbeddable.php create mode 100644 Form/Type/CommentType.php create mode 100644 Templating/Entity/CommentRender.php diff --git a/Entity/Embeddable/CommentEmbeddable.php b/Entity/Embeddable/CommentEmbeddable.php new file mode 100644 index 000000000..730d530d5 --- /dev/null +++ b/Entity/Embeddable/CommentEmbeddable.php @@ -0,0 +1,79 @@ +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; + } +} diff --git a/Form/Type/CommentType.php b/Form/Type/CommentType.php new file mode 100644 index 000000000..fe14290f3 --- /dev/null +++ b/Form/Type/CommentType.php @@ -0,0 +1,79 @@ + + * + * 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 . + */ +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, + ]); + } +} diff --git a/Templating/Entity/CommentRender.php b/Templating/Entity/CommentRender.php new file mode 100644 index 000000000..6291f3d47 --- /dev/null +++ b/Templating/Entity/CommentRender.php @@ -0,0 +1,91 @@ +, + * + * 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 . + */ +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'). + ''. + nl2br($entity->getComment()). + ''. + ''. + $entity->getDate()->format('d/m/Y h:i'); + ''; + + if (strlen($username) > 0) { + $str .= ''. + $username. + '' + ; + } + + $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; + } +} diff --git a/config/services/form.yaml b/config/services/form.yaml index 9355f309c..d3a01ff5d 100644 --- a/config/services/form.yaml +++ b/config/services/form.yaml @@ -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 } diff --git a/config/services/templating.yaml b/config/services/templating.yaml index 3f75eaf6e..0dfb92380 100644 --- a/config/services/templating.yaml +++ b/config/services/templating.yaml @@ -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 } \ No newline at end of file + - { name: twig.extension } + + Chill\MainBundle\Templating\Entity\CommentRender: + arguments: + - '@chill.main.user_repository' + tags: + - { name: 'chill.render_entity' }