vendor/store.shopware.com/viorepresentativelogin/src/VioRepresentativeLogin.php line 21

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