From bed40129718664403807199fef7691494ad32958 Mon Sep 17 00:00:00 2001 From: thisfro Date: Fri, 28 May 2021 11:23:04 +0200 Subject: [PATCH 1/2] start working on trade email sending --- src/Controller/TradeController.php | 39 +++++++++++++++++++++ templates/offer/trade/trade_email.html.twig | 5 +++ 2 files changed, 44 insertions(+) create mode 100644 src/Controller/TradeController.php create mode 100644 templates/offer/trade/trade_email.html.twig diff --git a/src/Controller/TradeController.php b/src/Controller/TradeController.php new file mode 100644 index 0000000..502cda9 --- /dev/null +++ b/src/Controller/TradeController.php @@ -0,0 +1,39 @@ +getUser(); + $offer = $offeringRepository->(); + + if(/* user not the logged in user */) + $email = (new Email()) + ->from('no-reply@pfleanz.li') + ->to(/* user of offer */) + ->replyTo(/* logged in user */) + ->subject('Time for Symfony Mailer!') + ->text('Sending emails is fun again!') + ->htmlTemplate('user/trade/offer_email.html.twig'); + try + { + $mailer->send($email); + } catch (TransportExceptionInterface $e) { + // TODO: + // print error + } + return $this->redirectToRoute('/offer', {'id': $id}); + } +} diff --git a/templates/offer/trade/trade_email.html.twig b/templates/offer/trade/trade_email.html.twig new file mode 100644 index 0000000..979ba20 --- /dev/null +++ b/templates/offer/trade/trade_email.html.twig @@ -0,0 +1,5 @@ +

{{ user.username}} wants to trade!

+ +

You can checkout their offer and reply to this email if you want to trade.

+ + \ No newline at end of file From e924c115583bc1bb1b2b6b3b465a65ccff86d648 Mon Sep 17 00:00:00 2001 From: Jannis Portmann Date: Fri, 28 May 2021 17:33:10 +0200 Subject: [PATCH 2/2] Implement basic trade offer via email --- src/Controller/TradeController.php | 48 ++++++++++++++-------- src/Repository/OfferingRepository.php | 11 +++++ templates/app/offer.html.twig | 14 ++++++- templates/user/trade/offer_email.html.twig | 5 +++ 4 files changed, 61 insertions(+), 17 deletions(-) create mode 100644 templates/user/trade/offer_email.html.twig diff --git a/src/Controller/TradeController.php b/src/Controller/TradeController.php index 502cda9..7afec17 100644 --- a/src/Controller/TradeController.php +++ b/src/Controller/TradeController.php @@ -2,38 +2,54 @@ namespace App\Controller; +use App\Entity\User; +use App\Entity\Offering; + use App\Repository\OfferingRepository; use App\Repository\WishRepository; +use Symfony\Bridge\Twig\Mime\TemplatedEmail; 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; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class TradeController extends AbstractController { #[Route('/trade/{id}', name: 'trade')] - public function sendEmail(MailerInterface $mailer, OfferingRepository $offeringRepository, WishRepository $wishRepository): Response + public function sendEmail(MailerInterface $mailer, Offering $offer, OfferingRepository $offeringRepository, WishRepository $wishRepository): Response { $user = $this->getUser(); - $offer = $offeringRepository->(); - if(/* user not the logged in user */) - $email = (new Email()) - ->from('no-reply@pfleanz.li') - ->to(/* user of offer */) - ->replyTo(/* logged in user */) - ->subject('Time for Symfony Mailer!') - ->text('Sending emails is fun again!') - ->htmlTemplate('user/trade/offer_email.html.twig'); - try + if($user != $offer->getByUser()) { - $mailer->send($email); - } catch (TransportExceptionInterface $e) { - // TODO: - // print error + $email = (new TemplatedEmail()) + ->from('no-reply@pfleanz.li') + ->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, + 'userOffersLink' => $this->generateUrl('user_public', [ + 'id' => $offer->getByUser()->getId(), + ], UrlGeneratorInterface::ABSOLUTE_URL) + ]) + ; + 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!'); } - return $this->redirectToRoute('/offer', {'id': $id}); + + return $this->redirectToRoute('show_offer', ['id' => $offer->getId()]); } } diff --git a/src/Repository/OfferingRepository.php b/src/Repository/OfferingRepository.php index 13c715b..cf5c531 100644 --- a/src/Repository/OfferingRepository.php +++ b/src/Repository/OfferingRepository.php @@ -33,6 +33,17 @@ class OfferingRepository extends ServiceEntityRepository ; } + public function findById($id) + { + return $this->createQueryBuilder('o') + ->andWhere('o.Id = :val') + ->setParameter('val', $id) + ->orderBy('o.id', 'ASC') + ->getQuery() + ->getResult() + ; + } + /* public function findOneBySomeField($value): ?Offering { diff --git a/templates/app/offer.html.twig b/templates/app/offer.html.twig index 2d75daa..fbf2e23 100644 --- a/templates/app/offer.html.twig +++ b/templates/app/offer.html.twig @@ -1,6 +1,18 @@ {% extends 'base.html.twig' %} {% block body %} + {% for message in app.flashes('error') %} + + {% endfor %} + + {% for message in app.flashes('success') %} +
+ {{ message }} +
+ {% endfor %} + {% if offer.byUser == user %} {% endif %} @@ -58,6 +70,6 @@ {% endif %} - +
Offer trade {% endif %} {% endblock %} diff --git a/templates/user/trade/offer_email.html.twig b/templates/user/trade/offer_email.html.twig new file mode 100644 index 0000000..c7da0a7 --- /dev/null +++ b/templates/user/trade/offer_email.html.twig @@ -0,0 +1,5 @@ +

{{ user.username }} wants to trade!

+ +

Checkout {{ user.username}}'s offers:

+Link +

Reply to this email to start trading.

\ No newline at end of file