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-06-14 14:10:07 +02:00
|
|
|
use App\Service\PlzToCoordinate;
|
|
|
|
use App\Service\DistanceCalculator;
|
2021-07-14 13:47:42 +02:00
|
|
|
use App\Service\OfferPhotoHelper;
|
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')]
|
2022-01-12 14:42:02 +01:00
|
|
|
public function newOffer(Request $request, PlzToCoordinate $plzconverter, string $photoDir, OfferPhotoHelper $offerPhotoHelper): 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
|
|
|
|
2022-01-12 14:42:02 +01:00
|
|
|
$coordinate = $plzconverter->convertPlzToCoordinate($form['zipCode']->getData());
|
|
|
|
if ($coordinate != null) {
|
|
|
|
$offer->setCoordinate($coordinate);
|
|
|
|
}
|
|
|
|
|
2021-05-03 18:32:31 +02:00
|
|
|
if ($photo = $form['photo']->getData()) {
|
2021-07-14 13:47:42 +02:00
|
|
|
$offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer);
|
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-07-10 12:46:44 +02:00
|
|
|
public function showOffer(Offering $offer, WishRepository $wishRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response
|
2021-05-03 18:32:31 +02:00
|
|
|
{
|
2021-07-14 12:03:07 +02:00
|
|
|
$distance = null;
|
2021-06-14 14:10:07 +02:00
|
|
|
$user = $this->getUser();
|
|
|
|
$offerPlz = $offer->getZipCode();
|
|
|
|
|
|
|
|
if (isset($user))
|
|
|
|
{
|
|
|
|
$userPlz = $user->getZipCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($userPlz))
|
|
|
|
{
|
2021-12-22 17:36:12 +01:00
|
|
|
if (isset($offerPlz))
|
|
|
|
{
|
|
|
|
$offerCoordinate = $plzconverter->convertPlzToCoordinate($offerPlz);
|
|
|
|
$userCoordinate = $plzconverter->convertPlzToCoordinate($userPlz);
|
|
|
|
|
|
|
|
if ($userCoordinate != null && $offerCoordinate != null)
|
|
|
|
{
|
|
|
|
$distance = $distanceCalculator->calculateDistance($offerCoordinate, $userCoordinate);
|
|
|
|
}
|
|
|
|
}
|
2021-06-14 14:10:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 18:32:31 +02:00
|
|
|
return $this->render('app/offer.html.twig', [
|
2021-06-14 14:10:07 +02:00
|
|
|
'user' => $user,
|
2021-05-05 22:11:24 +02:00
|
|
|
'offer' => $offer,
|
|
|
|
'wishes' => $wishRepository->findByUser($offer->getByUser()),
|
2021-06-14 14:10:07 +02:00
|
|
|
'distance' => $distance,
|
2021-05-03 18:32:31 +02:00
|
|
|
]);
|
|
|
|
}
|
2021-05-09 17:23:02 +02:00
|
|
|
|
|
|
|
#[Route('/offer/edit/{id}', name: 'edit_offer')]
|
2021-07-14 13:47:42 +02:00
|
|
|
public function editOffer(Offering $offer, OfferingRepository $offerRepository, Request $request, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response
|
2021-05-09 17:23:02 +02:00
|
|
|
{
|
|
|
|
$form = $this->createForm(OfferingFormType::class, $offer);
|
2021-06-15 15:56:42 +02:00
|
|
|
$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()) {
|
2021-07-14 13:47:42 +02:00
|
|
|
$oldFilename = $offer->getPhotoFilename();
|
|
|
|
$offerPhotoHelper->uploadOfferPhoto($photoDir, $photo, $offer);
|
|
|
|
$offerPhotoHelper->deleteOfferPhoto($photoDir, $oldFilename);
|
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(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:01:41 +02:00
|
|
|
throw new HttpException(403, "No permission");
|
2021-05-09 17:23:02 +02:00
|
|
|
}
|
2021-05-15 12:18:23 +02:00
|
|
|
|
|
|
|
#[Route('/offer/delete/{id}', name: 'delete_offer')]
|
2021-07-14 13:47:42 +02:00
|
|
|
public function deleteOffer(Offering $offer, string $photoDir, OfferPhotoHelper $offerPhotoHelper): Response
|
2021-05-15 12:18:23 +02:00
|
|
|
{
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
if ($offer->getByUser() === $user)
|
|
|
|
{
|
2021-09-14 23:43:29 +02:00
|
|
|
if($offer->getPhotoFilename() != null)
|
|
|
|
{
|
|
|
|
$offerPhotoHelper->deleteOfferPhoto($photoDir, $offer->getPhotoFilename());
|
|
|
|
}
|
2021-06-15 17:24:15 +02:00
|
|
|
$this->entityManager->remove($offer);
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash(
|
|
|
|
'success','Successfully removed offer!'
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->redirectToRoute('own_offers');
|
2021-05-16 15:33:53 +02:00
|
|
|
}
|
2021-05-15 12:18:23 +02:00
|
|
|
|
2021-06-15 17:24:15 +02:00
|
|
|
throw new HttpException(403);
|
2021-05-16 15:33:53 +02:00
|
|
|
}
|
2021-05-19 13:38:37 +02:00
|
|
|
|
|
|
|
#[Route('/myoffers', name: 'own_offers')]
|
|
|
|
public function ownOffers(OfferingRepository $offeringRepository): Response
|
|
|
|
{
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
return $this->render('user/offers.html.twig', [
|
|
|
|
'user' => $user,
|
|
|
|
'offers' => $offeringRepository->findByUser($user),
|
|
|
|
]);
|
|
|
|
}
|
2021-05-03 18:32:31 +02:00
|
|
|
}
|