custom/plugins/MoorlFormBuilder/src/MoorlFormBuilder.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlFormBuilder;
  3. use Doctrine\DBAL\Connection;
  4. use MoorlFormBuilder\Core\Event\CmsFormEvent;
  5. use MoorlFormBuilder\Core\Event\OrderRequestFormEvent;
  6. use MoorlFormBuilder\Core\Event\ProductRequestFormEvent;
  7. use MoorlFoundation\Core\PluginFoundation;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. class MoorlFormBuilder extends Plugin
  15. {
  16.     public const MAILTYPE_CMS 'moorl_form_builder_cms';
  17.     public const MAILTYPE_PRODUCT_REQUEST 'moorl_form_builder_product_request';
  18.     public const MAILTYPE_ORDER_REQUEST 'moorl_form_builder_order_request';
  19.     public const MAIL_TEMPLATE_MAIL_SEND_ACTION 'moorl_form_builder.action.mail.send';
  20.     public function activate(ActivateContext $activateContext): void
  21.     {
  22.         parent::activate($activateContext); // TODO: Change the autogenerated stub
  23.         $this->refreshPluginData($activateContext->getContext());
  24.     }
  25.     public function update(UpdateContext $updateContext): void
  26.     {
  27.         parent::update($updateContext); // TODO: Change the autogenerated stub
  28.         $this->refreshPluginData($updateContext->getContext());
  29.     }
  30.     private function refreshPluginData(Context $context$justDelete null): void
  31.     {
  32.         /* @var $foundation PluginFoundation */
  33.         $foundation $this->container->get(PluginFoundation::class);
  34.         $foundation->setContext($context);
  35.         $foundation->removeMailTemplates([
  36.             self::MAILTYPE_CMS,
  37.             self::MAILTYPE_PRODUCT_REQUEST,
  38.             self::MAILTYPE_ORDER_REQUEST
  39.         ], $justDelete);
  40.         $foundation->removeEventActions([
  41.             CmsFormEvent::EVENT_NAME,
  42.             ProductRequestFormEvent::EVENT_NAME,
  43.             OrderRequestFormEvent::EVENT_NAME
  44.         ]);
  45.         if ($justDelete) {
  46.             $foundation->removeCustomFields('moorl_fb');
  47.             $foundation->removeCmsSlots(['moorl_form_builder']);
  48.             $foundation->removeCmsBlocks(['moorl_form_builder']);
  49.             $foundation->dropTables([
  50.                 'moorl_form_rule',
  51.                 'moorl_form_product',
  52.                 'moorl_form',
  53.                 'moorl_form_history'
  54.             ]);
  55.             $foundation->executeQuery('ALTER TABLE `product` DROP COLUMN `forms`');
  56.             return;
  57.         }
  58.         $data = [
  59.             [
  60.                 'technical_name' => MoorlFormBuilder::MAILTYPE_CMS,
  61.                 'event_name' => CmsFormEvent::EVENT_NAME,
  62.                 'action_name' => self::MAIL_TEMPLATE_MAIL_SEND_ACTION,
  63.                 'locale' => [
  64.                     'en-GB' => [
  65.                         'name' => 'moori - Form builder',
  66.                         'description' => 'moori - Form builder - default',
  67.                         'content_html' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS'en-GB''html'),
  68.                         'content_plain' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS'en-GB''txt'),
  69.                     ],
  70.                     'de-DE' => [
  71.                         'name' => 'moori - Formular Baukasten',
  72.                         'description' => 'moori - Formular Baukasten - Standard',
  73.                         'content_html' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS'de-DE''html'),
  74.                         'content_plain' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS'de-DE''txt'),
  75.                     ],
  76.                 ],
  77.                 'availableEntities' => [
  78.                     'salesChannel' => 'sales_channel',
  79.                     'form' => 'moorl_form',
  80.                     'product' => 'product',
  81.                     'order' => 'order',
  82.                     'customer' => 'customer'
  83.                 ]
  84.             ]
  85.         ];
  86.         $foundation->addMailTemplates($data);
  87.         $data = [[
  88.             'name' => 'moorl_fb',
  89.             'config' => [
  90.                 'label' => [
  91.                     'en-GB' => 'Form builder',
  92.                     'de-DE' => 'Formular Baukasten',
  93.                 ],
  94.             ],
  95.             'relations' => [
  96.                 ['entityName' => 'category'],
  97.                 ['entityName' => 'product'],
  98.                 ['entityName' => 'customer'],
  99.                 ['entityName' => 'order'],
  100.                 ['entityName' => 'media'],
  101.                 ['entityName' => 'order_line_item'],
  102.             ],
  103.             'customFields' => [
  104.                 [
  105.                     'name' => 'moorl_fb_text',
  106.                     'type' => 'text',
  107.                     'config' => [
  108.                         'componentName' => 'sw-field',
  109.                         'customFieldType' => 'text',
  110.                         'label' => [
  111.                             'en-GB' => 'Example text',
  112.                             'de-DE' => 'Beispiel Text',
  113.                         ],
  114.                     ]
  115.                 ],
  116.                 [
  117.                     'name' => 'moorl_fb_html',
  118.                     'type' => 'html',
  119.                     'config' => [
  120.                         'componentName' => 'sw-text-editor',
  121.                         'customFieldType' => 'textEditor',
  122.                         'label' => [
  123.                             'en-GB' => 'Example HTML',
  124.                             'de-DE' => 'Beispiel HTML',
  125.                         ],
  126.                     ]
  127.                 ],
  128.                 [
  129.                     'name' => 'moorl_fb_code',
  130.                     'type' => 'text',
  131.                     'config' => [
  132.                         'componentName' => 'sw-code-editor',
  133.                         'customFieldType' => 'text',
  134.                         'label' => [
  135.                             'en-GB' => 'Example code',
  136.                             'de-DE' => 'Beispiel Code',
  137.                         ],
  138.                     ]
  139.                 ],
  140.                 [
  141.                     'name' => 'moorl_fb_number',
  142.                     'type' => 'float',
  143.                     'config' => [
  144.                         'componentName' => 'sw-field',
  145.                         'customFieldType' => 'number',
  146.                         'numberType' => 'float',
  147.                         'label' => [
  148.                             'en-GB' => 'Example number',
  149.                             'de-DE' => 'Beispiel Zahl',
  150.                         ],
  151.                     ]
  152.                 ],
  153.                 [
  154.                     'name' => 'moorl_fb_bool',
  155.                     'type' => 'bool',
  156.                     'config' => [
  157.                         'componentName' => 'sw-field',
  158.                         'customFieldType' => 'switch',
  159.                         'label' => [
  160.                             'en-GB' => 'Example switch',
  161.                             'de-DE' => 'Beispiel an/aus',
  162.                         ],
  163.                     ]
  164.                 ],
  165.                 [
  166.                     'name' => 'moorl_fb_media',
  167.                     'type' => 'media',
  168.                     'config' => [
  169.                         'componentName' => 'sw-media-field',
  170.                         'customFieldType' => 'media',
  171.                         'label' => [
  172.                             'en-GB' => 'Example media',
  173.                             'de-DE' => 'Beispiel Medien',
  174.                         ],
  175.                     ]
  176.                 ],
  177.                 [
  178.                     'name' => 'moorl_fb_date',
  179.                     'type' => 'text',
  180.                     'config' => [
  181.                         'type' => 'date',
  182.                         'componentName' => 'sw-field',
  183.                         'customFieldType' => 'date',
  184.                         'dateType' => 'date',
  185.                         'config' => [
  186.                             'time_24hr' => true
  187.                         ],
  188.                         'label' => [
  189.                             'en-GB' => 'Example date',
  190.                             'de-DE' => 'Beispiel Datum',
  191.                         ],
  192.                     ]
  193.                 ]
  194.             ]
  195.         ]];
  196.         $foundation->updateCustomFields($data'moorl_fb');
  197.     }
  198.     private function getTemplateFile($name$locale$extension)
  199.     {
  200.         $file __DIR__ '/../content/mail-templates/' $name '-' $locale '.' $extension '.twig';
  201.         if (file_exists($file)) {
  202.             return file_get_contents($file);
  203.         }
  204.         return 'Template file not found: ' $file;
  205.     }
  206.     public function install(InstallContext $context): void
  207.     {
  208.         parent::install($context);
  209.         $this->refreshPluginData($context->getContext());
  210.     }
  211.     public function uninstall(UninstallContext $context): void
  212.     {
  213.         parent::uninstall($context);
  214.         if ($context->keepUserData()) {
  215.             return;
  216.         }
  217.         $this->refreshPluginData($context->getContext(), true);
  218.     }
  219. }