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\SyliusCmsPlugin\Twig\Extension; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Entity\Page; |
14
|
|
|
use BitBag\SyliusCmsPlugin\Repository\PageRepositoryInterface; |
15
|
|
|
use BitBag\SyliusCmsPlugin\Sorter\SectionsSorter; |
16
|
|
|
use BitBag\SyliusCmsPlugin\Twig\Runtime\RenderProductPagesRuntime; |
17
|
|
|
use DateTimeImmutable; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Exception; |
20
|
|
|
use Psr\Container\ContainerExceptionInterface; |
21
|
|
|
use Psr\Container\ContainerInterface; |
22
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
23
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
24
|
|
|
use Twig\Environment; |
25
|
|
|
use Twig\Error\LoaderError; |
26
|
|
|
use Twig\Error\RuntimeError; |
27
|
|
|
use Twig\Error\SyntaxError; |
28
|
|
|
use Twig\Extension\AbstractExtension; |
29
|
|
|
use Twig\TwigFunction; |
30
|
|
|
|
31
|
|
|
final class RenderProductPagesExtension extends AbstractExtension |
32
|
|
|
{ |
33
|
|
|
/** @var ContainerInterface */ |
34
|
|
|
private $container; |
35
|
|
|
|
36
|
|
|
/** @var EntityManagerInterface */ |
37
|
|
|
private $entityManager; |
38
|
|
|
|
39
|
|
|
/** @var Environment */ |
40
|
|
|
private $environment; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
|
|
ContainerInterface $container, |
44
|
|
|
EntityManagerInterface $entityManager, |
45
|
|
|
Environment $environment |
46
|
|
|
) { |
47
|
|
|
$this->container = $container; |
48
|
|
|
$this->entityManager = $entityManager; |
49
|
|
|
$this->environment = $environment; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getFunctions(): array |
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
new TwigFunction('bitbag_cms_render_product_pages', [RenderProductPagesRuntime::class, 'renderProductPages'], ['is_safe' => ['html']]), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws NotFoundExceptionInterface |
61
|
|
|
* @throws SyntaxError |
62
|
|
|
* @throws ContainerExceptionInterface |
63
|
|
|
* @throws RuntimeError |
64
|
|
|
* @throws LoaderError |
65
|
|
|
* @throws Exception |
66
|
|
|
*/ |
67
|
|
|
public function renderProductPages( |
68
|
|
|
ProductInterface $product, |
69
|
|
|
?string $sectionCode = null, |
70
|
|
|
?string $date = null |
71
|
|
|
): string { |
72
|
|
|
$channelCode = $this->container->get('sylius.context.channel')->getChannel(); |
73
|
|
|
|
74
|
|
|
$parsedDate = null; |
75
|
|
|
if (null !== $date) { |
76
|
|
|
$parsedDate = new DateTimeImmutable($date); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** @var PageRepositoryInterface $pageRepository */ |
80
|
|
|
$pageRepository = $this->entityManager->getRepository(Page::class); |
81
|
|
|
if (null !== $sectionCode) { |
82
|
|
|
$pages = $pageRepository->findByProductAndSectionCode($product, $sectionCode, $channelCode, $parsedDate); |
83
|
|
|
} else { |
84
|
|
|
$pages = $pageRepository->findByProduct($product, $channelCode, $parsedDate); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$data = $this->sortBySections($pages); |
88
|
|
|
|
89
|
|
|
return $this->environment->render('@BitBagSyliusCmsPlugin/Shop/Product/_pagesBySection.html.twig', [ |
90
|
|
|
'data' => $data, |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
private function sortBySections(array $pages): array |
95
|
|
|
{ |
96
|
|
|
$sectionsSorter = new SectionsSorter(); |
97
|
|
|
|
98
|
|
|
return $sectionsSorter->sortBySections($pages); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|