custom/plugins/EconsorAtBitConfiguratorConnector/src/AtBitConfiguratorConnector.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Econsor\Shopware\AtBitConfiguratorConnector;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. use Shopware\Core\System\CustomField\CustomFieldTypes;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. class AtBitConfiguratorConnector extends Plugin
  17. {
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         if ($this->customFieldsExist($installContext->getContext())) {
  21.             return;
  22.         }
  23.         $this->getCustomFieldSetRepository()->upsert([
  24.             [
  25.                 'name'         => 'ec_atbit',
  26.                 'config'       => [
  27.                     'label' => [
  28.                         'en-GB' => 'ECONSOR AtBit Integration',
  29.                         'de-DE' => 'ECONSOR AtBit Integration',
  30.                     ],
  31.                 ],
  32.                 'customFields' => [
  33.                     self::getEnableFieldDefinition(),
  34.                     [
  35.                         'name'   => 'ec_atbit_name',
  36.                         'type'   => CustomFieldTypes::TEXT,
  37.                         'config' => [
  38.                             'componentName'       => 'sw-field',
  39.                             'customFieldPosition' => 2,
  40.                             'customFieldType'     => CustomFieldTypes::TEXT,
  41.                             'type'                => CustomFieldTypes::TEXT,
  42.                             'label'               => [
  43.                                 'en-GB' => 'AtBit Name (For URL)',
  44.                                 'de-DE' => 'AtBit Name (Für URL)',
  45.                             ],
  46.                         ],
  47.                     ],
  48.                 ],
  49.                 'relations'    => [
  50.                     ['entityName' => 'product'],
  51.                 ],
  52.             ],
  53.         ], $installContext->getContext());
  54.     }
  55.     public function update(UpdateContext $updateContext): void
  56.     {
  57.         if (!$this->container instanceof ContainerInterface) {
  58.             throw new \RuntimeException('Service container not available.');
  59.         }
  60.         $connection $this->container->get(Connection::class);
  61.         if (!$connection instanceof Connection) {
  62.             throw new \RuntimeException('DBAL Connection not available.');
  63.         }
  64.         $fieldDef self::getEnableFieldDefinition();
  65.         $connection->executeUpdate(
  66.             "UPDATE `custom_field`
  67.              SET `type` = :type,
  68.                  `config` = :config,
  69.                  `updated_at` = NOW(3)
  70.              WHERE `name` = 'ec_atbit_enable'",
  71.             [
  72.                 'type'   => $fieldDef['type'],
  73.                 'config' => json_encode($fieldDef['config']),
  74.             ]
  75.         );
  76.     }
  77.     public function uninstall(UninstallContext $uninstallContext): void
  78.     {
  79.         if ($uninstallContext->keepUserData()) {
  80.             parent::uninstall($uninstallContext);
  81.             return;
  82.         }
  83.         $this->removeCustomField($uninstallContext);
  84.         parent::uninstall($uninstallContext);
  85.     }
  86.     private static function getEnableFieldDefinition(): array
  87.     {
  88.         return [
  89.             'name'   => 'ec_atbit_enable',
  90.             'type'   => CustomFieldTypes::SELECT,
  91.             'config' => [
  92.                 'componentName'       => 'sw-single-select',
  93.                 'customFieldType'     => 'select',
  94.                 'customFieldPosition' => 1,
  95.                 'options'             => [
  96.                     ['value' => 'off',   'label' => ['en-GB' => 'Off',   'de-DE' => 'Aus']],
  97.                     ['value' => 'loft',  'label' => ['en-GB' => 'Loft',  'de-DE' => 'Loft']],
  98.                     ['value' => 'groke''label' => ['en-GB' => 'Groke''de-DE' => 'Groke']],
  99.                 ],
  100.                 'label'               => [
  101.                     'en-GB' => 'AtBit Mode',
  102.                     'de-DE' => 'AtBit Modus',
  103.                 ],
  104.             ],
  105.         ];
  106.     }
  107.     private function removeCustomField(UninstallContext $uninstallContext): void
  108.     {
  109.         $fieldIds $this->customFieldsExist($uninstallContext->getContext());
  110.         if ($fieldIds) {
  111.             $fields array_values($fieldIds->getData());
  112.             $this->getCustomFieldSetRepository()->delete($fields$uninstallContext->getContext());
  113.         }
  114.     }
  115.     private function customFieldsExist(Context $context): ?IdSearchResult
  116.     {
  117.         $criteria = new Criteria();
  118.         $criteria->addFilter(new EqualsAnyFilter('name', ['ec_atbit']));
  119.         $ids $this->getCustomFieldSetRepository()->searchIds($criteria$context);
  120.         return $ids->getTotal() > $ids null;
  121.     }
  122.     private function getCustomFieldSetRepository(): EntityRepository
  123.     {
  124.         if (!$this->container instanceof ContainerInterface) {
  125.             throw new \RuntimeException('Service container not available.');
  126.         }
  127.         $repository $this->container->get('custom_field_set.repository');
  128.         if (!$repository instanceof EntityRepository) {
  129.             throw new \RuntimeException('custom_field_set.repository is not an EntityRepository.');
  130.         }
  131.         return $repository;
  132.     }
  133. }