Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add field to set publication time #94

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ $('.datepicker').datepicker({
firstDay: 1
});

$('.timepicker').timepicker({
twelveHour: false
});

$('.dismiss').click(function ($event) {
$event.preventDefault();
$(this).parent().fadeOut();
Expand Down
9 changes: 1 addition & 8 deletions src/Controller/Admin/PostsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
14 changes: 4 additions & 10 deletions src/Form/Blog/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,13 +47,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'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('publishedAt', DateTimeType::class, [
'required' => false,
])
->add('save', SubmitType::class, [
'label' => $this->translator->trans('default.action.save', [], 'admin'),
'attr' => ['class' => 'btn right'],
Expand Down
39 changes: 39 additions & 0 deletions src/Form/DateTimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

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;

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());
}
}
54 changes: 54 additions & 0 deletions src/Form/Transformer/DateTimeModelTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Form\Transformer;

use Symfony\Component\Form\DataTransformerInterface;

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