From 68616b85ad3d3916c423917191a05154c0bbe611 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 16:51:37 +0200 Subject: [PATCH 1/8] Add publication dates on pages entities --- src/Entity/Page.php | 30 ++++++++++++++++ src/Entity/PageInterface.php | 11 ++++++ src/Migrations/Version20240924135829.php | 40 ++++++++++++++++++++++ src/Resources/config/doctrine/Page.orm.xml | 2 ++ 4 files changed, 83 insertions(+) create mode 100644 src/Migrations/Version20240924135829.php diff --git a/src/Entity/Page.php b/src/Entity/Page.php index 2bde616..f604f83 100644 --- a/src/Entity/Page.php +++ b/src/Entity/Page.php @@ -63,6 +63,10 @@ class Page implements PageInterface */ protected $updatedAt; + protected ?DateTimeInterface $publishAt = null; + + protected ?DateTimeInterface $unpublishAt = null; + /** * Page constructor. */ @@ -115,6 +119,32 @@ public function hasChannel(ChannelInterface $channel): bool return $this->channels->contains($channel); } + public function getPublishAt(): ?DateTimeInterface + { + return $this->publishAt; + } + + public function setPublishAt(?DateTimeInterface $publishAt): void + { + $this->publishAt = $publishAt; + } + + public function getUnpublishAt(): ?DateTimeInterface + { + return $this->unpublishAt; + } + + public function setUnpublishAt(?DateTimeInterface $unpublishAt): void + { + $this->unpublishAt = $unpublishAt; + } + + public function isPublished(DateTimeInterface $dateTime): bool + { + return (null == $this->publishAt || $this->publishAt <= $dateTime) + && (null === $this->unpublishAt || $this->unpublishAt > $dateTime); + } + public function getTitle(): ?string { return $this->getTranslation()->getTitle(); diff --git a/src/Entity/PageInterface.php b/src/Entity/PageInterface.php index 13b9045..3488866 100644 --- a/src/Entity/PageInterface.php +++ b/src/Entity/PageInterface.php @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusCmsPagePlugin\Entity; +use DateTimeInterface; use Gedmo\Timestampable\Timestampable; use Sylius\Component\Channel\Model\ChannelsAwareInterface; use Sylius\Component\Resource\Model\CodeAwareInterface; @@ -33,6 +34,16 @@ public function getCode(): ?string; public function setCode(?string $title): void; + public function getPublishAt(): ?DateTimeInterface; + + public function setPublishAt(?DateTimeInterface $publishAt): void; + + public function getUnpublishAt(): ?DateTimeInterface; + + public function setUnpublishAt(?DateTimeInterface $unpublishAt): void; + + public function isPublished(DateTimeInterface $dateTime): bool; + public function getTitle(): ?string; public function setTitle(?string $title): void; diff --git a/src/Migrations/Version20240924135829.php b/src/Migrations/Version20240924135829.php new file mode 100644 index 0000000..1c9add0 --- /dev/null +++ b/src/Migrations/Version20240924135829.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace MonsieurBiz\SyliusCmsPagePlugin\Migrations; + +use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; + +/** + * Auto-generated Migration: Please modify to your needs! + */ +final class Version20240924135829 extends AbstractMigration +{ + public function getDescription(): string + { + return ''; + } + + public function up(Schema $schema): void + { + // this up() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE monsieurbiz_cms_page ADD publish_at DATETIME DEFAULT NULL, ADD unpublish_at DATETIME DEFAULT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE monsieurbiz_cms_page DROP publish_at, DROP unpublish_at'); + } +} diff --git a/src/Resources/config/doctrine/Page.orm.xml b/src/Resources/config/doctrine/Page.orm.xml index 86211bb..136d0a2 100644 --- a/src/Resources/config/doctrine/Page.orm.xml +++ b/src/Resources/config/doctrine/Page.orm.xml @@ -16,6 +16,8 @@ + + From e9419d8a8947c24f656219949f7bc83d03251f6c Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 16:51:57 +0200 Subject: [PATCH 2/8] Update page fixtures with publication dates --- src/Fixture/Factory/PageFixtureFactory.php | 19 ++++++++++++++++ src/Fixture/PageFixture.php | 2 ++ src/Resources/config/sylius/fixtures.yaml | 25 ++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/Fixture/Factory/PageFixtureFactory.php b/src/Fixture/Factory/PageFixtureFactory.php index 1613270..416029e 100644 --- a/src/Fixture/Factory/PageFixtureFactory.php +++ b/src/Fixture/Factory/PageFixtureFactory.php @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusCmsPagePlugin\Fixture\Factory; +use DateTime; use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface; use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageTranslationInterface; use Sylius\Bundle\CoreBundle\Fixture\Factory\AbstractExampleFactory; @@ -85,6 +86,16 @@ public function create(array $options = []): PageInterface $page->setEnabled($options['enabled']); $page->setCode($options['code']); + $publishAt = $options['publish_at'] ?? null; + if ($publishAt) { + $page->setPublishAt(new DateTime($publishAt)); + } + + $unpublishAt = $options['unpublish_at'] ?? null; + if ($unpublishAt) { + $page->setUnpublishAt(new DateTime($unpublishAt)); + } + foreach ($options['channels'] as $channel) { $page->addChannel($channel); } @@ -116,6 +127,8 @@ private function createTranslations(PageInterface $page, array $options): void */ protected function configureOptions(OptionsResolver $resolver): void { + $publishAt = $this->faker->dateTimeBetween('-1 year', '+1 year'); + $hasPublishAt = $this->faker->boolean(20); $resolver ->setDefault('enabled', function (Options $options): bool { return $this->faker->boolean(80); @@ -126,6 +139,12 @@ protected function configureOptions(OptionsResolver $resolver): void ->setDefault('translations', function (OptionsResolver $translationResolver): void { $translationResolver->setDefaults($this->configureDefaultTranslations()); }) + ->setDefault('publish_at', function (Options $options) use ($publishAt, $hasPublishAt): ?string { + return $hasPublishAt ? $publishAt->format('Y-m-d H:i:s') : null; + }) + ->setDefault('unpublish_at', function (Options $options) use ($publishAt): ?string { + return $this->faker->boolean(20) ? (clone $publishAt)->modify('+' . $this->faker->numberBetween(1, 20) . ' days')->format('Y-m-d H:i:s') : null; + }) ->setDefault('channels', LazyOption::all($this->channelRepository)) ->setAllowedTypes('channels', 'array') ->setNormalizer('channels', LazyOption::findBy($this->channelRepository, 'code')) diff --git a/src/Fixture/PageFixture.php b/src/Fixture/PageFixture.php index f56a901..7f6cacc 100644 --- a/src/Fixture/PageFixture.php +++ b/src/Fixture/PageFixture.php @@ -43,6 +43,8 @@ protected function configureResourceNode(ArrayNodeDefinition $resourceNode): voi ->children() ->booleanNode('enabled')->end() ->scalarNode('code')->cannotBeEmpty()->end() + ->scalarNode('publish_at')->end() + ->scalarNode('unpublish_at')->end() ->arrayNode('channels') ->scalarPrototype()->end() ->end() diff --git a/src/Resources/config/sylius/fixtures.yaml b/src/Resources/config/sylius/fixtures.yaml index 7de4c81..6f6c196 100644 --- a/src/Resources/config/sylius/fixtures.yaml +++ b/src/Resources/config/sylius/fixtures.yaml @@ -8,6 +8,8 @@ sylius_fixtures: custom: - code: 'test-page' enabled: true + publish_at: 'now' + unpublish_at: 'now + 1 year' channels: - 'FASHION_WEB' translations: @@ -27,3 +29,26 @@ sylius_fixtures: metaTitle: '' metaDescription: '' metaKeywords: '' + - code: 'old-test-page' + enabled: true + publish_at: 'now -1 year' + unpublish_at: 'yesterday' + channels: + - 'FASHION_WEB' + translations: + en_US: + title: 'Old Test Page' + slug: 'old-test-page' + content: | + [{"code":"monsieurbiz.text","data":{"content":"

Old CMS Example page

Lorem\r\n ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \r\ntempor incididunt ut labore et dolore magna aliqua. Amet commodo nulla \r\nfacilisi nullam vehicula ipsum. Eu mi bibendum neque egestas congue \r\nquisque egestas. Dictumst vestibulum rhoncus est pellentesque elit \r\nullamcorper dignissim cras tincidunt. Ultrices sagittis orci a \r\nscelerisque purus. Proin gravida hendrerit lectus a. Cras ornare arcu \r\ndui vivamus arcu felis. Lacus viverra vitae congue eu consequat ac \r\nfelis. Pharetra diam sit amet nisl suscipit. Nisl suscipit adipiscing \r\nbibendum est ultricies. Nunc non blandit massa enim nec dui. Phasellus \r\negestas tellus rutrum tellus. Purus ut faucibus pulvinar elementum. \r\nScelerisque eu ultrices vitae auctor eu augue ut lectus arcu. Bibendum \r\nat varius vel pharetra vel. Morbi tristique senectus et netus et \r\nmalesuada. Vulputate mi sit amet mauris commodo. Cras semper auctor \r\nneque vitae tempus quam pellentesque nec nam.

Eget lorem dolor sed

\r\n

Risus quis varius quam quisque id diam. Et ultrices neque ornare \r\naenean euismod elementum nisi quis. Sagittis purus sit amet volutpat \r\nconsequat mauris nunc congue nisi. Eget felis eget nunc lobortis mattis \r\naliquam faucibus purus in. Proin libero nunc consequat interdum varius. \r\nIn hac habitasse platea dictumst. Luctus accumsan tortor posuere ac ut \r\nconsequat semper viverra nam. Neque vitae tempus quam pellentesque nec \r\nnam aliquam. Quis ipsum suspendisse ultrices gravida. Blandit volutpat \r\nmaecenas volutpat blandit aliquam etiam erat velit. Et malesuada fames \r\nac turpis egestas sed tempus urna et. Consequat ac felis donec et odio \r\npellentesque diam volutpat. Parturient montes nascetur ridiculus mus \r\nmauris vitae ultricies leo integer. Eget sit amet tellus cras adipiscing\r\n enim eu. Eget lorem dolor sed viverra ipsum nunc. Nibh mauris cursus \r\nmattis molestie a iaculis at. Faucibus pulvinar elementum integer enim. \r\nUt porttitor leo a diam sollicitudin tempor id eu.

\r\n

Nec sagittis aliquam malesuada bibendum arcu vitae elementum \r\ncurabitur. Dolor magna eget est lorem ipsum. Neque ornare aenean euismod\r\n elementum nisi quis eleifend. Volutpat sed cras ornare arcu. Cursus \r\neget nunc scelerisque viverra. Pretium quam vulputate dignissim \r\nsuspendisse in est ante in nibh. Leo vel orci porta non pulvinar. \r\nRhoncus urna neque viverra justo. Est ullamcorper eget nulla facilisi \r\netiam dignissim diam quis enim. Augue interdum velit euismod in \r\npellentesque. Elementum tempus egestas sed sed. Vel fringilla est \r\nullamcorper eget nulla facilisi. Libero justo laoreet sit amet cursus \r\nsit amet.

\r\n

Ipsum suspendisse ultrices gravida

Ipsum suspendisse ultrices gravida dictum fusce ut placerat orci. \r\nAdipiscing bibendum est ultricies integer. Fermentum leo vel orci porta \r\nnon. Pharetra magna ac placerat vestibulum lectus mauris ultrices eros \r\nin. Quis auctor elit sed vulputate. Quisque egestas diam in arcu cursus.\r\n Pulvinar mattis nunc sed blandit libero volutpat sed cras. Ultrices \r\nvitae auctor eu augue ut lectus arcu bibendum. Ipsum dolor sit amet \r\nconsectetur adipiscing elit pellentesque. Lectus mauris ultrices eros in\r\n cursus turpis massa. Diam maecenas ultricies mi eget mauris pharetra et\r\n ultrices. Amet porttitor eget dolor morbi non. Consectetur adipiscing \r\nelit ut aliquam purus sit amet. Enim neque volutpat ac tincidunt vitae \r\nsemper. Porta nibh venenatis cras sed felis eget. Gravida arcu ac tortor\r\n dignissim convallis aenean et tortor. Tortor consequat id porta nibh \r\nvenenatis cras sed felis eget. A diam sollicitudin tempor id. Sed \r\nelementum tempus egestas sed sed risus pretium quam vulputate. Turpis \r\negestas integer eget aliquet nibh.

\r\n

Purus gravida quis blandit turpis cursus in. Pellentesque dignissim \r\nenim sit amet venenatis urna cursus eget. In pellentesque massa placerat\r\n duis ultricies lacus. Ipsum consequat nisl vel pretium lectus quam id. \r\nRisus pretium quam vulputate dignissim suspendisse in est. Sed turpis \r\ntincidunt id aliquet risus feugiat in. Vitae ultricies leo integer \r\nmalesuada nunc vel risus. Aliquet bibendum enim facilisis gravida neque.\r\n Adipiscing enim eu turpis egestas pretium. Lacus vestibulum sed arcu \r\nnon odio euismod lacinia at. Libero justo laoreet sit amet cursus sit \r\namet dictum.

"}},{"code":"monsieurbiz.separator","data":{"hidden":false}},{"code":"monsieurbiz.quote","data":{"author":"A customer","content":"

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

"}},{"code":"monsieurbiz.separator","data":{"hidden":true}},{"code":"monsieurbiz.button","data":{"label":"An amazing web agency","link":"https://monsieurbiz.com"}}] + metaTitle: '' + metaDescription: '' + metaKeywords: '' + fr_FR: + title: 'Page Test' + slug: 'page-test' + content: | + [{"code":"monsieurbiz.text","data":{"content":"

Ancien exemple de page CMS

Lorem\r\n ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod \r\ntempor incididunt ut labore et dolore magna aliqua. Amet commodo nulla \r\nfacilisi nullam vehicula ipsum. Eu mi bibendum neque egestas congue \r\nquisque egestas. Dictumst vestibulum rhoncus est pellentesque elit \r\nullamcorper dignissim cras tincidunt. Ultrices sagittis orci a \r\nscelerisque purus. Proin gravida hendrerit lectus a. Cras ornare arcu \r\ndui vivamus arcu felis. Lacus viverra vitae congue eu consequat ac \r\nfelis. Pharetra diam sit amet nisl suscipit. Nisl suscipit adipiscing \r\nbibendum est ultricies. Nunc non blandit massa enim nec dui. Phasellus \r\negestas tellus rutrum tellus. Purus ut faucibus pulvinar elementum. \r\nScelerisque eu ultrices vitae auctor eu augue ut lectus arcu. Bibendum \r\nat varius vel pharetra vel. Morbi tristique senectus et netus et \r\nmalesuada. Vulputate mi sit amet mauris commodo. Cras semper auctor \r\nneque vitae tempus quam pellentesque nec nam.

Eget lorem dolor sed

\r\n

Risus quis varius quam quisque id diam. Et ultrices neque ornare \r\naenean euismod elementum nisi quis. Sagittis purus sit amet volutpat \r\nconsequat mauris nunc congue nisi. Eget felis eget nunc lobortis mattis \r\naliquam faucibus purus in. Proin libero nunc consequat interdum varius. \r\nIn hac habitasse platea dictumst. Luctus accumsan tortor posuere ac ut \r\nconsequat semper viverra nam. Neque vitae tempus quam pellentesque nec \r\nnam aliquam. Quis ipsum suspendisse ultrices gravida. Blandit volutpat \r\nmaecenas volutpat blandit aliquam etiam erat velit. Et malesuada fames \r\nac turpis egestas sed tempus urna et. Consequat ac felis donec et odio \r\npellentesque diam volutpat. Parturient montes nascetur ridiculus mus \r\nmauris vitae ultricies leo integer. Eget sit amet tellus cras adipiscing\r\n enim eu. Eget lorem dolor sed viverra ipsum nunc. Nibh mauris cursus \r\nmattis molestie a iaculis at. Faucibus pulvinar elementum integer enim. \r\nUt porttitor leo a diam sollicitudin tempor id eu.

\r\n

Nec sagittis aliquam malesuada bibendum arcu vitae elementum \r\ncurabitur. Dolor magna eget est lorem ipsum. Neque ornare aenean euismod\r\n elementum nisi quis eleifend. Volutpat sed cras ornare arcu. Cursus \r\neget nunc scelerisque viverra. Pretium quam vulputate dignissim \r\nsuspendisse in est ante in nibh. Leo vel orci porta non pulvinar. \r\nRhoncus urna neque viverra justo. Est ullamcorper eget nulla facilisi \r\netiam dignissim diam quis enim. Augue interdum velit euismod in \r\npellentesque. Elementum tempus egestas sed sed. Vel fringilla est \r\nullamcorper eget nulla facilisi. Libero justo laoreet sit amet cursus \r\nsit amet.

\r\n

Ipsum suspendisse ultrices gravida

Ipsum suspendisse ultrices gravida dictum fusce ut placerat orci. \r\nAdipiscing bibendum est ultricies integer. Fermentum leo vel orci porta \r\nnon. Pharetra magna ac placerat vestibulum lectus mauris ultrices eros \r\nin. Quis auctor elit sed vulputate. Quisque egestas diam in arcu cursus.\r\n Pulvinar mattis nunc sed blandit libero volutpat sed cras. Ultrices \r\nvitae auctor eu augue ut lectus arcu bibendum. Ipsum dolor sit amet \r\nconsectetur adipiscing elit pellentesque. Lectus mauris ultrices eros in\r\n cursus turpis massa. Diam maecenas ultricies mi eget mauris pharetra et\r\n ultrices. Amet porttitor eget dolor morbi non. Consectetur adipiscing \r\nelit ut aliquam purus sit amet. Enim neque volutpat ac tincidunt vitae \r\nsemper. Porta nibh venenatis cras sed felis eget. Gravida arcu ac tortor\r\n dignissim convallis aenean et tortor. Tortor consequat id porta nibh \r\nvenenatis cras sed felis eget. A diam sollicitudin tempor id. Sed \r\nelementum tempus egestas sed sed risus pretium quam vulputate. Turpis \r\negestas integer eget aliquet nibh.

\r\n

Purus gravida quis blandit turpis cursus in. Pellentesque dignissim \r\nenim sit amet venenatis urna cursus eget. In pellentesque massa placerat\r\n duis ultricies lacus. Ipsum consequat nisl vel pretium lectus quam id. \r\nRisus pretium quam vulputate dignissim suspendisse in est. Sed turpis \r\ntincidunt id aliquet risus feugiat in. Vitae ultricies leo integer \r\nmalesuada nunc vel risus. Aliquet bibendum enim facilisis gravida neque.\r\n Adipiscing enim eu turpis egestas pretium. Lacus vestibulum sed arcu \r\nnon odio euismod lacinia at. Libero justo laoreet sit amet cursus sit \r\namet dictum.

"}},{"code":"monsieurbiz.separator","data":{"hidden":false}},{"code":"monsieurbiz.quote","data":{"author":"A customer","content":"

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

"}},{"code":"monsieurbiz.separator","data":{"hidden":true}},{"code":"monsieurbiz.button","data":{"label":"Une superbe agence web","link":"https://monsieurbiz.com"}}] + metaTitle: '' + metaDescription: '' + metaKeywords: '' From 713487eacbd7a7af8809003be5bbc3d2d0da8174 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 16:52:16 +0200 Subject: [PATCH 3/8] Update page form with publication dates --- src/Form/Type/PageType.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Form/Type/PageType.php b/src/Form/Type/PageType.php index 90be33e..2b19188 100644 --- a/src/Form/Type/PageType.php +++ b/src/Form/Type/PageType.php @@ -18,6 +18,7 @@ use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Symfony\Component\Form\Extension\Core\Type\DateTimeType; use Symfony\Component\Form\FormBuilderInterface; class PageType extends AbstractResourceType @@ -33,6 +34,16 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'required' => false, 'label' => 'monsieurbiz_cms_page.ui.form.enabled', ]) + ->add('publishAt', DateTimeType::class, [ + 'label' => 'monsieurbiz_cms_page.ui.form.publish_at', + 'required' => false, + 'widget' => 'single_text', + ]) + ->add('unpublishAt', DateTimeType::class, [ + 'label' => 'monsieurbiz_cms_page.ui.form.unpublish_at', + 'required' => false, + 'widget' => 'single_text', + ]) ->add('channels', ChannelChoiceType::class, [ 'label' => 'monsieurbiz_cms_page.ui.form.channels', 'required' => false, From 686ca3c0df38bd08ce3f003b880312b31fd0236e Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 16:52:35 +0200 Subject: [PATCH 4/8] Manage pages grid in admin with publication date --- src/Resources/config/sylius/grid.yaml | 6 +++++ .../Admin/Grid/Field/published.html.twig | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/Resources/views/Admin/Grid/Field/published.html.twig diff --git a/src/Resources/config/sylius/grid.yaml b/src/Resources/config/sylius/grid.yaml index 3c40fa7..dba79a1 100644 --- a/src/Resources/config/sylius/grid.yaml +++ b/src/Resources/config/sylius/grid.yaml @@ -38,6 +38,12 @@ sylius_grid: sortable: ~ options: template: "@SyliusUi/Grid/Field/enabled.html.twig" + published: + type: twig + label: monsieurbiz_cms_page.ui.is_published + path: . + options: + template: "@MonsieurBizSyliusCmsPagePlugin/Admin/Grid/Field/published.html.twig" actions: main: create: diff --git a/src/Resources/views/Admin/Grid/Field/published.html.twig b/src/Resources/views/Admin/Grid/Field/published.html.twig new file mode 100644 index 0000000..61d1f7e --- /dev/null +++ b/src/Resources/views/Admin/Grid/Field/published.html.twig @@ -0,0 +1,22 @@ +{% set publishInformationText = '' %} +{% if data.publishAt %} + {% set publishInformationText = publishInformationText ~ ('monsieurbiz_cms_page.ui.form.publish_at'|trans) ~ ' ' ~ data.publishAt|date('Y-m-d H:i:s') %} +{% endif %} +{% if data.unpublishAt %} + {% if publishInformationText is not empty %} + {% set publishInformationText = publishInformationText ~ ' - ' %} + {% endif %} + {% set publishInformationText = publishInformationText ~ ('monsieurbiz_cms_page.ui.form.unpublish_at'|trans) ~ ' ' ~ data.unpublishAt|date('Y-m-d H:i:s') %} +{% endif %} + +{% if data.isPublished(date()) %} + + + {{ 'sylius.ui.yes_label'|trans }} + +{% else %} + + + {{ 'sylius.ui.no_label'|trans }} + +{% endif %} From d1ef1b9ea2eef20367a4597d9080b77a47b9e4e8 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 16:53:05 +0200 Subject: [PATCH 5/8] Update page repository to deals with publication dates --- src/Repository/PageRepository.php | 20 ++++++++++++++++++++ src/Resources/config/services.yaml | 8 ++++++++ 2 files changed, 28 insertions(+) diff --git a/src/Repository/PageRepository.php b/src/Repository/PageRepository.php index a54c223..c9534b2 100644 --- a/src/Repository/PageRepository.php +++ b/src/Repository/PageRepository.php @@ -13,14 +13,28 @@ namespace MonsieurBiz\SyliusCmsPagePlugin\Repository; +use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\QueryBuilder; use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; +use Sylius\Calendar\Provider\DateTimeProviderInterface; use Sylius\Component\Channel\Model\ChannelInterface; class PageRepository extends EntityRepository implements PageRepositoryInterface { + private DateTimeProviderInterface $dateTimeProvider; + + public function __construct( + EntityManagerInterface $em, + ClassMetadata $class, + DateTimeProviderInterface $dateTimeProvider + ) { + $this->dateTimeProvider = $dateTimeProvider; + parent::__construct($em, $class); + } + public function createListQueryBuilder(string $localeCode): QueryBuilder { return $this->createQueryBuilder('o') @@ -53,6 +67,9 @@ public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?str $queryBuilder = $this->createQueryBuilderExistOne($channel, $locale, $slug); $queryBuilder ->andWhere('p.enabled = true') + ->andWhere('p.publishAt IS NULL OR p.publishAt <= :now') + ->andWhere('p.unpublishAt IS NULL OR p.unpublishAt >= :now') + ->setParameter('now', $this->dateTimeProvider->now()) ; $count = (int) $queryBuilder @@ -75,6 +92,9 @@ public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeC ->andWhere('translation.slug = :slug') ->andWhere('channels.code = :channelCode') ->andWhere('p.enabled = true') + ->andWhere('p.publishAt IS NULL OR p.publishAt <= :now') + ->andWhere('p.unpublishAt IS NULL OR p.unpublishAt >= :now') + ->setParameter('now', $this->dateTimeProvider->now()) ->setParameter('localeCode', $localeCode) ->setParameter('slug', $slug) ->setParameter('channelCode', $channelCode) diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 67f917c..b69c1f1 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -35,3 +35,11 @@ services: MonsieurBiz\SyliusCmsPagePlugin\Routing\RequestContext: decorates: router.request_context arguments: ['@MonsieurBiz\SyliusCmsPagePlugin\Routing\RequestContext.inner'] + + # Page Repository + monsieurbiz_cms_page.repository.page: + class: MonsieurBiz\SyliusCmsPagePlugin\Repository\PageRepository + arguments: + $em: '@doctrine.orm.default_entity_manager' + $class: '@=service("doctrine.orm.default_entity_manager").getClassMetadata("MonsieurBiz\\SyliusCmsPagePlugin\\Entity\\PageInterface")' + $dateTimeProvider: '@Sylius\Calendar\Provider\DateTimeProviderInterface' From f45856f75aa264849540e9d26e42796a815c8a1e Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 16:53:35 +0200 Subject: [PATCH 6/8] Add translations for publication dates feature --- src/Resources/translations/messages.en.yaml | 3 +++ src/Resources/translations/messages.fr.yaml | 3 +++ src/Resources/translations/messages.pl.yaml | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/Resources/translations/messages.en.yaml b/src/Resources/translations/messages.en.yaml index 95a3a5a..de510c5 100644 --- a/src/Resources/translations/messages.en.yaml +++ b/src/Resources/translations/messages.en.yaml @@ -6,6 +6,7 @@ monsieurbiz_cms_page: cms_content: "CMS content" pages_subheader: "CMS Page management" back_to_admin: "Back to admin" + is_published: "Published" form: id: 'ID' page_info: 'Page information' @@ -19,6 +20,8 @@ monsieurbiz_cms_page: meta_title: "Meta Title" meta_description: "Meta Description" meta_keywords: "Meta Keyword" + publish_at: "Publish at" + unpublish_at: "Unpublish at" show_in: "Show in" show_page_in_shop_page: "Show page in shop page" actions: diff --git a/src/Resources/translations/messages.fr.yaml b/src/Resources/translations/messages.fr.yaml index 31f7e2b..c56bf3f 100644 --- a/src/Resources/translations/messages.fr.yaml +++ b/src/Resources/translations/messages.fr.yaml @@ -6,6 +6,7 @@ monsieurbiz_cms_page: cms_content: "Contenus CMS" pages_subheader: "Gestion des contenus CMS" back_to_admin: "Retour à l'admin" + is_published: "Publiée" form: id: 'ID' page_info: 'Informations de la page' @@ -19,6 +20,8 @@ monsieurbiz_cms_page: meta_title: "Meta Title" meta_description: "Meta Description" meta_keywords: "Meta Keyword" + publish_at: "Publier le" + unpublish_at: "Dépublier le" show_in: "Afficher dans" show_page_in_shop_page: "Afficher la page dans la boutique" actions: diff --git a/src/Resources/translations/messages.pl.yaml b/src/Resources/translations/messages.pl.yaml index 64049ae..8605fdc 100644 --- a/src/Resources/translations/messages.pl.yaml +++ b/src/Resources/translations/messages.pl.yaml @@ -6,6 +6,7 @@ monsieurbiz_cms_page: cms_content: "Treść CMS" pages_subheader: "Zarządzanie stronami CMS" back_to_admin: "Powrót do administracji" + is_published: "Opublikowana" form: id: 'ID' page_info: 'Informacje o stronie' @@ -19,6 +20,8 @@ monsieurbiz_cms_page: meta_title: "Tytuł meta" meta_description: "Opis meta" meta_keywords: "Słowa kluczowe meta" + publish_at: "Opublikuj w" + unpublish_at: "Niepublikuj w" show_in: "Pokaż w" show_page_in_shop_page: "Pokaż stronę na stronie sklepu" actions: From 78302a6c7132f40a3e0b636e8cd12a79704098ba Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 17:15:52 +0200 Subject: [PATCH 7/8] Update page repository to use date in function and not in construct --- src/Repository/PageRepository.php | 23 +++++----------------- src/Repository/PageRepositoryInterface.php | 5 +++-- src/Resources/config/routing/shop.yaml | 1 + src/Resources/config/services.yaml | 10 +++------- src/Routing/PageSlugConditionChecker.php | 13 ++++++++++-- 5 files changed, 23 insertions(+), 29 deletions(-) diff --git a/src/Repository/PageRepository.php b/src/Repository/PageRepository.php index c9534b2..e76bd56 100644 --- a/src/Repository/PageRepository.php +++ b/src/Repository/PageRepository.php @@ -13,28 +13,15 @@ namespace MonsieurBiz\SyliusCmsPagePlugin\Repository; -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Mapping\ClassMetadata; +use DateTimeInterface; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\QueryBuilder; use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface; use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository; -use Sylius\Calendar\Provider\DateTimeProviderInterface; use Sylius\Component\Channel\Model\ChannelInterface; class PageRepository extends EntityRepository implements PageRepositoryInterface { - private DateTimeProviderInterface $dateTimeProvider; - - public function __construct( - EntityManagerInterface $em, - ClassMetadata $class, - DateTimeProviderInterface $dateTimeProvider - ) { - $this->dateTimeProvider = $dateTimeProvider; - parent::__construct($em, $class); - } - public function createListQueryBuilder(string $localeCode): QueryBuilder { return $this->createQueryBuilder('o') @@ -62,14 +49,14 @@ public function existsOneByChannelAndSlug(ChannelInterface $channel, ?string $lo return $count > 0; } - public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug): bool + public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool { $queryBuilder = $this->createQueryBuilderExistOne($channel, $locale, $slug); $queryBuilder ->andWhere('p.enabled = true') ->andWhere('p.publishAt IS NULL OR p.publishAt <= :now') ->andWhere('p.unpublishAt IS NULL OR p.unpublishAt >= :now') - ->setParameter('now', $this->dateTimeProvider->now()) + ->setParameter('now', $dateTime) ; $count = (int) $queryBuilder @@ -83,7 +70,7 @@ public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?str /** * @throws NonUniqueResultException */ - public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode): ?PageInterface + public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface { return $this->createQueryBuilder('p') ->leftJoin('p.translations', 'translation') @@ -94,7 +81,7 @@ public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeC ->andWhere('p.enabled = true') ->andWhere('p.publishAt IS NULL OR p.publishAt <= :now') ->andWhere('p.unpublishAt IS NULL OR p.unpublishAt >= :now') - ->setParameter('now', $this->dateTimeProvider->now()) + ->setParameter('now', $dateTime) ->setParameter('localeCode', $localeCode) ->setParameter('slug', $slug) ->setParameter('channelCode', $channelCode) diff --git a/src/Repository/PageRepositoryInterface.php b/src/Repository/PageRepositoryInterface.php index e188ef8..d79404a 100644 --- a/src/Repository/PageRepositoryInterface.php +++ b/src/Repository/PageRepositoryInterface.php @@ -13,6 +13,7 @@ namespace MonsieurBiz\SyliusCmsPagePlugin\Repository; +use DateTimeInterface; use Doctrine\ORM\QueryBuilder; use MonsieurBiz\SyliusCmsPagePlugin\Entity\PageInterface; use Sylius\Component\Channel\Model\ChannelInterface; @@ -24,7 +25,7 @@ public function createListQueryBuilder(string $localeCode): QueryBuilder; public function existsOneByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, array $excludedPages = []): bool; - public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug): bool; + public function existsOneEnabledByChannelAndSlug(ChannelInterface $channel, ?string $locale, string $slug, DateTimeInterface $dateTime): bool; - public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode): ?PageInterface; + public function findOneEnabledBySlugAndChannelCode(string $slug, string $localeCode, string $channelCode, DateTimeInterface $dateTime): ?PageInterface; } diff --git a/src/Resources/config/routing/shop.yaml b/src/Resources/config/routing/shop.yaml index e8d65b6..f39d952 100644 --- a/src/Resources/config/routing/shop.yaml +++ b/src/Resources/config/routing/shop.yaml @@ -12,6 +12,7 @@ monsieurbiz_cms_page_show: - $slug - "expr:service('sylius.context.locale').getLocaleCode()" - "expr:service('sylius.context.channel').getChannel().getCode()" + - "expr:service('Sylius\\\\Calendar\\\\Provider\\\\DateTimeProviderInterface').now()" requirements: slug: .+ condition: "not(context.getPathInfo() matches '`^%sylius.security.new_api_route%`') and context.checkPageSlug(request)" diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index b69c1f1..d223203 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -36,10 +36,6 @@ services: decorates: router.request_context arguments: ['@MonsieurBiz\SyliusCmsPagePlugin\Routing\RequestContext.inner'] - # Page Repository - monsieurbiz_cms_page.repository.page: - class: MonsieurBiz\SyliusCmsPagePlugin\Repository\PageRepository - arguments: - $em: '@doctrine.orm.default_entity_manager' - $class: '@=service("doctrine.orm.default_entity_manager").getClassMetadata("MonsieurBiz\\SyliusCmsPagePlugin\\Entity\\PageInterface")' - $dateTimeProvider: '@Sylius\Calendar\Provider\DateTimeProviderInterface' + Sylius\Calendar\Provider\DateTimeProviderInterface: + class: Sylius\Calendar\Provider\Calendar + public: true diff --git a/src/Routing/PageSlugConditionChecker.php b/src/Routing/PageSlugConditionChecker.php index 3b5d229..bb42786 100644 --- a/src/Routing/PageSlugConditionChecker.php +++ b/src/Routing/PageSlugConditionChecker.php @@ -14,6 +14,7 @@ namespace MonsieurBiz\SyliusCmsPagePlugin\Routing; use MonsieurBiz\SyliusCmsPagePlugin\Repository\PageRepositoryInterface; +use Sylius\Calendar\Provider\DateTimeProviderInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Channel\Context\ChannelNotFoundException; use Sylius\Component\Locale\Context\LocaleContextInterface; @@ -35,17 +36,24 @@ final class PageSlugConditionChecker */ private $localeContext; + /** + * @var DateTimeProviderInterface + */ + private $dateTimeProvider; + /** * PageSlugConditionChecker constructor. */ public function __construct( PageRepositoryInterface $pageRepository, ChannelContextInterface $channelContext, - LocaleContextInterface $localeContext + LocaleContextInterface $localeContext, + DateTimeProviderInterface $dateTimeProvider ) { $this->pageRepository = $pageRepository; $this->channelContext = $channelContext; $this->localeContext = $localeContext; + $this->dateTimeProvider = $dateTimeProvider; } public function isPageSlug(string $slug): bool @@ -54,7 +62,8 @@ public function isPageSlug(string $slug): bool return $this->pageRepository->existsOneEnabledByChannelAndSlug( $this->channelContext->getChannel(), $this->localeContext->getLocaleCode(), - $slug + $slug, + $this->dateTimeProvider->now() ); } catch (ChannelNotFoundException $channelNotFoundException) { return false; From 0cb86db91193095544e1a8f0d85427e657aa06f6 Mon Sep 17 00:00:00 2001 From: Maxime Huran Date: Tue, 24 Sep 2024 17:16:06 +0200 Subject: [PATCH 8/8] Show in shop is disabled if page is not published --- src/Resources/views/Admin/Page/_showInShopButton.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/views/Admin/Page/_showInShopButton.html.twig b/src/Resources/views/Admin/Page/_showInShopButton.html.twig index 6d1066c..0a5eceb 100644 --- a/src/Resources/views/Admin/Page/_showInShopButton.html.twig +++ b/src/Resources/views/Admin/Page/_showInShopButton.html.twig @@ -1,7 +1,7 @@ {% set enabledChannels = resource.channels|filter(channel => channel.enabled == true) %} {% if sylius_bundle_loaded_checker('SyliusShopBundle') %} - {% if not resource.enabled or enabledChannels|length < 1 %} + {% if not resource.enabled or not resource.isPublished(date()) or enabledChannels|length < 1 %} {{ 'monsieurbiz_cms_page.ui.show_page_in_shop_page'|trans }}