custom/plugins/426-fourtwosixregistrationfields/src/FourtwosixRegistrationFields.php line 18

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