custom/plugins/AcrisSuppliesUpsellingCS/src/AcrisSuppliesUpsellingCS.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\SuppliesUpselling;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. class AcrisSuppliesUpsellingCS extends Plugin
  13. {
  14.     public function postUpdate(UpdateContext $updateContext): void
  15.     {
  16.         if((version_compare($updateContext->getCurrentPluginVersion(), '2.0.0''<') && version_compare($updateContext->getUpdatePluginVersion(), '2.0.0''>=')) && $updateContext->getPlugin()->isActive() === true) {
  17.             $this->getConfigurationSettings($updateContext->getContext());
  18.         }
  19.     }
  20.     private function getConfigurationSettings(Context $context): void
  21.     {
  22.         /** @var EntityRepositoryInterface $systemConfigRepository */
  23.         $systemConfigRepository $this->container->get('system_config.repository');
  24.         /** @var EntityRepositoryInterface $productCrossSellingRepository */
  25.         $productCrossSellingRepository $this->container->get('product_cross_selling.repository');
  26.         /** @var EntityRepositoryInterface $acrisCrossSellingDisplayRepository */
  27.         $acrisCrossSellingDisplayRepository $this->container->get('acris_cross_selling_display.repository');
  28.         /** @var EntitySearchResult $systemConfigResults */
  29.         $systemConfigResults $systemConfigRepository->search((new Criteria())->addFilter(new ContainsFilter('configurationKey''AcrisSuppliesUpsellingCS.config')), $context);
  30.         /** @var EntitySearchResult $productCrossSellingResults */
  31.         $productCrossSellingResults $productCrossSellingRepository->search((new Criteria()), $context);
  32.         if ($productCrossSellingResults->getElements()) {
  33.             foreach ($productCrossSellingResults->getElements() as $productCrossSelling) {
  34.                 $data null;
  35.                 if ($systemConfigResults->first() && $systemConfigResults->getTotal() > 0) {
  36.                     $listing false;
  37.                     foreach ($systemConfigResults as $systemConfig) {
  38.                         if ($systemConfig->getConfigurationValue() === "listing") {
  39.                             $listing true;
  40.                         }
  41.                         if ($listing) {
  42.                             if ($systemConfig->getConfigurationValue() === "above") {
  43.                                 $data = [
  44.                                     'productCrossSellingId' => $productCrossSelling->getId(),
  45.                                     'crossSellingLayout' => 'layoutList',
  46.                                     'crossSellingDisplay' => 'displayAbovePrice',
  47.                                     'crossSellingFlyoutDisplay' => 'flyoutHide'
  48.                                 ];
  49.                             } else if ($systemConfig->getConfigurationValue() === "tab") {
  50.                                 $data = [
  51.                                     'productCrossSellingId' => $productCrossSelling->getId(),
  52.                                     'crossSellingLayout' => 'layoutList',
  53.                                     'crossSellingDisplay' => 'displaySwStandard',
  54.                                     'crossSellingFlyoutDisplay' => 'flyoutHide'
  55.                                 ];
  56.                             }
  57.                         }
  58.                     }
  59.                 }
  60.                 if ($data) {
  61.                     $acrisCrossSellingDisplayRepository->upsert([$data], $context);
  62.                 }
  63.             }
  64.         }
  65.     }
  66.     public
  67.     function uninstall(UninstallContext $context): void
  68.     {
  69.         if ($context->keepUserData()) {
  70.             return;
  71.         }
  72.         $this->cleanupDatabase();
  73.     }
  74.     private
  75.     function cleanupDatabase(): void
  76.     {
  77.         $connection $this->container->get(Connection::class);
  78.         $connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling_display');
  79.         $connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling_translation');
  80.         $connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling');
  81.         $connection->executeStatement('DROP TABLE IF EXISTS acris_cross_selling');
  82.         $this->removeInheritance($connection'product_cross_selling''acrisCrossSellingDisplay');
  83.         $this->removeInheritance($connection'product_cross_selling_assigned_products''acrisProductCrossSellingAssignedProduct');
  84.     }
  85.     protected function removeInheritance(Connection $connectionstring $entitystring $propertyName): void
  86.     {
  87.         $sql str_replace(
  88.             ['#table#''#column#'],
  89.             [$entity$propertyName],
  90.             'ALTER TABLE `#table#` DROP COLUMN `#column#`'
  91.         );
  92.         $connection->executeUpdate($sql);
  93.     }
  94. }