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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uvala\uvalaBadgeManagerVsix;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. class uvalaBadgeManagerVsix extends Plugin {
  15.     public const PROPERTY_GROUP_EXCLUDED_CF_NAME 'uvala_bdmg_property_group_excluded_';
  16.     public function install(InstallContext $installContext): void {
  17.         parent::install($installContext);
  18.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  19.         try {
  20.             $customFieldSetRepository->upsert($this->uvoutputfields(), $installContext->getContext());
  21.         } catch (\Exception $e) {}
  22.     }
  23.     public function activate(ActivateContext $activateContext): void {
  24.         parent::activate($activateContext);
  25.     }
  26.     public function deactivate(DeactivateContext $deactivateContext): void {
  27.         parent::deactivate($deactivateContext);
  28.     }
  29.     public function uninstall(UninstallContext $uninstallContext): void {
  30.         parent::uninstall($uninstallContext);
  31.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  32.         $outputResIds = [];
  33.         $outputResIds[] = ['id' => $customFieldSetRepository->searchIds(
  34.             (new Criteria())->addFilter(new EqualsFilter('name''uvala_bdmg_product')),
  35.             \Shopware\Core\Framework\Context::createDefaultContext()
  36.             , $uninstallContext->getContext())->firstId()];
  37.         $outputResIds[] = ['id' => $customFieldSetRepository->searchIds(
  38.             (new Criteria())->addFilter(new EqualsFilter('name''uvala_bmgh_property_excluded_set')),
  39.             \Shopware\Core\Framework\Context::createDefaultContext()
  40.             , $uninstallContext->getContext())->firstId()];
  41.         $customFieldSetRepository->delete($outputResIds$uninstallContext->getContext());
  42.         if ($uninstallContext->keepUserData()) {
  43.             return;
  44.         }
  45.         $this->removeConfiguration($uninstallContext->getContext());
  46.     }
  47.     private function removeConfiguration(Context $context): void {
  48.         /** @var EntityRepositoryInterface $systemConfigRepository */
  49.         $systemConfigRepository $this->container->get('system_config.repository');
  50.         $criteria = (new Criteria())->addFilter(new ContainsFilter('configurationKey'$this->getName() . '.config.'));
  51.         $idSearchResult $systemConfigRepository->searchIds($criteria$context);
  52.         $ids array_map(static function ($id) {
  53.             return ['id' => $id];
  54.         }, $idSearchResult->getIds());
  55.         if ($ids === []) {
  56.             return;
  57.         }
  58.         $systemConfigRepository->delete($ids$context);
  59.     }
  60.     private function uvoutputfields() {
  61.         $customFieldSets = [];
  62.         $propertyGroupCustomFields = [];
  63.         for ($i 1$i <= 8$i++) {
  64.             $propertyGroupCustomFields[] = [
  65.                 'name' => self::PROPERTY_GROUP_EXCLUDED_CF_NAME.$i,
  66.                 'type' => CustomFieldTypes::BOOL,
  67.                 'config' => [
  68.                     'label' => [
  69.                         'de-DE' => 'Property-Gruppe für Badge ausgeschlossen '$i,
  70.                         'en-GB' => 'Property-group excluded for badge '.$i,
  71.                     ],
  72.                     'helpText' => [
  73.                         'de-DE' => 'Deaktivieren Sie die Anzeige der Eigenschaftsgruppe.',
  74.                         'en-GB' => 'Disable the display of the property group.',
  75.                     ],
  76.                     'customFieldPosition' => $i,
  77.                 ]
  78.             ];
  79.         }
  80.         $customFieldSets[] = [
  81.             'name' => 'uvala_bdmg_product',
  82.             'config' => [
  83.                 'label' => [
  84.                     'de-DE' => 'Badge Manager',
  85.                     'en-GB' => 'Badge Manager'
  86.                 ]
  87.             ],
  88.             'customFields' => [
  89.                 [
  90.                     'name' => 'uvala_bdmg_plugin_disable',
  91.                     'type' => CustomFieldTypes::BOOL,
  92.                     'config' => [
  93.                         'label' => [
  94.                             'de-DE' => 'Badge Manager für dieses Produkt deaktivieren',
  95.                             'en-GB' => 'Disable badge manager for this product',
  96.                         ],
  97.                         'helpText' => [
  98.                             'de-DE' => 'Deaktiviert den Badge Manager nur für dieses Produkt (Gilt bei Varianten NUR für das Produkt, welches im Listing erscheint!).',
  99.                             'en-GB' => 'Deactivates the Badge Manager only for this product (only applies to variants for the product that appears in the listing!).',
  100.                         ],
  101.                         'customFieldPosition' => 1
  102.                     ]
  103.                 ]
  104.             ],
  105.             'relations' => [
  106.                 ['entityName' => 'product']
  107.             ],
  108.         ];
  109.         $customFieldSets[] = [
  110.             'name' => 'uvala_bmgh_property_excluded_set',
  111.             'config' => [
  112.                 'label' => [
  113.                     'de-DE' => 'Abzeichen für Eigentumsgruppe ausgeschlossen',
  114.                     'en-GB' => 'Badge for property group excluded'
  115.                 ]
  116.             ],
  117.             'customFields' => $propertyGroupCustomFields,
  118.             'relations' => [
  119.                 ['entityName' => 'property_group']
  120.             ],
  121.             'global' => true
  122.         ];
  123.         return $customFieldSets;
  124.     }
  125. }