This repository has been archived on 2024-10-16. You can view files and clone it, but cannot push or open issues or pull requests.
pflaenz.li-Symfony/src/Controller/OfferController.php

84 lines
2.6 KiB
PHP
Raw Normal View History

2021-05-03 18:32:31 +02:00
<?php
namespace App\Controller;
use App\Entity\Offering;
use App\Form\OfferingFormType;
use App\Repository\OfferingRepository;
2021-05-04 12:33:58 +02:00
use App\Repository\WishRepository;
2021-05-03 18:32:31 +02:00
2021-05-04 11:52:51 +02:00
use Doctrine\ORM\EntityManagerInterface;
2021-05-03 18:32:31 +02:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
class OfferController extends AbstractController
{
2021-05-04 11:52:51 +02:00
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
2021-05-09 11:20:45 +02:00
#[Route('/offers', name: 'offers')]
2021-05-05 22:11:24 +02:00
public function index(Environment $twig, OfferingRepository $offerRepository): Response
2021-05-03 18:32:31 +02:00
{
return new Response($twig->render('app/index.html.twig', [
2021-05-05 22:11:24 +02:00
'offers' => $offerRepository->findAll(),
2021-05-03 18:32:31 +02:00
]));
}
2021-05-05 22:11:24 +02:00
#[Route('/new', name: 'new_offer')]
public function newOffer(Request $request, string $photoDir): Response
2021-05-03 18:32:31 +02:00
{
2021-05-05 22:11:24 +02:00
$offer = new Offering();
$form = $this->createForm(OfferingFormType::class, $offer);
2021-05-03 18:32:31 +02:00
$user = $this->getUser();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
2021-05-05 22:11:24 +02:00
$offer->setByUser($user);
$offer->setCreatedAt(new \DateTime());
2021-05-03 18:32:31 +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);
2021-05-05 22:11:24 +02:00
return $this->redirectToRoute('new_offer');
2021-05-03 18:32:31 +02:00
}
2021-05-05 22:11:24 +02:00
$offer->setPhotoFilename($filename);
2021-05-03 18:32:31 +02:00
}
2021-05-05 22:11:24 +02:00
$this->entityManager->persist($offer);
2021-05-03 18:32:31 +02:00
$this->entityManager->flush();
2021-05-05 22:11:24 +02:00
$this->addFlash("success", "Successfully added the new offer!");
2021-05-09 11:20:45 +02:00
return $this->redirectToRoute('offers');
2021-05-03 18:32:31 +02:00
}
2021-05-05 22:11:24 +02:00
return $this->render('app/new_offer.html.twig', [
2021-05-03 18:32:31 +02:00
'user' => $this->getUser(),
2021-05-05 22:11:24 +02:00
'offer_form' => $form->createView(),
2021-05-03 18:32:31 +02:00
]);
}
#[Route('/offer/{id}', name: 'show_offer')]
2021-05-05 22:11:24 +02:00
public function show_offer(Offering $offer, WishRepository $wishRepository): Response
2021-05-03 18:32:31 +02:00
{
return $this->render('app/offer.html.twig', [
2021-05-09 11:20:45 +02:00
'user' => $this->getUser(),
2021-05-05 22:11:24 +02:00
'offer' => $offer,
'wishes' => $wishRepository->findByUser($offer->getByUser()),
2021-05-03 18:32:31 +02:00
]);
}
}