<?php declare(strict_types=1);
namespace FourtwosixSeparateStreetAndNumber;
use FourtwosixSeparateStreetAndNumber\Components\Constants\Constants;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
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;
class FourtwosixSeparateStreetAndNumber extends Plugin
{
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(), [Constants::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET]);
}
private function addCustomFields(Context $context): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get(Constants::CUSTOM_FIELD_SET_REPOSITORY);
if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter(Constants::FIELD_NAME, Constants::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET)), $context)->count() == 0) {
$customFieldSet->create([[
Constants::FIELD_NAME => Constants::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET,
Constants::FIELD_CONFIG => [
Constants::FIELD_LABEL => [
Constants::FIELD_LABEL_EN_KEY => Constants::FIELD_LABEL_EN,
Constants::FIELD_LABEL_DE_KEY => Constants::FIELD_LABEL_DE,
Constants::FIELD_LABEL_IT_KEY => Constants::FIELD_LABEL_IT
]
],
Constants::ADDRESS_CUSTOM_FIELD_KEY => [
[Constants::FIELD_NAME => Constants::ADDRESS_CUSTOM_FIELD_STREET_KEY, Constants::FIELD_TYPE => CustomFieldTypes::TEXT,
Constants::FIELD_CONFIG => [
Constants::FIELD_COMPONENT_NAME => Constants::FIELD_SW,
Constants::FIELD_TYPE => CustomFieldTypes::TEXT,
Constants::FIELD_CUSTOM_FIELD_TYPE => CustomFieldTypes::TEXT,
Constants::FIELD_CUSTOM_FIELD_POSITION => 1,
Constants::FIELD_LABEL => [
Constants::FIELD_LABEL_EN_KEY => Constants::FIELD_LABEL_STREET_CONFIG_EN,
Constants::FIELD_LABEL_DE_KEY => Constants::FIELD_LABEL_STREET_CONFIG_DE,
Constants::FIELD_LABEL_IT_KEY => Constants::FIELD_LABEL_STREET_CONFIG_IT
]
]],
[Constants::FIELD_NAME => Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEY, Constants::FIELD_TYPE => CustomFieldTypes::TEXT,
Constants::FIELD_CONFIG => [
Constants::FIELD_COMPONENT_NAME => Constants::FIELD_SW,
Constants::FIELD_TYPE => CustomFieldTypes::TEXT,
Constants::FIELD_CUSTOM_FIELD_TYPE => CustomFieldTypes::TEXT,
Constants::FIELD_CUSTOM_FIELD_POSITION => 1,
Constants::FIELD_LABEL => [
Constants::FIELD_LABEL_EN_KEY => Constants::FIELD_LABEL_HOUSE_NUMBER_CONFIG_EN,
Constants::FIELD_LABEL_DE_KEY => Constants::FIELD_LABEL_HOUSE_NUMBER_CONFIG_DE,
Constants::FIELD_LABEL_IT_KEY => Constants::FIELD_LABEL_HOUSE_NUMBER_CONFIG_IT
]
]]
],
]], $context);
};
}
private function removeCustomFields(Context $context, array $setNames): void
{
/* Check for snippets if they exist for custom fields */
$this->checkForExistingCustomFieldSnippets($context);
$customFieldSet = $this->container->get(Constants::CUSTOM_FIELD_SET_REPOSITORY);
foreach ($setNames as $setName) {
$id = $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter(Constants::FIELD_NAME, $setName)), $context)->firstId();
if($id) $customFieldSet->delete([[Constants::ID_KEY => $id]], $context);
}
}
private function checkForExistingCustomFieldSnippets(Context $context)
{
$snippetRepository = $this->container->get(Constants::SNIPPET_REPOSITORY);
$criteria = new Criteria();
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter(Constants::SNIPPET_TRANSLATION_KEY, Constants::ADDRESS_CUSTOM_FIELD_KEY.Constants::POINT.Constants::ADDRESS_CUSTOM_FIELD_STREET_KEY),
new EqualsFilter(Constants::SNIPPET_TRANSLATION_KEY, Constants::ADDRESS_CUSTOM_FIELD_KEY.Constants::POINT.Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEY)
]));
$searchResult = $snippetRepository->search($criteria, $context);
if ($searchResult->count() > 0) {
$snippetIds = [];
foreach ($searchResult->getEntities()->getElements() as $snippet) {
$snippetIds[] = [
Constants::ID_KEY => $snippet->getId()
];
}
if (!empty($snippetIds)) {
$snippetRepository->delete($snippetIds, $context);
}
}
}
}