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;
|
2021-05-09 17:53:41 +02:00
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
2021-05-03 18:32:31 +02:00
|
|
|
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
|
|
|
{
|
2021-05-09 17:23:02 +02:00
|
|
|
return new Response($twig->render('offer/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
|
|
|
]);
|
|
|
|
}
|
2021-05-09 17:23:02 +02:00
|
|
|
|
|
|
|
#[Route('/offer/edit/{id}', name: 'edit_offer')]
|
|
|
|
public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir): Response
|
|
|
|
{
|
|
|
|
$form = $this->createForm(OfferingFormType::class, $offer);
|
|
|
|
$user = $this->getUser();
|
2021-05-09 17:53:41 +02:00
|
|
|
if ($offer->getByUser() === $user)
|
|
|
|
{
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$offer->setByUser($user);
|
|
|
|
$offer->setCreatedAt(new \DateTime());
|
|
|
|
|
|
|
|
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_offer');
|
|
|
|
}
|
|
|
|
$offer->setPhotoFilename($filename);
|
2021-05-09 17:23:02 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 17:53:41 +02:00
|
|
|
$this->entityManager->persist($offer);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
}
|
2021-05-09 17:23:02 +02:00
|
|
|
|
2021-05-09 17:53:41 +02:00
|
|
|
return $this->render('offer/edit.html.twig', [
|
|
|
|
'user' => $this->getUser(),
|
|
|
|
'offer' => $offer,
|
|
|
|
'offer_form' => $form->createView(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new HttpException(403, "No permisison");
|
2021-05-09 17:23:02 +02:00
|
|
|
}
|
2021-05-03 18:32:31 +02:00
|
|
|
}
|