custom/plugins/AcrisFaqCS/src/AcrisFaqCS.php line 29

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Faq;
  3. use Acris\Faq\Custom\FaqDefinition;
  4. use Acris\Faq\Storefront\Framework\Seo\SeoUrlRoute\FaqPageSeoUrlRoute;
  5. use Doctrine\DBAL\Connection;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockCollection;
  7. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  8. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionCollection;
  9. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  10. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotCollection;
  11. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  12. use Shopware\Core\Content\Cms\CmsPageEntity;
  13. use Shopware\Core\Content\Media\Aggregate\MediaDefaultFolder\MediaDefaultFolderEntity;
  14. use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
  15. use Shopware\Core\Framework\Context;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  20. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  21. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  22. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  23. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  24. use Shopware\Core\Framework\Plugin;
  25. use Shopware\Core\Framework\Uuid\Uuid;
  26. class AcrisFaqCS extends Plugin
  27. {
  28.     public const DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD 'acrisFaqDefaultFolder';
  29.     public const DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME "FAQ Documents";
  30.     public const DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS 'acris_faq_document';
  31.     public const DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS 'acrisFaqDocuments';
  32.     public const DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD 'acrisFaqImageDefaultFolder';
  33.     public const DEFAULT_IMAGE_MEDIA_FOLDER_NAME "FAQ Images";
  34.     public const DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS 'acris_faq';
  35.     public const DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS 'acrisFaqs';
  36.     public const DEFAULT_FAQ_LAYOUT_CMS_PAGE_NAME 'ACRIS FAQ';
  37.     public const DEFAULT_FAQ_LAYOUT_CMS_PAGE_TYPE 'cms_faq';
  38.     public function install(InstallContext $installContext): void
  39.     {
  40.         $this->createDefaultMediaUploadFolder($installContext->getContext());
  41.     }
  42.     public function postUpdate(UpdateContext $context): void
  43.     {
  44.         if (version_compare($context->getCurrentPluginVersion(), '1.2.6''<')) {
  45.             if ($context->getPlugin()->isActive() === true) {
  46.                 $newMediaFolderId Uuid::randomHex();
  47.                 $newDefaultMediaFolderId Uuid::randomHex();
  48.                 $this->updateDefaultMediaUploadFolder($newMediaFolderId$newDefaultMediaFolderId$context->getContext());
  49.             }
  50.         }
  51.         if (version_compare($context->getCurrentPluginVersion(), '2.3.0''<')
  52.             && version_compare($context->getUpdatePluginVersion(), '2.3.0''>=')
  53.             && $context->getPlugin()->isActive() === true) {
  54.             $this->insertDefaultSeoUrlTemplate($context->getContext());
  55.             $this->insertDefaultCmsLayout($context->getContext());
  56.         }
  57.     }
  58.     public function activate(ActivateContext $context): void
  59.     {
  60.         parent::activate($context);
  61.         $this->insertDefaultSeoUrlTemplate($context->getContext());
  62.         $this->insertDefaultCmsLayout($context->getContext());
  63.     }
  64.     public function update(UpdateContext $context): void
  65.     {
  66.         parent::update($context);
  67.         $this->insertDefaultCmsLayout($context->getContext());
  68.     }
  69.     public function uninstall(UninstallContext $context): void
  70.     {
  71.         if ($context->keepUserData()) {
  72.             return;
  73.         }
  74.         $this->cleanupDatabase();
  75.         $this->cleanDefaultLayout($context->getContext());
  76.         $this->removeMediaUploadFolder($context->getContext());
  77.     }
  78.     private function insertDefaultCmsLayout(Context $context): void
  79.     {
  80.         $cmsPageRepository $this->container->get('cms_page.repository');
  81.         $cmsPageName self::DEFAULT_FAQ_LAYOUT_CMS_PAGE_NAME;
  82.         $existingSearchResult $cmsPageRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$cmsPageName))->addFilter(new EqualsFilter('locked'true)), $context);
  83.         if ($existingSearchResult->getTotal() > 0) {
  84.             return;
  85.         }
  86.         $cmsPage = new CmsPageEntity();
  87.         $cmsPage->setId(Uuid::randomHex());
  88.         $cmsPage->setName($cmsPageName);
  89.         $cmsPage->setLocked(true);
  90.         $cmsPage->setType(self::DEFAULT_FAQ_LAYOUT_CMS_PAGE_TYPE);
  91.         $cmsPage->setCustomFields(['acrisFaqDefault' => true]);
  92.         $sectionPosition 0;
  93.         $sectionCollection = new CmsSectionCollection();
  94.         $cmsPage->setSections($sectionCollection);
  95.         list($blockFirst) = $this->getBlockAndAddToSectionCollection($sectionCollection$sectionPosition$cmsPage->getId());
  96.         $this->createSlotAndAdd($blockFirst$context);
  97.         $cmsPageData $this->convertCmsPageEntity($cmsPage);
  98.         $cmsPageRepository->upsert([$cmsPageData], $context);
  99.         $connection $this->container->get(Connection::class);
  100.         $connection->executeStatement(
  101.             'UPDATE cms_page SET locked = true WHERE id = (:id)',
  102.             ['id' => Uuid::fromHexToBytes($cmsPage->getId())]
  103.         );
  104.     }
  105.     private function getBlockAndAddToSectionCollection(CmsSectionCollection $sectionCollectionint $positionstring $cmsPageId): array
  106.     {
  107.         $section = new CmsSectionEntity();
  108.         $section->setId(Uuid::randomHex());
  109.         $section->setPageId($cmsPageId);
  110.         $section->setType('default');
  111.         $section->setSizingMode('boxed');
  112.         $section->setMobileBehavior('wrap');
  113.         $section->setPosition($position);
  114.         $sectionCollection->add($section);
  115.         $blocksCollection = new CmsBlockCollection();
  116.         $block = new CmsBlockEntity();
  117.         $block->setId(Uuid::randomHex());
  118.         $block->setSectionId($section->getId());
  119.         $block->setType('text');
  120.         $block->setPosition(1);
  121.         $block->setSectionPosition('main');
  122.         $block->setBackgroundMediaMode('cover');
  123.         $block->setMarginTop('0px');
  124.         $block->setMarginRight('0px');
  125.         $block->setMarginBottom('0px');
  126.         $block->setMarginLeft('0px');
  127.         $blocksCollection->add($block);
  128.         $section->setBlocks($blocksCollection);
  129.         $slotCollection = new CmsSlotCollection();
  130.         $block->setSlots($slotCollection);
  131.         return [$block];
  132.     }
  133.     private function createSlotAndAdd(CmsBlockEntity $blockContext $context): void
  134.     {
  135.         $slot = new CmsSlotEntity();
  136.         $slot->setId(Uuid::randomHex());
  137.         $slot->setBlockId($block->getId());
  138.         $slot->setType('text');
  139.         $slot->setSlot('content');
  140.         $slot->setConfig([
  141.             "content" => [
  142.                 "source" => "mapped",
  143.                 "value" => 'acris_faq.description'
  144.             ]
  145.         ]);
  146.         $slotCollection $block->getSlots();
  147.         $slotCollection->add($slot);
  148.         $block->setSlots($slotCollection);
  149.     }
  150.     private function convertCmsPageEntity(CmsPageEntity $cmsPageEntity): array
  151.     {
  152.         $sectionArray = [];
  153.         foreach ($cmsPageEntity->getSections()->getElements() as $sectionEntity) {
  154.             $blockArray = [];
  155.             foreach ($sectionEntity->getBlocks()->getElements() as $blockEntity) {
  156.                 $slotArray = [];
  157.                 foreach ($blockEntity->getSlots()->getElements() as $slotEntity) {
  158.                     $slotData $slotEntity->getVars();
  159.                     unset($slotData['block']);
  160.                     $slotArray[] = $slotData;
  161.                 }
  162.                 $blockData $blockEntity->getVars();
  163.                 $blockData['slots'] = $slotArray;
  164.                 unset($blockData['section']);
  165.                 unset($blockData['backgroundMedia']);
  166.                 $blockArray[] = $blockData;
  167.             }
  168.             $sectionData $sectionEntity->getVars();
  169.             $sectionData['blocks'] = $blockArray;
  170.             unset($sectionData['page']);
  171.             unset($sectionData['backgroundMedia']);
  172.             $sectionArray[] = $sectionData;
  173.         }
  174.         $cmsPageData $cmsPageEntity->getVars();
  175.         $cmsPageData['sections'] = $sectionArray;
  176.         unset($cmsPageData['previewMedia']);
  177.         return $cmsPageData;
  178.     }
  179.     private function cleanDefaultLayout(Context $context): void
  180.     {
  181.         $cmsPageRepository $this->container->get('cms_page.repository');
  182.         $connection $this->container->get(Connection::class);
  183.         $searchResult $cmsPageRepository->searchIds((new Criteria())->addFilter(
  184.             new EqualsFilter('type'self::DEFAULT_FAQ_LAYOUT_CMS_PAGE_TYPE)
  185.         ), $context);
  186.         $ids = [];
  187.         if ($searchResult->getTotal() > 0) {
  188.             foreach ($searchResult->getIds() as $id) {
  189.                 $connection->executeStatement(
  190.                     'UPDATE cms_page SET locked = false WHERE id = (:id) AND locked = true',
  191.                     ['id' => Uuid::fromHexToBytes($id)]
  192.                 );
  193.                 $ids[] = ['id' => $id];
  194.             }
  195.             $cmsPageRepository->delete($ids$context);
  196.         }
  197.     }
  198.     private function cleanupDatabase(): void
  199.     {
  200.         $connection $this->container->get(Connection::class);
  201.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faq_group_product_stream');
  202.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faqs_groups');
  203.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faq_group');
  204.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faq_translation');
  205.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faq_document_language');
  206.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faq_document_translation');
  207.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faq_document');
  208.         $connection->executeStatement('DROP TABLE IF EXISTS acris_faq');
  209.         $this->removeInheritance($connection'media''acrisFaqDocuments');
  210.         $this->removeInheritance($connection'media''acrisFaqs');
  211.         $this->removeInheritance($connection'language''acrisFaqDocuments');
  212.         $this->removeInheritance($connection'cms_page''acrisFaqs');
  213.     }
  214.     private function createDefaultMediaUploadFolder(Context $context): void
  215.     {
  216.         $documentMediaUploadFolderId Uuid::randomHex();
  217.         $documentDefaultMediaUploadFolderId Uuid::randomHex();
  218.         $imageMediaUploadFolderId Uuid::randomHex();
  219.         $imageDefaultMediaUploadFolderId Uuid::randomHex();
  220.         $defaultMediaFolderRepository $this->container->get('media_default_folder.repository');
  221.         $mediaFolderRepository $this->container->get('media_folder.repository');
  222.         $documentDefaultExistingMediaUploadFolder $this->getDocumentDefaultMediaUploadFolder($defaultMediaFolderRepository$context);
  223.         $documentExistingMediaUploadFolder $this->getDocumentMediaUploadFolder($mediaFolderRepository$context);
  224.         $imageDefaultExistingMediaUploadFolder $this->getImageDefaultMediaUploadFolder($defaultMediaFolderRepository$context);
  225.         $imageExistingMediaUploadFolder $this->getImageMediaUploadFolder($mediaFolderRepository$context);
  226.         // document folder
  227.         if (!$documentDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
  228.             $defaultMediaUploadFolder = [
  229.                 'id' => $documentDefaultMediaUploadFolderId,
  230.                 'entity' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
  231.                 'associationFields' => [self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
  232.                 'customFields' => [
  233.                     self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
  234.                 ]
  235.             ];
  236.             $this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
  237.         } else {
  238.             $documentDefaultMediaUploadFolderId $documentDefaultExistingMediaUploadFolder->getId();
  239.         }
  240.         if (!$documentExistingMediaUploadFolder instanceof MediaFolderEntity) {
  241.             $defaultMediaUploadFolder = [
  242.                 'id' => $documentMediaUploadFolderId,
  243.                 'name' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME,
  244.                 'useParentConfiguration' => false,
  245.                 'defaultFolderId' => $documentDefaultMediaUploadFolderId,
  246.                 'configuration' => [
  247.                     'createThumbnails' => false,
  248.                     'keepAspectRatio' => true,
  249.                     'thumbnailQuality' => 80,
  250.                     'private' => false,
  251.                     'noAssociation' => false,
  252.                 ],
  253.                 'customFields' => [
  254.                     self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
  255.                 ]
  256.             ];
  257.             $this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
  258.         }
  259.         // image folder
  260.         if (!$imageDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
  261.             $defaultMediaUploadFolder = [
  262.                 'id' => $imageDefaultMediaUploadFolderId,
  263.                 'entity' => self::DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
  264.                 'associationFields' => [self::DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
  265.                 'customFields' => [
  266.                     self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
  267.                 ]
  268.             ];
  269.             $this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
  270.         } else {
  271.             $imageDefaultMediaUploadFolderId $imageDefaultExistingMediaUploadFolder->getId();
  272.         }
  273.         if (!$imageExistingMediaUploadFolder instanceof MediaFolderEntity) {
  274.             $mediaThumbnailSizes $this->container->get('media_thumbnail_size.repository')->search((new Criteria()), $context);
  275.             $thumbnails = [];
  276.             if (!empty($mediaThumbnailSizes) && $mediaThumbnailSizes instanceof EntitySearchResult && $mediaThumbnailSizes->count() > 0) {
  277.                 foreach ($mediaThumbnailSizes->getElements() as $element) {
  278.                     $thumbnails[] = ['id' => $element->getId()];
  279.                 }
  280.             }
  281.             $defaultMediaUploadFolder = [
  282.                 'id' => $imageMediaUploadFolderId,
  283.                 'name' => self::DEFAULT_IMAGE_MEDIA_FOLDER_NAME,
  284.                 'useParentConfiguration' => false,
  285.                 'defaultFolderId' => $imageDefaultMediaUploadFolderId,
  286.                 'configuration' => [
  287.                     'createThumbnails' => true,
  288.                     'keepAspectRatio' => true,
  289.                     'thumbnailQuality' => 80,
  290.                     'private' => false,
  291.                     'noAssociation' => false,
  292.                     'mediaThumbnailSizes' => $thumbnails
  293.                 ],
  294.                 'customFields' => [
  295.                     self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
  296.                 ]
  297.             ];
  298.             $this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
  299.         }
  300.     }
  301.     private function updateDefaultMediaUploadFolder(string $mediaUploadFolderIdstring $defaultMediaUploadFolderIdContext $context): void
  302.     {
  303.         $documentMediaUploadFolderId Uuid::randomHex();
  304.         $documentDefaultMediaUploadFolderId Uuid::randomHex();
  305.         $imageMediaUploadFolderId Uuid::randomHex();
  306.         $imageDefaultMediaUploadFolderId Uuid::randomHex();
  307.         $defaultMediaFolderRepository $this->container->get('media_default_folder.repository');
  308.         $mediaFolderRepository $this->container->get('media_folder.repository');
  309.         $documentDefaultExistingMediaUploadFolder $this->getDocumentDefaultMediaUploadFolder($defaultMediaFolderRepository$context);
  310.         $documentExistingMediaUploadFolder $this->getDocumentMediaUploadFolder($mediaFolderRepository$context);
  311.         $imageDefaultExistingMediaUploadFolder $this->getImageDefaultMediaUploadFolder($defaultMediaFolderRepository$context);
  312.         $imageExistingMediaUploadFolder $this->getImageMediaUploadFolder($mediaFolderRepository$context);
  313.         // document folder
  314.         if (!$documentDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
  315.             $defaultMediaUploadFolder = [
  316.                 'id' => $documentDefaultMediaUploadFolderId,
  317.                 'entity' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
  318.                 'associationFields' => [self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
  319.                 'customFields' => [
  320.                     self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
  321.                 ]
  322.             ];
  323.             $this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
  324.         } else {
  325.             $documentDefaultMediaUploadFolderId $documentDefaultExistingMediaUploadFolder->getId();
  326.             $defaultMediaUploadFolder = [
  327.                 'id' => $documentDefaultMediaUploadFolderId,
  328.                 'entity' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
  329.                 'associationFields' => [self::DEFAULT_DOCUMENT_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
  330.                 'customFields' => [
  331.                     self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
  332.                 ]
  333.             ];
  334.             $this->container->get('media_default_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
  335.         }
  336.         if (!$documentExistingMediaUploadFolder instanceof MediaFolderEntity) {
  337.             $defaultMediaUploadFolder = [
  338.                 'id' => $documentMediaUploadFolderId,
  339.                 'name' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME,
  340.                 'useParentConfiguration' => false,
  341.                 'defaultFolderId' => $documentDefaultMediaUploadFolderId,
  342.                 'configuration' => [
  343.                     'createThumbnails' => false,
  344.                     'keepAspectRatio' => true,
  345.                     'thumbnailQuality' => 80,
  346.                     'private' => false,
  347.                     'noAssociation' => false,
  348.                 ],
  349.                 'customFields' => [
  350.                     self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
  351.                 ]
  352.             ];
  353.             $this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
  354.         } else {
  355.             $defaultMediaUploadFolder = [
  356.                 'id' => $documentExistingMediaUploadFolder->getId(),
  357.                 'name' => self::DEFAULT_DOCUMENT_MEDIA_FOLDER_NAME,
  358.                 'useParentConfiguration' => false,
  359.                 'defaultFolderId' => $documentDefaultMediaUploadFolderId,
  360.                 'configuration' => [
  361.                     'createThumbnails' => false,
  362.                     'keepAspectRatio' => true,
  363.                     'thumbnailQuality' => 80,
  364.                     'private' => false,
  365.                     'noAssociation' => false,
  366.                 ],
  367.                 'customFields' => [
  368.                     self::DEFAULT_DOCUMENT_MEDIA_FOLDER_CUSTOM_FIELD => true
  369.                 ]
  370.             ];
  371.             $this->container->get('media_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
  372.         }
  373.         // image folder
  374.         if (!$imageDefaultExistingMediaUploadFolder instanceof MediaDefaultFolderEntity) {
  375.             $defaultMediaUploadFolder = [
  376.                 'id' => $imageDefaultMediaUploadFolderId,
  377.                 'entity' => self::DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
  378.                 'associationFields' => [self::DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
  379.                 'customFields' => [
  380.                     self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
  381.                 ]
  382.             ];
  383.             $this->container->get('media_default_folder.repository')->create([$defaultMediaUploadFolder], $context);
  384.         } else {
  385.             $imageDefaultMediaUploadFolderId $imageDefaultExistingMediaUploadFolder->getId();
  386.             $defaultMediaUploadFolder = [
  387.                 'id' => $imageDefaultMediaUploadFolderId,
  388.                 'entity' => self::DEFAULT_IMAGE_MEDIA_FOLDER_ENTITY_FAQ_DOCUMENTS,
  389.                 'associationFields' => [self::DEFAULT_IMAGE_MEDIA_FOLDER_ASSOCIATION_FAQ_DOCUMENTS],
  390.                 'customFields' => [
  391.                     self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
  392.                 ]
  393.             ];
  394.             $this->container->get('media_default_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
  395.         }
  396.         if (!$imageExistingMediaUploadFolder instanceof MediaFolderEntity) {
  397.             $mediaThumbnailSizes $this->container->get('media_thumbnail_size.repository')->search((new Criteria()), $context);
  398.             $thumbnails = [];
  399.             if (!empty($mediaThumbnailSizes) && $mediaThumbnailSizes instanceof EntitySearchResult && $mediaThumbnailSizes->count() > 0) {
  400.                 foreach ($mediaThumbnailSizes->getElements() as $element) {
  401.                     $thumbnails[] = ['id' => $element->getId()];
  402.                 }
  403.             }
  404.             $defaultMediaUploadFolder = [
  405.                 'id' => $imageMediaUploadFolderId,
  406.                 'name' => self::DEFAULT_IMAGE_MEDIA_FOLDER_NAME,
  407.                 'useParentConfiguration' => false,
  408.                 'defaultFolderId' => $imageDefaultMediaUploadFolderId,
  409.                 'configuration' => [
  410.                     'createThumbnails' => true,
  411.                     'keepAspectRatio' => true,
  412.                     'thumbnailQuality' => 80,
  413.                     'private' => false,
  414.                     'noAssociation' => false,
  415.                     'mediaThumbnailSizes' => $thumbnails
  416.                 ],
  417.                 'customFields' => [
  418.                     self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
  419.                 ]
  420.             ];
  421.             $this->container->get('media_folder.repository')->create([$defaultMediaUploadFolder], $context);
  422.         } else {
  423.             $mediaThumbnailSizes $this->container->get('media_thumbnail_size.repository')->search((new Criteria()), $context);
  424.             $thumbnails = [];
  425.             if (!empty($mediaThumbnailSizes) && $mediaThumbnailSizes instanceof EntitySearchResult && $mediaThumbnailSizes->count() > 0) {
  426.                 foreach ($mediaThumbnailSizes->getElements() as $element) {
  427.                     $thumbnails[] = ['id' => $element->getId()];
  428.                 }
  429.             }
  430.             $defaultMediaUploadFolder = [
  431.                 'id' => $imageExistingMediaUploadFolder->getId(),
  432.                 'name' => self::DEFAULT_IMAGE_MEDIA_FOLDER_NAME,
  433.                 'useParentConfiguration' => false,
  434.                 'defaultFolderId' => $imageDefaultMediaUploadFolderId,
  435.                 'configuration' => [
  436.                     'createThumbnails' => true,
  437.                     'keepAspectRatio' => true,
  438.                     'thumbnailQuality' => 80,
  439.                     'private' => false,
  440.                     'noAssociation' => false,
  441.                     'mediaThumbnailSizes' => $thumbnails
  442.                 ],
  443.                 'customFields' => [
  444.                     self::DEFAULT_IMAGE_MEDIA_FOLDER_CUSTOM_FIELD => true
  445.                 ]
  446.             ];
  447.             $this->container->get('media_folder.repository')->upsert([$defaultMediaUploadFolder], $context);
  448.         }
  449.     }
  450.     private function removeMediaUploadFolder(Context $context): void
  451.     {
  452.         $defaultMediaFolderRepository $this->container->get('media_default_folder.repository');
  453.         $mediaFolderRepository $this->container->get('media_folder.repository');
  454.         $documentMediaUploadFolder $this->getDocumentMediaUploadFolder($mediaFolderRepository$context);
  455.         $documentDefaultMediaUploadFolder $this->getDocumentDefaultMediaUploadFolder($defaultMediaFolderRepository$context);
  456.         $imageMediaUploadFolder $this->getImageMediaUploadFolder($mediaFolderRepository$context);
  457.         $imageDefaultMediaUploadFolder $this->getImageDefaultMediaUploadFolder($defaultMediaFolderRepository$context);
  458.         // document folder
  459.         if ($documentMediaUploadFolder instanceof MediaFolderEntity) {
  460.             if ($documentMediaUploadFolder->getMedia() && $documentMediaUploadFolder->getMedia()->count() > 0) {
  461.                 return;
  462.             }
  463.             $defaultMediaUploadFolderConfiguration $documentMediaUploadFolder->getConfiguration();
  464.             $deleteConfigurationId null;
  465.             if ($defaultMediaUploadFolderConfiguration && $defaultMediaUploadFolderConfiguration->getMediaFolders()) {
  466.                 if ($defaultMediaUploadFolderConfiguration->getMediaFolders()->count() < 2) {
  467.                     $deleteConfigurationId $defaultMediaUploadFolderConfiguration->getId();
  468.                 }
  469.             }
  470.             $mediaFolderRepository->delete([['id' => $documentMediaUploadFolder->getId()]], $context);
  471.             if ($documentDefaultMediaUploadFolder instanceof MediaDefaultFolderEntity) {
  472.                 $defaultMediaFolderRepository->delete([['id' => $documentDefaultMediaUploadFolder->getId()]], $context);
  473.             }
  474.             if ($deleteConfigurationId !== null) {
  475.                 $this->container->get('media_folder_configuration.repository')->delete([['id' => $deleteConfigurationId]], $context);
  476.             }
  477.         }
  478.         // image folder
  479.         if ($imageMediaUploadFolder instanceof MediaFolderEntity) {
  480.             if ($imageMediaUploadFolder->getMedia() && $imageMediaUploadFolder->getMedia()->count() > 0) {
  481.                 return;
  482.             }
  483.             $defaultMediaUploadFolderConfiguration $imageMediaUploadFolder->getConfiguration();
  484.             $deleteConfigurationId null;
  485.             if ($defaultMediaUploadFolderConfiguration && $defaultMediaUploadFolderConfiguration->getMediaFolders()) {
  486.                 if ($defaultMediaUploadFolderConfiguration->getMediaFolders()->count() < 2) {
  487.                     $deleteConfigurationId $defaultMediaUploadFolderConfiguration->getId();
  488.                 }
  489.             }
  490.             $mediaFolderRepository->delete([['id' => $imageMediaUploadFolder->getId()]], $context);
  491.             if ($imageDefaultMediaUploadFolder instanceof MediaDefaultFolderEntity) {
  492.                 $defaultMediaFolderRepository->delete([['id' => $imageDefaultMediaUploadFolder->getId()]], $context);
  493.             }
  494.             if ($deleteConfigurationId !== null) {
  495.                 $this->container->get('media_folder_configuration.repository')->delete([['id' => $deleteConfigurationId]], $context);
  496.             }
  497.         }
  498.     }
  499.     private function getDocumentDefaultMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepositoryContext $context): ?MediaDefaultFolderEntity
  500.     {
  501.         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();
  502.     }
  503.     private function getDocumentMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepositoryContext $context): ?MediaFolderEntity
  504.     {
  505.         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();
  506.     }
  507.     private function getImageDefaultMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepositoryContext $context): ?MediaDefaultFolderEntity
  508.     {
  509.         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();
  510.     }
  511.     private function getImageMediaUploadFolder(EntityRepositoryInterface $mediaFolderRepositoryContext $context): ?MediaFolderEntity
  512.     {
  513.         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();
  514.     }
  515.     private function removeInheritance(Connection $connectionstring $entitystring $propertyName): void
  516.     {
  517.         $sql str_replace(
  518.             ['#table#''#column#'],
  519.             [$entity$propertyName],
  520.             'ALTER TABLE `#table#` DROP `#column#`'
  521.         );
  522.         $connection->executeStatement($sql);
  523.     }
  524.     private function insertDefaultSeoUrlTemplate(Context $context)
  525.     {
  526.         /** @var EntityRepositoryInterface $seoUrlTemplateRepository */
  527.         $seoUrlTemplateRepository $this->container->get('seo_url_template.repository');
  528.         $defaultSeoUrlTemplates = [
  529.             [
  530.                 'routeName' => FaqPageSeoUrlRoute::ROUTE_NAME,
  531.                 'entityName' => FaqDefinition::ENTITY_NAME,
  532.                 'template' => FaqPageSeoUrlRoute::DEFAULT_TEMPLATE,
  533.                 'isValid' => true
  534.             ]
  535.         ];
  536.         foreach ($defaultSeoUrlTemplates as $defaultSeoUrlTemplate) {
  537.             $this->createSeoUrlTemplateIfNotExists($seoUrlTemplateRepository$context$defaultSeoUrlTemplate);
  538.         }
  539.     }
  540.     /**
  541.      * @param EntityRepositoryInterface $entityRepository
  542.      * @param Context $context
  543.      * @param array $seoUrlTemplateData
  544.      */
  545.     private function createSeoUrlTemplateIfNotExists(EntityRepositoryInterface $entityRepositoryContext $context, array $seoUrlTemplateData): void
  546.     {
  547.         $exists $entityRepository->search((new Criteria())->addFilter(new EqualsFilter('routeName'$seoUrlTemplateData['routeName']))->addFilter(new EqualsFilter('entityName'$seoUrlTemplateData['entityName']))->addFilter(new EqualsFilter('template'$seoUrlTemplateData['template'])), $context);
  548.         if ($exists->getTotal() === 0) {
  549.             $entityRepository->create([$seoUrlTemplateData], $context);
  550.         }
  551.     }
  552. }