custom/plugins/AcrisRulesCS/src/AcrisRulesCS.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\Rules;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. use Shopware\Core\System\Snippet\SnippetEntity;
  15. class AcrisRulesCS extends Plugin
  16. {
  17.     const CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET 'acris_rules_customer';
  18.     const CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT 'acris_credit_limit';
  19.     const CUSTOM_FIELD_SET_NAME_CUSTOMER_RESTRICTED_PAYMENT 'acris_restricted_payment';
  20.     public function uninstall(UninstallContext $context): void
  21.     {
  22.         parent::uninstall($context);
  23.         if ($context->keepUserData()) {
  24.             return;
  25.         }
  26.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET]);
  27.     }
  28.     public function install(InstallContext $context): void
  29.     {
  30.         $this->addCustomFields($context->getContext());
  31.     }
  32.     public function update(UpdateContext $context): void
  33.     {
  34.         if(version_compare($context->getUpdatePluginVersion(), '1.3.0''>=') && version_compare($context->getCurrentPluginVersion(), '1.3.0''<')) {
  35.             $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET]);
  36.         }
  37.         $this->addCustomFields($context->getContext());
  38.     }
  39.     private function addCustomFields(Context $context): void
  40.     {
  41.         /* Check for snippets if they exist for custom fields */
  42.         $this->checkForExistingCustomFieldSnippets($context);
  43.         $customFieldSet $this->container->get('custom_field_set.repository');
  44.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET)), $context)->count() == 0) {
  45.             $customFieldSet->create([[
  46.                 'name' => self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT_SET,
  47.                 'config' => [
  48.                     'label' => [
  49.                         'en-GB' => 'ACRIS Rule Builder Extension',
  50.                         'de-DE' => 'ACRIS Rule Builder Erweiterung'
  51.                     ]
  52.                 ],
  53.                 'relations' => [
  54.                     [
  55.                         'entityName' => 'customer'
  56.                     ]
  57.                 ],
  58.                 'customFields' => [
  59.                     ['name' => self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT'type' => 'number',
  60.                         'config' => [
  61.                             'componentName' => 'sw-field',
  62.                             'type' => 'text',
  63.                             'customFieldType' => 'number',
  64.                             'customFieldPosition' => 1,
  65.                             'numberType' => 'float',
  66.                             'label' => [
  67.                                 'en-GB' => 'Credit limit',
  68.                                 'de-DE' => 'Kredit Limit'
  69.                             ],
  70.                             'helpText' => [
  71.                                 'en-GB' => 'The credit limit ca be used in the rules.',
  72.                                 'de-DE' => 'Das Kredit Limit kann in den Regeln verwendet werden.'
  73.                             ]
  74.                         ]],
  75.                     ['name' => self::CUSTOM_FIELD_SET_NAME_CUSTOMER_RESTRICTED_PAYMENT'type' => CustomFieldTypes::BOOL,
  76.                         'config' => [
  77.                             'componentName' => 'sw-field',
  78.                             'type' => 'checkbox',
  79.                             'customFieldType' => 'checkbox',
  80.                             'customFieldPosition' => 1,
  81.                             'label' => [
  82.                                 'en-GB' => 'Restrict payments',
  83.                                 'de-DE' => 'Zahlungsarten einschränken'
  84.                             ],
  85.                             'helpText' => [
  86.                                 'en-GB' => 'The field "Restrict payments" can be used in the Rule Builder to restrict payment methods for the customer.',
  87.                                 'de-DE' => 'Das Feld "Zahlungsarten einschränken" kann im Rule Builder verwendet werden um Zahlungsarten für den Kunden einzuschränken.'
  88.                             ]
  89.                         ]]
  90.                 ],
  91.             ]], $context);
  92.         }
  93.     }
  94.     private function removeCustomFields(Context $context, array $setNames): void
  95.     {
  96.         /* Check for snippets if they exist for custom fields */
  97.         $this->checkForExistingCustomFieldSnippets($context);
  98.         $customFieldSet $this->container->get('custom_field_set.repository');
  99.         foreach ($setNames as $setName) {
  100.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  101.             if($id$customFieldSet->delete([['id' => $id]], $context);
  102.         }
  103.     }
  104.     private function checkForExistingCustomFieldSnippets(Context $context)
  105.     {
  106.         /** @var EntityRepository $snippetRepository */
  107.         $snippetRepository $this->container->get('snippet.repository');
  108.         $criteria = new Criteria();
  109.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  110.             new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_SET_NAME_CREDIT_LIMIT),
  111.             new EqualsFilter('translationKey''customFields.' self::CUSTOM_FIELD_SET_NAME_CUSTOMER_RESTRICTED_PAYMENT),
  112.         ]));
  113.         /** @var EntitySearchResult $searchResult */
  114.         $searchResult $snippetRepository->search($criteria$context);
  115.         if ($searchResult->count() > 0) {
  116.             $snippetIds = [];
  117.             /** @var SnippetEntity $snippet */
  118.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  119.                 $snippetIds[] = [
  120.                     'id' => $snippet->getId()
  121.                 ];
  122.             }
  123.             if (!empty($snippetIds)) {
  124.                 $snippetRepository->delete($snippetIds$context);
  125.             }
  126.         }
  127.     }
  128. }