custom/plugins/zenitPlatformDataBadges/src/zenitPlatformDataBadges.php line 21

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformDataBadges;
  3. use Shopware\Core\Content\Product\ProductDefinition;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  7. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  8. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  9. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  16. class zenitPlatformDataBadges extends Plugin
  17. {
  18.     const CUSTOM_BADGES_COUNT = 3;
  19.     public function install(InstallContext $context): void
  20.     {
  21.         $this->addDefaultConfiguration();
  22.         $this->getCustomFields($context->getContext());
  23.     }
  24.     public function activate(ActivateContext $context): void
  25.     {
  26.         $this->addDefaultConfiguration();
  27.     }
  28.     public function update(UpdateContext $context): void
  29.     {
  30.         $this->addDefaultConfiguration();
  31.     }
  32.     public function deactivate(DeactivateContext $context): void
  33.     {
  34.     }
  35.     public function uninstall(UninstallContext $context): void
  36.     {
  37.         parent::uninstall($context);
  38.         if ($context->keepUserData()) {
  39.             return;
  40.         }
  41.         $this->removeConfiguration($context->getContext());
  42.         $this->deleteCustomFields($context->getContext());
  43.     }
  44.     private function removeConfiguration(Context $context): void
  45.     {
  46.         /** @var EntityRepositoryInterface $systemConfigRepository */
  47.         $systemConfigRepository = $this->container->get('system_config.repository');
  48.         $criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey', $this->getName() . '.config.'));
  49.         $idSearchResult = $systemConfigRepository->searchIds($criteria, $context);
  50.         $ids = array_map(static function ($id) {
  51.             return ['id' => $id];
  52.         }, $idSearchResult->getIds());
  53.         if ($ids === []) {
  54.             return;
  55.         }
  56.         $systemConfigRepository->delete($ids, $context);
  57.     }
  58.     private function deleteCustomFields($context): void
  59.     {
  60.         $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  61.         $criteria = new Criteria();
  62.         $criteria->addFilter(new ContainsFilter('name', 'zenit_data_badge'));
  63.         $criteriaResult = $customFieldSetRepository->search($criteria, $context);
  64.         $ids = $criteriaResult->getIds();
  65.         if(!empty($ids)) {
  66.             $data = [];
  67.             foreach ($ids as $id) {
  68.                 $data[] = [
  69.                     'id' => $id
  70.                 ];
  71.             }
  72.             try {
  73.                 $customFieldSetRepository->delete($data, $context);
  74.             } catch (\Exception $e) {}
  75.         }
  76.     }
  77.     private function getCustomFields($context): void
  78.     {
  79.         $relation = [
  80.             [
  81.                 'entityName' =>  $this->container->get(ProductDefinition::class)->getEntityName()
  82.             ]
  83.         ];
  84.         $customFields = [];
  85.         for($i = 1; $i <= self::CUSTOM_BADGES_COUNT; $i++) {
  86.             $customFields = array_merge($customFields,
  87.                 [
  88.                     [
  89.                         'name' => 'zenit_data_badge_' . $i,
  90.                         'type' => 'CustomFieldTypes::TEXT',
  91.                         'config' => [
  92.                             'customFieldPosition' => 1 + 10 * 2 * $i,
  93.                             'label' => [
  94.                                 'en-GB' => $i . ') Text',
  95.                                 'de-DE' => $i . ') Text',
  96.                             ],
  97.                         ],
  98.                     ],
  99.                     [
  100.                         'name' => 'zenit_data_badge_tooltip_' . $i,
  101.                         'type' => 'string',
  102.                         'config' => [
  103.                             'customFieldPosition' => 2 + 10 * 2 * $i,
  104.                             'label' => [
  105.                                 'en-GB' => $i . ') Tooltip',
  106.                                 'de-DE' => $i . ') Tooltip',
  107.                             ],
  108.                         ],
  109.                     ],
  110.                     [
  111.                         'name' => 'zenit_data_badge_color_' . $i,
  112.                         'type' => 'string',
  113.                         'config' => [
  114.                             'customFieldPosition' => 3 + 10 * 2 * $i,
  115.                             'label' => [
  116.                                 'en-GB' => $i . ') Background-Color',
  117.                                 'de-DE' => $i . ') Hintergrundfarbe',
  118.                             ],
  119.                         ],
  120.                     ],
  121.                     [
  122.                         'name' => 'zenit_data_badge_textcolor_' . $i,
  123.                         'type' => 'string',
  124.                         'config' => [
  125.                             'customFieldPosition' => 4 + 10 * 2 * $i,
  126.                             'label' => [
  127.                                 'en-GB' => $i . ') Text-Color',
  128.                                 'de-DE' => $i . ') Textfarbe',
  129.                             ],
  130.                         ],
  131.                     ]
  132.                 ]
  133.             );
  134.         }
  135.         $customFieldSet = [
  136.             'name' => 'zenit_data_badges',
  137.             'config' => [
  138.                 'label' => [
  139.                     'en-GB' => 'Custom Labels',
  140.                     'de-DE' => 'Individuelle Labels'
  141.                 ]
  142.             ],
  143.             'customFields' => $customFields,
  144.             'relations' => $relation
  145.         ];
  146.         $customFieldSetRepository = $this->container->get('custom_field_set.repository');
  147.         try {
  148.             $customFieldSetRepository->create([$customFieldSet], $context);
  149.         } catch (\Exception $e) {}
  150.     }
  151.     private function addDefaultConfiguration(): void
  152.     {
  153.         $this->setValue('active', true);
  154.         $this->setValue('hideTooltips', false);
  155.         $this->setValue('stockListing', false);
  156.         $this->setValue('stockDetail', 'false');
  157.         $this->setValue('stockMinimum', 50);
  158.         $this->setValue('stockMaximum', 999999);
  159.         $this->setValue('stockValueType', 'minimum');
  160.         $this->setValue('stockIcon', 'products');
  161.         $this->setValue('stockText', 'Stück');
  162.         $this->setValue('stockTooltip', 'Lagerbestand');
  163.         $this->setValue('stockVariant', 'variant');
  164.         $this->setValue('stockBadgeColor', '#5F7285');
  165.         $this->setValue('variantListing', false);
  166.         $this->setValue('variantDetail', 'false');
  167.         $this->setValue('variantIcon', 'variants');
  168.         $this->setValue('variantText', 'Variante');
  169.         $this->setValue('variantTooltip', 'Variantenartikel');
  170.         $this->setValue('variantBadgeColor', '#bcc1c7');
  171.         $this->setValue('discountListing', true);
  172.         $this->setValue('discountDetail', 'false');
  173.         $this->setValue('discountMinimum', 10);
  174.         $this->setValue('discountTooltip', 'Ihr Vorteil');
  175.         $this->setValue('closeoutListing', false);
  176.         $this->setValue('closeoutSoldOutOnly', false);
  177.         $this->setValue('closeoutDetail', 'false');
  178.         $this->setValue('closeoutIcon', 'blocked');
  179.         $this->setValue('closeoutText', 'Abverkauf');
  180.         $this->setValue('closeoutTooltip', 'Ausverkauf');
  181.         $this->setValue('closeoutBadgeColor', '#E74C3C');
  182.         $this->setValue('shippingfreeListing', false);
  183.         $this->setValue('shippingfreeDetail', 'false');
  184.         $this->setValue('shippingfreeIcon', 'package-closed');
  185.         $this->setValue('shippingfreeText', 'Versandkostenfrei');
  186.         $this->setValue('shippingfreeTooltip', 'Versand');
  187.         $this->setValue('shippingfreeBadgeColor', '#2ECC71');
  188.         $this->setValue('topsellerListing', true);
  189.         $this->setValue('topsellerDetail', 'false');
  190.         $this->setValue('topsellerIcon', 'star');
  191.         $this->setValue('topsellerText', 'Tipp!');
  192.         $this->setValue('topsellerTooltip', 'Topseller');
  193.         $this->setValue('newArticleListing', true);
  194.         $this->setValue('newArticleDetail', 'false');
  195.         $this->setValue('newArticleIcon', 'plus');
  196.         $this->setValue('newArticleText', 'Neu');
  197.         $this->setValue('newArticleTooltip', 'Neu im Sortiment');
  198.         $this->setValue('manufacturerListing', false);
  199.         $this->setValue('manufacturerDetail', 'false');
  200.         $this->setValue('manufacturerTooltip', 'Hersteller');
  201.         $this->setValue('manufacturerBadgeColor', '#5F7285');
  202.         $this->setValue('custom1Listing', false);
  203.         $this->setValue('custom1Detail', 'false');
  204.         $this->setValue('custom1Tooltip');
  205.         $this->setValue('custom1BadgeColor', '#F1C40F');
  206.         $this->setValue('custom2Listing', false);
  207.         $this->setValue('custom2Detail', 'false');
  208.         $this->setValue('custom2Tooltip');
  209.         $this->setValue('custom2BadgeColor', '#F1C40F');
  210.         $this->setValue('custom3Listing', false);
  211.         $this->setValue('custom3Detail', 'false');
  212.         $this->setValue('custom3Tooltip');
  213.         $this->setValue('custom3BadgeColor', '#F1C40F');
  214.         $this->setValue('listingsFontSize', 14);
  215.         $this->setValue('listingsPadding', 5);
  216.         $this->setValue('listingsRadiusLeft', 0);
  217.         $this->setValue('listingsRadiusRight', 3);
  218.         $this->setValue('detailsFontSize', 14);
  219.         $this->setValue('detailsPadding', 5);
  220.         $this->setValue('detailsRadiusLeft', 0);
  221.         $this->setValue('detailsRadiusRight', 3);
  222.         $this->setValue('customCSS');
  223.     }
  224.     /**
  225.      * @param string $configName
  226.      * @param null $default
  227.      */
  228.     public function setValue(string $configName, $default = null) : void
  229.     {
  230.         $systemConfigService = $this->container->get(SystemConfigService::class);
  231.         $domain = $this->getName() . '.config.';
  232.         if( $systemConfigService->get($domain . $configName) === null )
  233.         {
  234.             $systemConfigService->set($domain . $configName, $default);
  235.         }
  236.     }
  237. }