72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Controller;
|
||
|
|
||
|
use App\Entity\Offering;
|
||
|
use App\Form\OfferingFormType;
|
||
|
|
||
|
use App\Repository\OfferingRepository;
|
||
|
|
||
|
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
|
||
|
{
|
||
|
#[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,
|
||
|
]);
|
||
|
}
|
||
|
}
|