custom/plugins/zenitPlatformStratus/src/zenitPlatformStratus.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformStratus;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Storefront\Framework\ThemeInterface;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use zenit\PlatformStratus\Core\CustomFieldsHelper;
  13. class zenitPlatformStratus extends Plugin implements ThemeInterface
  14. {
  15.     /**
  16.      * @var Context
  17.      */
  18.     private $context;
  19.     public function getThemeConfigPath(): string
  20.     {
  21.         return 'theme.json';
  22.     }
  23.     public function install(InstallContext $installContext): void
  24.     {
  25.         parent::install($installContext);
  26.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  27.         $customFields = new CustomFieldsHelper($customFieldSetRepository);
  28.         $customFields->getCustomFields($this->container$installContext->getContext());
  29.     }
  30.     public function update(UpdateContext $updateContext): void
  31.     {
  32.         parent::update($updateContext);
  33.         if (version_compare($updateContext->getCurrentPluginVersion(), '2.4.2''<')) {
  34.             $customFieldSetRepository $this->container->get('custom_field_set.repository');
  35.             $customFields = new CustomFieldsHelper($customFieldSetRepository);
  36.             $customFields->getCustomFields($this->container$updateContext->getContext());
  37.         }
  38.     }
  39.     public function postUpdate(UpdateContext $updateContext): void
  40.     {
  41.         parent::postUpdate($updateContext);
  42.         $this->updateThemeDuplicates();
  43.     }
  44.     public function uninstall(UninstallContext $uninstallContext): void
  45.     {
  46.         parent::uninstall($uninstallContext);
  47.         if ($uninstallContext->keepUserData()) {
  48.             return;
  49.         }
  50.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  51.         $customFields = new CustomFieldsHelper($customFieldSetRepository);
  52.         $customFields->deleteCustomFields($uninstallContext->getContext());
  53.     }
  54.     private function updateThemeDuplicates(): void
  55.     {
  56.         $this->context Context::createDefaultContext();
  57.         $criteriaTheme = new Criteria();
  58.         $criteriaTheme->addFilter(new EqualsFilter('technicalName''zenitPlatformStratus'));
  59.         /** @var EntityRepository $themeRepo */
  60.         $themeRepo $this->container->get('theme.repository');
  61.         $parentTheme $themeRepo->search($criteriaTheme$this->context)->first();
  62.         if (!$parentTheme) {
  63.             return;
  64.         }
  65.         $criteriaThemeDuplicates = new Criteria();
  66.         $criteriaThemeDuplicates->addFilter(new EqualsFilter('parentThemeId'$parentTheme->get('id')));
  67.         $resultThemeDuplicates $themeRepo->search($criteriaThemeDuplicates$this->context)->getElements();
  68.         if (!$resultThemeDuplicates) {
  69.             return;
  70.         }
  71.         foreach ($resultThemeDuplicates as $themeDuplicate) {
  72.             $data = [
  73.                 'id' => $themeDuplicate->get('id'),
  74.                 'baseConfig' => $parentTheme->get('baseConfig')
  75.             ];
  76.             if (!$themeDuplicate->get('previewMediaId')) {
  77.                 $data['previewMediaId'] = $parentTheme->get('previewMediaId');
  78.             }
  79.             $themeRepo->update([$data], $this->context);
  80.         }
  81.     }
  82. }