<?php declare(strict_types=1);
namespace Acris\Faq;
use Acris\Faq\Custom\FaqDefinition;
use Acris\Faq\Storefront\Framework\Seo\SeoUrlRoute\FaqPageSeoUrlRoute;
use Doctrine\DBAL\Connection;
use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockCollection;
use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionCollection;
use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotCollection;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\CmsPageEntity;
use Shopware\Core\Content\Media\Aggregate\MediaDefaultFolder\MediaDefaultFolderEntity;
use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Uuid\Uuid;
class AcrisFaqCS extends Plugin
{
public const DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD = 'acrisFaqDefaultFolder';
public const DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME = "FAQ Documents";
public const DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS = 'acris_faq_document';
public const DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS = 'acrisFaqDocuments';
public const DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD = 'acrisFaqImageDefaultFolder';
public const DEFAULT_IMAGE_MEDIA_FOLDER_NAME = "FAQ Images";
public const DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS = 'acris_faq';
public const DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS = 'acrisFaqs';
public const DEFAULT_FAQ_LAYOUT_CMS_PAGE_NAME = 'ACRIS FAQ';
public const DEFAULT_FAQ_LAYOUT_CMS_PAGE_TYPE = 'cms_faq';
public function install(InstallContext $installContext): void
{
$this->createDefaultMediaUploadFolder($installContext->getContext());
}
public function postUpdate(UpdateContext $context): void
{
if (version_compare($context->getCurrentPluginVersion(), '1.2.6', '<')) {
if ($context->getPlugin()->isActive() === true) {
$newMediaFolderId = Uuid::randomHex();
$newDefaultMediaFolderId = Uuid::randomHex();
$this->updateDefaultMediaUploadFolder($newMediaFolderId, $newDefaultMediaFolderId, $context->getContext());
}
}
if (version_compare($context->getCurrentPluginVersion(), '2.3.0', '<')
&& version_compare($context->getUpdatePluginVersion(), '2.3.0', '>=')
&& $context->getPlugin()->isActive() === true) {
$this->insertDefaultSeoUrlTemplate($context->getContext());
$this->insertDefaultCmsLayout($context->getContext());
}
}
public function activate(ActivateContext $context): void
{
parent::activate($context);
$this->insertDefaultSeoUrlTemplate($context->getContext());
$this->insertDefaultCmsLayout($context->getContext());
}
public function update(UpdateContext $context): void
{
parent::update($context);
$this->insertDefaultCmsLayout($context->getContext());
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$this->cleanupDatabase();
$this->cleanDefaultLayout($context->getContext());
$this->removeMediaUploadFolder($context->getContext());
}
private function insertDefaultCmsLayout(Context $context): void
{
$cmsPageRepository = $this->container->get('cms_page.repository');
$cmsPageName = self::DEFAULT_FAQ_LAYOUT_CMS_PAGE_NAME;
$existingSearchResult = $cmsPageRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name', $cmsPageName))->addFilter(new EqualsFilter('locked', true)), $context);
if ($existingSearchResult->getTotal() > 0) {
return;
}
$cmsPage = new CmsPageEntity();
$cmsPage->setId(Uuid::randomHex());
$cmsPage->setName($cmsPageName);
$cmsPage->setLocked(true);
$cmsPage->setType(self::DEFAULT_FAQ_LAYOUT_CMS_PAGE_TYPE);
$cmsPage->setCustomFields(['acrisFaqDefault' => true]);
$sectionPosition = 0;
$sectionCollection = new CmsSectionCollection();
$cmsPage->setSections($sectionCollection);
list($blockFirst) = $this->getBlockAndAddToSectionCollection($sectionCollection, $sectionPosition, $cmsPage->getId());
$this->createSlotAndAdd($blockFirst, $context);
$cmsPageData = $this->convertCmsPageEntity($cmsPage);
$cmsPageRepository->upsert([$cmsPageData], $context);
$connection = $this->container->get(Connection::class);
$connection->executeStatement(
'UPDATE cms_page SET locked = true WHERE id = (:id)',
['id' => Uuid::fromHexToBytes($cmsPage->getId())]
);
}
private function getBlockAndAddToSectionCollection(CmsSectionCollection $sectionCollection, int $position, string $cmsPageId): array
{
$section = new CmsSectionEntity();
$section->setId(Uuid::randomHex());
$section->setPageId($cmsPageId);
$section->setType('default');
$section->setSizingMode('boxed');
$section->setMobileBehavior('wrap');
$section->setPosition($position);
$sectionCollection->add($section);
$blocksCollection = new CmsBlockCollection();
$block = new CmsBlockEntity();
$block->setId(Uuid::randomHex());
$block->setSectionId($section->getId());
$block->setType('text');
$block->setPosition(1);
$block->setSectionPosition('main');
$block->setBackgroundMediaMode('cover');
$block->setMarginTop('0px');
$block->setMarginRight('0px');
$block->setMarginBottom('0px');
$block->setMarginLeft('0px');
$blocksCollection->add($block);
$section->setBlocks($blocksCollection);
$slotCollection = new CmsSlotCollection();
$block->setSlots($slotCollection);
return [$block];
}
private function createSlotAndAdd(CmsBlockEntity $block, Context $context): void
{
$slot = new CmsSlotEntity();
$slot->setId(Uuid::randomHex());
$slot->setBlockId($block->getId());
$slot->setType('text');
$slot->setSlot('content');
$slot->setConfig([
"content" => [
"source" => "mapped",
"value" => 'acris_faq.description'
]
]);
$slotCollection = $block->getSlots();
$slotCollection->add($slot);
$block->setSlots($slotCollection);
}
private function convertCmsPageEntity(CmsPageEntity $cmsPageEntity): array
{
$sectionArray = [];
foreach ($cmsPageEntity->getSections()->getElements() as $sectionEntity) {
$blockArray = [];
foreach ($sectionEntity->getBlocks()->getElements() as $blockEntity) {
$slotArray = [];
foreach ($blockEntity->getSlots()->getElements() as $slotEntity) {
$slotData = $slotEntity->getVars();
unset($slotData['block']);
$slotArray[] = $slotData;
}
$blockData = $blockEntity->getVars();
$blockData['slots'] = $slotArray;
unset($blockData['section']);
unset($blockData['backgroundMedia']);
$blockArray[] = $blockData;
}
$sectionData = $sectionEntity->getVars();
$sectionData['blocks'] = $blockArray;
unset($sectionData['page']);
unset($sectionData['backgroundMedia']);
$sectionArray[] = $sectionData;
}
$cmsPageData = $cmsPageEntity->getVars();
$cmsPageData['sections'] = $sectionArray;
unset($cmsPageData['previewMedia']);
return $cmsPageData;
}
private function cleanDefaultLayout(Context $context): void
{
$cmsPageRepository = $this->container->get('cms_page.repository');
$connection = $this->container->get(Connection::class);
$searchResult = $cmsPageRepository->searchIds((new Criteria())->addFilter(
new EqualsFilter('type', self::DEFAULT_FAQ_LAYOUT_CMS_PAGE_TYPE)
), $context);
$ids = [];
if ($searchResult->getTotal() > 0) {
foreach ($searchResult->getIds() as $id) {
$connection->executeStatement(
'UPDATE cms_page SET locked = false WHERE id = (:id) AND locked = true',
['id' => Uuid::fromHexToBytes($id)]
);
$ids[] = ['id' => $id];
}
$cmsPageRepository->delete($ids, $context);
}
}
private function cleanupDatabase(): void
{
$connection = $this->container->get(Connection::class);
$connection->executeStatement('DROP TABLE IF EXISTS acris_faq_group_product_stream');
$connection->executeStatement('DROP TABLE IF EXISTS acris_faqs_groups');
$connection->executeStatement('DROP TABLE IF EXISTS acris_faq_group');
$connection->executeStatement('DROP TABLE IF EXISTS acris_faq_translation');
$connection->executeStatement('DROP TABLE IF EXISTS acris_faq_document_language');
$connection->executeStatement('DROP TABLE IF EXISTS acris_faq_document_translation');
$connection->executeStatement('DROP TABLE IF EXISTS acris_faq_document');
$connection->executeStatement('DROP TABLE IF EXISTS acris_faq');
$this->removeInheritance($connection, 'media', 'acrisFaqDocuments');
$this->removeInheritance($connection, 'media', 'acrisFaqs');
$this->removeInheritance($connection, 'language', 'acrisFaqDocuments');
$this->removeInheritance($connection, 'cms_page', 'acrisFaqs');
}
private function createDefaultMediaUploadFolder(Context $context): void
{
$documentMediaUploadFolderId = Uuid::randomHex();
$documentDefaultMediaUploadFolderId = Uuid::randomHex();
$imageMediaUploadFolderId = Uuid::randomHex();
$imageDefaultMediaUploadFolderId = Uuid::randomHex();
$defaultMediaFolderRepository = $this->container->get('media_default_folder.repository');
$mediaFolderRepository = $this->container->get('media_folder.repository');
$documentDefaultExistingMediaUploadFolder = $this->getDocumentDefaultMediaUploadFolder($defaultMediaFolderRepository, $context);
$documentExistingMediaUploadFolder = $this->getDocumentMediaUploadFolder($mediaFolderRepository, $context);
$imageDefaultExistingMediaUploadFolder = $this->getImageDefaultMediaUploadFolder($defaultMediaFolderRepository, $context);
$imageExistingMediaUploadFolder = $this->getImageMediaUploadFolder($mediaFolderRepository, $context);
// document folder
if (!$documentDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
$defaultMediaUploadFolder = [
'id' => $documentDefaultMediaUploadFolderId,
'entity' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
'associationFields' => [self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
'customFields' => [
self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
} else {
$documentDefaultMediaUploadFolderId = $documentDefaultExistingMediaUploadFolder->getId();
}
if (!$documentExistingMediaUploadFolder instanceof MediaFolderEntity) {
$defaultMediaUploadFolder = [
'id' => $documentMediaUploadFolderId,
'name' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME,
'useParentConfiguration' => false,
'defaultFolderId' => $documentDefaultMediaUploadFolderId,
'configuration' => [
'createThumbnails' => false,
'keepAspectRatio' => true,
'thumbnailQuality' => 80,
'private' => false,
'noAssociation' => false,
],
'customFields' => [
self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
}
// image folder
if (!$imageDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
$defaultMediaUploadFolder = [
'id' => $imageDefaultMediaUploadFolderId,
'entity' => self::DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
'associationFields' => [self::DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
'customFields' => [
self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
} else {
$imageDefaultMediaUploadFolderId = $imageDefaultExistingMediaUploadFolder->getId();
}
if (!$imageExistingMediaUploadFolder instanceof MediaFolderEntity) {
$mediaThumbnailSizes = $this->container->get('media_thumbnail_size.repository')->search((new Criteria()), $context);
$thumbnails = [];
if (!empty($mediaThumbnailSizes) && $mediaThumbnailSizes instanceof EntitySearchResult && $mediaThumbnailSizes->count() > 0) {
foreach ($mediaThumbnailSizes->getElements() as $element) {
$thumbnails[] = ['id' => $element->getId()];
}
}
$defaultMediaUploadFolder = [
'id' => $imageMediaUploadFolderId,
'name' => self::DEFAULT_IMAGE_MEDIA_FOLDER_NAME,
'useParentConfiguration' => false,
'defaultFolderId' => $imageDefaultMediaUploadFolderId,
'configuration' => [
'createThumbnails' => true,
'keepAspectRatio' => true,
'thumbnailQuality' => 80,
'private' => false,
'noAssociation' => false,
'mediaThumbnailSizes' => $thumbnails
],
'customFields' => [
self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
}
}
private function updateDefaultMediaUploadFolder(string $mediaUploadFolderId, string $defaultMediaUploadFolderId, Context $context): void
{
$documentMediaUploadFolderId = Uuid::randomHex();
$documentDefaultMediaUploadFolderId = Uuid::randomHex();
$imageMediaUploadFolderId = Uuid::randomHex();
$imageDefaultMediaUploadFolderId = Uuid::randomHex();
$defaultMediaFolderRepository = $this->container->get('media_default_folder.repository');
$mediaFolderRepository = $this->container->get('media_folder.repository');
$documentDefaultExistingMediaUploadFolder = $this->getDocumentDefaultMediaUploadFolder($defaultMediaFolderRepository, $context);
$documentExistingMediaUploadFolder = $this->getDocumentMediaUploadFolder($mediaFolderRepository, $context);
$imageDefaultExistingMediaUploadFolder = $this->getImageDefaultMediaUploadFolder($defaultMediaFolderRepository, $context);
$imageExistingMediaUploadFolder = $this->getImageMediaUploadFolder($mediaFolderRepository, $context);
// document folder
if (!$documentDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
$defaultMediaUploadFolder = [
'id' => $documentDefaultMediaUploadFolderId,
'entity' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
'associationFields' => [self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
'customFields' => [
self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
} else {
$documentDefaultMediaUploadFolderId = $documentDefaultExistingMediaUploadFolder->getId();
$defaultMediaUploadFolder = [
'id' => $documentDefaultMediaUploadFolderId,
'entity' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
'associationFields' => [self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
'customFields' => [
self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_default_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
}
if (!$documentExistingMediaUploadFolder instanceof MediaFolderEntity) {
$defaultMediaUploadFolder = [
'id' => $documentMediaUploadFolderId,
'name' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME,
'useParentConfiguration' => false,
'defaultFolderId' => $documentDefaultMediaUploadFolderId,
'configuration' => [
'createThumbnails' => false,
'keepAspectRatio' => true,
'thumbnailQuality' => 80,
'private' => false,
'noAssociation' => false,
],
'customFields' => [
self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
} else {
$defaultMediaUploadFolder = [
'id' => $documentExistingMediaUploadFolder->getId(),
'name' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME,
'useParentConfiguration' => false,
'defaultFolderId' => $documentDefaultMediaUploadFolderId,
'configuration' => [
'createThumbnails' => false,
'keepAspectRatio' => true,
'thumbnailQuality' => 80,
'private' => false,
'noAssociation' => false,
],
'customFields' => [
self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
}
// image folder
if (!$imageDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
$defaultMediaUploadFolder = [
'id' => $imageDefaultMediaUploadFolderId,
'entity' => self::DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
'associationFields' => [self::DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
'customFields' => [
self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
} else {
$imageDefaultMediaUploadFolderId = $imageDefaultExistingMediaUploadFolder->getId();
$defaultMediaUploadFolder = [
'id' => $imageDefaultMediaUploadFolderId,
'entity' => self::DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
'associationFields' => [self::DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
'customFields' => [
self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_default_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
}
if (!$imageExistingMediaUploadFolder instanceof MediaFolderEntity) {
$mediaThumbnailSizes = $this->container->get('media_thumbnail_size.repository')->search((new Criteria()), $context);
$thumbnails = [];
if (!empty($mediaThumbnailSizes) && $mediaThumbnailSizes instanceof EntitySearchResult && $mediaThumbnailSizes->count() > 0) {
foreach ($mediaThumbnailSizes->getElements() as $element) {
$thumbnails[] = ['id' => $element->getId()];
}
}
$defaultMediaUploadFolder = [
'id' => $imageMediaUploadFolderId,
'name' => self::DEFAULT_IMAGE_MEDIA_FOLDER_NAME,
'useParentConfiguration' => false,
'defaultFolderId' => $imageDefaultMediaUploadFolderId,
'configuration' => [
'createThumbnails' => true,
'keepAspectRatio' => true,
'thumbnailQuality' => 80,
'private' => false,
'noAssociation' => false,
'mediaThumbnailSizes' => $thumbnails
],
'customFields' => [
self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
} else {
$mediaThumbnailSizes = $this->container->get('media_thumbnail_size.repository')->search((new Criteria()), $context);
$thumbnails = [];
if (!empty($mediaThumbnailSizes) && $mediaThumbnailSizes instanceof EntitySearchResult && $mediaThumbnailSizes->count() > 0) {
foreach ($mediaThumbnailSizes->getElements() as $element) {
$thumbnails[] = ['id' => $element->getId()];
}
}
$defaultMediaUploadFolder = [
'id' => $imageExistingMediaUploadFolder->getId(),
'name' => self::DEFAULT_IMAGE_MEDIA_FOLDER_NAME,
'useParentConfiguration' => false,
'defaultFolderId' => $imageDefaultMediaUploadFolderId,
'configuration' => [
'createThumbnails' => true,
'keepAspectRatio' => true,
'thumbnailQuality' => 80,
'private' => false,
'noAssociation' => false,
'mediaThumbnailSizes' => $thumbnails
],
'customFields' => [
self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
]
];
$this->container->get('media_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
}
}
private function removeMediaUploadFolder(Context $context): void
{
$defaultMediaFolderRepository = $this->container->get('media_default_folder.repository');
$mediaFolderRepository = $this->container->get('media_folder.repository');
$documentMediaUploadFolder = $this->getDocumentMediaUploadFolder($mediaFolderRepository, $context);
$documentDefaultMediaUploadFolder = $this->getDocumentDefaultMediaUploadFolder($defaultMediaFolderRepository, $context);
$imageMediaUploadFolder = $this->getImageMediaUploadFolder($mediaFolderRepository, $context);
$imageDefaultMediaUploadFolder = $this->getImageDefaultMediaUploadFolder($defaultMediaFolderRepository, $context);
// document folder
if ($documentMediaUploadFolder instanceof MediaFolderEntity) {
if ($documentMediaUploadFolder->getMedia() && $documentMediaUploadFolder->getMedia()->count() > 0) {
return;
}
$defaultMediaUploadFolderConfiguration = $documentMediaUploadFolder->getConfiguration();
$deleteConfigurationId = null;
if ($defaultMediaUploadFolderConfiguration && $defaultMediaUploadFolderConfiguration->getMediaFolders()) {
if ($defaultMediaUploadFolderConfiguration->getMediaFolders()->count() < 2) {
$deleteConfigurationId = $defaultMediaUploadFolderConfiguration->getId();
}
}
$mediaFolderRepository->delete([['id' => $documentMediaUploadFolder->getId()]], $context);
if ($documentDefaultMediaUploadFolder instanceof MediaDefaultFolderEntity) {
$defaultMediaFolderRepository->delete([['id' => $documentDefaultMediaUploadFolder->getId()]], $context);
}
if ($deleteConfigurationId !== null) {
$this->container->get('media_folder_configuration.repository')->delete([['id' => $deleteConfigurationId]], $context);
}
}
// image folder
if ($imageMediaUploadFolder instanceof MediaFolderEntity) {
if ($imageMediaUploadFolder->getMedia() && $imageMediaUploadFolder->getMedia()->count() > 0) {
return;
}
$defaultMediaUploadFolderConfiguration = $imageMediaUploadFolder->getConfiguration();
$deleteConfigurationId = null;
if ($defaultMediaUploadFolderConfiguration && $defaultMediaUploadFolderConfiguration->getMediaFolders()) {
if ($defaultMediaUploadFolderConfiguration->getMediaFolders()->count() < 2) {
$deleteConfigurationId = $defaultMediaUploadFolderConfiguration->getId();
}
}
$mediaFolderRepository->delete([['id' => $imageMediaUploadFolder->getId()]], $context);
if ($imageDefaultMediaUploadFolder instanceof MediaDefaultFolderEntity) {
$defaultMediaFolderRepository->delete([['id' => $imageDefaultMediaUploadFolder->getId()]], $context);
}
if ($deleteConfigurationId !== null) {
$this->container->get('media_folder_configuration.repository')->delete([['id' => $deleteConfigurationId]], $context);
}
}
}
private function getDocumentDefaultMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepository, Context $context): ?MediaDefaultFolderEntity
{
return $mediaFolderRepository->search((new Criteria())->addAssociation('folder')->addAssociation('folder.media')->addAssociation('folder.configuration')->addAssociation('folder.configuration.mediaFolders')->addFilter(new EqualsFilter('customFields.' . self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD, 'true')), $context)->first();
}
private function getDocumentMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepository, Context $context): ?MediaFolderEntity
{
return $mediaFolderRepository->search((new Criteria())->addAssociation('media')->addAssociation('configuration')->addAssociation('configuration.mediaFolders')->addFilter(new EqualsFilter('customFields.' . self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD, 'true')), $context)->first();
}
private function getImageDefaultMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepository, Context $context): ?MediaDefaultFolderEntity
{
return $mediaFolderRepository->search((new Criteria())->addAssociation('folder')->addAssociation('folder.media')->addAssociation('folder.configuration')->addAssociation('folder.configuration.mediaFolders')->addFilter(new EqualsFilter('customFields.' . self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD, 'true')), $context)->first();
}
private function getImageMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepository, Context $context): ?MediaFolderEntity
{
return $mediaFolderRepository->search((new Criteria())->addAssociation('media')->addAssociation('configuration')->addAssociation('configuration.mediaFolders')->addFilter(new EqualsFilter('customFields.' . self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD, 'true')), $context)->first();
}
private function removeInheritance(Connection $connection, string $entity, string $propertyName): void
{
$sql = str_replace(
['#table#', '#column#'],
[$entity, $propertyName],
'ALTER TABLE `#table#` DROP `#column#`'
);
$connection->executeStatement($sql);
}
private function insertDefaultSeoUrlTemplate(Context $context)
{
/** @var EntityRepositoryInterface $seoUrlTemplateRepository */
$seoUrlTemplateRepository = $this->container->get('seo_url_template.repository');
$defaultSeoUrlTemplates = [
[
'routeName' => FaqPageSeoUrlRoute::ROUTE_NAME,
'entityName' => FaqDefinition::ENTITY_NAME,
'template' => FaqPageSeoUrlRoute::DEFAULT_TEMPLATE,
'isValid' => true
]
];
foreach ($defaultSeoUrlTemplates as $defaultSeoUrlTemplate) {
$this->createSeoUrlTemplateIfNotExists($seoUrlTemplateRepository, $context, $defaultSeoUrlTemplate);
}
}
/**
* @param EntityRepositoryInterface $entityRepository
* @param Context $context
* @param array $seoUrlTemplateData
*/
private function createSeoUrlTemplateIfNotExists(EntityRepositoryInterface $entityRepository, Context $context, array $seoUrlTemplateData): void
{
$exists = $entityRepository->search((new Criteria())->addFilter(new EqualsFilter('routeName', $seoUrlTemplateData['routeName']))->addFilter(new EqualsFilter('entityName', $seoUrlTemplateData['entityName']))->addFilter(new EqualsFilter('template', $seoUrlTemplateData['template'])), $context);
if ($exists->getTotal() === 0) {
$entityRepository->create([$seoUrlTemplateData], $context);
}
}
}