Skip to content

Commit c3a4d02

Browse files
authored
Merge pull request #332 from leszczuu/fix/published_at
Added published at to page
2 parents d54f865 + 82b01ca commit c3a4d02

6 files changed

Lines changed: 107 additions & 8 deletions

File tree

src/Entity/Page.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class Page implements PageInterface
3434
/** @var string */
3535
protected $code;
3636

37+
/** @var \DateTimeImmutable|null */
38+
protected $publishAt;
39+
3740
public function __construct()
3841
{
3942
$this->initializeProductsCollection();
@@ -177,4 +180,14 @@ protected function createTranslation(): PageTranslationInterface
177180
{
178181
return new PageTranslation();
179182
}
183+
184+
public function getPublishAt(): ?\DateTimeImmutable
185+
{
186+
return $this->publishAt;
187+
}
188+
189+
public function setPublishAt(?\DateTimeImmutable $publishAt): void
190+
{
191+
$this->publishAt = $publishAt;
192+
}
180193
}

src/Form/Type/PageType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
1919
use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType;
2020
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
21+
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
2122
use Symfony\Component\Form\Extension\Core\Type\TextType;
2223
use Symfony\Component\Form\FormBuilderInterface;
2324

@@ -51,6 +52,13 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5152
'multiple' => true,
5253
'expanded' => true,
5354
])
55+
->add('publishAt', DateTimeType::class, [
56+
'input' => 'datetime_immutable',
57+
'label' => 'bitbag_sylius_cms_plugin.ui.publish_at',
58+
'date_widget' => 'single_text',
59+
'time_widget' => 'single_text',
60+
'required' => false,
61+
])
5462
;
5563
}
5664

src/Repository/PageRepository.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,23 @@ public function findBySectionCode(string $sectionCode, ?string $localeCode): arr
103103
;
104104
}
105105

106-
public function findByProduct(ProductInterface $product, string $channelCode): array
106+
public function findByProduct(ProductInterface $product, string $channelCode, ?\DateTimeInterface $date = null): array
107107
{
108-
return $this->createQueryBuilder('o')
108+
$qb = $this->createQueryBuilder('o')
109109
->innerJoin('o.products', 'product')
110110
->innerJoin('o.channels', 'channel')
111111
->where('o.enabled = true')
112112
->andWhere('product = :product')
113113
->andWhere('channel.code = :channelCode')
114114
->setParameter('product', $product)
115115
->setParameter('channelCode', $channelCode)
116+
;
117+
118+
if (!empty($date)) {
119+
$this->addDateFilter($qb);
120+
}
121+
122+
return $qb
116123
->getQuery()
117124
->getResult()
118125
;
@@ -121,9 +128,10 @@ public function findByProduct(ProductInterface $product, string $channelCode): a
121128
public function findByProductAndSectionCode(
122129
ProductInterface $product,
123130
string $sectionCode,
124-
string $channelCode
131+
string $channelCode,
132+
?\DateTimeInterface $date = null
125133
): array {
126-
return $this->createQueryBuilder('o')
134+
$qb = $this->createQueryBuilder('o')
127135
->innerJoin('o.products', 'product')
128136
->innerJoin('o.sections', 'section')
129137
->innerJoin('o.channels', 'channel')
@@ -134,8 +142,26 @@ public function findByProductAndSectionCode(
134142
->setParameter('product', $product)
135143
->setParameter('sectionCode', $sectionCode)
136144
->setParameter('channelCode', $channelCode)
137-
->getQuery()
145+
;
146+
147+
if (!empty($date)) {
148+
$this->addDateFilter($qb, $date);
149+
}
150+
151+
return $qb->getQuery()
138152
->getResult()
139153
;
140154
}
155+
156+
private function addDateFilter(QueryBuilder $qb, \DateTimeInterface $date): void
157+
{
158+
$qb
159+
->andWhere(
160+
$qb->expr()->orX(
161+
'o.publishAt is NULL',
162+
'o.publishAt <= :date'
163+
)
164+
)
165+
->setParameter('date', $date);
166+
}
141167
}

src/Repository/PageRepositoryInterface.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ public function createShopListQueryBuilder(string $sectionCode, string $channelC
3535

3636
public function findBySectionCode(string $sectionCode, ?string $localeCode): array;
3737

38-
public function findByProduct(ProductInterface $product, string $channelCode): array;
39-
40-
public function findByProductAndSectionCode(ProductInterface $product, string $sectionCode, string $channelCode): array;
38+
public function findByProduct(ProductInterface $product, string $channelCode, ?\DateTimeInterface $date): array;
39+
40+
public function findByProductAndSectionCode(
41+
ProductInterface $product,
42+
string $sectionCode,
43+
string $channelCode,
44+
?\DateTimeInterface $date
45+
): array;
4146
}

src/Resources/config/doctrine/Page.orm.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ BitBag\SyliusCmsPlugin\Entity\Page:
3030
gedmo:
3131
timestampable:
3232
on: update
33+
publishAt:
34+
column: publish_at
35+
type: date_immutable
36+
nullable: true
37+
3338
manyToMany:
3439
products:
3540
targetEntity: Sylius\Component\Product\Model\ProductInterface

src/Twig/Extension/RenderProductPagesExtension.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,46 @@ public function getFunctions(): array
2424
new TwigFunction('bitbag_cms_render_product_pages', [RenderProductPagesRuntime::class, 'renderProductPages'], ['is_safe' => ['html']]),
2525
];
2626
}
27+
28+
public function renderProductPages(ProductInterface $product, ?string $sectionCode = null, ?string $date = null): string
29+
{
30+
$channelCode = $this->channelContext->getChannel()->getCode();
31+
32+
$parsedDate = null;
33+
if (!empty($date)) {
34+
$parsedDate = new \DateTimeImmutable($date);
35+
}
36+
37+
if (null !== $sectionCode) {
38+
$pages = $this->pageRepository->findByProductAndSectionCode($product, $sectionCode, $channelCode, $parsedDate);
39+
} else {
40+
$pages = $this->pageRepository->findByProduct($product, $channelCode, $parsedDate);
41+
}
42+
43+
$data = $this->sortBySections($pages);
44+
45+
return $this->templatingEngine->render('@BitBagSyliusCmsPlugin/Shop/Product/_pagesBySection.html.twig', [
46+
'data' => $data,
47+
]);
48+
}
49+
50+
private function sortBySections(array $pages): array
51+
{
52+
$result = [];
53+
54+
/** @var PageInterface $page */
55+
foreach ($pages as $page) {
56+
foreach ($page->getSections() as $section) {
57+
$sectionCode = $section->getCode();
58+
if (!array_key_exists($sectionCode, $result)) {
59+
$result[$sectionCode] = [];
60+
$result[$sectionCode]['section'] = $section;
61+
}
62+
63+
$result[$sectionCode][] = $page;
64+
}
65+
}
66+
67+
return $result;
68+
}
2769
}

0 commit comments

Comments
 (0)