vendor/store.shopware.com/viob2blogin/src/VioB2BLogin.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace VioB2BLogin;
  4. use Doctrine\DBAL\Connection;
  5. use RuntimeException;
  6. use Shopware\Core\Checkout\Order\OrderDefinition;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  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\Aggregate\CustomFieldSet\CustomFieldSetDefinition;
  15. use Shopware\Core\System\CustomField\CustomFieldTypes;
  16. use VioB2BLogin\Entity\Employee\EmployeeDefinition;
  17. class VioB2BLogin extends Plugin
  18. {
  19.     public const CUSTOM_FIELDSET_NAME 'vio_b2b';
  20.     public const EMPLOYEE_FIELD 'vio_b2b_employee';
  21.     public function install(InstallContext $installContext): void
  22.     {
  23.         $this->updateCustomFields($installContext->getContext());
  24.         parent::install($installContext);
  25.     }
  26.     public function update(UpdateContext $updateContext): void
  27.     {
  28.         $this->updateCustomFields($updateContext->getContext());
  29.         parent::update($updateContext);
  30.     }
  31.     public function uninstall(UninstallContext $uninstallContext): void
  32.     {
  33.         parent::uninstall($uninstallContext);
  34.         if ($uninstallContext->keepUserData()) {
  35.             return;
  36.         }
  37.         $connection $this->container->get(Connection::class);
  38.         if ($connection) {
  39.             $connection->executeStatement('DROP TABLE IF EXISTS vio_b2b_employee_address');
  40.             $connection->executeStatement('DROP TABLE IF EXISTS vio_b2b_employee_privilege');
  41.             $connection->executeStatement('DROP TABLE IF EXISTS vio_b2b_employee_recovery');
  42.             $connection->executeStatement('DROP TABLE IF EXISTS vio_b2b_employee');
  43.             $connection->executeStatement('DROP TABLE IF EXISTS vio_b2b_privilege');
  44.         }
  45.         // drop custom field
  46.         $this->removeCustomFields($uninstallContext->getContext());
  47.     }
  48.     /**
  49.      * @param Context $context
  50.      */
  51.     private function updateCustomFields(Context $context): void
  52.     {
  53.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  54.         if (!$customFieldSetRepository) {
  55.             throw new RuntimeException("Couldn't resolve service 'custom_field_set.repository'");
  56.         }
  57.         $fieldSetName = static::CUSTOM_FIELDSET_NAME;
  58.         $fieldSetId $customFieldSetRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$fieldSetName)), $context)->firstId();
  59.         if ($fieldSetId === null) {
  60.             $fieldSetData = [
  61.                 'name' => $fieldSetName,
  62.                 'config' => [
  63.                     'label' => [
  64.                         'de-DE' => 'B2B Daten',
  65.                         'en-GB' => 'B2B Data'
  66.                     ]
  67.                 ],
  68.                 'relations' => [
  69.                     ['entityName' => OrderDefinition::ENTITY_NAME]
  70.                 ]
  71.             ];
  72.             $fieldSetData['id'] = $fieldSetId;
  73.             /** @var string[] $primaryKeys */
  74.             $primaryKeys $customFieldSetRepository
  75.                 ->upsert([$fieldSetData], $context)
  76.                 ->getPrimaryKeys(CustomFieldSetDefinition::ENTITY_NAME);
  77.             $fieldSetId array_shift($primaryKeys);
  78.         }
  79.         if ($fieldSetId !== null) {
  80.             $this->updateCustomField(
  81.                 $fieldSetId,
  82.                 static::EMPLOYEE_FIELD,
  83.                 [
  84.                     'name' => static::EMPLOYEE_FIELD,
  85.                     'type' => CustomFieldTypes::ENTITY,
  86.                     'config' => [
  87.                         'label' => [
  88.                             'de-DE' => 'Verursachender Mitarbeiter',
  89.                             'en-GB' => 'Initiating employee'
  90.                         ],
  91.                         'entity' => EmployeeDefinition::ENTITY_NAME,
  92.                         'componentName' => 'sw-entity-single-select',
  93.                         'labelProperty' => ['number''firstName''lastName']
  94.                     ],
  95.                 ],
  96.                 $context
  97.             );
  98.         }
  99.     }
  100.     /**
  101.      * @param string $fieldSetId
  102.      * @param string $fieldName
  103.      * @param array $fieldData
  104.      * @param Context $context
  105.      * @noinspection DuplicatedCode
  106.      * @noinspection PhpSameParameterValueInspection
  107.      */
  108.     private function updateCustomField(string $fieldSetIdstring $fieldName, array $fieldDataContext $context): void
  109.     {
  110.         $customFieldRepository $this->container->get('custom_field.repository');
  111.         if (!$customFieldRepository) {
  112.             throw new RuntimeException("Couldn't resolve service 'custom_field.repository'");
  113.         }
  114.         if (!array_key_exists('name'$fieldData)) {
  115.             $fieldData['name'] = $fieldName;
  116.         }
  117.         if (!array_key_exists('customFieldSetId'$fieldData)) {
  118.             $fieldData['customFieldSetId'] = $fieldSetId;
  119.         }
  120.         $fieldId $customFieldRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$fieldName)), $context)->firstId();
  121.         if ($fieldId !== null) {
  122.             $fieldData['id'] = $fieldId;
  123.         }
  124.         $customFieldRepository->upsert([$fieldData], $context);
  125.     }
  126.     private function removeCustomFields(Context $context): void
  127.     {
  128.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  129.         if (!$customFieldSetRepository) {
  130.             throw new RuntimeException("Couldn't resolve service 'custom_field_set.repository'");
  131.         }
  132.         $fieldSetName self::CUSTOM_FIELDSET_NAME;
  133.         $fieldSetId $customFieldSetRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$fieldSetName)), $context)->firstId();
  134.         if ($fieldSetId) {
  135.             $customFieldSetRepository->delete([
  136.                 [
  137.                     'id' => $fieldSetId
  138.                 ]
  139.             ], $context);
  140.         }
  141.     }
  142. }