custom/plugins/plugin-MoorlOrderNotice-master/src/MoorlOrderNotice.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlOrderNotice;
  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\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Doctrine\DBAL\Connection;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. class MoorlOrderNotice extends Plugin
  16. {
  17.     public function install(InstallContext $installContext): void
  18.     {
  19.         $shopwareContext $installContext->getContext();
  20.         $this->addCustomFields($shopwareContext);
  21.         parent::install($installContext);
  22.     }
  23.     public function activate(ActivateContext $activateContext): void
  24.     {
  25.         parent::activate($activateContext);
  26.     }
  27.     private function addCustomFields(Context $context): void
  28.     {
  29.         $repo $this->container->get('custom_field.repository');
  30.         $customFieldIds $this->getCustomFieldIds($repo$context);
  31.         if ($customFieldIds->getTotal() !== 0) {
  32.             return;
  33.         }
  34.         $repo $this->container->get('custom_field_set.repository');
  35.         $repo->create([[
  36.             'name' => 'order_notice',
  37.             'config' => [
  38.                 'label' => [
  39.                     'en-GB' => 'Order Notice',
  40.                     'de-DE' => 'Order Notice',
  41.                 ],
  42.             ],
  43.             'relations' => [
  44.                 ['entityName' => 'order']
  45.             ],
  46.             'customFields' => [
  47.                 [
  48.                     'name' => 'order_notice_notice',
  49.                     'type' => 'text',
  50.                     'config' => [
  51.                         'componentName' => 'sw-text-editor',
  52.                         'customFieldType' => 'textEditor',
  53.                         'customFieldPosition' => 1,
  54.                         'label' => [
  55.                             'en-GB' => 'Order Notice',
  56.                             'de-DE' => 'Bestellnotiz',
  57.                         ],
  58.                     ]
  59.                 ],
  60.                 [
  61.                     'name' => 'order_products_to_continous',
  62.                     'type' => 'text',
  63.                     'config' => [
  64.                         'componentName' => 'sw-text-editor',
  65.                         'customFieldType' => 'textEditor',
  66.                         'customFieldPosition' => 2,
  67.                         'label' => [
  68.                             'en-GB' => 'Products to continous',
  69.                             'de-DE' => 'Produkte mit Fortsetzungsbezug',
  70.                         ],
  71.                     ]
  72.                 ],
  73.                 [
  74.                     'name' => 'order_products_libreka_download_links',
  75.                     'type' => 'text',
  76.                     'config' => [
  77.                         'componentName' => 'sw-text-editor',
  78.                         'customFieldType' => 'textEditor',
  79.                         'customFieldPosition' => 3,
  80.                         'label' => [
  81.                             'en-GB' => 'Libreka download links',
  82.                             'de-DE' => 'Libreka Download Links',
  83.                         ],
  84.                     ]
  85.                 ]
  86.             ]
  87.         ]], $context);
  88.     }
  89.     public function uninstall(UninstallContext $uninstallContext): void
  90.     {
  91.         $shopwareContext $uninstallContext->getContext();
  92.         $this->removeCustomFields($shopwareContext);
  93.         parent::uninstall($uninstallContext);
  94.     }
  95.     private function removeCustomFields(Context $context)
  96.     {
  97.         $repo $this->container->get('custom_field.repository');
  98.         $customFieldIds $this->getCustomFieldIds($repo$context);
  99.         if ($customFieldIds->getTotal() == 0) {
  100.             return;
  101.         }
  102.         $ids array_map(static function ($id) {
  103.             return ['id' => $id];
  104.         }, $customFieldIds->getIds());
  105.         $repo->delete($ids$context);
  106.         $repo $this->container->get('custom_field_set.repository');
  107.         $customFieldSetIds $this->getCustomFieldSetIds($repo$context);
  108.         if ($customFieldSetIds->getTotal() == 0) {
  109.             return;
  110.         }
  111.         $ids array_map(static function ($id) {
  112.             return ['id' => $id];
  113.         }, $customFieldSetIds->getIds());
  114.         $repo->delete($ids$context);
  115.     }
  116.     private function getCustomFieldIds(EntityRepositoryInterface $customFieldRepositoryContext $context): IdSearchResult
  117.     {
  118.         $criteria = new Criteria();
  119.         $criteria->addFilter(new ContainsFilter('name''order_notice'));
  120.         return $customFieldRepository->searchIds($criteria$context);
  121.     }
  122.     private function getCustomFieldSetIds(EntityRepositoryInterface $customFieldSetRepositoryContext $context): IdSearchResult
  123.     {
  124.         $criteria = new Criteria();
  125.         $criteria->addFilter(new EqualsFilter('name''order_notice'));
  126.         return $customFieldSetRepository->searchIds($criteria$context);
  127.     }
  128. }