<?php declare(strict_types=1);
namespace Samson\Subscriber;
/***
*
* This file is part of the "SAMSON Shop" project.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* (c) 2022
*
***/
use Samson\CustomFieldSet\Constants\CmsSectionCustomFieldConstants;
use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\CmsPageEntity;
use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* class CmsPageLoadedSubscriber
* @package Samson\Subscriber
* @author Christian Schmitz <christian.schmitz@dkd.de>
*/
class CmsPageLoadedSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $mediaRepository;
public function __construct(EntityRepositoryInterface $mediaRepository)
{
$this->mediaRepository = $mediaRepository;
}
public static function getSubscribedEvents(): array
{
return [
CmsPageLoadedEvent::class => 'onCmsPageLoaded',
];
}
public function onCmsPageLoaded(CmsPageLoadedEvent $event): void
{
foreach ($event->getResult() as $cmsPage) {
$this->sliderDefaults($cmsPage);
$this->processBlockMedia($cmsPage, $event->getSalesChannelContext());
}
}
private function sliderDefaults(CmsPageEntity $cmsPage) {
$sliderSections = $cmsPage->getSections()->filterByProperty( 'type', "slider-section" );
$sliderSections->map( function ( CmsSectionEntity $section ) {
$customFields = $section->getCustomFields() ?? [];
$sliderConfig = [
'verticalAlign' => [
'value' => 'center'
],
'displayMode' => [
'value' => 'standard'
],
'navigationArrows' => [
'value' => ( $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_VISIBILITY ] ?? false ) ? 'outer' : false
],
'navigationArrowsColor' => [
'value' => $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_COLOR ] ?? false
],
'navigationArrowsBackgroundColor' => [
'value' => $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_BACKGROUND_COLOR ] ?? false
],
'navigationArrowsBorderColor' => [
'value' => $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_ARROW_BORDER_COLOR ] ?? false
],
'navigationDots' => [
'value' => $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_BULLET_VISIBILITY ] ?? false
],
'navigationDotsColor' => [
'value' => $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_BULLET_COLOR ] ?? false
],
'navigationDotsPosition' => [
'value' => $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_BULLET_POSITION ] ?? 'outer'
],
'slideMaxWidth' => [
'value' => $customFields[ CmsSectionCustomFieldConstants::CUSTOM_FIELD_SLIDER_SECTION_SLIDE_MAX_WIDTH ] ?? 'false'
],
];
$section->assign( [ 'sliderConfig' => $sliderConfig ] );
} );
}
private function processBlockMedia(CmsPageEntity $cmsPage, SalesChannelContext $context): void
{
$contentTypes = [
'image-carousel-content' => 'carouselContents',
];
foreach ($cmsPage->getSections()->getBlocks() as $value) {
if (!in_array($value->getType(), [
'hero-tiles-column',
'image-carousel',
])) {
continue;
}
foreach ($value->getSlots()->getElements() as $element) {
if ($element->getType() === 'hero-tiles-media') {
$this->processElementMedia($element, $context->getContext());
}
if (array_key_exists($element->getType(), $contentTypes)) {
$content = $contentTypes[$element->getType()];
$this->processContentMedia($element, $content, $context->getContext());
}
}
}
}
private function processElementMedia(CmsSlotEntity $cmsSlotEntity, Context $context)
{
$config = $cmsSlotEntity->getConfig();
$ids = [];
foreach (['media', 'mobileMedia', 'mediaVideo', 'mediaImage'] as $mediaKey) {
$mediaId = $this->getMediaIdFromConfig($config, $mediaKey);
if (!is_null($mediaId)) {
$ids[] = $mediaId;
}
}
$criteria = new Criteria($ids);
$media = $this->mediaRepository
->search($criteria, $context)
->getEntities();
if ($media->count() > 0) {
$cmsSlotEntity->setData($media);
}
}
private function processContentMedia(CmsSlotEntity $cmsSlotEntity, string $content, Context $context)
{
$config = $cmsSlotEntity->getConfig();
$mediaIds = array_column($config[$content]['value'], 'mediaId');
$criteria = new Criteria($mediaIds);
$media = $this->mediaRepository
->search($criteria, $context)
->getEntities();
if ($media->count() > 0) {
$cmsSlotEntity->setData($media);
}
}
private function getMediaIdFromConfig(array $config, string $key): ?string
{
return isset($config[$key]) && isset($config[$key]['value']) ? $config[$key]['value'] : null;
}
}