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/TradeController.php

61 lines
2.1 KiB
PHP
Raw Normal View History

2021-05-28 11:23:04 +02:00
<?php
namespace App\Controller;
2021-05-28 17:33:10 +02:00
use App\Entity\User;
use App\Entity\Offering;
2021-05-28 11:23:04 +02:00
use App\Repository\OfferingRepository;
use App\Repository\WishRepository;
2021-05-28 17:33:10 +02:00
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
2021-05-28 11:23:04 +02:00
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
2021-05-28 17:33:10 +02:00
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2021-06-01 14:05:04 +02:00
use Symfony\Component\Routing\RouterInterface;
2021-05-28 11:23:04 +02:00
2021-05-29 19:45:45 +02:00
2021-05-28 11:23:04 +02:00
class TradeController extends AbstractController
{
#[Route('/trade/{id}', name: 'trade')]
2021-06-01 14:05:04 +02:00
public function sendEmail(MailerInterface $mailer, Offering $offer, OfferingRepository $offeringRepository, WishRepository $wishRepository, RouterInterface $router): Response
2021-05-28 11:23:04 +02:00
{
$user = $this->getUser();
2021-06-01 14:05:04 +02:00
$this->router = $router;
$mail_url = $this->router->generate('user_public', [
2021-05-29 19:45:45 +02:00
'id' => $offer->getByUser()->getId(),
], UrlGeneratorInterface::ABSOLUTE_URL);
2021-05-28 11:23:04 +02:00
2021-05-28 17:33:10 +02:00
if($user != $offer->getByUser())
2021-05-28 11:23:04 +02:00
{
2021-05-28 17:33:10 +02:00
$email = (new TemplatedEmail())
2021-05-28 17:43:40 +02:00
->from('no-reply@thisfro.ch')
2021-05-28 17:33:10 +02:00
->to($offer->getByUser()->getEmail())
->replyTo($user->getEmail())
->subject($user->getUsername() . ' wants to trade with you!')
->text('Trade offer')
->htmlTemplate('user/trade/offer_email.html.twig')
->context([
'user' => $user,
2021-05-29 09:02:25 +02:00
'id' => $offer->getByUser()->getId(),
2021-05-29 19:45:45 +02:00
'userOffersLink' => $mail_url,
2021-05-28 17:33:10 +02:00
])
;
try
{
$mailer->send($email);
$this->addFlash('success', 'Trade offer sent!');
} catch (TransportExceptionInterface $e) {
$this->addFlash('error', 'There was an error: ' . $e);
}
} else {
$this->addFlash('error','You can\'t trade with yourself!');
2021-05-28 11:23:04 +02:00
}
2021-05-28 17:33:10 +02:00
return $this->redirectToRoute('show_offer', ['id' => $offer->getId()]);
2021-05-28 11:23:04 +02:00
}
}