custom/plugins/b2bsellerscore/addons/B2bOffer/B2bOffer.php line 20

Open in your IDE?
  1. <?php
  2. namespace B2bOffer;
  3. use B2bOffer\Setup\FlowBuilderSetup;
  4. use B2bOffer\Setup\PlatformMenuInstaller;
  5. use Doctrine\DBAL\Connection;
  6. use B2bOffer\Setup\CustomFieldInstaller;
  7. use B2bOffer\Setup\DocumentTypeInstaller;
  8. use B2bOffer\Setup\EventActionInstaller;
  9. use B2bOffer\Setup\MailTemplateSetup;
  10. use Shopware\Core\Defaults;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\Plugin;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  17. class B2bOffer extends Plugin
  18. {
  19.     private const NUMBER_RANGE_ID '44ae3edc5358424fa2a59086c3825828';
  20.     private const NUMBER_TYPE_ID '0eb345859fe7418ab417a305688adf7a';
  21.     public const DEFAULT_OPEN_STATUS_ID '1f7fe3c343074c6b833a132aab4f17f3';
  22.     public const DEFAULT_REQUEST_STATUS_ID '3730574503094101ac91bcedc3787195';
  23.     public const DEFAULT_CLOSED_STATUS_ID '1607ba0a3ee444cfa86fc773157b82b5';
  24.     public const DEFAULT_DECLINED_STATUS_ID '9c5cbe1f2d4c4785865f51203586ff25';
  25.     public const DEFAULT_ARCHIVED_STATUS_ID '053abb1e7d9143bf8bb567957f2f975e';
  26.     public function install(InstallContext $context): void
  27.     {
  28.         $this->createNumberRange($context->getContext());
  29.         $this->setDefaultConfigValues();
  30.         (new MailTemplateSetup($this->container))->install();
  31.         /** @var Connection $connection */
  32.         $connection $this->container->get(Connection::class);
  33.         (new DocumentTypeInstaller())->install($connection);
  34.         (new CustomFieldInstaller($this->container$context->getContext()))->install();
  35.         if ($this->container->has('flow.repository')) {
  36.             (new FlowBuilderSetup($this->container$context->getContext()))->install();
  37.         } else {
  38.             (new EventActionInstaller($this->container$context->getContext()))->install();
  39.         }
  40.         (new PlatformMenuInstaller())->install($connection);
  41.     }
  42.     public function update(UpdateContext $context): void
  43.     {
  44.         $this->createNumberRange($context->getContext());
  45.         $this->setDefaultConfigValues();
  46.         (new MailTemplateSetup($this->container))->install();
  47.         /** @var Connection $connection */
  48.         $connection $this->container->get(Connection::class);
  49.         (new DocumentTypeInstaller())->install($connection);
  50.         (new CustomFieldInstaller($this->container$context->getContext()))->install();
  51.         if ($this->container->has('flow.repository')) {
  52.             (new FlowBuilderSetup($this->container$context->getContext()))->install();
  53.         } else {
  54.             (new EventActionInstaller($this->container$context->getContext()))->install();
  55.         }
  56.         (new PlatformMenuInstaller())->install($connection);
  57.     }
  58.     public function uninstall(UninstallContext $context): void
  59.     {
  60.         parent::uninstall($context);
  61.         if ($context->keepUserData()) {
  62.             return;
  63.         }
  64.         $this->deleteNumberRange($context->getContext());
  65.         (new MailTemplateSetup($this->container))->uninstall();
  66.         /** @var Connection $connection */
  67.         $connection $this->container->get(Connection::class);
  68.         (new DocumentTypeInstaller())->uninstall($connection);
  69.         (new CustomFieldInstaller($this->container$context->getContext()))->uninstall();
  70.         (new EventActionInstaller($this->container$context->getContext()))->uninstall();
  71.         if ($this->container->has('flow.repository')) {
  72.             (new FlowBuilderSetup($this->container$context->getContext()))->uninstall();
  73.         }
  74.         $connection->executeStatement("
  75.             SET FOREIGN_KEY_CHECKS = 0;
  76.             
  77.             DROP TABLE IF EXISTS `b2b_offer`;
  78.             DROP TABLE IF EXISTS `b2b_offer_item`;
  79.             DROP TABLE IF EXISTS `b2b_offer_address`;
  80.             DROP TABLE IF EXISTS `b2b_offer_customer`;
  81.             DROP TABLE IF EXISTS `b2b_offer_status`;
  82.             DROP TABLE IF EXISTS `b2b_offer_status_translation`;
  83.             
  84.             SET FOREIGN_KEY_CHECKS = 1;
  85.         ");
  86.     }
  87.     private function setDefaultConfigValues()
  88.     {
  89.         $config $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  90.         if (empty($config->get('B2bOffer.config.defaultOpenStatusId'))) {
  91.             $config->set('B2bOffer.config.defaultOpenStatusId'self::DEFAULT_OPEN_STATUS_ID);
  92.         }
  93.         if (empty($config->get('B2bOffer.config.defaultCartStatusId'))) {
  94.             $config->set('B2bOffer.config.defaultCartStatusId'self::DEFAULT_REQUEST_STATUS_ID);
  95.         }
  96.         if (empty($config->get('B2bOffer.config.defaultClosedStatusId'))) {
  97.             $config->set('B2bOffer.config.defaultClosedStatusId'self::DEFAULT_CLOSED_STATUS_ID);
  98.         }
  99.         if (empty($config->get('B2bOffer.config.defaultValidUntil'))) {
  100.             $config->set('B2bOffer.config.defaultValidUntil'30);
  101.         }
  102.         if (empty($config->get('B2bOffer.config.defaultInvalidOfferStatusId'))) {
  103.             $config->set('B2bOffer.config.defaultInvalidOfferStatusId'self::DEFAULT_DECLINED_STATUS_ID);
  104.         }
  105.     }
  106.     private function createNumberRange(Context $context)
  107.     {
  108.         /** @var EntityRepositoryInterface $numberRangeRepository */
  109.         $numberRangeRepository $this->container->get('number_range.repository');
  110.         $numberRangeRepository->upsert([[
  111.             'id' => self::NUMBER_RANGE_ID,
  112.             'global' => true,
  113.             'pattern' => '{n}',
  114.             'start' => 1000,
  115.             'type' => [
  116.                 'id' => self::NUMBER_TYPE_ID,
  117.                 'global' => true,
  118.                 'technicalName' => 'b2b_offer',
  119.                 'translations' => [[
  120.                     'typeName' => 'Offers',
  121.                     'languageId' => Defaults::LANGUAGE_SYSTEM
  122.                 ]]
  123.             ],
  124.             'translations' => [[
  125.                 'name' => 'Offers',
  126.                 'languageId' => Defaults::LANGUAGE_SYSTEM
  127.             ]]
  128.         ]], $context);
  129.     }
  130.     private function deleteNumberRange(Context $context)
  131.     {
  132.         /** @var EntityRepositoryInterface $numberRangeRepository */
  133.         $numberRangeRepository $this->container->get('number_range.repository');
  134.         $numberRangeRepository->delete([['id' => self::NUMBER_RANGE_ID]], $context);
  135.     }
  136. }