<?php declare(strict_types=1);
namespace Acris\ShippingCalculation;
use Doctrine\DBAL\Connection;
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\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\Snippet\SnippetEntity;
class AcrisShippingCalculationCS extends Plugin
{
const CUSTOM_FIELD_SET_NAME_SHIPPING_CALCULATION = 'acris_calculation';
public function install(Plugin\Context\InstallContext $context): void
{
$this->addCustomFields($context->getContext());
}
public function uninstall(Plugin\Context\UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_SHIPPING_CALCULATION]);
}
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 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_SHIPPING_CALCULATION)), $context)->count() == 0) {
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME_SHIPPING_CALCULATION,
'config' => [
'label' => [
'en-GB' => 'Shipping calculation',
'de-DE' => 'Berechnung des Versands'
]
],
'customFields' => [
['name' => 'acris_shipping_code', 'type' => CustomFieldTypes::HTML,
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'text',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Shipping calculation template',
'de-DE' => 'Vorlage für die Versandberechnung'
],
'helpText' => [
'en-GB' => 'The template will be used for shipping calculation price.',
'de-DE' => 'Die Vorlage wird für den Preis der Versandberechnung verwendet.'
]
]],
['name' => 'acris_shipping_calculation', 'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'switch',
'customFieldType' => 'switch',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Shipping calculation active',
'de-DE' => 'Versandberechnung aktiv'
],
'helpText' => [
'en-GB' => 'When shipping calculation is active, template will be used for shipping price calculation.',
'de-DE' => 'Wenn die Versandberechnung aktiv ist, wird die Vorlage für die Versandpreisberechnung verwendet.'
]
]]
]
]], $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_shipping_code'),
new EqualsFilter('translationKey', 'customFields.' . 'acris_shipping_calculation')
]));
/** @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);
}
}
}
}