custom/plugins/MoorlCustomerAccounts/src/MoorlCustomerAccounts.php line 18

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlCustomerAccounts;
  3. use MoorlCustomerAccounts\Core\Event\InitialPasswordEvent;
  4. use MoorlFoundation\Core\PluginFoundation;
  5. use MoorlFoundation\Core\Service\DataService;
  6. use Shopware\Core\Content\MailTemplate\MailTemplateActions;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\System\CustomField\CustomFieldTypes;
  14. use Doctrine\DBAL\Connection;
  15. class MoorlCustomerAccounts extends Plugin
  16. {
  17.     public const NAME 'MoorlCustomerAccounts';
  18.     public const DATA_CREATED_AT '2000-07-02 00:00:00.000';
  19.     public const PLUGIN_TABLES = [];
  20.     public const SHOPWARE_TABLES = [
  21.         'mail_template_type',
  22.         'mail_template_type_translation',
  23.         'mail_template',
  24.         'mail_template_translation',
  25.         'event_action',
  26.         'custom_field_set',
  27.         'flow',
  28.         'flow_sequence',
  29.     ];
  30.     public const INHERITANCES = [];
  31.     public const EVENT_NAME 'moorl_ca_initial_password.send';
  32.     public const TECHNICAL_NAME 'moorl_ca_initial_password';
  33.     public const PLUGIN_CUSTOM_FIELDS = [
  34.         'moorl_ca_parent_id',
  35.         'moorl_ca_customer_id',
  36.         'moorl_ca_order_copy',
  37.     ];
  38.     public function activate(ActivateContext $activateContext): void
  39.     {
  40.         parent::activate($activateContext); // TODO: Change the autogenerated stub
  41.         /* @var $dataService DataService */
  42.         $dataService $this->container->get(DataService::class);
  43.         $dataService->install(self::NAME);
  44.     }
  45.     public function update(UpdateContext $updateContext): void
  46.     {
  47.         parent::update($updateContext); // TODO: Change the autogenerated stub
  48.         try {
  49.             /* @var $dataService DataService */
  50.             $dataService $this->container->get(DataService::class);
  51.             $dataService->install(self::NAME);
  52.         } catch (\Exception $exception) {
  53.         }
  54.     }
  55.     public function uninstall(UninstallContext $context): void
  56.     {
  57.         parent::uninstall($context);
  58.         if ($context->keepUserData()) {
  59.             return;
  60.         }
  61.         $this->removePluginData();
  62.         $this->dropTables();
  63.     }
  64.     private function removePluginData(): void
  65.     {
  66.         $connection $this->container->get(Connection::class);
  67.         foreach (array_reverse(self::SHOPWARE_TABLES) as $table) {
  68.             $sql sprintf("SET FOREIGN_KEY_CHECKS=0; DELETE FROM `%s` WHERE `created_at` = '%s';"$tableself::DATA_CREATED_AT);
  69.             try {
  70.                 $connection->executeStatement($sql);
  71.             } catch (\Exception $exception) {
  72.                 continue;
  73.             }
  74.         }
  75.         try {
  76.             $connection->executeStatement('ALTER TABLE `product` DROP COLUMN `forms`');
  77.         } catch (\Exception $exception) {
  78.         }
  79.     }
  80.     private function dropTables(): void
  81.     {
  82.         $connection $this->container->get(Connection::class);
  83.         foreach (self::PLUGIN_TABLES as $table) {
  84.             $sql sprintf('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `%s`;'$table);
  85.             $connection->executeStatement($sql);
  86.         }
  87.     }
  88.     private function __refreshPluginData(Context $context$justDelete null): void
  89.     {
  90.         return;
  91.         /* @var $foundation PluginFoundation */
  92.         $foundation $this->container->get(PluginFoundation::class);
  93.         $foundation->setContext($context);
  94.         $data = [
  95.             [
  96.                 'name' => 'moorl_ca',
  97.                 'config' => [
  98.                     'label' => [
  99.                         'en-GB' => 'Customer accounts',
  100.                         'de-DE' => 'Kunden Accounts',
  101.                     ],
  102.                 ],
  103.                 'relations' => [
  104.                     ['entityName' => 'customer'],
  105.                     ['entityName' => 'order']
  106.                 ],
  107.                 'customFields' => [
  108.                     [
  109.                         'name' => 'moorl_ca_order_copy',
  110.                         'type' => 'bool',
  111.                         'config' => [
  112.                             'componentName' => 'sw-field',
  113.                             'customFieldType' => 'switch',
  114.                             'label' => [
  115.                                 'en-GB' => 'Send copy of orders',
  116.                                 'de-DE' => 'Sende Kopie von Bestellungen',
  117.                             ],
  118.                         ]
  119.                     ],
  120.                     [
  121.                         'name' => 'moorl_ca_parent_id',
  122.                         'type' => CustomFieldTypes::JSON,
  123.                         'config' => [
  124.                             'label' => [
  125.                                 'en-GB' => 'Main customer',
  126.                                 'de-DE' => 'Hauptkunde',
  127.                             ],
  128.                             'componentName' => "sw-entity-single-select",
  129.                             'entity' => 'customer',
  130.                             'customFieldType' => CustomFieldTypes::JSON,
  131.                             'labelProperty' => "email"
  132.                         ]
  133.                     ],
  134.                     [
  135.                         'name' => 'moorl_ca_customer_id',
  136.                         'type' => CustomFieldTypes::JSON,
  137.                         'config' => [
  138.                             'label' => [
  139.                                 'en-GB' => 'Customer',
  140.                                 'de-DE' => 'Kunde',
  141.                             ],
  142.                             'componentName' => "sw-entity-single-select",
  143.                             'entity' => 'customer',
  144.                             'customFieldType' => CustomFieldTypes::JSON,
  145.                             'labelProperty' => "email"
  146.                         ]
  147.                     ]
  148.                 ]
  149.             ]
  150.         ];
  151.         $foundation->updateCustomFields($data'moorl_ca');
  152.         $foundation->removeMailTemplates([
  153.             self::TECHNICAL_NAME
  154.         ], $justDelete);
  155.         $foundation->removeEventActions([
  156.             InitialPasswordEvent::EVENT_NAME
  157.         ]);
  158.         if ($justDelete) {
  159.             return;
  160.         }
  161.         $data = [
  162.             [
  163.                 'technical_name' => self::TECHNICAL_NAME,
  164.                 'event_name' => InitialPasswordEvent::EVENT_NAME,
  165.                 'action_name' => MailTemplateActions::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  166.                 'locale' => [
  167.                     'en-GB' => [
  168.                         'name' => 'Your access',
  169.                         'description' => 'Customer Accounts Initial Password',
  170.                         'content_html' => $this->getTemplateFile(self::TECHNICAL_NAME'en-GB''html'),
  171.                         'content_plain' => $this->getTemplateFile(self::TECHNICAL_NAME'en-GB''txt'),
  172.                     ],
  173.                     'de-DE' => [
  174.                         'name' => 'Dein Zugang',
  175.                         'description' => 'Kunden Accounts Initiales Passwort',
  176.                         'content_html' => $this->getTemplateFile(self::TECHNICAL_NAME'de-DE''html'),
  177.                         'content_plain' => $this->getTemplateFile(self::TECHNICAL_NAME'de-DE''txt'),
  178.                     ],
  179.                 ],
  180.                 'availableEntities' => [
  181.                     'salesChannel' => 'sales_channel',
  182.                     'customer' => 'customer',
  183.                     'parent' => 'customer'
  184.                 ]
  185.             ]
  186.         ];
  187.         $foundation->addMailTemplates($data);
  188.     }
  189.     public function __activate(ActivateContext $activateContext): void
  190.     {
  191.         parent::activate($activateContext);
  192.         $this->refreshPluginData($activateContext->getContext());
  193.     }
  194.     public function __install(InstallContext $context): void
  195.     {
  196.         parent::install($context);
  197.         $this->refreshPluginData($context->getContext());
  198.     }
  199.     public function __uninstall(UninstallContext $context): void
  200.     {
  201.         parent::uninstall($context);
  202.         if ($context->keepUserData()) {
  203.             return;
  204.         }
  205.         $this->refreshPluginData($context->getContext(), true);
  206.     }
  207.     private function __getTemplateFile($name$locale$extension)
  208.     {
  209.         $file __DIR__ '/../content/mail-templates/' $name '-' $locale '.' $extension '.twig';
  210.         if (file_exists($file)) {
  211.             return file_get_contents($file);
  212.         }
  213.         return 'Template file not found: ' $file;
  214.     }
  215. }