From ee7a3e4969826a7f8a9ec62689c0e54f3aef0083 Mon Sep 17 00:00:00 2001 From: jkindly Date: Fri, 21 Jun 2024 13:22:48 +0200 Subject: [PATCH] OP-321: Init update block functionality --- src/Entity/Block.php | 19 ++-- src/Entity/BlockContent.php | 57 +++++++++++ src/Entity/BlockContentAwareInterface.php | 26 +++++ src/Entity/BlockContentAwareTrait.php | 48 +++++++++ src/Entity/BlockContentInterface.php | 30 ++++++ src/Entity/BlockInterface.php | 3 +- src/Entity/BlockTranslation.php | 26 ----- src/Entity/BlockTranslationInterface.php | 8 -- .../BlockContentTextConfigurationType.php | 32 ++++++ src/Form/Type/BlockContentType.php | 98 +++++++++++++++++++ src/Form/Type/BlockType.php | 22 +++-- .../Type/Translation/BlockTranslationType.php | 8 -- src/Migrations/Version20240621093611.php | 37 +++++++ src/Repository/BlockContentRepository.php | 17 ++++ src/Resources/config/config.yml | 1 + src/Resources/config/doctrine/Block.orm.xml | 8 ++ .../config/doctrine/BlockContent.orm.xml | 26 +++++ .../config/doctrine/BlockTranslation.orm.xml | 6 +- src/Resources/config/resources.yml | 1 + .../config/resources/block_content.yml | 10 ++ src/Resources/config/serialization/Block.xml | 3 + .../config/serialization/BlockTranslation.xml | 6 -- .../config/serializer/Entity.Block.yml | 4 + src/Resources/config/services/form.xml | 17 ++++ .../config/validation/BlockTranslation.xml | 24 ----- .../views/Block/Crud/_form.html.twig | 30 +++--- 26 files changed, 453 insertions(+), 114 deletions(-) create mode 100644 src/Entity/BlockContent.php create mode 100644 src/Entity/BlockContentAwareInterface.php create mode 100644 src/Entity/BlockContentAwareTrait.php create mode 100644 src/Entity/BlockContentInterface.php create mode 100644 src/Form/Type/BlockContent/BlockContentTextConfigurationType.php create mode 100644 src/Form/Type/BlockContentType.php create mode 100644 src/Migrations/Version20240621093611.php create mode 100755 src/Repository/BlockContentRepository.php create mode 100644 src/Resources/config/doctrine/BlockContent.orm.xml create mode 100644 src/Resources/config/resources/block_content.yml diff --git a/src/Entity/Block.php b/src/Entity/Block.php index 277940816..f036a3de7 100755 --- a/src/Entity/Block.php +++ b/src/Entity/Block.php @@ -21,6 +21,7 @@ class Block implements BlockInterface use ProductsAwareTrait; use TaxonAwareTrait; use ChannelsAwareTrait; + use BlockContentAwareTrait; use TranslatableTrait { __construct as protected initializeTranslationsCollection; } @@ -32,13 +33,14 @@ public function __construct() $this->initializeProductsCollection(); $this->initializeTaxonCollection(); $this->initializeChannelsCollection(); + $this->initializeContentsCollection(); } - /** @var int|null */ - protected $id; + protected ?int $id; - /** @var string|null */ - protected $code; + protected ?string $code; + + protected ?string $name; public function getId(): ?int { @@ -57,17 +59,12 @@ public function setCode(?string $code): void public function getName(): ?string { - /** @var BlockTranslationInterface $blockTranslationInterface */ - $blockTranslationInterface = $this->getBlockTranslation(); - - return $blockTranslationInterface->getName(); + return $this->name; } public function setName(?string $name): void { - /** @var BlockTranslationInterface $blockTranslationInterface */ - $blockTranslationInterface = $this->getBlockTranslation(); - $blockTranslationInterface->setName($name); + $this->name = $name; } public function getContent(): ?string diff --git a/src/Entity/BlockContent.php b/src/Entity/BlockContent.php new file mode 100644 index 000000000..ec598425b --- /dev/null +++ b/src/Entity/BlockContent.php @@ -0,0 +1,57 @@ +id; + } + + public function getType(): ?string + { + return $this->type; + } + + public function setType(?string $type): void + { + $this->type = $type; + } + + public function getConfiguration(): array + { + return $this->configuration; + } + + public function setConfiguration(array $configuration): void + { + $this->configuration = $configuration; + } + + public function getBlock(): ?BlockInterface + { + return $this->block; + } + + public function setBlock(?BlockInterface $block): void + { + $this->block = $block; + } +} diff --git a/src/Entity/BlockContentAwareInterface.php b/src/Entity/BlockContentAwareInterface.php new file mode 100644 index 000000000..52cae0d5a --- /dev/null +++ b/src/Entity/BlockContentAwareInterface.php @@ -0,0 +1,26 @@ +contents = new ArrayCollection(); + } + + public function getContents(): Collection + { + return $this->contents; + } + + public function hasContent(BlockContentInterface $contentItem): bool + { + return $this->contents->contains($contentItem); + } + + public function addContent(BlockContentInterface $contentItem): void + { + if (!$this->hasContent($contentItem)) { + $this->contents->add($contentItem); + } + } + + public function removeContent(BlockContentInterface $contentItem): void + { + if ($this->hasContent($contentItem)) { + $this->contents->removeElement($contentItem); + } + } +} diff --git a/src/Entity/BlockContentInterface.php b/src/Entity/BlockContentInterface.php new file mode 100644 index 000000000..952e9552b --- /dev/null +++ b/src/Entity/BlockContentInterface.php @@ -0,0 +1,30 @@ +name; - } - - public function setName(?string $name): void - { - $this->name = $name; - } - public function getContent(): ?string { return $this->content; @@ -50,14 +34,4 @@ public function getId(): ?int { return $this->id; } - - public function getLink(): ?string - { - return $this->link; - } - - public function setLink(?string $link): void - { - $this->link = $link; - } } diff --git a/src/Entity/BlockTranslationInterface.php b/src/Entity/BlockTranslationInterface.php index 28676df90..d09dec2a8 100755 --- a/src/Entity/BlockTranslationInterface.php +++ b/src/Entity/BlockTranslationInterface.php @@ -15,15 +15,7 @@ interface BlockTranslationInterface extends ResourceInterface, TranslationInterface { - public function getName(): ?string; - - public function setName(?string $name): void; - public function getContent(): ?string; public function setContent(?string $content): void; - - public function getLink(): ?string; - - public function setLink(?string $link): void; } diff --git a/src/Form/Type/BlockContent/BlockContentTextConfigurationType.php b/src/Form/Type/BlockContent/BlockContentTextConfigurationType.php new file mode 100644 index 000000000..e721b5706 --- /dev/null +++ b/src/Form/Type/BlockContent/BlockContentTextConfigurationType.php @@ -0,0 +1,32 @@ +setDefaults([ + 'label' => 'bitbag_sylius_cms_plugin.ui.text', + ]); + } + + public function getParent(): string + { + return WysiwygType::class; + } +} diff --git a/src/Form/Type/BlockContentType.php b/src/Form/Type/BlockContentType.php new file mode 100644 index 000000000..140f8b479 --- /dev/null +++ b/src/Form/Type/BlockContentType.php @@ -0,0 +1,98 @@ + $formType) { + $this->actionConfigurationTypes[$type] = $formType::class; + $this->actionTypes['bitbag_sylius_cms_plugin.block_content.action.' . $type] = $type; + } + } + + public function buildForm(FormBuilderInterface $builder, array $options) + { + $defaultActionType = current($this->actionTypes); + $defaultActionConfigurationType = $this->actionConfigurationTypes[$defaultActionType]; + + $builder + ->add('type', ChoiceType::class, [ + 'label' => 'sylius.ui.type', + 'choices' => $this->actionTypes, + ]) + ->add('configuration', $defaultActionConfigurationType, [ + 'label' => false, + ]) + ; + + $builder + ->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void { + $this->addConfigurationTypeToForm($event); + }) + ->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event): void { + /** @var array|null $data */ + $data = $event->getData(); + if (null === $data) { + return; + } + + $form = $event->getForm(); + $formData = $form->getData(); + + if ($formData !== null && $formData->getType() !== $data['type']) { + $formData->setConfiguration([]); + } + + $this->addConfigurationTypeToForm($event); + }) + ; + } + + private function addConfigurationTypeToForm(FormEvent $event): void + { + $data = $event->getData(); + if ($data === null) { + return; + } + + $form = $event->getForm(); + + $dataType = $data instanceof CatalogPromotionActionInterface ? $data->getType() : $data['type']; + + $actionConfigurationType = $this->actionConfigurationTypes[$dataType]; + $form->add('configuration', $actionConfigurationType, [ + 'label' => false, + ]); + } + + public function getBlockPrefix(): string + { + return 'bitbag_sylius_cms_plugin_block_content'; + } +} diff --git a/src/Form/Type/BlockType.php b/src/Form/Type/BlockType.php index 5463527cd..5160d9997 100755 --- a/src/Form/Type/BlockType.php +++ b/src/Form/Type/BlockType.php @@ -13,14 +13,13 @@ use BitBag\SyliusCmsPlugin\Entity\BlockInterface; use BitBag\SyliusCmsPlugin\Form\Type\Translation\BlockTranslationType; use Sylius\Bundle\ChannelBundle\Form\Type\ChannelChoiceType; -use Sylius\Bundle\ProductBundle\Form\Type\ProductAutocompleteChoiceType; use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType; use Sylius\Bundle\ResourceBundle\Form\Type\ResourceTranslationsType; -use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Constraints\Valid; +use \Symfony\Component\Form\Extension\Core\Type\CollectionType as SymfonyCollectionType; final class BlockType extends AbstractResourceType { @@ -34,6 +33,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void 'label' => 'bitbag_sylius_cms_plugin.ui.code', 'disabled' => null !== $block->getCode(), ]) + ->add('name', TextType::class, [ + 'label' => 'bitbag_sylius_cms_plugin.ui.name', + ]) ->add('collections', CollectionAutocompleteChoiceType::class, [ 'label' => 'bitbag_sylius_cms_plugin.ui.collections', 'multiple' => true, @@ -41,20 +43,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ->add('enabled', CheckboxType::class, [ 'label' => 'bitbag_sylius_cms_plugin.ui.enabled', ]) - ->add('products', ProductAutocompleteChoiceType::class, [ - 'label' => 'bitbag_sylius_cms_plugin.ui.products', - 'multiple' => true, - ]) - ->add('taxons', TaxonAutocompleteChoiceType::class, [ - 'label' => 'bitbag_sylius_cms_plugin.ui.taxons', - 'multiple' => true, - ]) ->add('channels', ChannelChoiceType::class, [ 'label' => 'bitbag_sylius_cms_plugin.ui.channels', 'required' => false, 'multiple' => true, 'expanded' => true, ]) + ->add('contents', SymfonyCollectionType::class, [ + 'label' => 'sylius.ui.actions', + 'entry_type' => BlockContentType::class, + 'allow_add' => true, + 'allow_delete' => true, + 'by_reference' => false, + 'required' => false, + ]) ->add('translations', ResourceTranslationsType::class, [ 'label' => 'bitbag_sylius_cms_plugin.ui.contents', 'entry_type' => BlockTranslationType::class, diff --git a/src/Form/Type/Translation/BlockTranslationType.php b/src/Form/Type/Translation/BlockTranslationType.php index 2c74b196c..7d54f09d5 100644 --- a/src/Form/Type/Translation/BlockTranslationType.php +++ b/src/Form/Type/Translation/BlockTranslationType.php @@ -20,14 +20,6 @@ final class BlockTranslationType extends AbstractResourceType public function buildForm(FormBuilderInterface $builder, array $options): void { $builder - ->add('name', TextType::class, [ - 'label' => 'bitbag_sylius_cms_plugin.ui.name', - 'required' => false, - ]) - ->add('link', TextType::class, [ - 'label' => 'bitbag_sylius_cms_plugin.ui.link', - 'required' => false, - ]) ->add('content', WysiwygType::class, [ 'required' => false, ]) diff --git a/src/Migrations/Version20240621093611.php b/src/Migrations/Version20240621093611.php new file mode 100644 index 000000000..8eab82fed --- /dev/null +++ b/src/Migrations/Version20240621093611.php @@ -0,0 +1,37 @@ +addSql('CREATE TABLE bitbag_cms_block_content (id INT AUTO_INCREMENT NOT NULL, block_id INT DEFAULT NULL, type VARCHAR(255) NOT NULL, configuration JSON NOT NULL COMMENT \'(DC2Type:json)\', INDEX IDX_FAA763A8E9ED820C (block_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE bitbag_cms_block_content ADD CONSTRAINT FK_FAA763A8E9ED820C FOREIGN KEY (block_id) REFERENCES bitbag_cms_block (id)'); + $this->addSql('ALTER TABLE bitbag_cms_block ADD name VARCHAR(250) DEFAULT NULL'); + $this->addSql('ALTER TABLE bitbag_cms_block_translation DROP name, DROP link'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE bitbag_cms_block_content DROP FOREIGN KEY FK_FAA763A8E9ED820C'); + $this->addSql('DROP TABLE bitbag_cms_block_content'); + $this->addSql('ALTER TABLE bitbag_cms_block_translation ADD name VARCHAR(255) DEFAULT NULL, ADD link LONGTEXT DEFAULT NULL'); + $this->addSql('ALTER TABLE bitbag_cms_block DROP name'); + } +} diff --git a/src/Repository/BlockContentRepository.php b/src/Repository/BlockContentRepository.php new file mode 100755 index 000000000..0e70df7c0 --- /dev/null +++ b/src/Repository/BlockContentRepository.php @@ -0,0 +1,17 @@ + + + @@ -58,5 +60,11 @@ + + + + + + diff --git a/src/Resources/config/doctrine/BlockContent.orm.xml b/src/Resources/config/doctrine/BlockContent.orm.xml new file mode 100644 index 000000000..b928a5afc --- /dev/null +++ b/src/Resources/config/doctrine/BlockContent.orm.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Resources/config/doctrine/BlockTranslation.orm.xml b/src/Resources/config/doctrine/BlockTranslation.orm.xml index b3ebfa897..f6ebc175a 100644 --- a/src/Resources/config/doctrine/BlockTranslation.orm.xml +++ b/src/Resources/config/doctrine/BlockTranslation.orm.xml @@ -10,10 +10,6 @@ - - - - - \ No newline at end of file + diff --git a/src/Resources/config/resources.yml b/src/Resources/config/resources.yml index df0a8c254..52b3c7564 100755 --- a/src/Resources/config/resources.yml +++ b/src/Resources/config/resources.yml @@ -1,5 +1,6 @@ imports: - { resource: resources/block.yml } + - { resource: resources/block_content.yml } - { resource: resources/page.yml } - { resource: resources/frequently_asked_question.yml } - { resource: resources/collection.yml } diff --git a/src/Resources/config/resources/block_content.yml b/src/Resources/config/resources/block_content.yml new file mode 100644 index 000000000..194fbe3f9 --- /dev/null +++ b/src/Resources/config/resources/block_content.yml @@ -0,0 +1,10 @@ +sylius_resource: + resources: + bitbag_sylius_cms_plugin.block_content: + driver: doctrine/orm + classes: + model: BitBag\SyliusCmsPlugin\Entity\BlockContent + interface: BitBag\SyliusCmsPlugin\Entity\BlockContentInterface + form: BitBag\SyliusCmsPlugin\Form\Type\BlockContentType + repository: BitBag\SyliusCmsPlugin\Repository\BlockContentRepository + factory: Sylius\Component\Resource\Factory\TranslatableFactory diff --git a/src/Resources/config/serialization/Block.xml b/src/Resources/config/serialization/Block.xml index 409b83860..7f41a675b 100644 --- a/src/Resources/config/serialization/Block.xml +++ b/src/Resources/config/serialization/Block.xml @@ -28,5 +28,8 @@ shop:cms:read + + shop:cms:read + diff --git a/src/Resources/config/serialization/BlockTranslation.xml b/src/Resources/config/serialization/BlockTranslation.xml index b56254f8c..10c34736c 100644 --- a/src/Resources/config/serialization/BlockTranslation.xml +++ b/src/Resources/config/serialization/BlockTranslation.xml @@ -4,14 +4,8 @@ xsi:schemaLocation="http://symfony.com/schema/dic/serializer-mapping https://symfony.com/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd" > - - shop:cms:read - shop:cms:read - - shop:cms:read - diff --git a/src/Resources/config/serializer/Entity.Block.yml b/src/Resources/config/serializer/Entity.Block.yml index 785a05373..0ddd253bf 100644 --- a/src/Resources/config/serializer/Entity.Block.yml +++ b/src/Resources/config/serializer/Entity.Block.yml @@ -11,6 +11,10 @@ BitBag\SyliusCmsPlugin\Entity\Block: expose: true type: string groups: [Autocomplete] + contents: + expose: true + type: iterable + groups: [Default] virtual_properties: getName: serialized_name: name diff --git a/src/Resources/config/services/form.xml b/src/Resources/config/services/form.xml index 87053c778..5c46b9591 100644 --- a/src/Resources/config/services/form.xml +++ b/src/Resources/config/services/form.xml @@ -1,6 +1,10 @@ + + BitBag\SyliusCmsPlugin\Form\Type\BlockContent\BlockContentTextConfigurationType::TYPE + + @@ -10,6 +14,13 @@ + + BitBag\SyliusCmsPlugin\Entity\BlockContent + %bitbag_sylius_cms_plugin.form.type.block_content.validation_groups% + + + + %bitbag_sylius_cms_plugin.model.block_translation.class% %bitbag_sylius_cms_plugin.form.type.translation.block.validation_groups% @@ -79,6 +90,12 @@ + + + + + + diff --git a/src/Resources/config/validation/BlockTranslation.xml b/src/Resources/config/validation/BlockTranslation.xml index bd3e547f1..61da5b5dd 100644 --- a/src/Resources/config/validation/BlockTranslation.xml +++ b/src/Resources/config/validation/BlockTranslation.xml @@ -4,30 +4,6 @@ http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> - - - - - - - - - - - - - - - - - - - - diff --git a/src/Resources/views/Block/Crud/_form.html.twig b/src/Resources/views/Block/Crud/_form.html.twig index 979e42571..f6ed4db1e 100755 --- a/src/Resources/views/Block/Crud/_form.html.twig +++ b/src/Resources/views/Block/Crud/_form.html.twig @@ -1,28 +1,28 @@ {% from '@BitBagSyliusCmsPlugin/Macro/translationForm.html.twig' import translationForm %} {% form_theme form '@BitBagSyliusCmsPlugin/Form/theme.html.twig' %} -{% include '@BitBagSyliusCmsPlugin/Modal/_resourcePreview.html.twig' %} - -
+
{{ form_errors(form) }} {{ form_row(form.code) }} + {{ form_row(form.name) }} {{ form_row(form.enabled) }} - {{ form_row(form.products) }} - {{ form_row(form.taxons) }} - {{ form_row(form.collections) }} {{ form_row(form.channels) }} + {{ form_row(form.collections) }} +
+
+
- - - {{ 'bitbag_sylius_cms_plugin.ui.preview'|trans }} - +
+
+
+

{{ 'sylius.ui.configuration'|trans }}

+
+
+ {{ form_row(form.contents) }} +
+