custom/plugins/HuebertAddOrderAttributes/src/HuebertAddOrderAttributes.php line 25

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HuebertAddOrderAttributes;
  3. use \Exception;
  4. use Doctrine\DBAL\Connection;
  5. use HuebertAddOrderAttributes\Core\Content\Attribute\AttributeDefinition;
  6. use HuebertAddOrderAttributes\Template\AttributesMailTemplateContent;
  7. use Shopware\Core\Defaults;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexerRegistry;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  15. use Shopware\Core\Framework\Plugin;
  16. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  17. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  18. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  19. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  20. use Shopware\Core\Framework\Migration\InheritanceUpdaterTrait;
  21. class HuebertAddOrderAttributes extends Plugin
  22. {
  23.     use InheritanceUpdaterTrait;
  24.     /**
  25.      * @var string
  26.      */
  27.     public const mailTemplateTypeId '0a63447aa36b086e2eaeff36bfbeb779';
  28.     /**
  29.      * @var string
  30.      */
  31.     public const mailTemplateId '8e990c3ba1d46e729f59d98b9db9c9dd';
  32.     /**
  33.      * @param InstallContext $installContext
  34.      * @return void
  35.      */
  36.     public function install(InstallContext $installContext): void
  37.     {
  38.         parent::install($installContext);
  39.         $this->createHuebertMediaFolder($installContext->getContext());
  40.         $this->createNotificationEmail($installContext->getContext());
  41.     }
  42.     /**
  43.      * @param UpdateContext $updateContext
  44.      * @return void
  45.      */
  46.     public function update(UpdateContext $updateContext): void
  47.     {
  48.         parent::update($updateContext);
  49.         $this->createNotificationEmail($updateContext->getContext());
  50.     }
  51.     /**
  52.      * @param ActivateContext $activateContext
  53.      * @return void
  54.      */
  55.     public function activate(ActivateContext $activateContext): void
  56.     {
  57.         $registry $this->container->get(EntityIndexerRegistry::class);
  58.         $registry->sendIndexingMessage(['product.indexer']);
  59.         $registry->sendIndexingMessage(['product_stream.indexer']);
  60.         $registry->sendIndexingMessage(['category.indexer']);
  61.     }
  62.     /**
  63.      * @param InstallContext $installContext
  64.      * @return void
  65.      */
  66.     public function postInstall(InstallContext $installContext): void
  67.     {
  68.         parent::postInstall($installContext); // TODO: Change the autogenerated stub
  69.         $connection $this->container->get(Connection::class);
  70.         $connection->executeUpdate('UPDATE product SET attributes = id WHERE attributes IS NULL');
  71.         $connection->executeUpdate('UPDATE category SET attributes = id WHERE attributes IS NULL');
  72.         $connection->executeUpdate('UPDATE product_stream SET attributes = id WHERE attributes IS NULL');
  73.     }
  74.     /**
  75.      * @param UninstallContext $uninstallContext
  76.      * @return void
  77.      */
  78.     public function uninstall(UninstallContext $uninstallContext): void
  79.     {
  80.         parent::uninstall($uninstallContext);
  81.         if($uninstallContext->keepUserData()) {
  82.             return;
  83.         }
  84.         $connection $this->container->get(Connection::class);
  85.         $connection->executeUpdate('DROP TRIGGER IF EXISTS `update_product_attributes`');
  86.         $connection->executeUpdate('DROP TRIGGER IF EXISTS `update_category_attributes`');
  87.         $productAttributesCheck $connection->executeQuery("
  88.             SHOW COLUMNS FROM `product` LIKE 'attributes'
  89.         ");
  90.         $productAttributesExists = (bool)$productAttributesCheck->rowCount();
  91.         if($productAttributesExists) {
  92.             $connection->executeUpdate('ALTER TABLE `product` DROP COLUMN `attributes`');
  93.         }
  94.         $categoryAttributesCheck $connection->executeQuery("
  95.             SHOW COLUMNS FROM `category` LIKE 'attributes'
  96.         ");
  97.         $categoryAttributesExists = (bool)$categoryAttributesCheck->rowCount();
  98.         if($categoryAttributesExists) {
  99.             $connection->executeUpdate('ALTER TABLE `category` DROP COLUMN `attributes`');
  100.         }
  101.         $productStreamAttributesCheck $connection->executeQuery("
  102.             SHOW COLUMNS FROM `product_stream` LIKE 'attributes'
  103.         ");
  104.         $productStreamAttributesExists = (bool)$productStreamAttributesCheck->rowCount();
  105.         if($productStreamAttributesExists) {
  106.             $connection->executeUpdate('ALTER TABLE `product_stream` DROP COLUMN `attributes`');
  107.         }
  108.         $connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_field_translation`');
  109.         $connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_product_stream`');
  110.         $connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_category`');
  111.         $connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_product`');
  112.         $connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_field`');
  113.         $connection->executeUpdate('DROP TABLE IF EXISTS `sysea_attribute_dependency`');
  114.         $connection->executeUpdate('DELETE FROM `media_folder` WHERE name = "Huebert Imported Media"');
  115.         $templateTypeRepository $this->container->get('mail_template_type.repository');
  116.         $templateTypeRepository->delete([['id' => self::mailTemplateTypeId]], $uninstallContext->getContext());
  117.         $mailTemplateRepository $this->container->get('mail_template.repository');
  118.         $mailTemplateRepository->delete([['id'=>self::mailTemplateId]], $uninstallContext->getContext());
  119.     }
  120.     /**
  121.      * @param UpdateContext $updateContext
  122.      * @return void
  123.      */
  124.     private function migrateProductStream(UpdateContext $updateContext)
  125.     {
  126.         $updateContext->getContext()->setConsiderInheritance(true);
  127.         $updateContext->setAutoMigrate(true);
  128.     }
  129.     /**
  130.      * @return void
  131.      * @throws \Doctrine\DBAL\Exception
  132.      */
  133.     private function migrateOptionsField()
  134.     {
  135.         /**
  136.          * @var Connection $connection
  137.          */
  138.         $connection $this->container->get(Connection::class);
  139.         $optionsCheck $connection->executeQuery("
  140.             SHOW COLUMNS FROM `sysea_attribute_field` LIKE 'options'
  141.         ");
  142.         $optionsExists = (bool)$optionsCheck->rowCount();
  143.         if(!$optionsExists) {
  144.             $connection->executeUpdate('
  145.                 ALTER TABLE sysea_attribute_field
  146.                 ADD options TEXT NULL
  147.             ');
  148.         }
  149.         $optionsCheck $connection->executeQuery("
  150.             SHOW COLUMNS FROM `sysea_attribute_field_translation` LIKE 'options'
  151.         ");
  152.         $optionsExists = (bool)$optionsCheck->rowCount();
  153.         if(!$optionsExists) {
  154.             $connection->executeUpdate('
  155.                 ALTER TABLE sysea_attribute_field_translation
  156.                 ADD options TEXT NULL
  157.             ');
  158.         }
  159.     }
  160.     /**
  161.      * @param Context $context
  162.      * @return void
  163.      */
  164.     public function createHuebertMediaFolder(Context $context): void
  165.     {
  166.         $this->deleteDefaultMediaFolder($context);
  167.         $this->checkForThumbnailSizes($context);
  168.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  169.         $mediaFolderRepository $this->container->get('media_default_folder.repository');
  170.         $mediaFolderRepository->upsert([
  171.             [
  172.                 'entity' => AttributeDefinition::ENTITY_NAME,
  173.                 'associationFields' => ['media'],
  174.                 'folder' => [
  175.                     'name' => 'Huebert Imported Media',
  176.                     'useParentConfiguration' => false,
  177.                     'configuration' => [
  178.                         'createThumbnails' => true,
  179.                         'mediaThumbnailSizes' => [
  180.                             [
  181.                                 'width' => 350,
  182.                                 'height' => 350,
  183.                             ],
  184.                         ],
  185.                     ],
  186.                 ],
  187.             ],
  188.         ], $context);
  189.     }
  190.     /**
  191.      * @param Context $context
  192.      * @return void
  193.      */
  194.     private function checkForThumbnailSizes(Context $context): void
  195.     {
  196.         $criteria = new Criteria();
  197.         $criteria->addFilter(
  198.             new MultiFilter(
  199.                 MultiFilter::CONNECTION_AND,
  200.                 [
  201.                     new EqualsFilter('width'350),
  202.                     new EqualsFilter('height'350),
  203.                 ]
  204.             )
  205.         );
  206.         /** @var EntityRepositoryInterface $thumbnailSizeRepository */
  207.         $thumbnailSizeRepository $this->container->get('media_thumbnail_size.repository');
  208.         $thumbnailIds $thumbnailSizeRepository->searchIds($criteria$context)->getIds();
  209.         if (!empty($thumbnailIds)) {
  210.             $ids array_map(static function ($id) {
  211.                 return ['id' => $id];
  212.             }, $thumbnailIds);
  213.             $thumbnailSizeRepository->delete($ids$context);
  214.         }
  215.     }
  216.     /**
  217.      * @param Context $context
  218.      * @return void
  219.      */
  220.     private function deleteDefaultMediaFolder(Context $context): void
  221.     {
  222.         $criteria = new Criteria();
  223.         $criteria->addFilter(
  224.             new EqualsAnyFilter('entity', [
  225.                 AttributeDefinition::ENTITY_NAME,
  226.             ])
  227.         );
  228.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  229.         $mediaFolderRepository $this->container->get('media_default_folder.repository');
  230.         $mediaFolderIds $mediaFolderRepository->searchIds($criteria$context)->getIds();
  231.         if (!empty($mediaFolderIds)) {
  232.             $ids array_map(static function ($id) {
  233.                 return ['id' => $id];
  234.             }, $mediaFolderIds);
  235.             $mediaFolderRepository->delete($ids$context);
  236.         }
  237.     }
  238.     /**
  239.      * @param Context $context
  240.      * @return void
  241.      */
  242.     private function createNotificationEmail(Context $context): void
  243.     {
  244.         $templateTypeRepository $this->container->get('mail_template_type.repository');
  245.         $mailTemplateType = [
  246.             [
  247.                 'id' => self::mailTemplateTypeId,
  248.                 'name' => 'Bestellbestätigung (Artikel Konfigurator)',
  249.                 'technicalName' => 'huebert.attributes',
  250.                 'availableEntities' => [
  251.                     'salesChannel' => 'sales_channel'
  252.                 ]
  253.             ]
  254.         ];
  255.         /** @var EntityRepositoryInterface $mailTemplateRepository */
  256.         $mailTemplateRepository $this->container->get('mail_template.repository');
  257.         $mailData = new AttributesMailTemplateContent();
  258.         $mailTemplate = [
  259.             [
  260.                 'id' => self::mailTemplateId,
  261.                 'mailTemplateTypeId' => self::mailTemplateTypeId,
  262.                 'senderName' => [
  263.                     Defaults::LANGUAGE_SYSTEM => '{{ salesChannel.name }}',
  264.                     'en-GB' => '{{ salesChannel.name }}',
  265.                     'de-DE' => '{{ salesChannel.name }}',
  266.                 ],
  267.                 'subject' => [
  268.                     Defaults::LANGUAGE_SYSTEM => 'Bestellbestätigung',
  269.                     'en-GB' => 'Order confirmation',
  270.                     'de-DE' => 'Bestellbestätigung '
  271.                 ],
  272.                 'description' => [
  273.                     Defaults::LANGUAGE_SYSTEM => 'Berücksichtigt die zusätzlichen Felder in der E-Mail',
  274.                     'en-GB' => 'Takes into account the additional fields in the e-mail',
  275.                     'de-DE' => 'Berücksichtigt die zusätzlichen Felder in der E-Mail',
  276.                 ],
  277.                 'contentHtml' => [
  278.                     'en-GB' => $mailData->mailTemplateData()['en-GB']['html'],
  279.                     'de-DE' => $mailData->mailTemplateData()['de-DE']['html'],
  280.                     Defaults::LANGUAGE_SYSTEM => $mailData->mailTemplateData()['de-DE']['html'],
  281.                 ],
  282.                 'contentPlain' => [
  283.                     'en-GB' => $mailData->mailTemplateData()['en-GB']['plain'],
  284.                     'de-DE' => $mailData->mailTemplateData()['de-DE']['plain'],
  285.                     Defaults::LANGUAGE_SYSTEM => $mailData->mailTemplateData()['de-DE']['plain'],
  286.                 ],
  287.             ],
  288.         ];
  289.         try {
  290.             $templateTypeRepository->upsert($mailTemplateType$context);
  291.             $mailTemplateRepository->upsert($mailTemplate$context);
  292.         } catch (Exception $exception) {
  293.             error_log(print_r(array('uslo: '$exception->getMessage()), true) . "\n"3'../error.log');
  294.         }
  295.     }
  296. }