vendor/store.shopware.com/zenitplatformdatabadges/src/zenitPlatformDataBadges.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace zenit\PlatformDataBadges;
  3. use Exception;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  7. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  8. use Shopware\Core\System\CustomField\CustomFieldTypes;
  9. use Shopware\Core\Content\Product\ProductDefinition;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. class zenitPlatformDataBadges extends Plugin
  14. {
  15.     const CUSTOM_BADGES_COUNT 3;
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         $this->addDefaultConfiguration();
  19.         $this->getCustomFields($installContext->getContext());
  20.         parent::install($installContext);
  21.     }
  22.     public function update(UpdateContext $updateContext): void
  23.     {
  24.         $this->addDefaultConfiguration();
  25.         if (version_compare($updateContext->getCurrentPluginVersion(), '2.3.0''<')) {
  26.             $this->updateCustomFields($updateContext->getContext());
  27.         }
  28.         parent::update($updateContext);
  29.     }
  30.     public function uninstall(UninstallContext $uninstallContext): void
  31.     {
  32.         parent::uninstall($uninstallContext);
  33.         if ($uninstallContext->keepUserData()) {
  34.             return;
  35.         }
  36.         $this->deleteCustomFields($uninstallContext->getContext());
  37.     }
  38.     private function deleteCustomFields($context): void
  39.     {
  40.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  41.         $criteria = new Criteria();
  42.         $criteria->addFilter(new ContainsFilter('name''zenit_data_badge'));
  43.         $criteriaResult $customFieldSetRepository->search($criteria$context);
  44.         $ids $criteriaResult->getIds();
  45.         if(!empty($ids)) {
  46.             $data = [];
  47.             foreach ($ids as $id) {
  48.                 $data[] = [
  49.                     'id' => $id
  50.                 ];
  51.             }
  52.             try {
  53.                 $customFieldSetRepository->delete($data$context);
  54.             } catch (Exception $e) {}
  55.         }
  56.     }
  57.     private function updateCustomFields($context): void
  58.     {
  59.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  60.         $criteria = new Criteria();
  61.         $criteria->addFilter(new ContainsFilter('name''zenit_data_badge'));
  62.         $criteria->setLimit(1);
  63.         $criteriaResult $customFieldSetRepository->search($criteria$context);
  64.         $id null;
  65.         if ($criteriaResult->count() === 1) {
  66.             $id $criteriaResult->first()->getId();
  67.         }
  68.         $customFields = [];
  69.         for($i 1$i <= self::CUSTOM_BADGES_COUNT$i++) {
  70.             $customFields array_merge($customFields,
  71.                 [
  72.                     [
  73.                         'id' => '6b982d312a3f47daa29e841d1731163' $i,
  74.                         'name' => 'zenit_data_badge_disabled_' $i,
  75.                         'type' => CustomFieldTypes::BOOL,
  76.                         'config' => [
  77.                             'type' => 'checkbox',
  78.                             'componentName' => 'sw-field',
  79.                             'customFieldType' => 'checkbox',
  80.                             'customFieldPosition' => 10 $i,
  81.                             'label' => [
  82.                                 'en-GB' => $i ') Disabled',
  83.                                 'de-DE' => $i ') Deaktiviert',
  84.                             ],
  85.                         ],
  86.                     ],
  87.                 ]
  88.             );
  89.         }
  90.         $customFieldSet = [
  91.             'id' => $id,
  92.             'name' => 'zenit_data_badges',
  93.             'config' => [
  94.                 'label' => [
  95.                     'en-GB' => 'Custom Labels',
  96.                     'de-DE' => 'Individuelle Labels'
  97.                 ]
  98.             ],
  99.             'customFields' => $customFields
  100.         ];
  101.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  102.         try {
  103.             $customFieldSetRepository->upsert([$customFieldSet], $context);
  104.         } catch (Exception $e) {}
  105.     }
  106.     private function getCustomFields($context): void
  107.     {
  108.         $relation = [
  109.             [
  110.                 'entityName' =>  $this->container->get(ProductDefinition::class)->getEntityName()
  111.             ]
  112.         ];
  113.         $customFields = [];
  114.         for($i 1$i <= self::CUSTOM_BADGES_COUNT$i++) {
  115.             $customFields array_merge($customFields,
  116.                 [
  117.                     [
  118.                         'id' => '6b982d312a3f47daa29e841d1731163' $i,
  119.                         'name' => 'zenit_data_badge_disabled_' $i,
  120.                         'type' => CustomFieldTypes::BOOL,
  121.                         'config' => [
  122.                             'type' => 'checkbox',
  123.                             'componentName' => 'sw-field',
  124.                             'customFieldType' => 'checkbox',
  125.                             'customFieldPosition' => 10 $i,
  126.                             'label' => [
  127.                                 'en-GB' => $i ') Disabled',
  128.                                 'de-DE' => $i ') Deaktiviert',
  129.                             ],
  130.                         ],
  131.                     ],
  132.                     [
  133.                         'name' => 'zenit_data_badge_' $i,
  134.                         'type' => CustomFieldTypes::TEXT,
  135.                         'config' => [
  136.                             'componentName' => 'sw-field',
  137.                             'customFieldType' => 'text',
  138.                             'customFieldPosition' => 10 $i,
  139.                             'label' => [
  140.                                 'en-GB' => $i ') Text',
  141.                                 'de-DE' => $i ') Text',
  142.                             ],
  143.                         ],
  144.                     ],
  145.                     [
  146.                         'name' => 'zenit_data_badge_tooltip_' $i,
  147.                         'type' => CustomFieldTypes::TEXT,
  148.                         'config' => [
  149.                             'componentName' => 'sw-field',
  150.                             'customFieldType' => 'text',
  151.                             'customFieldPosition' => 10 $i,
  152.                             'label' => [
  153.                                 'en-GB' => $i ') Tooltip',
  154.                                 'de-DE' => $i ') Tooltip',
  155.                             ],
  156.                         ],
  157.                     ],
  158.                     [
  159.                         'name' => 'zenit_data_badge_color_' $i,
  160.                         'type' => CustomFieldTypes::COLORPICKER,
  161.                         'config' => [
  162.                             'componentName' => 'sw-field',
  163.                             'customFieldType' => 'colorpicker',
  164.                             'customFieldPosition' => 10 $i,
  165.                             'label' => [
  166.                                 'en-GB' => $i ') Background-Color',
  167.                                 'de-DE' => $i ') Hintergrundfarbe',
  168.                             ],
  169.                         ],
  170.                     ],
  171.                     [
  172.                         'name' => 'zenit_data_badge_textcolor_' $i,
  173.                         'type' => CustomFieldTypes::COLORPICKER,
  174.                         'config' => [
  175.                             'componentName' => 'sw-field',
  176.                             'customFieldType' => 'colorpicker',
  177.                             'customFieldPosition' => 10 $i,
  178.                             'label' => [
  179.                                 'en-GB' => $i ') Text-Color',
  180.                                 'de-DE' => $i ') Textfarbe',
  181.                             ],
  182.                         ],
  183.                     ]
  184.                 ]
  185.             );
  186.         }
  187.         $customFieldSet = [
  188.             'name' => 'zenit_data_badges',
  189.             'config' => [
  190.                 'label' => [
  191.                     'en-GB' => 'Custom Labels',
  192.                     'de-DE' => 'Individuelle Labels'
  193.                 ]
  194.             ],
  195.             'customFields' => $customFields,
  196.             'relations' => $relation
  197.         ];
  198.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  199.         try {
  200.             $customFieldSetRepository->upsert([$customFieldSet], $context);
  201.         } catch (Exception $e) {}
  202.     }
  203.     private function addDefaultConfiguration(): void
  204.     {
  205.         $this->setValue('active'true);
  206.         $this->setValue('hideTooltips'false);
  207.         $this->setValue('stockListing'false);
  208.         $this->setValue('stockDetail''false');
  209.         $this->setValue('stockMinimum'50);
  210.         $this->setValue('stockMaximum'999999);
  211.         $this->setValue('stockValueType''minimum');
  212.         $this->setValue('stockIcon''products');
  213.         $this->setValue('stockVariant''variant');
  214.         $this->setValue('stockBadgeColor''#5F7285');
  215.         $this->setValue('salesListing'false);
  216.         $this->setValue('salesDetail'false);
  217.         $this->setValue('salesMinimum'50);
  218.         $this->setValue('salesValueType''minimum');
  219.         $this->setValue('salesIcon''pulse');
  220.         $this->setValue('salesBadgeColor''#e52427');
  221.         $this->setValue('variantListing'false);
  222.         $this->setValue('variantDetail''false');
  223.         $this->setValue('variantIcon''variants');
  224.         $this->setValue('variantBadgeColor''#bcc1c7');
  225.         $this->setValue('discountListing'true);
  226.         $this->setValue('discountDetail''false');
  227.         $this->setValue('discountMinimum'10);
  228.         $this->setValue('discountTooltip''Ihr Vorteil');
  229.         $this->setValue('closeoutListing'false);
  230.         $this->setValue('closeoutSoldOutOnly'false);
  231.         $this->setValue('closeoutDetail''false');
  232.         $this->setValue('closeoutIcon''blocked');
  233.         $this->setValue('closeoutBadgeColor''#E74C3C');
  234.         $this->setValue('shippingfreeListing'false);
  235.         $this->setValue('shippingfreeDetail''false');
  236.         $this->setValue('shippingfreeIcon''package-closed');
  237.         $this->setValue('shippingfreeBadgeColor''#2ECC71');
  238.         $this->setValue('topsellerListing'true);
  239.         $this->setValue('topsellerDetail''false');
  240.         $this->setValue('topsellerIcon''star');
  241.         $this->setValue('newArticleListing'true);
  242.         $this->setValue('newArticleDetail''false');
  243.         $this->setValue('newArticleIcon''plus');
  244.         $this->setValue('releaseListing'false);
  245.         $this->setValue('releaseDetail''false');
  246.         $this->setValue('releaseIcon''clock');
  247.         $this->setValue('releaseBadgeColor''#ffbd5d');
  248.         $this->setValue('preorderListing'false);
  249.         $this->setValue('preorderDetail''false');
  250.         $this->setValue('preorderIcon''calendar');
  251.         $this->setValue('preorderBadgeColor''#932AEF');
  252.         $this->setValue('manufacturerListing'false);
  253.         $this->setValue('manufacturerDetail''false');
  254.         $this->setValue('manufacturerBadgeColor''#5F7285');
  255.         $this->setValue('custom1Listing'false);
  256.         $this->setValue('custom1Detail''false');
  257.         $this->setValue('custom1BadgeColor''#F1C40F');
  258.         $this->setValue('custom2Listing'false);
  259.         $this->setValue('custom2Detail''false');
  260.         $this->setValue('custom2BadgeColor''#F1C40F');
  261.         $this->setValue('custom3Listing'false);
  262.         $this->setValue('custom3Detail''false');
  263.         $this->setValue('custom3BadgeColor''#F1C40F');
  264.         $this->setValue('listingsFontSize'14);
  265.         $this->setValue('listingsPadding'5);
  266.         $this->setValue('listingsRadiusLeft'0);
  267.         $this->setValue('listingsRadiusRight'3);
  268.         $this->setValue('detailsFontSize'14);
  269.         $this->setValue('detailsPadding'5);
  270.         $this->setValue('detailsRadiusLeft'0);
  271.         $this->setValue('detailsRadiusRight'3);
  272.         $this->setValue('customCSS');
  273.     }
  274.     /**
  275.      * @param string $configName
  276.      * @param null $default
  277.      */
  278.     public function setValue(string $configName$default null) : void
  279.     {
  280.         $systemConfigService $this->container->get(SystemConfigService::class);
  281.         $domain $this->getName() . '.config.';
  282.         if( $systemConfigService->get($domain $configName) === null )
  283.         {
  284.             $systemConfigService->set($domain $configName$default);
  285.         }
  286.     }
  287. }