<?php declare(strict_types=1);
namespace Acris\Rules;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
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\Framework\Plugin\Context\UpdateContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\Snippet\SnippetEntity;
class AcrisRulesCS extends Plugin
{
const CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET = 'acris_rules_customer';
const CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT = 'acris_credit_limit';
const CUSTOM_FIELD_SET_NAME_CUSTOMER_RESTRICTED_PAYMENT = 'acris_restricted_payment';
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET]);
}
public function install(InstallContext $context): void
{
$this->addCustomFields($context->getContext());
}
public function update(UpdateContext $context): void
{
if(version_compare($context->getUpdatePluginVersion(), '1.3.0', '>=') && version_compare($context->getCurrentPluginVersion(), '1.3.0', '<')) {
$this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET]);
}
$this->addCustomFields($context->getContext());
}
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_CREDIT_LIMIT_SET)), $context)->count() == 0) {
$customFieldSet->create([[
'name' => self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET,
'config' => [
'label' => [
'en-GB' => 'ACRIS Rule Builder Extension',
'de-DE' => 'ACRIS Rule Builder Erweiterung'
]
],
'relations' => [
[
'entityName' => 'customer'
]
],
'customFields' => [
['name' => self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT, 'type' => 'number',
'config' => [
'componentName' => 'sw-field',
'type' => 'text',
'customFieldType' => 'number',
'customFieldPosition' => 1,
'numberType' => 'float',
'label' => [
'en-GB' => 'Credit limit',
'de-DE' => 'Kredit Limit'
],
'helpText' => [
'en-GB' => 'The credit limit ca be used in the rules.',
'de-DE' => 'Das Kredit Limit kann in den Regeln verwendet werden.'
]
]],
['name' => self::CUSTOM_FIELD_SET_NAME_CUSTOMER_RESTRICTED_PAYMENT, 'type' => CustomFieldTypes::BOOL,
'config' => [
'componentName' => 'sw-field',
'type' => 'checkbox',
'customFieldType' => 'checkbox',
'customFieldPosition' => 1,
'label' => [
'en-GB' => 'Restrict payments',
'de-DE' => 'Zahlungsarten einschränken'
],
'helpText' => [
'en-GB' => 'The field "Restrict payments" can be used in the Rule Builder to restrict payment methods for the customer.',
'de-DE' => 'Das Feld "Zahlungsarten einschränken" kann im Rule Builder verwendet werden um Zahlungsarten für den Kunden einzuschränken.'
]
]]
],
]], $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('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 EntityRepository $snippetRepository */
$snippetRepository = $this->container->get('snippet.repository');
$criteria = new Criteria();
$criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
new EqualsFilter('translationKey', 'customFields.' . self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT),
new EqualsFilter('translationKey', 'customFields.' . self::CUSTOM_FIELD_SET_NAME_CUSTOMER_RESTRICTED_PAYMENT),
]));
/** @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);
}
}
}
}