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

@@ -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,
]);
}
}