custom/plugins/AcrisCategoryCustomerGroupCS/src/AcrisCategoryCustomerGroupCS.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CategoryCustomerGroup;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\System\CustomField\CustomFieldTypes;
  15. use Shopware\Core\System\Snippet\SnippetEntity;
  16. class AcrisCategoryCustomerGroupCS extends Plugin
  17. {
  18.     const CUSTOM_FIELD_SET_NAME_CATEGORY_CUSTOMER_GROUP 'acris_category_customer_group';
  19.     public function uninstall(UninstallContext $context): void
  20.     {
  21.         parent::uninstall($context);
  22.         if ($context->keepUserData()) {
  23.             return;
  24.         }
  25.         $connection $this->container->get(Connection::class);
  26.         $this->removeTableAndFields($connection);
  27.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_CATEGORY_CUSTOMER_GROUP]);
  28.     }
  29.     public function install(InstallContext $context): void
  30.     {
  31.         $this->addCustomFields($context->getContext());
  32.     }
  33.     public function update(UpdateContext $context): void
  34.     {
  35.         $this->addCustomFields($context->getContext());
  36.     }
  37.     public function removeTableAndFields(Connection $connection)
  38.     {
  39.         $connection->executeUpdate('DROP TABLE IF EXISTS `acris_category_customer_group`');
  40.         $connection->executeUpdate('ALTER TABLE `category` DROP COLUMN `customerGroup`');
  41.         $connection->executeUpdate('ALTER TABLE `customer_group` DROP COLUMN `category`');
  42.     }
  43.     private function addCustomFields(Context $context): void
  44.     {
  45.         /* Check for snippets if they exist for custom fields */
  46.         $this->checkForExistingCustomFieldSnippets($context);
  47.         $customFieldSet $this->container->get('custom_field_set.repository');
  48.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_CATEGORY_CUSTOMER_GROUP)), $context)->count() == 0) {
  49.             $customFieldSet->create([[
  50.                 'name' => self::CUSTOM_FIELD_SET_NAME_CATEGORY_CUSTOMER_GROUP,
  51.                 'config' => [
  52.                     'label' => [
  53.                         'en-GB' => 'Category customer group',
  54.                         'de-DE' => 'Category customer group'
  55.                     ]
  56.                 ],
  57.                 'customFields' => [
  58.                     ['name' => 'acris_category_customer_group_exclude_sitemap''type' => CustomFieldTypes::BOOL,
  59.                         'config' => [
  60.                             'componentName' => 'sw-field',
  61.                             'type' => 'checkbox',
  62.                             'customFieldType' => 'checkbox',
  63.                             'customFieldPosition' => 1,
  64.                             'label' => [
  65.                                 'en-GB' => 'Exclude from sitemap',
  66.                                 'de-DE' => 'Von Sitemap ausnehmen'
  67.                             ]
  68.                         ]]
  69.                 ],
  70.             ]], $context);
  71.         }
  72.     }
  73.     private function removeCustomFields(Context $context, array $setNames): void
  74.     {
  75.         /* Check for snippets if they exist for custom fields */
  76.         $this->checkForExistingCustomFieldSnippets($context);
  77.         $customFieldSet $this->container->get('custom_field_set.repository');
  78.         foreach ($setNames as $setName) {
  79.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  80.             if($id$customFieldSet->delete([['id' => $id]], $context);
  81.         }
  82.     }
  83.     private function checkForExistingCustomFieldSnippets(Context $context)
  84.     {
  85.         /** @var EntityRepositoryInterface $snippetRepository */
  86.         $snippetRepository $this->container->get('snippet.repository');
  87.         $criteria = new Criteria();
  88.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  89.             new EqualsFilter('translationKey''customFields.' 'acris_category_customer_group_exclude_sitemap')
  90.         ]));
  91.         /** @var EntitySearchResult $searchResult */
  92.         $searchResult $snippetRepository->search($criteria$context);
  93.         if ($searchResult->count() > 0) {
  94.             $snippetIds = [];
  95.             /** @var SnippetEntity $snippet */
  96.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  97.                 $snippetIds[] = [
  98.                     'id' => $snippet->getId()
  99.                 ];
  100.             }
  101.             if (!empty($snippetIds)) {
  102.                 $snippetRepository->delete($snippetIds$context);
  103.             }
  104.         }
  105.     }
  106. }