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

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