<?php declare(strict_types=1);
namespace K3n\K3nAgrilityConnector;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class K3nAgrilityConnector extends Plugin
{
public const AGRILITY_SUPPORT_EMAIL_TEMPLATE_ID = 'a9bbbc03f58938c74da782f000727bbb';
public const AGRILITY_SUPPORT_EMAIL_TEMPLATE = [
'id' => '3f048d0978003735ab7d14a471e3fc2f',
'name' => 'Agrility support',
'technicalName' => 'agrility_support',
'availableEntities' => ['order' => 'order', 'salesChannel' => 'sales_channel'],
'translations' => [
'de-DE' => [
'name' => 'Agrility-Support-E-Mail',
],
'en-GB' => [
'name' => 'Agrility support email',
],
],
];
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->createCustomerCustomFields($installContext);
$this->createMailTemplate($installContext);
$this->setAgrilitySupportEmailField();
}
private function createCustomerCustomFields(InstallContext $context): void
{
$context->getContext()->scope(Context::SYSTEM_SCOPE, function (Context $context): void {
/**
* @var EntityRepositoryInterface $customFieldSetRepository
*/
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->upsert($this->getCustomFields(), $context);
});
}
private function createMailTemplate(InstallContext $context): void
{
$template = $this->getAgrilityMailTemplate();
$context->getContext()->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($template): void {
try {
$mailTemplateRepository = $this->container->get('mail_template.repository');
$mailTemplateRepository->create($template, $context);
} catch (\Exception $e) {
}
});
}
public function setAgrilitySupportEmailField()
{
$config = $this->container->get(SystemConfigService::class);
$config->set('K3nAgrilityConnector.config.agrilitySupportEmailTemplate', self::AGRILITY_SUPPORT_EMAIL_TEMPLATE_ID);
}
private function getCustomFields()
{
return [
[
'id' => 'd9ee93f3664a6d02b27065eec61afef3',
'name' => 'agrility',
'active' => true,
'config' => [
'label' => [
'en-GB' => 'Agrility',
'de-DE' => 'Agrility',
],
],
'customFields' => [
[
'id' => '46b73c7cb526e4e8a65b476ad58199c5',
'name' => 'agrility_farmer_id',
'active' => true,
'type' => CustomFieldTypes::INT,
'config' => [
'label' => [
'en-GB' => 'Agrility FarmID',
'de-DE' => 'Agrility FarmID',
],
],
],
[
'id' => 'a2a1f1b634a4062e30b03410ae3dc9ca',
'name' => 'agrility_creation_date',
'active' => true,
'type' => CustomFieldTypes::DATETIME,
'config' => [
'label' => [
'en-GB' => 'Agrility Created At',
'de-DE' => 'Agrility Created At',
],
],
],
],
'relations' => [
[
'id' => 'a6e4a78b7ba265ca4eec577b87d42095',
'entityName' => 'customer',
],
],
],
];
}
private function getAgrilityMailTemplate()
{
return [
[
'id' => self::AGRILITY_SUPPORT_EMAIL_TEMPLATE_ID,
'systemDefault' => false,
'mailTemplateType' => self::AGRILITY_SUPPORT_EMAIL_TEMPLATE,
'description' => 'Agrility support email',
'subject' => 'Report problem with Agrility API',
'senderName' => '{{ salesChannel.name }}',
'contentHtml' => '<div>
<br/>
<p>
Hello Admin, <br/>
<br/>
This message is automatically generated.
<br/>
An error occurred while using the Agrilty API.
<br/>
Please check.
<br/>
Error details:
<br/>
</p>
<p>
<code>
{{ errorDetail }}
</code>
</p>
</div>',
'contentPlain' => 'Dear,
This message is automatically generated.
An error occurred while using the Agrilty API.
Please check.
Error details:
{{ errorDetail }}
',
'translations' => [
'de-DE' => [
'description' => 'Agrility-Support-E-Mail',
'senderName' => '{{ salesChannel.name }}',
'subject' => 'Problem mit der Agrility-API melden',
'contentHtml' => '<div>
<br/>
<p>
Hallo Admin, <br/>
<br/>
Diese Nachricht wird automatisch generiert.
<br/>
Bei der Verwendung der Agrilty-API ist ein Fehler aufgetreten.
<br/>
Fehlerdetails:
<br/>
</p>
<p>
<code>
{{ errorDetail }}
</code>
</p>
</div>',
'contentPlain' => 'Hallo Admin,
Diese Nachricht wird automatisch generiert.
Bei der Verwendung der Agrilty-API ist ein Fehler aufgetreten.
Fehlerdetails:
{{ errorDetail }}',
],
'en-GB' => [
'description' => 'Agrility support email',
'senderName' => '{{ salesChannel.name }}',
'subject' => 'Report problem with Agrility API',
'contentHtml' => '<div>
<br/>
<p>
Hello Admin, <br/>
<br/>
This message is automatically generated.
<br/>
An error occurred while using the Agrilty API.
<br/>
Please check.
<br/>
Error details:
<br/>
</p>
<p>
<code>
{{ errorDetail }}
</code>
</p>
</div>',
'contentPlain' => 'Hello Admin,
This message is automatically generated.
An error occurred while using the Agrilty API.
Please check.
Error details:
{{ errorDetail }}',
],
],
],
];
}
}