Implement basic trade offer via email
This commit is contained in:
		
							parent
							
								
									bed4012971
								
							
						
					
					
						commit
						e924c11558
					
				
					 4 changed files with 61 additions and 17 deletions
				
			
		| 
						 | 
					@ -2,38 +2,54 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace App\Controller;
 | 
					namespace App\Controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use App\Entity\User;
 | 
				
			||||||
 | 
					use App\Entity\Offering;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use App\Repository\OfferingRepository;
 | 
					use App\Repository\OfferingRepository;
 | 
				
			||||||
use App\Repository\WishRepository;
 | 
					use App\Repository\WishRepository;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					use Symfony\Bridge\Twig\Mime\TemplatedEmail;
 | 
				
			||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 | 
					use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
 | 
				
			||||||
use Symfony\Component\HttpFoundation\Response;
 | 
					use Symfony\Component\HttpFoundation\Response;
 | 
				
			||||||
use Symfony\Component\Mailer\MailerInterface;
 | 
					use Symfony\Component\Mailer\MailerInterface;
 | 
				
			||||||
use Symfony\Component\Mime\Email;
 | 
					use Symfony\Component\Mime\Email;
 | 
				
			||||||
use Symfony\Component\Routing\Annotation\Route;
 | 
					use Symfony\Component\Routing\Annotation\Route;
 | 
				
			||||||
 | 
					use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TradeController extends AbstractController
 | 
					class TradeController extends AbstractController
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    #[Route('/trade/{id}', name: 'trade')]
 | 
					    #[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();
 | 
					        $user = $this->getUser();
 | 
				
			||||||
        $offer = $offeringRepository->();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if(/* user not the logged in user */)
 | 
					        if($user != $offer->getByUser())
 | 
				
			||||||
        $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);
 | 
					            $email = (new TemplatedEmail())
 | 
				
			||||||
        } catch (TransportExceptionInterface $e) {
 | 
					                ->from('no-reply@pfleanz.li')
 | 
				
			||||||
            // TODO:
 | 
					                ->to($offer->getByUser()->getEmail())
 | 
				
			||||||
            // print error
 | 
					                ->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()]);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
					    public function findOneBySomeField($value): ?Offering
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,18 @@
 | 
				
			||||||
{% extends 'base.html.twig' %}
 | 
					{% extends 'base.html.twig' %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{% block body %}
 | 
					{% block body %}
 | 
				
			||||||
 | 
					    {% for message in app.flashes('error') %}
 | 
				
			||||||
 | 
					        <div class="alert alert-danger" role="alert">
 | 
				
			||||||
 | 
					            {{ message }}
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    {% endfor %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    {% for message in app.flashes('success') %}
 | 
				
			||||||
 | 
					        <div class="alert alert-success" role="success">
 | 
				
			||||||
 | 
					            {{ message }}
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					    {% endfor %}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    {% if offer.byUser == user %}
 | 
					    {% if offer.byUser == user %}
 | 
				
			||||||
        <div class="alert alert-info" role="alert">This is your offer!</div>
 | 
					        <div class="alert alert-info" role="alert">This is your offer!</div>
 | 
				
			||||||
    {% endif %}
 | 
					    {% endif %}
 | 
				
			||||||
| 
						 | 
					@ -58,6 +70,6 @@
 | 
				
			||||||
                </ul>
 | 
					                </ul>
 | 
				
			||||||
            {% endif %}
 | 
					            {% endif %}
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
            <button class="btn btn-primary">Offer trade</button>
 | 
					            <a class="btn btn-primary" href="{{ path('trade', {'id': offer.id}) }}">Offer trade</a>
 | 
				
			||||||
        {% endif %}
 | 
					        {% endif %}
 | 
				
			||||||
{% endblock %}
 | 
					{% endblock %}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										5
									
								
								templates/user/trade/offer_email.html.twig
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								templates/user/trade/offer_email.html.twig
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,5 @@
 | 
				
			||||||
 | 
					<h1>{{ user.username }} wants to trade!</h1>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					<p>Checkout {{ user.username}}'s offers:</p>
 | 
				
			||||||
 | 
					<a href="{{ userOffersLink }}">Link</a>
 | 
				
			||||||
 | 
					<p>Reply to this email to start trading.</p>
 | 
				
			||||||
		Reference in a new issue