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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MasterFFL\Checkout;
  3. use MasterFFL\Checkout\Service\FFLAttributeService;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. class MasterFFLCheckout extends Plugin
  14. {
  15.     public const PLUGIN_NAME 'MasterFFLCheckout';
  16.     
  17.     public function install(InstallContext $installContext): void
  18.     {
  19.         parent::install($installContext);
  20.         $this->createFFLAttribute($installContext->getContext());
  21.         $this->createCustomFields($installContext);
  22.     }
  23.     private function createCustomFields(InstallContext $installContext): void
  24.     {
  25.         $ctx $installContext->getContext();
  26.         $setRepo   $this->container->get('custom_field_set.repository');
  27.         $fieldRepo $this->container->get('custom_field.repository');
  28.         $setId $setRepo->searchIds(
  29.             (new Criteria())->addFilter(new EqualsFilter('name''masterffl_order_fields')),
  30.             $ctx
  31.         )->firstId();
  32.         if ($setId) {
  33.             return;
  34.         }
  35.         $setRepo->create([[
  36.             'name'   => 'masterffl_order_fields',
  37.             'config' => ['label' => [
  38.                 'en-GB' => 'MasterFFL Order Fields',
  39.                 'de-DE' => 'MasterFFL Bestellfelder'
  40.             ]],
  41.             'customFields' => [
  42.                 [
  43.                     'name'   => 'masterffl_license_number',
  44.                     'type'   => CustomFieldTypes::TEXT,
  45.                     'config' => [
  46.                         'label' => [
  47.                             'en-GB' => 'FFL License Number',
  48.                             'de-DE' => 'FFL Lizenznummer'
  49.                         ],
  50.                         'customFieldPosition' => 1,
  51.                         'componentName'       => 'sw-text-field',
  52.                         'customFieldType'     => 'text',
  53.                     ],
  54.                 ],
  55.                 [
  56.                     'name'   => 'masterffl_transfer_url',
  57.                     'type'   => CustomFieldTypes::TEXT,
  58.                     'config' => [
  59.                         'label' => [
  60.                             'en-GB' => 'FFL Transfer URL',
  61.                             'de-DE' => 'FFL Transfer URL'
  62.                         ],
  63.                         'customFieldPosition' => 2,
  64.                         'componentName'       => 'sw-text-field',
  65.                         'customFieldType'     => 'text',
  66.                     ],
  67.                 ],
  68.             ],
  69.             'relations' => [['entityName' => 'order']],
  70.         ]], $ctx);
  71.     }
  72.     public function uninstall(UninstallContext $uninstallContext): void
  73.     {
  74.         parent::uninstall($uninstallContext);
  75.         
  76.         if ($uninstallContext->keepUserData()) {
  77.             return;
  78.         }
  79.         $this->removeFFLAttribute($uninstallContext->getContext());
  80.         $this->removeCustomFields($uninstallContext);
  81.     }
  82.     private function createFFLAttribute(Context $context): void
  83.     {
  84.         /** @var EntityRepository $customFieldSetRepository */
  85.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  86.         
  87.         /** @var EntityRepository $customFieldRepository */
  88.         $customFieldRepository $this->container->get('custom_field.repository');
  89.         /** @var SystemConfigService $systemConfigService */
  90.         $systemConfigService $this->container->get(SystemConfigService::class);
  91.         $attributeService = new FFLAttributeService(
  92.             $customFieldSetRepository,
  93.             $customFieldRepository,
  94.             $systemConfigService
  95.         );
  96.         $attributeService->createFFLAttribute($context);
  97.     }
  98.     private function removeFFLAttribute(Context $context): void
  99.     {
  100.         /** @var EntityRepository $customFieldSetRepository */
  101.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  102.         
  103.         /** @var EntityRepository $customFieldRepository */
  104.         $customFieldRepository $this->container->get('custom_field.repository');
  105.         /** @var SystemConfigService $systemConfigService */
  106.         $systemConfigService $this->container->get(SystemConfigService::class);
  107.         $attributeService = new FFLAttributeService(
  108.             $customFieldSetRepository,
  109.             $customFieldRepository,
  110.             $systemConfigService
  111.         );
  112.         $attributeService->removeFFLAttribute($context);
  113.     }
  114.     private function removeCustomFields(UninstallContext $context): void
  115.     {
  116.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  117.         
  118.         $criteria = new \Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria();
  119.         $criteria->addFilter(new \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter('name''masterffl_order_fields'));
  120.         
  121.         $customFieldSetIds $customFieldSetRepository->searchIds($criteria$context->getContext());
  122.         
  123.         if ($customFieldSetIds->getTotal() > 0) {
  124.             $customFieldSetRepository->delete([
  125.                 ['id' => $customFieldSetIds->getIds()[0]]
  126.             ], $context->getContext());
  127.         }
  128.     }
  129. }