vendor/store.shopware.com/spnosalutationorder/src/SpnoSalutationOrder.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) webpiloten. <kontakt@web-piloten.de>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Spno\SalutationOrder;
  8. use Doctrine\DBAL\Connection;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  12. use Shopware\Core\Framework\Plugin;
  13. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  15. use Spno\SalutationOrder\Entity\SalutationPosition\SalutationPositionDefinition;
  16. class SpnoSalutationOrder extends Plugin
  17. {
  18.     public function postInstall(InstallContext $installContext): void
  19.     {
  20.         /** @var EntityRepositoryInterface $salutationRepository */
  21.         $salutationRepository $this->container->get('salutation.repository');
  22.         $criteria = new Criteria();
  23.         $criteria->addSorting(new FieldSorting('salutationKey'FieldSorting::DESCENDING));
  24.         $salutationIds $salutationRepository->searchIds($criteria$installContext->getContext())->getIds();
  25.         $salutations = [];
  26.         $position 10;
  27.         foreach ($salutationIds as $salutationId) {
  28.             $salutations[] = [
  29.                 'salutationId' => $salutationId,
  30.                 'position' => $position,
  31.             ];
  32.             $position $position 10;
  33.         }
  34.         /** @var Connection $connection */
  35.         $connection $this->container->get(Connection::class);
  36.         foreach ($salutations as $salutation) {
  37.             $connection->executeStatement(\sprintf(
  38.                 "INSERT INTO `%s` (`salutation_id`, `position`, `created_at`) VALUES (UNHEX('%s'), %s, date(now())) ON DUPLICATE KEY UPDATE `salutation_id` = UNHEX('%s')",
  39.                 SalutationPositionDefinition::ENTITY_NAME,
  40.                 $salutation['salutationId'],
  41.                 $salutation['position'],
  42.                 $salutation['salutationId']
  43.             ));
  44.         }
  45.     }
  46.     public function uninstall(UninstallContext $uninstallContext): void
  47.     {
  48.         if ($uninstallContext->keepUserData()) {
  49.             parent::uninstall($uninstallContext);
  50.             return;
  51.         }
  52.         /** @var Connection $connection */
  53.         $connection $this->container->get(Connection::class);
  54.         $connection->executeStatement(\sprintf('DROP TABLE IF EXISTS `%s`'SalutationPositionDefinition::ENTITY_NAME));
  55.     }
  56. }