<?php
declare(strict_types=1);
namespace FourtwosixRegistrationFields;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class FourtwosixRegistrationFields extends Plugin
{
public const TEXT_COMPONENT_NAME = 'sw-text-field';
public const EDITOR_COMPONENT_NAME = 'sw-text-editor';
public const SELECT_COMPONENT_NAME = 'sw-single-select';
public const ENTITY_COMPONENT_NAME = 'sw-entity-single-select';
public const ATTRIBUTE_SET_NAME = "custom_register_extensions_set";
public const ATTRIBUTE_SET_LABEL_EN = "Customer Additional Fields (426)";
public const ATTRIBUTE_SET_LABEL_DE = "Kunden Zusatzfelder (426)";
public const ATTRIBUTE_SET_LABEL_IT = "Campi aggiuntivi dei clienti (426)";
public const FISCAL_CODE_ATTRIBUTE_NAME = "custom_register_extensions_fiscalcode";
public const FISCAL_CODE_ATTRIBUTE_LABEL_EN = "Fiscal Code";
public const FISCAL_CODE_ATTRIBUTE_LABEL_DE = "Steuernummer";
public const FISCAL_CODE_ATTRIBUTE_LABEL_IT = "Codice fiscale";
public const PEC_ADDRESS_ATTRIBUTE_NAME = "custom_register_extensions_pecaddress";
public const PEC_ADDRESS_ATTRIBUTE_LABEL_EN = "PEC Address";
public const PEC_ADDRESS_ATTRIBUTE_LABEL_DE = "PEC Adresse";
public const PEC_ADDRESS_ATTRIBUTE_LABEL_IT = "Indirizzo PEC";
public const RECIPIENT_CODE_ATTRIBUTE_NAME = "custom_register_extensions_recipientcode";
public const RECIPIENT_CODE_ATTRIBUTE_LABEL_EN = "Recipient Code";
public const RECIPIENT_CODE_ATTRIBUTE_LABEL_DE = "Empfängerkodex";
public const RECIPIENT_CODE_ATTRIBUTE_LABEL_IT = "Codice destinatario";
public const COMPANY_TYPE_ATTRIBUTE_NAME = "custom_register_extensions_companytype";
public const COMPANY_TYPE_ATTRIBUTE_LABEL_EN = "Company Type";
public const COMPANY_TYPE_ATTRIBUTE_LABEL_DE = "Unternehmensform";
public const COMPANY_TYPE_ATTRIBUTE_LABEL_IT = "Tipo azienda";
public function install(InstallContext $installContext): void
{
$this->createCustomFields($installContext->getContext());
}
public function uninstall(UninstallContext $uninstallContext): void
{
if ($uninstallContext->keepUserData()) {
return;
}
$this->deleteCustomFields($uninstallContext->getContext());
}
private function createCustomFields(Context $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', self::ATTRIBUTE_SET_NAME));
if ($customFieldSetRepository->search($criteria, $context)->first()) {
return;
}
$customFieldData = $this->getCustomFieldData();
$customFieldSetRepository->upsert([$customFieldData], $context);
}
private function deleteCustomFields(Context $context): void
{
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
if (!$customFields = $this->getCustomFields($customFieldSetRepository, $context)) {
return;
}
$customFieldSetRepository->delete(array_values($customFields->getData()), $context);
}
private function getCustomFields(EntityRepository &$customFieldSetRepository, Context &$context): ?IdSearchResult
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('name', [self::ATTRIBUTE_SET_NAME]));
$customFields = $customFieldSetRepository->searchIds($criteria, $context);
return ($customFields->getTotal() > 0) ? $customFields : null;
}
private function getCustomFieldData(): array
{
return [
'name' => self::ATTRIBUTE_SET_NAME,
'config' => [
'label' => [
'en-GB' => self::ATTRIBUTE_SET_LABEL_EN,
'de-DE' => self::ATTRIBUTE_SET_LABEL_DE,
'it-IT' => self::ATTRIBUTE_SET_LABEL_IT,
]
],
'customFields' => [
$this->getCustomField(
self::FISCAL_CODE_ATTRIBUTE_NAME,
CustomFieldTypes::TEXT,
[
'en-GB' => self::FISCAL_CODE_ATTRIBUTE_LABEL_EN,
'de-DE' => self::FISCAL_CODE_ATTRIBUTE_LABEL_DE,
'it-IT' => self::FISCAL_CODE_ATTRIBUTE_LABEL_IT,
],
self::TEXT_COMPONENT_NAME,
1
),
$this->getCustomField(
self::RECIPIENT_CODE_ATTRIBUTE_NAME,
CustomFieldTypes::TEXT,
[
'en-GB' => self::RECIPIENT_CODE_ATTRIBUTE_LABEL_EN,
'de-DE' => self::RECIPIENT_CODE_ATTRIBUTE_LABEL_DE,
'it-IT' => self::RECIPIENT_CODE_ATTRIBUTE_LABEL_IT,
],
self::TEXT_COMPONENT_NAME,
2
),
$this->getCustomField(
self::PEC_ADDRESS_ATTRIBUTE_NAME,
CustomFieldTypes::TEXT,
[
'en-GB' => self::PEC_ADDRESS_ATTRIBUTE_LABEL_EN,
'de-DE' => self::PEC_ADDRESS_ATTRIBUTE_LABEL_DE,
'it-IT' => self::PEC_ADDRESS_ATTRIBUTE_LABEL_IT,
],
self::TEXT_COMPONENT_NAME,
3
),
$this->getCustomField(
self::COMPANY_TYPE_ATTRIBUTE_NAME,
CustomFieldTypes::SELECT,
[
'en-GB' => self::COMPANY_TYPE_ATTRIBUTE_LABEL_EN,
'de-DE' => self::COMPANY_TYPE_ATTRIBUTE_LABEL_DE,
'it-IT' => self::COMPANY_TYPE_ATTRIBUTE_LABEL_IT,
],
self::SELECT_COMPONENT_NAME,
4,
[
'options' => [
[
'label' => [
'en-GB' => 'Company',
'de-DE' => 'Gesellschaft/Verein (GmbH, KG, ...)',
'it-IT' => 'Società/Associazione (srl, sas, ...)',
],
'value' => 'company',
],
[
'label' => [
'en-GB' => 'Self employed',
'de-DE' => 'Einzelunternehmen',
'it-IT' => 'Professionista con Partita IVA',
],
'value' => 'selfEmployed',
],
[
'label' => [
'en-GB' => 'Public administration',
'de-DE' => 'Öffentliche Verwaltung',
'it-IT' => 'Pubblica Amministrazione',
],
'value' => 'publicAdministration',
],
],
]
),
],
'relations' => [
[
'entityName' => 'customer_address',
]
],
];
}
/**
* @param string $name
* @param string $type
* @param array<string> $label
* @param string $component
* @param int $position
* @param ?array<array<string>> $optional
*/
private function getCustomField(
string $name,
string $type,
array $label,
string $component,
int $position,
?array $optional = null
): array {
$data = [
'name' => $name,
'type' => $type,
'config' => [
'label' => $label,
"componentName" => $component,
"customFieldType" => $type,
'customFieldPosition' => $position,
],
];
if ($optional) {
$data['config'] = array_merge($data['config'], $optional);
}
return $data;
}
}