<?php declare(strict_types=1);
namespace Acris\SeparateStreet;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
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;
use Shopware\Core\System\Snippet\SnippetEntity;
class AcrisSeparateStreetCS extends Plugin
{
const CUSTOM_FIELD_SET_NAME_ADDRESS_STREET = 'acris_separate_street_address';
public function install(InstallContext $context): void
{
$this->addCustomFields($context->getContext());
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET]);
}
/**
* @param Context $context
*/
private function addCustomFields(Context $context): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get('custom_field_set.repository');
if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name', self::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET)), $context)->count() == 0) {
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET,
'config' => [
'label' => [
'en-GB' => 'ACRIS Separate Street Address',
'de-DE' => 'ACRIS Getrennte Straßenadresse'
]
],
'customFields' => [
['name' => 'acris_separate_street_address_street', 'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => CustomFieldTypes::TEXT,
'customFieldType' => CustomFieldTypes::TEXT,
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Street',
'de-DE' => 'Straße'
]
]],
['name' => 'acris_separate_street_address_house_number', 'type' => CustomFieldTypes::TEXT,
'config' => [
'componentName' => 'sw-field',
'type' => CustomFieldTypes::TEXT,
'customFieldType' => CustomFieldTypes::TEXT,
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'House number',
'de-DE' => 'Hausnummer'
]
]]
],
]], $context);
};
}
/**
* @param Context $context
* @param array $setNames
*/
private function removeCustomFields(Context $context, array $setNames): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get('custom_field_set.repository');
foreach ($setNames as $setName) {
$id = $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name', $setName)), $context)->firstId();
if($id) $customFieldSet->delete([['id' => $id]], $context);
}
}
private function checkForExistingCustomFieldSnippets(Context $context)
{
/** @var EntityRepositoryInterface $snippetRepository */
$snippetRepository = $this->container->get('snippet.repository');
$criteria = new Criteria();
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('translationKey', 'customFields.' . 'acris_separate_street_address_street'),
new EqualsFilter('translationKey', 'customFields.' . 'acris_separate_street_address_house_number')
]));
/** @var EntitySearchResult $searchResult */
$searchResult = $snippetRepository->search($criteria, $context);
if ($searchResult->count() > 0) {
$snippetIds = [];
/** @var SnippetEntity $snippet */
foreach ($searchResult->getEntities()->getElements() as $snippet) {
$snippetIds[] = [
'id' => $snippet->getId()
];
}
if (!empty($snippetIds)) {
$snippetRepository->delete($snippetIds, $context);
}
}
}
}