fix emails and some other stuff

This commit is contained in:
Constantin Simonis 2025-02-13 21:42:39 +01:00
parent fcabbaae65
commit 2b96383433
Signed by: csimonis
GPG Key ID: 3878FF77C24AF4D2
5 changed files with 28 additions and 48 deletions

View File

@ -1,24 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use chillerlan\QRCode\QRCode;
use Nucleos\DompdfBundle\Factory\DompdfFactory;
use Nucleos\DompdfBundle\Wrapper\DompdfWrapper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class TestController extends AbstractController
{
#[Route('/test')]
public function index(Filesystem $filesystem): Response
{
$filesystem->dumpFile('test.pdf', $content);
return $this->render('test.html.twig', ['qr' => (new QRCode())->render('https://www.google.com')]);
}
}

View File

@ -6,7 +6,7 @@ class TicketData
{ {
public const TICKET_DATA = [ public const TICKET_DATA = [
1 => [ 1 => [
'name' => 'Ticket', 'name' => 'All-Inclusive Ticket',
'price' => 50, 'price' => 50,
], ],
2 => [ 2 => [

View File

@ -7,6 +7,7 @@ use App\Controller\Admin\TicketCrudController;
use App\Entity\Payment; use App\Entity\Payment;
use App\Entity\Ticket; use App\Entity\Ticket;
use chillerlan\QRCode\QRCode; use chillerlan\QRCode\QRCode;
use Doctrine\Common\Collections\Collection;
use Nucleos\DompdfBundle\Factory\DompdfFactory; use Nucleos\DompdfBundle\Factory\DompdfFactory;
use Nucleos\DompdfBundle\Wrapper\DompdfWrapper; use Nucleos\DompdfBundle\Wrapper\DompdfWrapper;
use Symfony\Bridge\Twig\Mime\TemplatedEmail; use Symfony\Bridge\Twig\Mime\TemplatedEmail;
@ -37,28 +38,14 @@ class TicketEmailService
->to(new Address($payment->getCustomer()?->getEmail(), $payment->getCustomer()?->getFirstname() . ' ' . $payment->getCustomer()?->getLastname())) ->to(new Address($payment->getCustomer()?->getEmail(), $payment->getCustomer()?->getFirstname() . ' ' . $payment->getCustomer()?->getLastname()))
->context([ ->context([
'payment' => $payment, 'payment' => $payment,
]); ])
->addPart(new DataPart($this->generateTicket($payment->getCustomer()?->getTickets()), 'ticket.pdf', 'application/pdf'));
$i = 0;
foreach ($payment->getCustomer()?->getTickets() as $ticket) {
++$i;
$mail->addPart(new DataPart($this->generateTicket($ticket), "ticket-$i.pdf", 'application/pdf'));
}
$this->mailer->send($mail); $this->mailer->send($mail);
} }
private function generateTicket(Ticket $ticket): string private function generateTicket(Collection $tickets): string
{ {
return (new DompdfWrapper(new DompdfFactory()))->getPdf($this->twig->render('test.html.twig', ['qr' => (new QRCode())->render($this->generateUrl($ticket))])); return (new DompdfWrapper(new DompdfFactory()))->getPdf($this->twig->render('test.html.twig', ['tickets' => $tickets]));
}
private function generateUrl(Ticket $ticket): string
{
return $this->urlGenerator->generate('admin', [
'crudAction' => 'detail',
'crudControllerFqcn' => TicketCrudController::class,
'entityId' => $ticket->getCustomer()?->getId()
], UrlGeneratorInterface::ABSOLUTE_URL);
} }
} }

View File

@ -2,20 +2,26 @@
namespace App\Twig; namespace App\Twig;
use App\Controller\Admin\TicketCrudController;
use App\Enum\FoodData; use App\Enum\FoodData;
use App\Enum\TicketData; use App\Enum\TicketData;
use chillerlan\QRCode\QRCode;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFilter; use Twig\TwigFilter;
use Twig\TwigFunction;
class Ticket extends AbstractExtension class Ticket extends AbstractExtension
{ {
public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
{
}
public function getFilters(): array public function getFilters(): array
{ {
return [ return [
new TwigFilter('food', $this->getFoodName(...)), new TwigFilter('food', $this->getFoodName(...)),
new TwigFilter('ticket', $this->getTicket(...)), new TwigFilter('ticket', $this->getTicket(...)),
new TwigFilter('qr', $this->generateUrl(...)),
]; ];
} }
@ -28,4 +34,13 @@ class Ticket extends AbstractExtension
{ {
return TicketData::TICKET_DATA[$id] ?? ['name' => 'N/A', 'price' => 'N/A']; return TicketData::TICKET_DATA[$id] ?? ['name' => 'N/A', 'price' => 'N/A'];
} }
public function generateUrl(\App\Entity\Ticket $ticket): string
{
return (new QRCode)->render($this->urlGenerator->generate('admin', [
'crudAction' => 'detail',
'crudControllerFqcn' => TicketCrudController::class,
'entityId' => $ticket->getCustomer()?->getId()
], UrlGeneratorInterface::ABSOLUTE_URL));
}
} }

File diff suppressed because one or more lines are too long