custom/plugins/VioCustomerPrice/src/VioCustomerPrice.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace VioCustomerPrice;
  3. use Shopware\Core\Defaults;
  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\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Doctrine\DBAL\Connection;
  13. class VioCustomerPrice extends Plugin
  14. {
  15.     public function uninstall(UninstallContext $uninstallContext): void
  16.     {
  17.         if (!$uninstallContext->keepUserData()) {
  18.             $connection $this->container->get(Connection::class);
  19.             if ($connection) {
  20.                 $column $connection->executeQuery('select COLUMN_NAME from information_schema.columns where table_name = \'product\' and column_name = \'customerPrices\';')
  21.                     ->fetchColumn();
  22.                 if($column === 'customerPrices') {
  23.                     $connection->executeStatement('ALTER TABLE product DROP COLUMN customerPrices;');
  24.                 }
  25.                 $connection->executeStatement('DROP TABLE IF EXISTS vio_customer_price;');
  26.             }
  27.             /** @var EntityRepositoryInterface $importExportProfileRepository */
  28.             $importExportProfileRepository $this->container->get('import_export_profile.repository');
  29.             if($importExportProfileRepository) {
  30.                 $importExportProfileIds $importExportProfileRepository->searchIds(
  31.                     (new Criteria())
  32.                         ->addFilter(new EqualsFilter('sourceEntity''vio_customer_price'))
  33.                     , $uninstallContext->getContext()
  34.                 )->getIds();
  35.                 $importExportProfileIds array_map(static function ($id) {
  36.                     return ["id" => $id];
  37.                 }, $importExportProfileIds);
  38.                 if(!empty($importExportProfileIds)) {
  39.                     // patch false Data from version < 1.4.2
  40.                     $update array_map(static function ($idArray) {
  41.                         $idArray['systemDefault'] = false;
  42.                         return $idArray;
  43.                     }, $importExportProfileIds);
  44.                     $importExportProfileRepository->update($update$uninstallContext->getContext());
  45.                     $importExportProfileRepository->delete($importExportProfileIds$uninstallContext->getContext());
  46.                 }
  47.             }
  48.         }
  49.         parent::uninstall($uninstallContext);
  50.     }
  51.     public function install(InstallContext $installContext): void
  52.     {
  53.         $this->addImportExportProfile($installContext->getContext());
  54.         parent::install($installContext);
  55.     }
  56.     public function update(UpdateContext $updateContext): void
  57.     {
  58.         if (version_compare($updateContext->getCurrentPluginVersion(), '1.3.0''<')) {
  59.             $this->addImportExportProfile($updateContext->getContext());
  60.         }
  61.         if (version_compare($updateContext->getCurrentPluginVersion(), '1.3.0''>=')
  62.             && version_compare($updateContext->getCurrentPluginVersion(), '1.4.3''<')) {
  63.             /** @var EntityRepositoryInterface $importExportProfileRepository */
  64.             $importExportProfileRepository $this->container->get('import_export_profile.repository');
  65.             if ($importExportProfileRepository) {
  66.                 $importExportProfileIds $importExportProfileRepository->searchIds(
  67.                     (new Criteria())
  68.                         ->addFilter(new EqualsFilter('sourceEntity''vio_customer_price'))
  69.                     , $updateContext->getContext()
  70.                 )->getIds();
  71.                 if (!empty($importExportProfileIds)) {
  72.                     $update array_map(static function ($id) {
  73.                         return [
  74.                             "id" => $id,
  75.                             "systemDefault" => false
  76.                         ];
  77.                     }, $importExportProfileIds);
  78.                     $importExportProfileRepository->update($update$updateContext->getContext());
  79.                 }
  80.             }
  81.         }
  82.         parent::update($updateContext);
  83.     }
  84.     private function addImportExportProfile(Context $context): void
  85.     {
  86.         /** @var EntityRepositoryInterface $repository */
  87.         $repository $this->container->get('import_export_profile.repository');
  88.         if ($repository) {
  89.             $profileId $repository->searchIds(
  90.                 (new Criteria())
  91.                     ->addFilter(new EqualsFilter('sourceEntity''vio_customer_price'))
  92.                 , $context
  93.             )->firstId();
  94.             if ($profileId === null) {
  95.                 $repository->create([[
  96.                     'name' => 'Default customer prices',
  97.                     'systemDefault' => false,
  98.                     'sourceEntity' => 'vio_customer_price',
  99.                     'fileType' => 'text/csv',
  100.                     'delimiter' => ';',
  101.                     'enclosure' => '"',
  102.                     'mapping' => [
  103.                         [
  104.                             "key" => "id",
  105.                             "mappedKey" => "id"
  106.                         ],
  107.                         [
  108.                             "key" => "productId",
  109.                             "mappedKey" => "productId"
  110.                         ],
  111.                         [
  112.                             "key" => "customerId",
  113.                             "mappedKey" => "customerId"
  114.                         ],
  115.                         [
  116.                             "key" => "quantityEnd",
  117.                             "mappedKey" => "quantityEnd"
  118.                         ],
  119.                         [
  120.                             "key" => "quantityStart",
  121.                             "mappedKey" => "quantityStart"
  122.                         ],
  123.                         [
  124.                             "key" => "price",
  125.                             "mappedKey" => "price"
  126.                         ]
  127.                     ],
  128.                     'translations' => [
  129.                         Defaults::LANGUAGE_SYSTEM =>
  130.                         [
  131.                             'label' => 'Default customer prices'
  132.                         ]
  133.                     ]
  134.                 ]], $context);
  135.             }
  136.         }
  137.     }
  138. }