custom/plugins/AcrisProductCustomerGroupCS/src/AcrisProductCustomerGroupCS.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductCustomerGroup;
  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\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. use Shopware\Core\System\Snippet\SnippetEntity;
  15. class AcrisProductCustomerGroupCS extends Plugin
  16. {
  17.     const CUSTOM_FIELD_SET_NAME_PRODUCT_CUSTOMER_GROUP 'acris_product_customer_group';
  18.     const CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP 'acris_product_customer_group_exclude_sitemap';
  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_PRODUCT_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->executeStatement('DROP TABLE IF EXISTS `acris_product_customer_group`');
  40.         $connection->executeStatement('ALTER TABLE `product` DROP COLUMN `acrisBlockCustomerGroup`');
  41.         $connection->executeStatement('ALTER TABLE `customer_group` DROP COLUMN `acrisBlockProduct`');
  42.         try {
  43.             $connection->executeStatement('ALTER TABLE `product` DROP COLUMN `customerGroup`');
  44.             $connection->executeStatement('ALTER TABLE `customer_group` DROP COLUMN `product`;');
  45.         } catch (\Throwable $e) {}
  46.     }
  47.     private function addCustomFields(Context $context): void
  48.     {
  49.         /* Check for snippets if they exist for custom fields */
  50.         $this->checkForExistingCustomFieldSnippets($context);
  51.         $customFieldSet $this->container->get('custom_field_set.repository');
  52.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_PRODUCT_CUSTOMER_GROUP)), $context)->count() == 0) {
  53.             $customFieldSet->create([[
  54.                 'name' => self::CUSTOM_FIELD_SET_NAME_PRODUCT_CUSTOMER_GROUP,
  55.                 'config' => [
  56.                     'label' => [
  57.                         'en-GB' => 'Product customer group',
  58.                         'de-DE' => 'Produkt-Kundengruppe'
  59.                     ]
  60.                 ],
  61.                 'customFields' => [
  62.                     ['name' => self::CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP'type' => CustomFieldTypes::BOOL,
  63.                         'config' => [
  64.                             'componentName' => 'sw-field',
  65.                             'type' => 'checkbox',
  66.                             'customFieldType' => 'checkbox',
  67.                             'customFieldPosition' => 1,
  68.                             'label' => [
  69.                                 'en-GB' => 'Exclude from sitemap',
  70.                                 'de-DE' => 'Von Sitemap ausnehmen'
  71.                             ]
  72.                         ]]
  73.                 ],
  74.             ]], $context);
  75.         }
  76.     }
  77.     private function removeCustomFields(Context $context, array $setNames): void
  78.     {
  79.         /* Check for snippets if they exist for custom fields */
  80.         $this->checkForExistingCustomFieldSnippets($context);
  81.         $customFieldSet $this->container->get('custom_field_set.repository');
  82.         foreach ($setNames as $setName) {
  83.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  84.             if($id$customFieldSet->delete([['id' => $id]], $context);
  85.         }
  86.     }
  87.     private function checkForExistingCustomFieldSnippets(Context $context)
  88.     {
  89.         /** @var EntityRepositoryInterface $snippetRepository */
  90.         $snippetRepository $this->container->get('snippet.repository');
  91.         $criteria = new Criteria();
  92.         $criteria->addFilter(new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_NAME_PRODUCT_EXCLUDE_FROM_SITEMAP));
  93.         /** @var EntitySearchResult $searchResult */
  94.         $searchResult $snippetRepository->search($criteria$context);
  95.         if ($searchResult->count() > 0) {
  96.             $snippetIds = [];
  97.             /** @var SnippetEntity $snippet */
  98.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  99.                 $snippetIds[] = [
  100.                     'id' => $snippet->getId()
  101.                 ];
  102.             }
  103.             if (!empty($snippetIds)) {
  104.                 $snippetRepository->delete($snippetIds$context);
  105.             }
  106.         }
  107.     }
  108. }