custom/plugins/H1webMegaMenu/src/H1webMegaMenu.php line 20

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace H1web\MegaMenu;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  5. use Shopware\Core\Framework\Uuid\Uuid;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  10. use Shopware\Core\Framework\Context;
  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\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  15. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSetRelation\CustomFieldSetRelationEntity;
  16. use Shopware\Core\System\CustomField\CustomFieldEntity;
  17. class H1webMegaMenu extends Plugin
  18. {
  19.     public const SYSTEM_CONFIG_DOMAIN 'H1webMegaMenu.config.';
  20.     public function install(InstallContext $context): void
  21.     {
  22.         $this->createCustomFields($context);
  23.     }
  24.     public function update(UpdateContext $context): void
  25.     {
  26.         $this->createCustomFields($context);
  27.     }
  28.     private function createCustomFields($context)
  29.     {
  30.         /** @var EntityRepositoryInterface $systemConfigRepository */
  31.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  32.         foreach ($this->getCustomFields() as $customFieldSet) {
  33.             $criteria = new Criteria();
  34.             $criteria->addFilter(new EqualsFilter('name'$customFieldSet['name']));
  35.             $criteria->addAssociation('relations');
  36.             $criteria->addAssociation('customFields');
  37.             $existingCustomFieldSet $customFieldSetRepository->search($criteria$context->getContext())->first();
  38.             if ($existingCustomFieldSet) {
  39.                 /** @var CustomFieldSetEntity $existingCustomFieldSet */
  40.                 $customFieldSet['id'] = $existingCustomFieldSet->getId();
  41.                 if (!empty($customFieldSet['relations'])) {
  42.                     $existingRelations = [];
  43.                     foreach ($existingCustomFieldSet->getRelations() as $field) {
  44.                         /** @var CustomFieldSetRelationEntity $field */
  45.                         $existingRelations[$field->getId()] = $field->getEntityName();
  46.                     }
  47.                     $relations = [];
  48.                     foreach ($customFieldSet['relations'] as $relation) {
  49.                         if (array_search($relation['entityName'], $existingRelations)) {
  50.                             $relation['id'] = array_search($relation['entityName'], $existingRelations);
  51.                         }
  52.                         $relations[] = $relation;
  53.                     }
  54.                     $customFieldSet['relations'] = $relations;
  55.                 }
  56.                 if (!empty($customFieldSet['customFields'])) {
  57.                     $existingCustomFields = [];
  58.                     foreach ($existingCustomFieldSet->getCustomFields() as $field) {
  59.                         /** @var CustomFieldEntity $field */
  60.                         $existingCustomFields[$field->getId()] = $field->getName();
  61.                     }
  62.                     $customFields = [];
  63.                     foreach ($customFieldSet['customFields'] as $customField) {
  64.                         if (array_search($customField['name'], $existingCustomFields)) {
  65.                             $customField['id'] = array_search($customField['name'], $existingCustomFields);
  66.                         }
  67.                         $customFields[] = $customField;
  68.                     }
  69.                     $customFieldSet['customFields'] = $customFields;
  70.                 }
  71.             }
  72.             $customFieldSetRepository->upsert([$customFieldSet], $context->getContext());
  73.         }
  74.     }
  75.     private function getCustomFields()
  76.     {
  77.         $customFields = [
  78.             'h1_mega_menu' => [
  79.                 'name' => 'h1_mega_menu',
  80.                 'config' => [
  81.                     'label' => [
  82.                         'en-GB' => 'Mega menu options',
  83.                         'de-DE' => 'Mega menu options',
  84.                         'nl-NL' => 'Mega menu options'
  85.                     ]
  86.                 ],
  87.                 'relations' => [['entityName' => 'category']],
  88.                 'customFields' => [
  89.                     [
  90.                         'name' => 'h1_mega_menu_category_level',
  91.                         'type' => 'select',
  92.                         'config' => [
  93.                             'label' => [
  94.                                 'en-GB' => 'Select folout menu layout',
  95.                                 'de-DE' => 'Wählen Sie das Layout des Folout-Menüs',
  96.                                 'nl-NL' => 'Selecteer foldout layout'
  97.                             ],
  98.                             'placeholder' => 'Foldout layout with left sidebar',
  99.                             'options' => [
  100.                                 [
  101.                                     'label' => [
  102.                                         'en-GB' => 'Foldout layout without left sidebar',
  103.                                         'de-DE' => 'Nur Haupt- + Unterkategorie',
  104.                                         'nl-NL' => 'Foldout layout zonder linker sidebar'
  105.                                     ],
  106.                                     'value' => 'h1_mega_menu_category_level_2'
  107.                                 ],
  108.                                 [
  109.                                     'label' => [
  110.                                         'en-GB' => 'Foldout layout with left sidebar',
  111.                                         'de-DE' => 'Alle Unterkategorien',
  112.                                         'nl-NL' => 'Foldout layout met linker sidebar'
  113.                                     ],
  114.                                     'value' => 'h1_mega_menu_category_level_more'
  115.                                 ]
  116.                             ],
  117.                             'helpText' => [
  118.                                 'en-GB' => 'If you choose the option "Foldout layout with left sidebar" it will show in the folout a left section with in the subcategories, its like a extra "foldout" for the sub sub catgories. Recommanded it you have a lot of categories.',
  119.                                 'de-DE' => 'Wenn Sie die Option "Ausklappbares Layout mit linker Seitenleiste" wählen, wird im Folout ein linker Abschnitt mit in den Unterkategorien angezeigt, ähnlich wie bei einem zusätzlichen "Ausklappen" für die Unterunterkategorien. Empfohlen haben Sie viele Kategorien.',
  120.                                 'nl-NL' => 'Als je de optie "Uitvouwbare lay-out met linkerzijbalk" kiest, wordt in de volgende sectie een linkergedeelte weergegeven met in de subcategorieën, het is als een extra "uitvouwbare" voor de sub-subcategorieën. Aanbevolen, je hebt veel categorieën.'
  121.                             ],
  122.                             'componentName' => 'sw-single-select',
  123.                             'customFieldType' => 'select',
  124.                             'customFieldPosition' => 1
  125.                         ]
  126.                     ],
  127.                     [
  128.                         'name' => 'h1_mega_menu_category_parent',
  129.                         'type' => 'bool',
  130.                         'config' => [
  131.                             'type' => 'checkbox',
  132.                             'label' => [
  133.                                 'en-GB' => 'Put main category in placeholder',
  134.                                 'de-DE' => 'Setzen Sie die Hauptkategorie in den Platzhalter',
  135.                                 'nl-NL' => 'Plaats hoofdcategorie in placeholder'
  136.                             ],
  137.                             'helpText' => [
  138.                                 'en-GB' => 'You can put the main category in a placeholder, the default is "Products". In your navigation you see "Products" and when u click on it you will see the subcategories. Its also posible to change the text, you can do this in the snippets.',
  139.                                 'de-DE' => 'Sie können die Hauptkategorie in einen Platzhalter einfügen. Die Standardeinstellung ist "Produkte". In Ihrer Navigation sehen Sie "Produkte" und wenn Sie darauf klicken, sehen Sie die Unterkategorien. Es ist auch möglich, den Text zu ändern. Sie können dies in den Snippets tun.',
  140.                                 'nl-NL' => 'U kunt de hoofdcategorie in een tijdelijke aanduiding plaatsen, de standaard is "Producten". In uw navigatie ziet u "Producten" en als u erop klikt, ziet u de subcategorieën. Het is ook mogelijk om de tekst te wijzigen, u kunt dit doen in de fragmenten.'
  141.                             ],
  142.                             'componentName' => 'sw-field',
  143.                             'customFieldType' => 'checkbox',
  144.                             'customFieldPosition' => 2
  145.                         ]
  146.                     ],
  147.                     [
  148.                         'name' => 'h1_mega_menu_category_icon',
  149.                         'type' => 'text',
  150.                         'config' => [
  151.                             'label' => [
  152.                                 'en-GB' => 'Category icon',
  153.                                 'de-DE' => 'Kategoriesymbol',
  154.                                 'nl-NL' => 'Category icon'
  155.                             ],
  156.                             'helpText' => [
  157.                                 'en-GB' => 'Check the plugin configuration, as default it will use the category image. In the config you can select use the custom field instead of the category image.',
  158.                                 'de-DE' => 'Überprüfen Sie die Plugin-Konfiguration. Standardmäßig wird das Kategorie-Image verwendet. In der Konfiguration können Sie das benutzerdefinierte Feld anstelle des Kategoriebilds verwenden auswählen.',
  159.                                 'nl-NL' => 'Controleer de configuratie van de plug-in, deze zal standaard de categorie-afbeelding gebruiken. In de configuratie kunt u ervoor kiezen om het aangepaste veld te gebruiken in plaats van de categorieafbeelding.'
  160.                             ],
  161.                             'componentName' => 'sw-media-field',
  162.                             'customFieldType' => 'media',
  163.                             'customFieldPosition' => 3
  164.                         ]
  165.                     ]
  166.                 ]
  167.             ]
  168.         ];
  169.         return $customFields;
  170.     }
  171.     public function uninstall(UninstallContext $uninstallContext): void
  172.     {
  173.         parent::uninstall($uninstallContext);
  174.         if ($uninstallContext->keepUserData()) {
  175.             return;
  176.         }
  177.         $this->removeCustomFields($uninstallContext->getContext());
  178.         $this->removeConfiguration($uninstallContext->getContext());
  179.     }
  180.     private function removeCustomFields(Context $context): void
  181.     {
  182.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  183.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  184.         $criteria = (new Criteria())
  185.             ->addFilter(new ContainsFilter('name''h1_mega_menu'));
  186.         $idSearchResult $customFieldSetRepository->searchIds($criteria$context);
  187.         if (=== $idSearchResult->getTotal()) {
  188.             return;
  189.         }
  190.         $ids array_map(static function ($id) {
  191.             return ['id' => $id];
  192.         }, $idSearchResult->getIds());
  193.         $customFieldSetRepository->delete($ids$context);
  194.         $connection $this->container->get(Connection::class);
  195.         $data $connection->executeQuery("SELECT `category_id`, `language_id`, `custom_fields` FROM `category_translation` WHERE `custom_fields` IS NOT NULL")->fetchAll();
  196.         foreach ($data as $index => $elements) {
  197.             $id Uuid::fromBytesToHex($elements['category_id']);
  198.             $languageId Uuid::fromBytesToHex($elements['language_id']);
  199.             $customFields json_decode($elements['custom_fields'], true);
  200.             if (!isset($customFields['h1_mega_menu_category_level']) && !isset($customFields['h1_mega_menu_category_parent']) && !isset($customFields['h1_mega_menu_category_icon'] )) {
  201.                 continue;
  202.             }
  203.             unset($customFields['h1_mega_menu_category_level'], $customFields['h1_mega_menu_category_parent'], $customFields['h1_mega_menu_category_icon']);
  204.             if (empty($customFields)) {
  205.                 $connection->executeQuery("UPDATE `category_translation` SET `custom_fields` = NULL WHERE `category_id` = UNHEX('"$id ."') AND `language_id` = UNHEX('"$languageId ."')");
  206.             } else {
  207.                 $connection->executeQuery("UPDATE `category_translation` SET `custom_fields` = '".json_encode($customFieldsJSON_PRESERVE_ZERO_FRACTION JSON_UNESCAPED_UNICODE)."' WHERE `category_id` = UNHEX('"$id ."') AND `language_id` = UNHEX('"$languageId ."')");
  208.             }
  209.         }
  210.     }
  211.     private function removeConfiguration(Context $context): void
  212.     {
  213.         /** @var EntityRepositoryInterface $systemConfigRepository */
  214.         $systemConfigRepository $this->container->get('system_config.repository');
  215.         $criteria = (new Criteria())
  216.             ->addFilter(new ContainsFilter('configurationKey'self::SYSTEM_CONFIG_DOMAIN));
  217.         $idSearchResult $systemConfigRepository->searchIds($criteria$context);
  218.         if($idSearchResult->getTotal() > 0) {
  219.             $ids array_map(static function ($id) {
  220.                 return ['id' => $id];
  221.             }, $idSearchResult->getIds());
  222.             $systemConfigRepository->delete(array_values($ids), $context);
  223.         }
  224.     }
  225. }