vendor/store.shopware.com/ndsrecipemanager/src/NdsRecipeManager.php line 23

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nds\RecipeManager;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\Uuid\Uuid;
  7. use Nds\RecipeManager\Util\Lifecycle\ActivateDeactivate;
  8. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Nds\RecipeManager\Core\Content\Recipe\RecipeDefinition;
  11. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\Content\Media\Aggregate\MediaFolder\MediaFolderEntity;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  20. class NdsRecipeManager extends Plugin
  21. {
  22.     private ?ActivateDeactivate $activateDeactivate null;
  23.     public function setActivateDeactivate(ActivateDeactivate $activateDeactivate): void
  24.     {
  25.         $this->activateDeactivate $activateDeactivate;
  26.     }
  27.     public function build(ContainerBuilder $container): void
  28.     {
  29.         parent::build($container);
  30.     }
  31.     public function install(InstallContext $installContext): void
  32.     {
  33.         $this->createRecipeMediaFolder($installContext->getContext());
  34.         $this->setDefaultThumbnailSize($installContext->getContext());
  35.         $this->createRecipeCmsListingPage($installContext->getContext());
  36.     }
  37.     public function activate(ActivateContext $activateContext): void
  38.     {
  39.         $this->activateDeactivate->activate($activateContext->getContext());
  40.         parent::activate($activateContext);
  41.     }
  42.     public function update(UpdateContext $updateContext): void
  43.     {
  44.         parent::update($updateContext);
  45.     }
  46.     public function deactivate(DeactivateContext $deactivateContext): void
  47.     {
  48.         parent::deactivate($deactivateContext);
  49.     }
  50.     public function uninstall(UninstallContext $uninstallContext): void
  51.     {
  52.         parent::uninstall($uninstallContext);
  53.         if ($uninstallContext->keepUserData()) {
  54.             return;
  55.         }
  56.         // Drop all tables from Plugin
  57.         $connection $this->container->get(Connection::class);
  58.         // Remove m_to_m first
  59.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_recipe_category`');
  60.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_recipe_tag`');
  61.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_recipe_ingredient`');
  62.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_related`');
  63.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_recipe_difficulty`');
  64.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_timeunit`');
  65.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_recipe_media`');
  66.         // Get rid of all translations
  67.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_category_translation`');
  68.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_tag_translation`');
  69.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_ingredient_translation`');
  70.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_difficulty_translation`');
  71.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_unit_translation`');
  72.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_recipe_translation`');
  73.         // Delete all main entities
  74.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_recipe`');
  75.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_difficulty`');
  76.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_ingredient`');
  77.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_unit`');
  78.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_tag`');
  79.         $connection->executeStatement('DROP TABLE IF EXISTS `nds_rcp_category`');
  80.         // Delete all plugin config rows and seo templates
  81.         $connection->executeStatement("DELETE FROM `system_config` WHERE `configuration_key` LIKE 'NdsRecipeManager%';");
  82.         $connection->executeStatement("DELETE FROM `seo_url_template` WHERE `route_name` LIKE 'rcp.frontend%';");
  83.         $connection->executeStatement("DELETE FROM `seo_url` WHERE `route_name` LIKE 'rcp.frontend%';");
  84.         // Delete all media folders
  85.         $this->deleteMediaFolder($uninstallContext->getContext());
  86.         $this->deleteDefaultMediaFolder($uninstallContext->getContext());
  87.         // Delete cms pages
  88.         $this->deleteCmsPages($uninstallContext->getContext());
  89.     }
  90.     public function createRecipeCmsListingPage(Context $context): void
  91.     {
  92.         $cmsPageRepository $this->container->get('cms_page.repository');
  93.         $cmsPage = [
  94.             [
  95.                 'id' => 'b008af97ec5e43a482ce4335f74f4a53',
  96.                 'type' => 'page',
  97.                 'translations' => [
  98.                     'de-DE' => [
  99.                         'name' => 'Rezeptliste',
  100.                     ],
  101.                     'en-GB' => [
  102.                         'name' => 'Recipes list page',
  103.                     ],
  104.                 ],
  105.                 'sections' => [
  106.                     [
  107.                         'id' => Uuid::randomHex(),
  108.                         'type' => 'default',
  109.                         'position' => 0,
  110.                         'name' => 'Recipe List',
  111.                         'blocks' => [
  112.                             [
  113.                                 'position' => 1,
  114.                                 'type' => 'recipe-list-block',
  115.                                 'name' => 'Recipe List',
  116.                                 'slots' => [
  117.                                     ['type' => 'recipe-list''slot' => 'recipeList'],
  118.                                 ],
  119.                             ],
  120.                         ],
  121.                     ],
  122.                 ],
  123.             ],
  124.         ];
  125.         $cmsPageRepository->upsert($cmsPage$context);
  126.     }
  127.     public function createRecipeMediaFolder(Context $context): void
  128.     {
  129.         $this->deleteDefaultMediaFolder($context);
  130.         // Create Folder
  131.         $mediaDefaultFolderRepository $this->container->get('media_default_folder.repository');
  132.         $mediaDefaultFolderRepository->create([
  133.             [
  134.                 'id' => Uuid::randomHex(),
  135.                 'entity' => RecipeDefinition::ENTITY_NAME,
  136.                 'associationFields' => [],
  137.                 'folder' => [
  138.                     'id' => Uuid::randomHex(),
  139.                     'useParentConfiguration' => false,
  140.                     'name' => 'Recipes Manager Media',
  141.                     'configuration' => [
  142.                         'createThumbnails' => true,
  143.                         'thumbnailQuality' => 80,
  144.                         'keepAspectRatio' => true,
  145.                     ],
  146.                 ],
  147.             ],
  148.         ], $context);
  149.     }
  150.     protected function deleteMediaFolder(Context $context): void
  151.     {
  152.         $criteria = new Criteria();
  153.         $criteria->addFilter(new EqualsFilter('name''Recipes Manager Media'));
  154.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  155.         $mediaFolderRepository $this->container->get('media_folder.repository');
  156.         $mediaFolderRepository->search($criteria$context);
  157.         $mediaFolderIds $mediaFolderRepository->searchIds($criteria$context)->getIds();
  158.         if (! empty($mediaFolderIds)) {
  159.             $ids \array_map(static function ($id) {
  160.                 return ['id' => $id];
  161.             }, $mediaFolderIds);
  162.             $mediaFolderRepository->delete($ids$context);
  163.         }
  164.     }
  165.     protected function deleteDefaultMediaFolder(Context $context): void
  166.     {
  167.         $criteria = new Criteria();
  168.         $criteria->addFilter(new EqualsFilter('entity'RecipeDefinition::ENTITY_NAME));
  169.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  170.         $mediaFolderRepository $this->container->get('media_default_folder.repository');
  171.         $mediaFolderIds $mediaFolderRepository->searchIds($criteria$context)->getIds();
  172.         if (! empty($mediaFolderIds)) {
  173.             $ids \array_map(static function ($id) {
  174.                 return ['id' => $id];
  175.             }, $mediaFolderIds);
  176.             $mediaFolderRepository->delete($ids$context);
  177.         }
  178.     }
  179.     protected function deleteCmsPages(Context $context): void
  180.     {
  181.         $cmsPageRepository $this->container->get('cms_page.repository');
  182.         $criteria = new Criteria();
  183.         $criteria->addFilter(
  184.             new MultiFilter(
  185.                 MultiFilter::CONNECTION_OR,
  186.                 [
  187.                     new EqualsFilter('name''Rezeptliste'),
  188.                 ]
  189.             )
  190.         );
  191.         $cmsPageRepository->searchIds($criteria$context);
  192.         $pageIds $cmsPageRepository->searchIds($criteria$context)->getIds();
  193.         if (! empty($pageIds)) {
  194.             $ids \array_map(static function ($id) {
  195.                 return ['id' => $id];
  196.             }, $pageIds);
  197.             $cmsPageRepository->delete($ids$context);
  198.         }
  199.     }
  200.     protected function setDefaultThumbnailSize(Context $context): void
  201.     {
  202.         // Get default thumbnail size IDs
  203.         $mediaThumbnailSizeRepository $this->container->get('media_thumbnail_size.repository');
  204.         $mediaThumbnailSizeIds $mediaThumbnailSizeRepository->search(new Criteria(), $context)->getIds();
  205.         // Get recipe media folder
  206.         $criteria = new Criteria();
  207.         $criteria->addFilter(new EqualsFilter('name''Recipes Manager Media'));
  208.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  209.         $mediaFolderRepository $this->container->get('media_folder.repository');
  210.         $mediaFolderRepository->search($criteria$context);
  211.         /** @var MediaFolderEntity $mediaFolder */
  212.         $mediaFolder $mediaFolderRepository->search($criteria$context)->getEntities()->first();
  213.         $folderConfigurationId $mediaFolder->getConfigurationId();
  214.         $connection $this->container->get(Connection::class);
  215.         foreach ($mediaThumbnailSizeIds as $mediaThumbnailSizeId) {
  216.             $connection->executeStatement('
  217.                     INSERT INTO `media_folder_configuration_media_thumbnail_size` (`media_folder_configuration_id`, `media_thumbnail_size_id`)
  218.                     VALUES (UNHEX(:folderConfigurationId), UNHEX(:thumbnailSizeId))
  219.                 ', [
  220.                 'folderConfigurationId' => $folderConfigurationId,
  221.                 'thumbnailSizeId' => $mediaThumbnailSizeId,
  222.             ]);
  223.         }
  224.     }
  225. }