custom/plugins/FourtwosixRegistrationFields/src/FourtwosixRegistrationFields.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace FourtwosixRegistrationFields;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  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. class FourtwosixRegistrationFields extends Plugin
  16. {
  17.     public const TEXT_COMPONENT_NAME 'sw-text-field';
  18.     public const EDITOR_COMPONENT_NAME 'sw-text-editor';
  19.     public const SELECT_COMPONENT_NAME 'sw-single-select';
  20.     public const ENTITY_COMPONENT_NAME 'sw-entity-single-select';
  21.     public const ATTRIBUTE_SET_NAME "custom_register_extensions_set";
  22.     public const ATTRIBUTE_SET_LABEL_EN "Customer Additional Fields (426)";
  23.     public const ATTRIBUTE_SET_LABEL_DE "Kunden Zusatzfelder (426)";
  24.     public const ATTRIBUTE_SET_LABEL_IT "Campi aggiuntivi dei clienti (426)";
  25.     public const FISCAL_CODE_ATTRIBUTE_NAME "custom_register_extensions_fiscalcode";
  26.     public const FISCAL_CODE_ATTRIBUTE_LABEL_EN "Fiscal Code";
  27.     public const FISCAL_CODE_ATTRIBUTE_LABEL_DE "Steuernummer";
  28.     public const FISCAL_CODE_ATTRIBUTE_LABEL_IT "Codice fiscale";
  29.     public const PEC_ADDRESS_ATTRIBUTE_NAME "custom_register_extensions_pecaddress";
  30.     public const PEC_ADDRESS_ATTRIBUTE_LABEL_EN "PEC Address";
  31.     public const PEC_ADDRESS_ATTRIBUTE_LABEL_DE "PEC Adresse";
  32.     public const PEC_ADDRESS_ATTRIBUTE_LABEL_IT "Indirizzo PEC";
  33.     public const RECIPIENT_CODE_ATTRIBUTE_NAME "custom_register_extensions_recipientcode";
  34.     public const RECIPIENT_CODE_ATTRIBUTE_LABEL_EN "Recipient Code";
  35.     public const RECIPIENT_CODE_ATTRIBUTE_LABEL_DE "Empfängerkodex";
  36.     public const RECIPIENT_CODE_ATTRIBUTE_LABEL_IT "Codice destinatario";
  37.     public const COMPANY_TYPE_ATTRIBUTE_NAME "custom_register_extensions_companytype";
  38.     public const COMPANY_TYPE_ATTRIBUTE_LABEL_EN "Company Type";
  39.     public const COMPANY_TYPE_ATTRIBUTE_LABEL_DE "Unternehmensform";
  40.     public const COMPANY_TYPE_ATTRIBUTE_LABEL_IT "Tipo azienda";
  41.     public const CONTACT_ATTRIBUTE_NAME "fts_register_extensions_contact_person";
  42.     public const CONTACT_ATTRIBUTE_LABEL_EN "Contact";
  43.     public const CONTACT_ATTRIBUTE_LABEL_DE "Kontaktperson";
  44.     public const CONTACT_ATTRIBUTE_LABEL_IT "Contatto";
  45.     public function install(InstallContext $installContext): void
  46.     {
  47.         $this->createCustomFields($installContext->getContext());
  48.     }
  49.     public function uninstall(UninstallContext $uninstallContext): void
  50.     {
  51.         if ($uninstallContext->keepUserData()) {
  52.             return;
  53.         }
  54.         $this->deleteCustomFields($uninstallContext->getContext());
  55.     }
  56.     public function update(UpdateContext $updateContext): void
  57.     {
  58.         $this->updateCustomFields($updateContext->getContext());
  59.     }
  60.     private function updateCustomFields(Context $context): void
  61.     {
  62.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  63.         $criteria = new Criteria();
  64.         $criteria->addFilter(new EqualsFilter('name'self::ATTRIBUTE_SET_NAME));
  65.         $result $customFieldSetRepository->search($criteria$context)->first();
  66.         if (!empty($result)) {
  67.             $customFieldRepository $this->container->get('custom_field.repository');
  68.             $criteria = new Criteria();
  69.             $criteria->addFilter(new EqualsFilter('customFieldSetId'$result->getId()));
  70.             $criteria->addFilter(new EqualsFilter('name'self::CONTACT_ATTRIBUTE_NAME));
  71.             $res $customFieldRepository->search($criteria$context)->first();
  72.             if (empty($res)) {
  73.                 $customField $this->getCustomField(
  74.                     self::CONTACT_ATTRIBUTE_NAME,
  75.                     CustomFieldTypes::TEXT,
  76.                     [
  77.                         'en-GB' => self::CONTACT_ATTRIBUTE_LABEL_EN,
  78.                         'de-DE' => self::CONTACT_ATTRIBUTE_LABEL_DE,
  79.                         'it-IT' => self::CONTACT_ATTRIBUTE_LABEL_IT,
  80.                     ],
  81.                     self::TEXT_COMPONENT_NAME,
  82.                     5
  83.                 );
  84.                 $customField['customFieldSetId'] = $result->getId();
  85.                 $customFieldRepository->upsert([$customField], $context);
  86.             }
  87.         } else {
  88.             $this->createCustomFields($context);
  89.         }
  90.     }
  91.     private function createCustomFields(Context $context): void
  92.     {
  93.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  94.         $criteria = new Criteria();
  95.         $criteria->addFilter(new EqualsFilter('name'self::ATTRIBUTE_SET_NAME));
  96.         if ($customFieldSetRepository->search($criteria$context)->first()) {
  97.             return;
  98.         }
  99.         $customFieldData $this->getCustomFieldData();
  100.         $customFieldSetRepository->upsert([$customFieldData], $context);
  101.     }
  102.     private function deleteCustomFields(Context $context): void
  103.     {
  104.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  105.         if (!$customFields $this->getCustomFields($customFieldSetRepository$context)) {
  106.             return;
  107.         }
  108.         $customFieldSetRepository->delete(array_values($customFields->getData()), $context);
  109.     }
  110.     private function getCustomFields(EntityRepository &$customFieldSetRepositoryContext &$context): ?IdSearchResult
  111.     {
  112.         $criteria = new Criteria();
  113.         $criteria->addFilter(new EqualsAnyFilter('name', [self::ATTRIBUTE_SET_NAME]));
  114.         $customFields $customFieldSetRepository->searchIds($criteria$context);
  115.         return ($customFields->getTotal() > 0) ? $customFields null;
  116.     }
  117.     private function getCustomFieldData(): array
  118.     {
  119.         return [
  120.             'name' => self::ATTRIBUTE_SET_NAME,
  121.             'config' => [
  122.                 'label' => [
  123.                     'en-GB' => self::ATTRIBUTE_SET_LABEL_EN,
  124.                     'de-DE' => self::ATTRIBUTE_SET_LABEL_DE,
  125.                     'it-IT' => self::ATTRIBUTE_SET_LABEL_IT,
  126.                 ]
  127.             ],
  128.             'customFields' => [
  129.                 $this->getCustomField(
  130.                     self::FISCAL_CODE_ATTRIBUTE_NAME,
  131.                     CustomFieldTypes::TEXT,
  132.                     [
  133.                         'en-GB' => self::FISCAL_CODE_ATTRIBUTE_LABEL_EN,
  134.                         'de-DE' => self::FISCAL_CODE_ATTRIBUTE_LABEL_DE,
  135.                         'it-IT' => self::FISCAL_CODE_ATTRIBUTE_LABEL_IT,
  136.                     ],
  137.                     self::TEXT_COMPONENT_NAME,
  138.                     1
  139.                 ),
  140.                 $this->getCustomField(
  141.                     self::RECIPIENT_CODE_ATTRIBUTE_NAME,
  142.                     CustomFieldTypes::TEXT,
  143.                     [
  144.                         'en-GB' => self::RECIPIENT_CODE_ATTRIBUTE_LABEL_EN,
  145.                         'de-DE' => self::RECIPIENT_CODE_ATTRIBUTE_LABEL_DE,
  146.                         'it-IT' => self::RECIPIENT_CODE_ATTRIBUTE_LABEL_IT,
  147.                     ],
  148.                     self::TEXT_COMPONENT_NAME,
  149.                     2
  150.                 ),
  151.                 $this->getCustomField(
  152.                     self::PEC_ADDRESS_ATTRIBUTE_NAME,
  153.                     CustomFieldTypes::TEXT,
  154.                     [
  155.                         'en-GB' => self::PEC_ADDRESS_ATTRIBUTE_LABEL_EN,
  156.                         'de-DE' => self::PEC_ADDRESS_ATTRIBUTE_LABEL_DE,
  157.                         'it-IT' => self::PEC_ADDRESS_ATTRIBUTE_LABEL_IT,
  158.                     ],
  159.                     self::TEXT_COMPONENT_NAME,
  160.                     3
  161.                 ),
  162.                 $this->getCustomField(
  163.                     self::COMPANY_TYPE_ATTRIBUTE_NAME,
  164.                     CustomFieldTypes::SELECT,
  165.                     [
  166.                         'en-GB' => self::COMPANY_TYPE_ATTRIBUTE_LABEL_EN,
  167.                         'de-DE' => self::COMPANY_TYPE_ATTRIBUTE_LABEL_DE,
  168.                         'it-IT' => self::COMPANY_TYPE_ATTRIBUTE_LABEL_IT,
  169.                     ],
  170.                     self::SELECT_COMPONENT_NAME,
  171.                     4,
  172.                     [
  173.                         'options' => [
  174.                             [
  175.                                 'label' => [
  176.                                     'en-GB' => 'Company',
  177.                                     'de-DE' => 'Gesellschaft/Verein (GmbH, KG, ...)',
  178.                                     'it-IT' => 'Società/Associazione (srl, sas, ...)',
  179.                                 ],
  180.                                 'value' => 'company',
  181.                             ],
  182.                             [
  183.                                 'label' => [
  184.                                     'en-GB' => 'Self employed',
  185.                                     'de-DE' => 'Einzelunternehmen',
  186.                                     'it-IT' => 'Professionista con Partita IVA',
  187.                                 ],
  188.                                 'value' => 'selfEmployed',
  189.                             ],
  190.                             [
  191.                                 'label' => [
  192.                                     'en-GB' => 'Public administration',
  193.                                     'de-DE' => 'Öffentliche Verwaltung',
  194.                                     'it-IT' => 'Pubblica Amministrazione',
  195.                                 ],
  196.                                 'value' => 'publicAdministration',
  197.                             ],
  198.                         ],
  199.                     ]
  200.                 ),
  201.                 $this->getCustomField(
  202.                     self::CONTACT_ATTRIBUTE_NAME,
  203.                     CustomFieldTypes::TEXT,
  204.                     [
  205.                         'en-GB' => self::CONTACT_ATTRIBUTE_LABEL_EN,
  206.                         'de-DE' => self::CONTACT_ATTRIBUTE_LABEL_DE,
  207.                         'it-IT' => self::CONTACT_ATTRIBUTE_LABEL_IT,
  208.                     ],
  209.                     self::TEXT_COMPONENT_NAME,
  210.                     5
  211.                 )
  212.             ],
  213.             'relations' => [
  214.                 [
  215.                     'entityName' => 'customer_address',
  216.                 ]
  217.             ],
  218.         ];
  219.     }
  220.     /**
  221.      * @param string $name
  222.      * @param string $type
  223.      * @param array<string> $label
  224.      * @param string $component
  225.      * @param int $position
  226.      * @param ?array<array<string>> $optional
  227.      */
  228.     private function getCustomField(
  229.         string $name,
  230.         string $type,
  231.         array $label,
  232.         string $component,
  233.         int $position,
  234.         ?array $optional null
  235.     ): array {
  236.         $data = [
  237.             'name' => $name,
  238.             'type' => $type,
  239.             'config' => [
  240.                 'label' => $label,
  241.                 "componentName" => $component,
  242.                 "customFieldType" => $type,
  243.                 'customFieldPosition' => $position,
  244.             ],
  245.         ];
  246.         if ($optional) {
  247.             $data['config'] = array_merge($data['config'], $optional);
  248.         }
  249.         return $data;
  250.     }
  251. }