custom/plugins/AcrisProductBadgesCS/src/AcrisProductBadgesCS.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductBadges;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  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\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. class AcrisProductBadgesCS extends Plugin
  15. {
  16.     public function activate(ActivateContext $context): void
  17.     {
  18.         parent::activate($context);
  19.         $this->insertDefaultBadges($context->getContext());
  20.     }
  21.     public function postUpdate(UpdateContext $context): void
  22.     {
  23.         if($context->getPlugin()->isActive() === true && version_compare($context->getCurrentPluginVersion(), '1.3.0''<')
  24.             && version_compare($context->getUpdatePluginVersion(), '1.3.0''>=')) {
  25.             $this->insertDefaultBadges($context->getContext());
  26.         }
  27.         if(version_compare($context->getCurrentPluginVersion(), '1.3.2''<')
  28.             && version_compare($context->getUpdatePluginVersion(), '1.3.2''>=')) {
  29.             $connection $this->container->get(Connection::class);
  30.             $connection->executeStatement("UPDATE acris_product_badge SET internal_name = 'ACRIS Bundle' WHERE internal_name = 'Acris Bundle'");
  31.         }
  32.         if(version_compare($context->getCurrentPluginVersion(), '2.1.0''<')
  33.             && version_compare($context->getUpdatePluginVersion(), '2.1.0''>=') && $context->getPlugin()->isActive() === true) {
  34.             $this->insertDefaultBadges($context->getContext());
  35.         }
  36.     }
  37.     public function uninstall(UninstallContext $context): void
  38.     {
  39.         if ($context->keepUserData()) {
  40.             return;
  41.         }
  42.         $this->cleanupDatabase();
  43.     }
  44.     private function cleanupDatabase(): void
  45.     {
  46.         $connection $this->container->get(Connection::class);
  47.         $connection->executeStatement('DROP TABLE IF EXISTS `acris_product_badge_stream`');
  48.         $connection->executeStatement('DROP TABLE IF EXISTS `acris_product_badge_rule`');
  49.         $connection->executeStatement('DROP TABLE IF EXISTS `acris_product_replace_badge_rule`');
  50.         $connection->executeStatement('DROP TABLE IF EXISTS `acris_product_badge_translation`');
  51.         $connection->executeStatement('DROP TABLE IF EXISTS `acris_product_badge`');
  52.         $connection->executeStatement('ALTER TABLE `product_stream` DROP COLUMN `acrisProductBadges`');
  53.         $connection->executeStatement('ALTER TABLE `rule` DROP COLUMN `acrisProductBadges`');
  54.         $connection->executeStatement('ALTER TABLE `rule` DROP COLUMN `acrisProductBadgesReplace`');
  55.         $connection->executeStatement('ALTER TABLE `promotion` DROP COLUMN `product_badge_id`');
  56.         $connection->executeStatement('ALTER TABLE `promotion` DROP COLUMN `productBadge`');
  57.     }
  58.     public function insertDefaultBadges(Context $context)
  59.     {
  60.         $badgeRepository $this->container->get('acris_product_badge.repository');
  61.         $defaultSaleId Uuid::randomHex();
  62.         $defaultTipId Uuid::randomHex();
  63.         $defaultNewId Uuid::randomHex();
  64.         $defaultBundleId Uuid::randomHex();
  65.         $badges =
  66.             [
  67.                 [
  68.                     'id' => $defaultSaleId,
  69.                     'internalName' => 'Sale',
  70.                     'active' => true,
  71.                     'priority' => 4,
  72.                     'badgeTextColor' => '#ffffff',
  73.                     'backgroundColor' => '#da1b1e',
  74.                     'backgroundShape' => 'rectangular',
  75.                     'foreground' => 'text',
  76.                     'background' => 'color_shape',
  77.                     'productListing' => 'left_top',
  78.                     'productDetailPage' => 'no_display',
  79.                     'isDefault' => true,
  80.                     'defaultType' => 'sale',
  81.                     'displayCondition' => 'replace_default_badges',
  82.                     'replaceDefaultBadgeId' => $defaultSaleId,
  83.                     'badgeText' => '%',
  84.                     'translations' => [
  85.                         'de-DE' => [
  86.                             'badgeText' => '%',
  87.                         ],
  88.                         'en-GB' => [
  89.                             'badgeText' => '%',
  90.                         ],
  91.                     ],
  92.                 ],
  93.                 [
  94.                     'id' => $defaultTipId,
  95.                     'internalName' => 'Tip',
  96.                     'active' => true,
  97.                     'priority' => 3,
  98.                     'badgeTextColor' => '#ffffff',
  99.                     'backgroundColor' => '#ffbd5d',
  100.                     'backgroundShape' => 'rectangular',
  101.                     'foreground' => 'text',
  102.                     'background' => 'color_shape',
  103.                     'productListing' => 'left_top',
  104.                     'productDetailPage' => 'no_display',
  105.                     'isDefault' => true,
  106.                     'defaultType' => 'tipp',
  107.                     'displayCondition' => 'replace_default_badges',
  108.                     'replaceDefaultBadgeId' => $defaultTipId,
  109.                     'badgeText' => 'Tip',
  110.                     'translations' => [
  111.                         'de-DE' => [
  112.                             'badgeText' => 'Tipp',
  113.                         ],
  114.                         'en-GB' => [
  115.                             'badgeText' => 'Tip',
  116.                         ],
  117.                     ],
  118.                 ],
  119.                 [
  120.                     'id' => $defaultNewId,
  121.                     'internalName' => 'New',
  122.                     'active' => true,
  123.                     'priority' => 2,
  124.                     'badgeTextColor' => '#ffffff',
  125.                     'backgroundColor' => '#30ab43',
  126.                     'backgroundShape' => 'rectangular',
  127.                     'foreground' => 'text',
  128.                     'background' => 'color_shape',
  129.                     'productListing' => 'left_top',
  130.                     'productDetailPage' => 'no_display',
  131.                     'isDefault' => true,
  132.                     'defaultType' => 'new',
  133.                     'displayCondition' => 'replace_default_badges',
  134.                     'replaceDefaultBadgeId' => $defaultNewId,
  135.                     'badgeText' => 'New',
  136.                     'translations' => [
  137.                         'de-DE' => [
  138.                             'badgeText' => 'Neu',
  139.                         ],
  140.                         'en-GB' => [
  141.                             'badgeText' => 'New',
  142.                         ],
  143.                     ],
  144.                 ],
  145.                 [
  146.                     'id' => $defaultBundleId,
  147.                     'internalName' => 'ACRIS Bundle',
  148.                     'active' => true,
  149.                     'priority' => 1,
  150.                     'badgeTextColor' => '#ffffff',
  151.                     'backgroundColor' => '#a6c836',
  152.                     'backgroundShape' => 'rectangular',
  153.                     'foreground' => 'text',
  154.                     'background' => 'color_shape',
  155.                     'productListing' => 'left_top',
  156.                     'productDetailPage' => 'left_top',
  157.                     'isDefault' => true,
  158.                     'defaultType' => 'acris_bundle',
  159.                     'displayCondition' => 'replace_default_badges',
  160.                     'replaceDefaultBadgeId' => $defaultBundleId,
  161.                     'badgeText' => 'Bundle',
  162.                     'translations' => [
  163.                         'de-DE' => [
  164.                             'badgeText' => 'Bundle',
  165.                         ],
  166.                         'en-GB' => [
  167.                             'badgeText' => 'Bundle',
  168.                         ],
  169.                     ],
  170.                 ],
  171.             ];
  172.         foreach ($badges as $badge) {
  173.             $this->createOrUpdateBadges($badgeRepository, [
  174.                 ['name' => 'defaultType''value' => $badge['defaultType']]
  175.             ], $badge$context);
  176.         }
  177.     }
  178.     private function createOrUpdateBadges(EntityRepositoryInterface $repository, array $equalFields, array $dataContext $context)
  179.     {
  180.         $badges = [];
  181.         foreach ($equalFields as $equalField) {
  182.             $badges[] = new EqualsFilter($equalField['name'], $equalField['value']);
  183.         }
  184.         if (sizeof($badges) > 1) {
  185.             $badge = new MultiFilter(MultiFilter::CONNECTION_OR$badges);
  186.         } else {
  187.             $badge array_shift($badges);
  188.         }
  189.         $searchResult $repository->search((new Criteria())->addFilter($badge), $context);
  190.         if ($searchResult->count() == 0) {
  191.             $repository->upsert([$data], $context);
  192.         } else {
  193.             $data['id'] = $searchResult->first()->getId();
  194.             $data['displayCondition'] = 'replace_default_badges';
  195.             $data['replaceDefaultBadgeId'] = $searchResult->first()->getId();
  196.             $repository->upsert([$data], $context);
  197.         }
  198.     }
  199. }