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 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-03 18:32:31 +02:00
|
|
|
#[Route('/', name: 'homepage')]
|
|
|
|
public function index(Environment $twig, OfferingRepository $offeringRepository): Response
|
|
|
|
{
|
|
|
|
return new Response($twig->render('app/index.html.twig', [
|
|
|
|
'offerings' => $offeringRepository->findAll(),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Route('/new', name: 'new_listing')]
|
|
|
|
public function new_listing(Request $request, string $photoDir): Response
|
|
|
|
{
|
|
|
|
$offering = new Offering();
|
|
|
|
$form = $this->createForm(OfferingFormType::class, $offering);
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$offering->setByUser($user);
|
|
|
|
$offering->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_listing');
|
|
|
|
}
|
|
|
|
$offering->setPhotoFilename($filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->entityManager->persist($offering);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
|
|
|
|
$this->addFlash("success", "Successfully added the new offering!");
|
|
|
|
return $this->redirectToRoute('homepage');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('app/new_listing.html.twig', [
|
|
|
|
'user' => $this->getUser(),
|
|
|
|
'offering_form' => $form->createView(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[Route('/offer/{id}', name: 'show_offer')]
|
|
|
|
public function show_offer(Offering $offering): Response
|
|
|
|
{
|
|
|
|
return $this->render('app/offer.html.twig', [
|
|
|
|
'offer' => $offering,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|