Skip to content

Commit

Permalink
Merge branch 'feature/OP-312-redesign-cms-plugin' into feature/OP-438
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly authored Sep 4, 2024
2 parents 4c99e55 + 30a6f53 commit ded8685
Show file tree
Hide file tree
Showing 21 changed files with 116 additions and 364 deletions.
13 changes: 3 additions & 10 deletions spec/Resolver/MediaResourceResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ final class MediaResourceResolverSpec extends ObjectBehavior
{
public function let(
MediaRepositoryInterface $mediaRepository,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
LoggerInterface $logger,
) {
$this->beConstructedWith($mediaRepository, $localeContext, $channelContext, $logger);
$this->beConstructedWith($mediaRepository, $channelContext, $logger);
}

public function it_is_initializable(): void
Expand All @@ -43,40 +42,34 @@ public function it_implements_media_resource_resolver_interface(): void

public function it_returns_media_when_found(
MediaRepositoryInterface $mediaRepository,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
MediaInterface $media,
ChannelInterface $channel,
) {
$code = 'media_code';
$localeCode = 'en_US';
$channelCode = 'ecommerce';

$channelContext->getChannel()->willReturn($channel);
$channel->getCode()->willReturn($channelCode);
$localeContext->getLocaleCode()->willReturn($localeCode);

$mediaRepository->findOneEnabledByCode($code, $localeCode, $channelCode)->willReturn($media);
$mediaRepository->findOneEnabledByCode($code, $channelCode)->willReturn($media);

$this->findOrLog($code)->shouldReturn($media);
}

public function it_logs_warning_and_returns_null_when_media_not_found(
MediaRepositoryInterface $mediaRepository,
LocaleContextInterface $localeContext,
ChannelContextInterface $channelContext,
LoggerInterface $logger,
ChannelInterface $channel,
) {
$code = 'non_existing_code';
$localeCode = 'en_US';
$channelCode = 'ecommerce';

$channelContext->getChannel()->willReturn($channel);
$channel->getCode()->willReturn($channelCode);
$localeContext->getLocaleCode()->willReturn($localeCode);

$mediaRepository->findOneEnabledByCode($code, $localeCode, $channelCode)->willReturn(null);
$mediaRepository->findOneEnabledByCode($code, $channelCode)->willReturn(null);

$logger->warning(sprintf('Media with "%s" code was not found in the database.', $code))->shouldBeCalled();

Expand Down
6 changes: 3 additions & 3 deletions src/Entity/MediaTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class MediaTranslation extends AbstractTranslation implements MediaTranslationIn
{
protected ?int $id;

protected ?string $content;
protected ?string $content = null;

protected ?string $alt;
protected ?string $alt = null;

protected ?string $link;
protected ?string $link = null;

public function getId(): ?int
{
Expand Down
32 changes: 32 additions & 0 deletions src/Form/DataTransformer/ContentElementDataTransformerChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file was created by developers working at BitBag
* Do you need more information about us and what we do? Visit our https://bitbag.io website!
* 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
*/

declare(strict_types=1);

namespace BitBag\SyliusCmsPlugin\Form\DataTransformer;

use Sylius\Component\Resource\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;

final class ContentElementDataTransformerChecker
{
public function check(FormBuilderInterface $builder, RepositoryInterface $repository, string $field): void
{
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($repository, $field): void {
$data = $event->getData();
$code = $data[$field] ?? null;
$entity = $repository->findOneBy(['code' => $code]);
if (null === $entity) {
$data[$field] = null;
$event->setData($data);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements;

use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker;
use BitBag\SyliusCmsPlugin\Form\Type\PageCollectionAutocompleteChoiceType;
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
use Sylius\Component\Resource\Repository\RepositoryInterface;
Expand All @@ -21,8 +22,10 @@ final class PagesCollectionContentElementType extends AbstractType
{
public const TYPE = 'pages_collection';

public function __construct(private RepositoryInterface $collectionRepository)
{
public function __construct(
private RepositoryInterface $collectionRepository,
private ContentElementDataTransformerChecker $contentElementDataTransformerChecker,
) {
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -36,6 +39,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->get(self::TYPE)->addModelTransformer(
new ReversedTransformer(new ResourceToIdentifierTransformer($this->collectionRepository, 'code')),
);

$this->contentElementDataTransformerChecker->check($builder, $this->collectionRepository, self::TYPE);
}

public function getBlockPrefix(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements;

use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker;
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType;
use Sylius\Component\Resource\Repository\RepositoryInterface;
Expand All @@ -21,8 +22,10 @@ final class ProductsCarouselByTaxonContentElementType extends AbstractType
{
public const TYPE = 'products_carousel_by_taxon';

public function __construct(private RepositoryInterface $taxonRepository)
{
public function __construct(
private RepositoryInterface $taxonRepository,
private ContentElementDataTransformerChecker $contentElementDataTransformerChecker,
) {
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -38,6 +41,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->get(self::TYPE)->addModelTransformer(
new ReversedTransformer(new ResourceToIdentifierTransformer($this->taxonRepository, 'code')),
);

$this->contentElementDataTransformerChecker->check($builder, $this->taxonRepository, self::TYPE);
}

public function getBlockPrefix(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements;

use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker;
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
use Sylius\Bundle\TaxonomyBundle\Form\Type\TaxonAutocompleteChoiceType;
use Sylius\Component\Resource\Repository\RepositoryInterface;
Expand All @@ -21,8 +22,10 @@ final class ProductsGridByTaxonContentElementType extends AbstractType
{
public const TYPE = 'products_grid_by_taxon';

public function __construct(private RepositoryInterface $taxonRepository)
{
public function __construct(
private RepositoryInterface $taxonRepository,
private ContentElementDataTransformerChecker $contentElementDataTransformerChecker,
) {
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -38,6 +41,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->get(self::TYPE)->addModelTransformer(
new ReversedTransformer(new ResourceToIdentifierTransformer($this->taxonRepository, 'code')),
);

$this->contentElementDataTransformerChecker->check($builder, $this->taxonRepository, self::TYPE);
}

public function getBlockPrefix(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BitBag\SyliusCmsPlugin\Form\Type\ContentElements;

use BitBag\SyliusCmsPlugin\Form\DataTransformer\ContentElementDataTransformerChecker;
use BitBag\SyliusCmsPlugin\Form\Type\MediaAutocompleteChoiceType;
use Sylius\Bundle\ResourceBundle\Form\DataTransformer\ResourceToIdentifierTransformer;
use Sylius\Component\Resource\Repository\RepositoryInterface;
Expand All @@ -21,8 +22,10 @@ final class SingleMediaContentElementType extends AbstractType
{
public const TYPE = 'single_media';

public function __construct(private RepositoryInterface $mediaRepository)
{
public function __construct(
private RepositoryInterface $mediaRepository,
private ContentElementDataTransformerChecker $contentElementDataTransformerChecker,
) {
}

public function buildForm(FormBuilderInterface $builder, array $options): void
Expand All @@ -36,6 +39,8 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->get(self::TYPE)->addModelTransformer(
new ReversedTransformer(new ResourceToIdentifierTransformer($this->mediaRepository, 'code')),
);

$this->contentElementDataTransformerChecker->check($builder, $this->mediaRepository, self::TYPE);
}

public function getBlockPrefix(): string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public function render(ContentConfigurationInterface $contentConfiguration): str
return '';
}

/** @var CollectionInterface $collection */
/** @var CollectionInterface|null $collection */
$collection = $this->collectionRepository->findOneBy(['code' => $code]);

return $this->twig->render('@BitBagSyliusCmsPlugin/Shop/ContentElement/index.html.twig', [
'content_element' => '@BitBagSyliusCmsPlugin/Shop/ContentElement/_pages_collection.html.twig',
'collection' => $collection->getPages(),
'collection' => $collection?->getPages(),
]);
}
}
46 changes: 0 additions & 46 deletions src/Repository/MediaRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,66 +26,20 @@ public function createListQueryBuilder(string $locale): QueryBuilder

public function findOneEnabledByCode(
string $code,
string $localeCode,
string $channelCode,
): ?MediaInterface {
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.channels', 'channels')
->where('translation.locale = :localeCode')
->andWhere('o.code = :code')
->andWhere('o.enabled = true')
->andWhere('channels.code = :channelCode')
->setParameter('code', $code)
->setParameter('localeCode', $localeCode)
->setParameter('channelCode', $channelCode)
->getQuery()
->getOneOrNullResult()
;
}

public function findByCollectionCode(
string $collectionCode,
string $localeCode,
string $channelCode,
): array {
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.collections', 'collection')
->innerJoin('o.channels', 'channels')
->andWhere('translation.locale = :localeCode')
->andWhere('collection.code = :collectionCode')
->andWhere('o.enabled = true')
->andWhere('channels.code = :channelCode')
->setParameter('localeCode', $localeCode)
->setParameter('collectionCode', $collectionCode)
->setParameter('channelCode', $channelCode)
->getQuery()
->getResult()
;
}

public function findByProductCode(
string $productCode,
string $localeCode,
string $channelCode,
): array {
return $this->createQueryBuilder('o')
->leftJoin('o.translations', 'translation')
->innerJoin('o.products', 'product')
->innerJoin('o.channels', 'channels')
->andWhere('translation.locale = :localeCode')
->andWhere('product.code = :productCode')
->andWhere('o.enabled = true')
->andWhere('channels.code = :channelCode')
->setParameter('localeCode', $localeCode)
->setParameter('productCode', $productCode)
->setParameter('channelCode', $channelCode)
->getQuery()
->getResult()
;
}

public function findByNamePart(string $phrase, array $mediaType): array
{
return $this->createQueryBuilder('o')
Expand Down
13 changes: 0 additions & 13 deletions src/Repository/MediaRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,8 @@ public function createListQueryBuilder(string $locale): QueryBuilder;

public function findOneEnabledByCode(
string $code,
string $localeCode,
string $channelCode,
): ?MediaInterface;

public function findByCollectionCode(
string $collectionCode,
string $localeCode,
string $channelCode,
): array;

public function findByProductCode(
string $productCode,
string $localeCode,
string $channelCode,
): array;

public function findByNamePart(string $phrase, array $mediaType): array;
}
3 changes: 0 additions & 3 deletions src/Resolver/MediaResourceResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface;
use Psr\Log\LoggerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Webmozart\Assert\Assert;

final class MediaResourceResolver implements MediaResourceResolverInterface
{
public function __construct(
private MediaRepositoryInterface $mediaRepository,
private LocaleContextInterface $localeContext,
private ChannelContextInterface $channelContext,
private LoggerInterface $logger,
) {
Expand All @@ -32,7 +30,6 @@ public function findOrLog(string $code): ?MediaInterface
Assert::notNull($this->channelContext->getChannel()->getCode());
$media = $this->mediaRepository->findOneEnabledByCode(
$code,
$this->localeContext->getLocaleCode(),
$this->channelContext->getChannel()->getCode(),
);

Expand Down
8 changes: 4 additions & 4 deletions src/Resources/assets/admin/js/bitbag/bitbag-cms-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,28 @@ export class HandlePreview {
}

_$_CKEDITOR_UPDATE_INSTANCES() {
[...CKEDITOR.instances].forEach((instance) => instance.updateElement());
Object.values(CKEDITOR.instances).forEach((instance) => instance.updateElement());
}

_resourcePreview() {
this.button.addEventListener('click', (e) => {
e.preventDefault();

this._$_CKEDITOR_UPDATE_INSTANCES;
this._$_CKEDITOR_UPDATE_INSTANCES();
this._createPreview();
this._$_CKEDITOR_MODAL_SHOW();
});
document.querySelector(`[${this.channelSelector}]`).addEventListener('change', (e) => {
e.preventDefault();

this._$_CKEDITOR_UPDATE_INSTANCES;
this._$_CKEDITOR_UPDATE_INSTANCES();
this._createPreview();
this._$_CKEDITOR_MODAL_SHOW();
});
document.querySelector(`[${this.localeSelector}]`).addEventListener('change', (e) => {
e.preventDefault();

this._$_CKEDITOR_UPDATE_INSTANCES;
this._$_CKEDITOR_UPDATE_INSTANCES();
this._createPreview();
this._$_CKEDITOR_MODAL_SHOW();
});
Expand Down
Loading

0 comments on commit ded8685

Please sign in to comment.