<?php declare(strict_types=1);
namespace Memo\PostcodenlPlugin;
use Doctrine\DBAL\Connection;
use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\Adapter\Cache\CacheClearer;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class memoPostcodePlugin extends Plugin
{
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createCustomFields();
}
public function uninstall(UninstallContext $uninstallContext): void
{
$this->deleteCustomFields();
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
}
private function createCustomFields()
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$postcodeUuid = Uuid::randomHex();
$customFieldSetRepository->upsert([
[
'id' => $postcodeUuid,
'name' => 'postcodenl',
'config' => [
'label' => [
'en-GB' => 'Postcode NL',
'nl-NL' => 'Postcode NL'
]
],
'customFields' => [
[
'id' => Uuid::randomHex(),
'name' => 'postcodenl_streetname',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-text',
'customFieldType' => 'text',
'customFieldPosition' => 1,
'disabled' => true,
'label' => [
'en-GB' => 'Street',
'nl-NL' => 'Straat'
]
]
],
[
'id' => Uuid::randomHex(),
'name' => 'postcodenl_housenumber',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-text',
'customFieldType' => 'text',
'customFieldPosition' => 2,
'disabled' => true,
'label' => [
'en-GB' => 'Housenumber',
'nl-NL' => 'Huisnummer'
]
]
],
[
'id' => Uuid::randomHex(),
'name' => 'postcodenl_housenumber_addition',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-text',
'customFieldType' => 'text',
'customFieldPosition' => 3,
'disabled' => true,
'label' => [
'en-GB' => 'Housenumber Addition',
'nl-NL' => 'Huisnummer Toevoeging'
]
]
],
[
'id' => Uuid::randomHex(),
'name' => 'postcodenl_zipcode',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-text',
'customFieldType' => 'text',
'customFieldPosition' => 4,
'disabled' => true,
'label' => [
'en-GB' => 'Zipcode',
'nl-NL' => 'Postcode'
]
]
],
[
'id' => Uuid::randomHex(),
'name' => 'postcodenl_city',
'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-text',
'customFieldType' => 'text',
'customFieldPosition' => 5,
'disabled' => true,
'label' => [
'en-GB' => 'City',
'nl-NL' => 'Plaats'
]
]
],
[
'id' => Uuid::randomHex(),
'name' => 'postcodenl_autocomplete_support',
'type' => CustomFieldTypes::JSON,
'config' => [
'componentName' => 'sw-hidden',
'customFieldType' => 'text',
'customFieldPosition' => 10,
'disabled' => true,
'label' => [
'en-GB' => 'Autocomplete support',
'nl-NL' => 'Autocomplete support'
]
]
]
],
'relations' => [
[
'id' => $postcodeUuid,
'entityName' => $this->container->get(CustomerAddressDefinition::class)->getEntityName()
],
]
]
], Context::createDefaultContext());
}
private function deleteCustomFields()
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$entityIds = $customFieldSetRepository->search(
(new Criteria())->addFilter(new EqualsFilter('name', 'postcodenl')),
Context::createDefaultContext()
)->getEntities()->getIds();
if (count($entityIds) < 1) {
return;
}
$entityIds = array_map(function ($element) {
return ['id' => $element];
}, array_values($entityIds));
$customFieldSetRepository->delete(
$entityIds,
Context::createDefaultContext()
);
}
}