pflaenz.li-Symfony/src/Controller/AppController.php

87 lines
2.7 KiB
PHP
Raw Normal View History

2021-04-22 17:44:16 +02:00
<?php
namespace App\Controller;
2021-04-28 00:15:13 +02:00
use App\Entity\Offering;
use App\Form\OfferingFormType;
2021-04-26 17:31:24 +02:00
use App\Repository\OfferingRepository;
2021-04-30 17:08:34 +02:00
use Doctrine\ORM\EntityManagerInterface;
2021-04-22 17:44:16 +02:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2021-04-30 17:08:34 +02:00
use Symfony\Component\HttpFoundation\Request;
2021-04-22 17:44:16 +02:00
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
2021-04-26 17:31:24 +02:00
use Twig\Environment;
2021-04-22 17:44:16 +02:00
class AppController extends AbstractController
{
2021-04-30 17:08:34 +02:00
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
2021-04-26 17:31:24 +02:00
#[Route('/', name: 'homepage')]
public function index(Environment $twig, OfferingRepository $offeringRepository): Response
2021-04-22 17:44:16 +02:00
{
2021-04-26 17:31:24 +02:00
return new Response($twig->render('app/index.html.twig', [
'offerings' => $offeringRepository->findAll(),
]));
2021-04-22 17:44:16 +02:00
}
2021-04-23 15:06:41 +02:00
#[Route('/user', name: 'user_page')]
public function user(): Response
{
2021-04-26 17:31:24 +02:00
return $this->render('app/user.html.twig', [
2021-04-23 15:06:41 +02:00
'user' => $this->getUser(),
]);
}
2021-04-28 00:15:13 +02:00
#[Route('/new', name: 'new_listing')]
2021-04-30 19:22:50 +02:00
public function new_listing(Request $request, string $photoDir): Response
2021-04-28 00:15:13 +02:00
{
$offering = new Offering();
$form = $this->createForm(OfferingFormType::class, $offering);
2021-04-30 17:08:34 +02:00
$user = $this->getUser();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$offering->setByUser($user);
$offering->setCreatedAt(new \DateTime());
2021-04-28 00:15:13 +02:00
2021-04-30 19:22:50 +02:00
if ($photo = $form['photo']->getData()) {
$filename = bin2hex(random_bytes(6)).'.'.$photo->guessExtension();
try {
$photo->move($photoDir, $filename);
} catch (FileException $e) {
// unable to upload the photo, give up
$this->addFlash("error", "There was an error uploading the photo: ".$e);
return $this->redirectToRoute('new_listing');
}
$offering->setPhotoFilename($filename);
}
2021-04-30 17:08:34 +02:00
$this->entityManager->persist($offering);
$this->entityManager->flush();
$this->addFlash("success", "Successfully added the new offering!");
return $this->redirectToRoute('homepage');
2021-04-30 17:08:34 +02:00
}
2021-04-28 00:15:13 +02:00
return $this->render('app/new_listing.html.twig', [
'user' => $this->getUser(),
'offering_form' => $form->createView(),
]);
}
2021-04-30 19:22:50 +02:00
#[Route('/offer/{id}', name: 'show_offer')]
2021-05-01 15:33:45 +02:00
public function show_offer(Offering $offering): Response
2021-04-30 19:22:50 +02:00
{
return $this->render('app/offer.html.twig', [
2021-05-01 15:33:45 +02:00
'offer' => $offering,
2021-04-30 19:22:50 +02:00
]);
}
2021-04-22 17:44:16 +02:00
}