From 9b9da19221449cd324566bb97d4c27a236bf4937 Mon Sep 17 00:00:00 2001 From: Nikolay Mikhaylov Date: Tue, 6 Oct 2020 02:19:53 +0300 Subject: [PATCH 1/3] Add field to set publication time --- assets/js/admin.js | 4 + src/Controller/Admin/PostsController.php | 9 +- src/Form/Blog/PostType.php | 83 +++++++++---------- src/Form/DateTimeType.php | 49 +++++++++++ .../Transformer/DateTimeModelTransformer.php | 64 ++++++++++++++ 5 files changed, 157 insertions(+), 52 deletions(-) create mode 100644 src/Form/DateTimeType.php create mode 100644 src/Form/Transformer/DateTimeModelTransformer.php diff --git a/assets/js/admin.js b/assets/js/admin.js index 796abba..22f412f 100644 --- a/assets/js/admin.js +++ b/assets/js/admin.js @@ -10,6 +10,10 @@ $('.datepicker').datepicker({ firstDay: 1 }); +$('.timepicker').timepicker({ + twelveHour: false +}); + $('.dismiss').click(function ($event) { $event.preventDefault(); $(this).parent().fadeOut(); diff --git a/src/Controller/Admin/PostsController.php b/src/Controller/Admin/PostsController.php index 9a87d5d..0807d88 100644 --- a/src/Controller/Admin/PostsController.php +++ b/src/Controller/Admin/PostsController.php @@ -48,10 +48,6 @@ public function new(Request $request, FileUploader $fileUploader, Security $secu if (($picture = $form['picture']->getData())) { $post->setPicture($fileUploader->upload($picture, $this->getParameter('posts_pictures_directory'))); } - if (($posted = $request->request->get('post', null))) { - $date = new DateTime($posted['publishedAt']); - $post->setPublishedAt($date); - } $post->setAuthor($security->getUser()); $em = $this->getDoctrine()->getManager(); @@ -99,10 +95,7 @@ public function edit(Request $request, Post $post, FileUploader $fileUploader): if (($picture = $form['picture']->getData())) { $post->setPicture($fileUploader->upload($picture, $this->getParameter('posts_pictures_directory'))); } - if (($posted = $request->request->get('post', null))) { - $date = new DateTime($posted['publishedAt']); - $post->setPublishedAt($date); - } + $this->getDoctrine()->getManager()->flush(); $this->addFlash('success', 'posts.success.edit'); diff --git a/src/Form/Blog/PostType.php b/src/Form/Blog/PostType.php index 2e5ff8d..78c4ff1 100644 --- a/src/Form/Blog/PostType.php +++ b/src/Form/Blog/PostType.php @@ -3,12 +3,10 @@ namespace App\Form\Blog; use App\Entity\Blog\Tag; -use DateTime; +use App\Form\DateTimeType; use FOS\CKEditorBundle\Form\Type\CKEditorType; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\CallbackTransformer; -use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -18,47 +16,44 @@ class PostType extends AbstractType { - /** - * @var TranslatorInterface - */ - private $translator; + /** + * @var TranslatorInterface + */ + private $translator; - public function __construct(TranslatorInterface $translator) - { - $this->translator = $translator; - } + public function __construct(TranslatorInterface $translator) + { + $this->translator = $translator; + } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder - ->add('title', TextType::class, ['required' => true]) - ->add('content', CKEditorType::class, [ - 'config' => ['height' => '500px'], - 'required' => true, - 'label_attr' => ['style' => 'transform: translateY(-14px) scale(0.8);transform-origin: 0 0;'], - ]) - ->add('picture', FileType::class, [ - 'mapped' => false, - 'required' => false, - 'constraints' => [new Image(['maxSize' => '2048k'])], - 'attr' => ['accept' => 'image/*'], - ]) - ->add('tags', EntityType::class, [ - 'class' => Tag::class, - 'choice_label' => 'name', - 'multiple' => true, - 'required' => false, - ]) - ->add('publishedAt', DateType::class, [ - 'widget' => 'single_text', - 'format' => 'yyyy-MM-dd', - 'required' => false, - 'attr' => ['class' => 'datepicker'], - 'label' => 'Date de publication', - ]) - ->add('save', SubmitType::class, [ - 'label' => $this->translator->trans('default.action.save', [], 'admin'), - 'attr' => ['class' => 'btn right'], - ]); - } + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('title', TextType::class, ['required' => true]) + ->add('content', CKEditorType::class, [ + 'config' => ['height' => '500px'], + 'required' => true, + 'label_attr' => ['style' => 'transform: translateY(-14px) scale(0.8);transform-origin: 0 0;'], + ]) + ->add('picture', FileType::class, [ + 'mapped' => false, + 'required' => false, + 'constraints' => [new Image(['maxSize' => '2048k'])], + 'attr' => ['accept' => 'image/*'], + ]) + ->add('tags', EntityType::class, [ + 'class' => Tag::class, + 'choice_label' => 'name', + 'multiple' => true, + 'required' => false, + ]) + ->add('publishedAt', DateTimeType::class, [ + 'required' => false, + ]) + ->add('published') + ->add('save', SubmitType::class, [ + 'label' => $this->translator->trans('default.action.save', [], 'admin'), + 'attr' => ['class' => 'btn right'], + ]); + } } diff --git a/src/Form/DateTimeType.php b/src/Form/DateTimeType.php new file mode 100644 index 0000000..bc33e7c --- /dev/null +++ b/src/Form/DateTimeType.php @@ -0,0 +1,49 @@ + + * @copyright Copyright (c) 2020, GameHer + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace App\Form; + +use App\Form\Transformer\DateTimeModelTransformer; +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\DateType; +use Symfony\Component\Form\Extension\Core\Type\TimeType; +use Symfony\Component\Form\FormBuilderInterface; + +/** + * Date time type + */ +class DateTimeType extends AbstractType +{ + /** + * @inheritDoc + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('date', DateType::class, [ + 'widget' => 'single_text', + 'format' => 'yyyy-MM-dd', + 'required' => true, + 'input' => 'datetime', + 'attr' => ['class' => 'datepicker'], + 'label' => 'Date de publication', + ]) + ->add('time', TimeType::class, [ + 'widget' => 'single_text', + 'required' => true, + 'html5' => false, + 'attr' => ['class' => 'timepicker'], + 'input' => 'array', + 'label' => 'Heure de publication', + 'empty_data' => '08:00', + ]); + + $builder->addModelTransformer(new DateTimeModelTransformer()); + } +} diff --git a/src/Form/Transformer/DateTimeModelTransformer.php b/src/Form/Transformer/DateTimeModelTransformer.php new file mode 100644 index 0000000..b30dd9f --- /dev/null +++ b/src/Form/Transformer/DateTimeModelTransformer.php @@ -0,0 +1,64 @@ + + * @copyright Copyright (c) 2020, GameHer + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace App\Form\Transformer; + +use Symfony\Component\Form\DataTransformerInterface; + +/** + * Date time model transformer + */ +class DateTimeModelTransformer implements DataTransformerInterface +{ + /** + * @param ?\DateTime $value + * + * @return array|null + */ + public function transform($value) + { + if ($value === null) { + return null; + } + + return [ + 'date' => (clone $value)->setTime(0, 0), + 'time' => [ + 'hour' => $value->format('h'), + 'minute' => $value->format('i'), + ], + ]; + } + + /** + * @param array $value + * + * @return \DateTime|null + * + * @throws \Exception + */ + public function reverseTransform($value) + { + if (empty($value)) { + return null; + } + + /** + * @var \DateTime $date + * @var array $time + */ + list('date' => $date, 'time' => $time) = $value; + + if (!$date instanceof \DateTimeInterface) { + return null; + } + + return $date->add(new \DateInterval(sprintf('PT%sH%sM', ...array_values($time)))); + } +} From aaddbba18a8f5e027c75c796bb3eb9982ded36da Mon Sep 17 00:00:00 2001 From: Nikolay Mikhaylov Date: Thu, 22 Oct 2020 14:18:04 +0300 Subject: [PATCH 2/3] Add field to set publication time --- src/Form/Blog/PostType.php | 53 +++++++++---------- src/Form/DateTimeType.php | 10 ---- .../Transformer/DateTimeModelTransformer.php | 10 ---- 3 files changed, 26 insertions(+), 47 deletions(-) diff --git a/src/Form/Blog/PostType.php b/src/Form/Blog/PostType.php index 78c4ff1..6a45d7e 100644 --- a/src/Form/Blog/PostType.php +++ b/src/Form/Blog/PostType.php @@ -26,34 +26,33 @@ public function __construct(TranslatorInterface $translator) $this->translator = $translator; } - public function buildForm(FormBuilderInterface $builder, array $options) - { - $builder - ->add('title', TextType::class, ['required' => true]) - ->add('content', CKEditorType::class, [ - 'config' => ['height' => '500px'], - 'required' => true, - 'label_attr' => ['style' => 'transform: translateY(-14px) scale(0.8);transform-origin: 0 0;'], - ]) - ->add('picture', FileType::class, [ - 'mapped' => false, - 'required' => false, - 'constraints' => [new Image(['maxSize' => '2048k'])], - 'attr' => ['accept' => 'image/*'], - ]) - ->add('tags', EntityType::class, [ - 'class' => Tag::class, - 'choice_label' => 'name', - 'multiple' => true, - 'required' => false, - ]) + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('title', TextType::class, ['required' => true]) + ->add('content', CKEditorType::class, [ + 'config' => ['height' => '500px'], + 'required' => true, + 'label_attr' => ['style' => 'transform: translateY(-14px) scale(0.8);transform-origin: 0 0;'], + ]) + ->add('picture', FileType::class, [ + 'mapped' => false, + 'required' => false, + 'constraints' => [new Image(['maxSize' => '2048k'])], + 'attr' => ['accept' => 'image/*'], + ]) + ->add('tags', EntityType::class, [ + 'class' => Tag::class, + 'choice_label' => 'name', + 'multiple' => true, + 'required' => false, + ]) ->add('publishedAt', DateTimeType::class, [ 'required' => false, ]) - ->add('published') - ->add('save', SubmitType::class, [ - 'label' => $this->translator->trans('default.action.save', [], 'admin'), - 'attr' => ['class' => 'btn right'], - ]); - } + ->add('save', SubmitType::class, [ + 'label' => $this->translator->trans('default.action.save', [], 'admin'), + 'attr' => ['class' => 'btn right'], + ]); + } } diff --git a/src/Form/DateTimeType.php b/src/Form/DateTimeType.php index bc33e7c..b011e0c 100644 --- a/src/Form/DateTimeType.php +++ b/src/Form/DateTimeType.php @@ -1,11 +1,4 @@ - * @copyright Copyright (c) 2020, GameHer - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ namespace App\Form; @@ -15,9 +8,6 @@ use Symfony\Component\Form\Extension\Core\Type\TimeType; use Symfony\Component\Form\FormBuilderInterface; -/** - * Date time type - */ class DateTimeType extends AbstractType { /** diff --git a/src/Form/Transformer/DateTimeModelTransformer.php b/src/Form/Transformer/DateTimeModelTransformer.php index b30dd9f..6516396 100644 --- a/src/Form/Transformer/DateTimeModelTransformer.php +++ b/src/Form/Transformer/DateTimeModelTransformer.php @@ -1,19 +1,9 @@ - * @copyright Copyright (c) 2020, GameHer - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ namespace App\Form\Transformer; use Symfony\Component\Form\DataTransformerInterface; -/** - * Date time model transformer - */ class DateTimeModelTransformer implements DataTransformerInterface { /** From 58979da419a40f92ef8fb8b7f49b810dfd5ef1f8 Mon Sep 17 00:00:00 2001 From: Nikolay Mikhaylov Date: Thu, 22 Oct 2020 14:27:01 +0300 Subject: [PATCH 3/3] Fix indentation --- src/Form/Blog/PostType.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Form/Blog/PostType.php b/src/Form/Blog/PostType.php index 6a45d7e..b793ca5 100644 --- a/src/Form/Blog/PostType.php +++ b/src/Form/Blog/PostType.php @@ -16,15 +16,15 @@ class PostType extends AbstractType { - /** - * @var TranslatorInterface - */ - private $translator; + /** + * @var TranslatorInterface + */ + private $translator; - public function __construct(TranslatorInterface $translator) - { - $this->translator = $translator; - } + public function __construct(TranslatorInterface $translator) + { + $this->translator = $translator; + } public function buildForm(FormBuilderInterface $builder, array $options) {