This commit is contained in:
2024-07-03 12:32:09 +02:00
parent 096d35d5cf
commit a2f2520617
16 changed files with 270 additions and 11 deletions

View File

@@ -10,6 +10,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
@@ -46,7 +47,9 @@ class CommentCrudController extends AbstractCrudController
yield TextareaField::new('text')
->hideOnIndex()
;
yield TextField::new('photoFilename')
yield ImageField::new('photoFilename')
->setBasePath('/uploads/photos')
->setLabel('Photo')
->onlyOnIndex()
;
@@ -56,9 +59,8 @@ class CommentCrudController extends AbstractCrudController
]);
if (Crud::PAGE_EDIT === $pageName) {
yield $createdAt->setFormTypeOption('disabled', true);
} else {
yield $createdAt;
}
}
}

View File

@@ -2,10 +2,15 @@
namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Conference;
use App\Form\CommentType;
use App\Repository\CommentRepository;
use App\Repository\ConferenceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
@@ -13,6 +18,10 @@ use Symfony\Component\Routing\Attribute\Route;
class ConferenceController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entityManager,
) {
}
#[Route('/', name: 'homepage')]
public function index(ConferenceRepository $conferenceRepository): Response
{
@@ -21,17 +30,41 @@ class ConferenceController extends AbstractController
]);
}
#[Route('/conference/{id}', name: 'conference')]
public function show(Request $request, Conference $conference, CommentRepository $commentRepository): Response
{
#[Route('/conference/{slug}', name: 'conference')]
public function show(
Request $request,
Conference $conference,
CommentRepository $commentRepository,
ConferenceRepository $conferenceRepository,
#[Autowire('%photo_dir%')] string $photoDir,
): Response {
$comment = new Comment();
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comment->setConference($conference);
if ($photo = $form['photo']->getData()) {
$filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension();
$photo->move($photoDir, $filename);
$comment->setPhotoFilename($filename);
}
$this->entityManager->persist($comment);
$this->entityManager->flush();
return $this->redirectToRoute('conference', ['slug' => $conference->getSlug()]);
}
$offset = max(0, $request->query->getInt('offset', 0));
$paginator = $commentRepository->getCommentPaginator($conference, $offset);
return $this->render('conference/show.html.twig', [
'conferences' => $conferenceRepository->findAll(),
'conference' => $conference,
'comments' => $paginator,
'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE,
'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE),
'comment_form' => $form,
]);
}
}

View File

@@ -5,8 +5,10 @@ namespace App\Entity;
use App\Repository\CommentRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: CommentRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Comment
{
#[ORM\Id]
@@ -15,12 +17,16 @@ class Comment
private ?int $id = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private ?string $author = null;
#[ORM\Column(type: Types::TEXT)]
#[Assert\NotBlank]
private ?string $text = null;
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
#[Assert\Email]
private ?string $email = null;
#[ORM\Column]
@@ -86,6 +92,11 @@ class Comment
return $this;
}
#[ORM\PrePersist]
public function setCreatedAtValue()
{
$this->createdAt = new \DateTimeImmutable();
}
public function getConference(): ?Conference
{
return $this->conference;

View File

@@ -6,8 +6,11 @@ use App\Repository\ConferenceRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\String\Slugger\SluggerInterface;
#[ORM\Entity(repositoryClass: ConferenceRepository::class)]
#[UniqueEntity('slug')]
class Conference
{
#[ORM\Id]
@@ -30,6 +33,9 @@ class Conference
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'conference', orphanRemoval: true)]
private Collection $comments;
#[ORM\Column(length: 255, unique: true)]
private ?string $slug = null;
public function __construct()
{
$this->comments = new ArrayCollection();
@@ -44,6 +50,13 @@ class Conference
return $this->id;
}
public function computeSlug(SluggerInterface $slugger)
{
if (!$this->slug || '-' === $this->slug) {
$this->slug = (string) $slugger->slug((string) $this)->lower();
}
}
public function getCity(): ?string
{
return $this->city;
@@ -109,4 +122,16 @@ class Conference
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): static
{
$this->slug = $slug;
return $this;
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\EntityListener;
use App\Entity\Conference;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Symfony\Component\String\Slugger\SluggerInterface;
#[AsEntityListener(event: Events::prePersist, entity: Conference::class)]
#[AsEntityListener(event: Events::preUpdate, entity: Conference::class)]
class ConferenceEntityListener
{
public function __construct(
private SluggerInterface $slugger,
) {
}
public function prePersist(Conference $conference, LifecycleEventArgs $event)
{
$conference->computeSlug($this->slugger);
}
public function preUpdate(Conference $conference, LifecycleEventArgs $event)
{
$conference->computeSlug($this->slugger);
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace App\EventSubscriber;
use App\Repository\ConferenceRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Twig\Environment;
class TwigEventSubscriber implements EventSubscriberInterface
{
private $twig;
private $conferenceRepository;
public function __construct(Environment $twig, ConferenceRepository $conferenceRepository)
{
$this->twig = $twig;
$this->conferenceRepository = $conferenceRepository;
}
public function onControllerEvent(ControllerEvent $event): void
{
$this->twig->addGlobal('conferences', $this->conferenceRepository->findAll());
}
public static function getSubscribedEvents(): array
{
return [
ControllerEvent::class => 'onControllerEvent',
];
}
}

43
src/Form/CommentType.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace App\Form;
use App\Entity\Comment;
use App\Entity\Conference;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Image;
class CommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('author', null, [
'label' => 'Your name',
])
->add('text')
->add('email', EmailType::class)
->add('photo', FileType::class, [
'required' => false,
'mapped' => false,
'constraints' => [
new Image(['maxSize' => '1024k'])
],
])
->add('submit', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Comment::class,
]);
}
}

View File

@@ -16,6 +16,11 @@ class ConferenceRepository extends ServiceEntityRepository
parent::__construct($registry, Conference::class);
}
public function findAll(): array
{
return $this->findBy([], ['year' => 'ASC', 'city' => 'ASC']);
}
// /**
// * @return Conference[] Returns an array of Conference objects
// */