|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file was created by developers working at BitBag |
|
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
|
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace BitBag\SyliusWishlistPlugin\Services\Exporter; |
|
12
|
|
|
|
|
13
|
|
|
use BitBag\SyliusWishlistPlugin\Command\Wishlist\WishlistItemInterface; |
|
14
|
|
|
use BitBag\SyliusWishlistPlugin\Exception\ProductVariantNotFoundException; |
|
15
|
|
|
use BitBag\SyliusWishlistPlugin\Model\Factory\VariantPdfModelFactoryInterface; |
|
16
|
|
|
use BitBag\SyliusWishlistPlugin\Resolver\VariantImagePathResolverInterface; |
|
17
|
|
|
use Doctrine\Common\Collections\Collection; |
|
18
|
|
|
use Dompdf\Dompdf; |
|
19
|
|
|
use Dompdf\Options; |
|
20
|
|
|
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
|
23
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
24
|
|
|
use Twig\Environment; |
|
25
|
|
|
|
|
26
|
|
|
final class ExporterWishlistToPdf implements ExporterWishlistToPdfInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private bool $isSelected = false; |
|
29
|
|
|
|
|
30
|
|
|
private ProductVariantRepositoryInterface $productVariantRepository; |
|
31
|
|
|
|
|
32
|
|
|
private VariantImagePathResolverInterface $variantImagePathResolver; |
|
33
|
|
|
|
|
34
|
|
|
private VariantPdfModelFactoryInterface $variantPdfModelFactory; |
|
35
|
|
|
|
|
36
|
|
|
private Environment $twigEnvironment; |
|
37
|
|
|
|
|
38
|
|
|
private FlashBagInterface $flashBag; |
|
39
|
|
|
|
|
40
|
|
|
private TranslatorInterface $translator; |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
public function __construct( |
|
44
|
|
|
ProductVariantRepositoryInterface $productVariantRepository, |
|
45
|
|
|
VariantImagePathResolverInterface $variantImagePathResolver, |
|
46
|
|
|
VariantPdfModelFactoryInterface $variantPdfModelFactory, |
|
47
|
|
|
Environment $twigEnvironment, |
|
48
|
|
|
FlashBagInterface $flashBag, |
|
49
|
|
|
TranslatorInterface $translator |
|
50
|
|
|
) { |
|
51
|
|
|
$this->productVariantRepository = $productVariantRepository; |
|
52
|
|
|
$this->variantImagePathResolver = $variantImagePathResolver; |
|
53
|
|
|
$this->variantPdfModelFactory = $variantPdfModelFactory; |
|
54
|
|
|
$this->twigEnvironment = $twigEnvironment; |
|
55
|
|
|
$this->flashBag = $flashBag; |
|
56
|
|
|
$this->translator = $translator; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function createModelToPdfAndExportToPdf(Collection $wishlistProducts, Request $request): void |
|
60
|
|
|
{ |
|
61
|
|
|
$selectedProducts = []; |
|
62
|
|
|
|
|
63
|
|
|
/** @var WishlistItemInterface $wishlistProduct */ |
|
64
|
|
|
foreach ($wishlistProducts as $wishlistProduct) { |
|
65
|
|
|
if ($wishlistProduct->isSelected()) { |
|
66
|
|
|
$this->isSelected = true; |
|
67
|
|
|
|
|
68
|
|
|
$variant = $this->productVariantRepository->find($wishlistProduct->getWishlistProduct()->getVariant()); |
|
69
|
|
|
|
|
70
|
|
|
if (null === $variant || null === $wishlistProduct) { |
|
71
|
|
|
throw new ProductVariantNotFoundException( |
|
72
|
|
|
sprintf('The Product does not exist') |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$cartItem = $wishlistProduct->getCartItem()->getCartItem(); |
|
77
|
|
|
$quantity = $cartItem->getQuantity(); |
|
78
|
|
|
$baseUrl = $request->getSchemeAndHttpHost(); |
|
79
|
|
|
$urlToImage = $this->variantImagePathResolver->resolve($variant, $baseUrl); |
|
80
|
|
|
$actualVariant = $cartItem->getVariant()->getCode(); |
|
81
|
|
|
$selectedProducts[] = $this->variantPdfModelFactory->createWithVariantAndImagePath( |
|
82
|
|
|
$variant, |
|
83
|
|
|
$urlToImage, |
|
84
|
|
|
$quantity, |
|
85
|
|
|
$actualVariant |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
$this->exportToPdf($selectedProducts); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private function exportToPdf(array $selectedProducts): void |
|
93
|
|
|
{ |
|
94
|
|
|
if ( $this->isSelected === false) { |
|
95
|
|
|
$this->flashBag->add( |
|
96
|
|
|
'error', |
|
97
|
|
|
$this->translator->trans('bitbag_sylius_wishlist_plugin.ui.select_products') |
|
98
|
|
|
); |
|
99
|
|
|
return ; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$pdfOptions = new Options(); |
|
103
|
|
|
$pdfOptions->set('isRemoteEnabled', true); |
|
104
|
|
|
$pdfOptions->set('defaultFont', 'Arial'); |
|
105
|
|
|
$dompdf = new Dompdf($pdfOptions); |
|
106
|
|
|
$html = $this->twigEnvironment->render('@BitBagSyliusWishlistPlugin/_wishlist_pdf.html.twig', [ |
|
107
|
|
|
'title' => 'My wishlist products', |
|
108
|
|
|
'date' => date('d.m.Y'), |
|
109
|
|
|
'products' => $selectedProducts, |
|
110
|
|
|
]); |
|
111
|
|
|
$dompdf->loadHtml($html); |
|
112
|
|
|
$dompdf->setPaper('A4', 'portrait'); |
|
113
|
|
|
$dompdf->render(); |
|
114
|
|
|
$dompdf->stream('wishlist.pdf', ['Attachment' => true]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
} |