custom/plugins/HochwarthCheckoutCheckbox/src/HochwarthCheckoutCheckbox.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HochwarthCheckoutCheckbox;
  3.     use Doctrine\DBAL\Connection;
  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\Core\Framework\Plugin;
  8.     use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  9.     use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10.     use Shopware\Core\System\CustomField\CustomFieldTypes;
  11.     class HochwarthCheckoutCheckbox extends Plugin
  12.     {
  13.         public const CUSTOMER_MARKETING_ALLOWED 'hochwarth_checkout_checkbox';
  14.         public const CUSTOMER_MARKETING_ALLOWED_MULTI 'hochwarth_checkout_checkbox_multi';
  15.         public function activate(ActivateContext $activateContext): void
  16.         {
  17.             /** @var EntityRepository $repo */
  18.             $repo $this->container->get('custom_field.repository');
  19.             $result $repo->searchIds(
  20.                 (new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOMER_MARKETING_ALLOWED)),
  21.                 $activateContext->getContext()
  22.             );
  23.             if ($result->getTotal() > 0) {
  24.                 return;
  25.             }
  26.             $repo->create(
  27.                 [
  28.                     [
  29.                         'name' => self::CUSTOMER_MARKETING_ALLOWED,
  30.                         'type' => CustomFieldTypes::BOOL,
  31.                     ],
  32.                 ],
  33.                 $activateContext->getContext()
  34.             );
  35.         }
  36.         public function uninstall(UninstallContext $uninstallContext): void
  37.         {
  38.             if ($uninstallContext->keepUserData()) {
  39.                 return;
  40.             }
  41.             /** @var EntityRepository $repo */
  42.             $repo $this->container->get('custom_field.repository');
  43.             $result $repo->searchIds(
  44.                 (new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOMER_MARKETING_ALLOWED)),
  45.                 $uninstallContext->getContext()
  46.             );
  47.             if (!$result->getTotal()) {
  48.                 return;
  49.             }
  50.             $result array_map(function ($v) {
  51.                 return ['id' => $v];
  52.             }, $result->getIds());
  53.             $repo->delete($result$uninstallContext->getContext());
  54.             $connection $this->container->get(Connection::class);
  55.             $connection->executeStatement("DROP TABLE IF EXISTS `hochwarth_checkout_checkbox_sales_channels`");
  56.             $connection->executeStatement("DROP TABLE IF EXISTS `hochwarth_checkout_checkbox_translation`");
  57.             $connection->executeStatement("DROP TABLE IF EXISTS `hochwarth_checkout_checkbox`");
  58.         }
  59.     }