<?php declare(strict_types=1);
namespace LenzPlatformVatIdValidation;
use Doctrine\DBAL\Connection;
use Exception;
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\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
use Shopware\Core\System\CustomField\CustomFieldTypes;
class LenzPlatformVatIdValidation extends Plugin
{
public function install(InstallContext $installContext): void
{
if(!extension_loaded('soap')) {
throw new Exception('PHP-Extension "soap" is not installed, but is a requirement for this plugin.');
}
foreach (array_keys($this->customFieldSets) as $key)
{
$this->createOrUpdateCustomFields($key);
}
}
public function activate(ActivateContext $activateContext): void
{
foreach (array_keys($this->customFieldSets) as $key)
{
$this->createOrUpdateCustomFields($key);
}
}
public function update(UpdateContext $updateContext): void
{
foreach (array_keys($this->customFieldSets) as $key)
{
$this->createOrUpdateCustomFields($key);
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
if(!$uninstallContext->keepUserData()) {
$connection = $this->container->get(Connection::class);
$tablesToDelete = [
'lenz_platform_vat_id_validation_result',
];
foreach ($tablesToDelete as $table) {
try {
$connection->executeStatement('DROP TABLE IF EXISTS `' . $table . '`');
} catch(Exception $e) {
echo "Table \"" . $table . "\" not deleted.\n\r";
}
}
foreach (array_keys($this->customFieldSets) as $key)
{
$this->deleteCustomFieldSet($key);
}
}
}
// -- Custom fields
private array $customFieldSets = [
'lenz_platform_vat_id_validation_customer' => [
'name' => 'lenz_platform_vat_id_validation_customer',
'config' => [
'label' => [
'en-GB' => 'VatID Validation - Customer',
'de-DE' => 'USTID Validierung - Kunde',
],
],
'relations' => [
['entityName' => 'customer']
],
'customFields' => [
[
'name' => 'lenz_platform_vat_id_validation_customer_validation_type',
'type' => CustomFieldTypes::SELECT,
'config' => [
'customFieldType' => 'select',
'componentName' => 'sw-single-select',
'customFieldPosition' => 101,
'label' => [
'en-GB' => 'Validation type',
'de-DE' => 'Validierungstyp',
],
'placeholder' => [
'en-GB' => 'Alternative validation type for this customer (Validation on taxfree delivery + checkout)',
'de-DE' => 'Alternativer Validierungstyp für diesen Kunden (Validierung bei Steuerfreiheit + Bestellabschluss)',
],
'options' => [
[
'label' => [
'en-GB' => 'None',
'de-DE' => 'Keine',
],
'value' => 0
],
[
'label' => [
'en-GB' => 'Offline (only syntax)',
'de-DE' => 'Offline (nur Syntax)',
],
'value' => 1
],
[
'label' => [
'en-GB' => 'Simple (DE + EU)',
'de-DE' => 'Einfach (DE + EU)',
],
'value' => 2
],
[
'label' => [
'en-GB' => 'Extended (DE + EU with suggestions) (recommended)',
'de-DE' => 'Erweitert (DE + EU mit Vorschlägen) (empfohlen)',
],
'value' => 3
]
],
],
],
],
],
];
private function findCustomFieldSet($name): ?CustomFieldSetEntity
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('name', $name));
$criteria->addAssociation('customFields');
$criteria->addAssociation('relations');
/** @var CustomFieldSetEntity|null $customFieldSet */
return $customFieldSetRepository->search($criteria, Context::createDefaultContext())->first();
}
public function createOrUpdateCustomFields($name)
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSet = $this->findCustomFieldSet($name);
$customFieldSetId = null;
$customFieldName2Id = [];
$relationEntity2Id = [];
if(!empty($customFieldSet)) {
$customFieldSetId = $customFieldSet->getId();
foreach ($customFieldSet->getCustomFields() as $customField) {
$customFieldName2Id[$customField->getName()] = $customField->getId();
}
foreach ($customFieldSet->getRelations() as $relation) {
$relationEntity2Id[$relation->getEntityName()] = $relation->getId();
}
}
$customFieldSet = $this->customFieldSets[$name];
$customFieldSet['id'] = $customFieldSetId;
foreach ($customFieldSet['customFields'] as $customFieldKey => $customField) {
if(!array_key_exists($customField['name'], $customFieldName2Id)) {
continue;
}
$customFieldSet['customFields'][$customFieldKey]['id'] = $customFieldName2Id[$customField['name']];
}
foreach ($customFieldSet['relations'] as $relationKey => $relation) {
if(!array_key_exists($relation['entityName'], $relationEntity2Id)) {
continue;
}
$customFieldSet['relations'][$relationKey]['id'] = $relationEntity2Id[$relation['entityName']];
}
$customFieldSetRepository->upsert(
[ $customFieldSet ],
Context::createDefaultContext()
);
}
private function deleteCustomFieldSet(string $name)
{
/** @var EntityRepositoryInterface $customFieldSetRepository */
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSet = $this->findCustomFieldSet($name);
if ($customFieldSet === null) {
return;
}
$customFieldSetRepository->delete(
[
['id' => $customFieldSet->getId()],
],
Context::createDefaultContext()
);
}
}