Skip to content

Commit d05d4cf

Browse files
committed
Added published at to page
1 parent d603db3 commit d05d4cf

6 files changed

Lines changed: 74 additions & 12 deletions

File tree

src/Entity/Page.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class Page implements PageInterface
3535
/** @var string */
3636
protected $code;
3737

38+
/** @var \DateTimeImmutable|null */
39+
protected $publishAt;
40+
3841
public function __construct()
3942
{
4043
$this->initializeProductsCollection();
@@ -174,8 +177,18 @@ protected function getPageTranslation(): PageTranslationInterface
174177
return $this->getTranslation();
175178
}
176179

177-
protected function createTranslation(): ?PageTranslationInterface
180+
protected function createTranslation(): PageTranslationInterface
178181
{
179182
return new PageTranslation();
180183
}
184+
185+
public function getPublishAt(): ?\DateTimeImmutable
186+
{
187+
return $this->publishAt;
188+
}
189+
190+
public function setPublishAt(?\DateTimeImmutable $publishAt): void
191+
{
192+
$this->publishAt = $publishAt;
193+
}
181194
}

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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@ public function getFunctions(): array
4646
];
4747
}
4848

49-
public function renderProductPages(ProductInterface $product, string $sectionCode = null): string
49+
public function renderProductPages(ProductInterface $product, ?string $sectionCode = null, ?string $date = null): string
5050
{
5151
$channelCode = $this->channelContext->getChannel()->getCode();
5252

53+
$parsedDate = null;
54+
if (!empty($date)) {
55+
$parsedDate = new \DateTimeImmutable($date);
56+
}
57+
5358
if (null !== $sectionCode) {
54-
$pages = $this->pageRepository->findByProductAndSectionCode($product, $sectionCode, $channelCode);
59+
$pages = $this->pageRepository->findByProductAndSectionCode($product, $sectionCode, $channelCode, $parsedDate);
5560
} else {
56-
$pages = $this->pageRepository->findByProduct($product, $channelCode);
61+
$pages = $this->pageRepository->findByProduct($product, $channelCode, $parsedDate);
5762
}
5863

5964
$data = $this->sortBySections($pages);

0 commit comments

Comments
 (0)