Compare commits
2 Commits
31f65e2489
...
fdae2ae025
| Author | SHA1 | Date | |
|---|---|---|---|
| fdae2ae025 | |||
| d090e3f36c |
@@ -2,9 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Entity\Comment;
|
||||||
use App\Entity\Conference;
|
use App\Entity\Conference;
|
||||||
|
use App\Form\CommentType;
|
||||||
use App\Repository\CommentRepository;
|
use App\Repository\CommentRepository;
|
||||||
use App\Repository\ConferenceRepository;
|
use App\Repository\ConferenceRepository;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
@@ -13,6 +16,11 @@ use Twig\Environment;
|
|||||||
|
|
||||||
class ConferenceController extends AbstractController
|
class ConferenceController extends AbstractController
|
||||||
{
|
{
|
||||||
|
public function __construct(
|
||||||
|
private EntityManagerInterface $entityManager,
|
||||||
|
){
|
||||||
|
}
|
||||||
|
|
||||||
#[Route('/', name: 'homepage')]
|
#[Route('/', name: 'homepage')]
|
||||||
public function index(ConferenceRepository $conferenceRepository): Response
|
public function index(ConferenceRepository $conferenceRepository): Response
|
||||||
{
|
{
|
||||||
@@ -24,6 +32,18 @@ class ConferenceController extends AbstractController
|
|||||||
#[Route('/conference/{slug}', name: 'conference')]
|
#[Route('/conference/{slug}', name: 'conference')]
|
||||||
public function show(Request $request, Conference $conference, CommentRepository $commentRepository): Response
|
public function show(Request $request, Conference $conference, CommentRepository $commentRepository): Response
|
||||||
{
|
{
|
||||||
|
$comment = new Comment();
|
||||||
|
$form = $this->createForm(CommentType::class, $comment);
|
||||||
|
$form->handleRequest($request);
|
||||||
|
if($form->isSubmitted() && $form->isValid()) {
|
||||||
|
$comment->setConference($conference);
|
||||||
|
dump($comment);
|
||||||
|
$this->entityManager->persist($comment);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
return $this->redirectToRoute('conference', ['slug' => $conference->getSlug()]);
|
||||||
|
}
|
||||||
|
|
||||||
$offset = max(0, $request->query->getInt('offset', 0));
|
$offset = max(0, $request->query->getInt('offset', 0));
|
||||||
$paginator = $commentRepository->getCommentPaginator($conference, $offset);
|
$paginator = $commentRepository->getCommentPaginator($conference, $offset);
|
||||||
|
|
||||||
@@ -31,7 +51,8 @@ class ConferenceController extends AbstractController
|
|||||||
'conference' => $conference,
|
'conference' => $conference,
|
||||||
'comments' => $paginator,
|
'comments' => $paginator,
|
||||||
'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE,
|
'previous' => $offset - CommentRepository::COMMENTS_PER_PAGE,
|
||||||
'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE)
|
'next' => min(count($paginator), $offset + CommentRepository::COMMENTS_PER_PAGE),
|
||||||
|
'comment_form' => $form,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace App\Entity;
|
|||||||
use App\Repository\CommentRepository;
|
use App\Repository\CommentRepository;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use Symphony\Component\Validator\Constraints as Assert;
|
||||||
|
|
||||||
#[ORM\Entity(repositoryClass: CommentRepository::class)]
|
#[ORM\Entity(repositoryClass: CommentRepository::class)]
|
||||||
#[ORM\HasLifecycleCallbacks]
|
#[ORM\HasLifecycleCallbacks]
|
||||||
@@ -16,12 +17,16 @@ class Comment
|
|||||||
private ?int $id = null;
|
private ?int $id = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 255)]
|
#[ORM\Column(length: 255)]
|
||||||
|
#[Assert\NotBlank]
|
||||||
private ?string $author = null;
|
private ?string $author = null;
|
||||||
|
|
||||||
#[ORM\Column(type: Types::TEXT)]
|
#[ORM\Column(type: Types::TEXT)]
|
||||||
|
#[Assert\NotBlank]
|
||||||
private ?string $text = null;
|
private ?string $text = null;
|
||||||
|
|
||||||
#[ORM\Column(length: 255)]
|
#[ORM\Column(length: 255)]
|
||||||
|
#[Assert\NotBlank]
|
||||||
|
#[Assert\Email]
|
||||||
private ?string $email = null;
|
private ?string $email = null;
|
||||||
|
|
||||||
#[ORM\Column]
|
#[ORM\Column]
|
||||||
|
|||||||
43
src/Form/CommentType.php
Normal file
43
src/Form/CommentType.php
Normal 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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
{% block title %}Conference Guestbook - {{ conference }}{% endblock %}
|
{% block title %}Conference Guestbook - {{ conference }}{% endblock %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<h2>{{ conference }} Conference<h2>
|
<h2>{{ conference }} Conference</h2>
|
||||||
|
|
||||||
{% if comments|length > 0 %}
|
{% if comments|length > 0 %}
|
||||||
<div>There are {{ comments|length }} comments.</div>
|
<div>There are {{ comments|length }} comments.</div>
|
||||||
@@ -30,4 +30,8 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<div>No comments have been posted yet for this conference.</div>
|
<div>No comments have been posted yet for this conference.</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
<h2>Add your own feedback</h2>
|
||||||
|
|
||||||
|
{{ form(comment_form) }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user