<?php declare(strict_types=1);
namespace MoorlFormBuilder;
use Doctrine\DBAL\Connection;
use MoorlFormBuilder\Core\Event\CmsFormEvent;
use MoorlFormBuilder\Core\Event\OrderRequestFormEvent;
use MoorlFormBuilder\Core\Event\ProductRequestFormEvent;
use MoorlFoundation\Core\PluginFoundation;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\ActivateContext;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\Framework\Plugin\Context\UpdateContext;
class MoorlFormBuilder extends Plugin
{
public const MAILTYPE_CMS = 'moorl_form_builder_cms';
public const MAILTYPE_PRODUCT_REQUEST = 'moorl_form_builder_product_request';
public const MAILTYPE_ORDER_REQUEST = 'moorl_form_builder_order_request';
public const MAIL_TEMPLATE_MAIL_SEND_ACTION = 'moorl_form_builder.action.mail.send';
public function activate(ActivateContext $activateContext): void
{
parent::activate($activateContext); // TODO: Change the autogenerated stub
$this->refreshPluginData($activateContext->getContext());
}
public function update(UpdateContext $updateContext): void
{
parent::update($updateContext); // TODO: Change the autogenerated stub
$this->refreshPluginData($updateContext->getContext());
}
private function refreshPluginData(Context $context, $justDelete = null): void
{
/* @var $foundation PluginFoundation */
$foundation = $this->container->get(PluginFoundation::class);
$foundation->setContext($context);
$foundation->removeMailTemplates([
self::MAILTYPE_CMS,
self::MAILTYPE_PRODUCT_REQUEST,
self::MAILTYPE_ORDER_REQUEST
], $justDelete);
$foundation->removeEventActions([
CmsFormEvent::EVENT_NAME,
ProductRequestFormEvent::EVENT_NAME,
OrderRequestFormEvent::EVENT_NAME
]);
if ($justDelete) {
$foundation->removeCustomFields('moorl_fb');
$foundation->removeCmsSlots(['moorl_form_builder']);
$foundation->removeCmsBlocks(['moorl_form_builder']);
$foundation->dropTables([
'moorl_form_rule',
'moorl_form_product',
'moorl_form',
'moorl_form_history'
]);
$foundation->executeQuery('ALTER TABLE `product` DROP COLUMN `forms`');
return;
}
$data = [
[
'technical_name' => MoorlFormBuilder::MAILTYPE_CMS,
'event_name' => CmsFormEvent::EVENT_NAME,
'action_name' => self::MAIL_TEMPLATE_MAIL_SEND_ACTION,
'locale' => [
'en-GB' => [
'name' => 'moori - Form builder',
'description' => 'moori - Form builder - default',
'content_html' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS, 'en-GB', 'html'),
'content_plain' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS, 'en-GB', 'txt'),
],
'de-DE' => [
'name' => 'moori - Formular Baukasten',
'description' => 'moori - Formular Baukasten - Standard',
'content_html' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS, 'de-DE', 'html'),
'content_plain' => $this->getTemplateFile(MoorlFormBuilder::MAILTYPE_CMS, 'de-DE', 'txt'),
],
],
'availableEntities' => [
'salesChannel' => 'sales_channel',
'form' => 'moorl_form',
'product' => 'product',
'order' => 'order',
'customer' => 'customer'
]
]
];
$foundation->addMailTemplates($data);
$data = [[
'name' => 'moorl_fb',
'config' => [
'label' => [
'en-GB' => 'Form builder',
'de-DE' => 'Formular Baukasten',
],
],
'relations' => [
['entityName' => 'category'],
['entityName' => 'product'],
['entityName' => 'customer'],
['entityName' => 'order'],
['entityName' => 'media'],
['entityName' => 'order_line_item'],
],
'customFields' => [
[
'name' => 'moorl_fb_text',
'type' => 'text',
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'text',
'label' => [
'en-GB' => 'Example text',
'de-DE' => 'Beispiel Text',
],
]
],
[
'name' => 'moorl_fb_html',
'type' => 'html',
'config' => [
'componentName' => 'sw-text-editor',
'customFieldType' => 'textEditor',
'label' => [
'en-GB' => 'Example HTML',
'de-DE' => 'Beispiel HTML',
],
]
],
[
'name' => 'moorl_fb_code',
'type' => 'text',
'config' => [
'componentName' => 'sw-code-editor',
'customFieldType' => 'text',
'label' => [
'en-GB' => 'Example code',
'de-DE' => 'Beispiel Code',
],
]
],
[
'name' => 'moorl_fb_number',
'type' => 'float',
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'number',
'numberType' => 'float',
'label' => [
'en-GB' => 'Example number',
'de-DE' => 'Beispiel Zahl',
],
]
],
[
'name' => 'moorl_fb_bool',
'type' => 'bool',
'config' => [
'componentName' => 'sw-field',
'customFieldType' => 'switch',
'label' => [
'en-GB' => 'Example switch',
'de-DE' => 'Beispiel an/aus',
],
]
],
[
'name' => 'moorl_fb_media',
'type' => 'media',
'config' => [
'componentName' => 'sw-media-field',
'customFieldType' => 'media',
'label' => [
'en-GB' => 'Example media',
'de-DE' => 'Beispiel Medien',
],
]
],
[
'name' => 'moorl_fb_date',
'type' => 'text',
'config' => [
'type' => 'date',
'componentName' => 'sw-field',
'customFieldType' => 'date',
'dateType' => 'date',
'config' => [
'time_24hr' => true
],
'label' => [
'en-GB' => 'Example date',
'de-DE' => 'Beispiel Datum',
],
]
]
]
]];
$foundation->updateCustomFields($data, 'moorl_fb');
}
private function getTemplateFile($name, $locale, $extension)
{
$file = __DIR__ . '/../content/mail-templates/' . $name . '-' . $locale . '.' . $extension . '.twig';
if (file_exists($file)) {
return file_get_contents($file);
}
return 'Template file not found: ' . $file;
}
public function install(InstallContext $context): void
{
parent::install($context);
$this->refreshPluginData($context->getContext());
}
public function uninstall(UninstallContext $context): void
{
parent::uninstall($context);
if ($context->keepUserData()) {
return;
}
$this->refreshPluginData($context->getContext(), true);
}
}