custom/static-plugins/K3nAgrilityConnector/src/K3nAgrilityConnector.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace K3n\K3nAgrilityConnector;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  7. use Shopware\Core\System\CustomField\CustomFieldTypes;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. class K3nAgrilityConnector extends Plugin
  10. {
  11.     public const AGRILITY_SUPPORT_EMAIL_TEMPLATE_ID 'a9bbbc03f58938c74da782f000727bbb';
  12.     public const AGRILITY_SUPPORT_EMAIL_TEMPLATE    = [
  13.         'id'                => '3f048d0978003735ab7d14a471e3fc2f',
  14.         'name'              => 'Agrility support',
  15.         'technicalName'     => 'agrility_support',
  16.         'availableEntities' => ['order' => 'order''salesChannel' => 'sales_channel'],
  17.         'translations'      => [
  18.             'de-DE' => [
  19.                 'name' => 'Agrility-Support-E-Mail',
  20.             ],
  21.             'en-GB' => [
  22.                 'name' => 'Agrility support email',
  23.             ],
  24.         ],
  25.     ];
  26.     public function install(InstallContext $installContext): void
  27.     {
  28.         parent::install($installContext);
  29.         $this->createCustomerCustomFields($installContext);
  30.         $this->createMailTemplate($installContext);
  31.         $this->setAgrilitySupportEmailField();
  32.     }
  33.     private function createCustomerCustomFields(InstallContext $context): void
  34.     {
  35.         $context->getContext()->scope(Context::SYSTEM_SCOPE, function (Context $context): void {
  36.             /**
  37.              * @var EntityRepositoryInterface $customFieldSetRepository
  38.              */
  39.             $customFieldSetRepository $this->container->get('custom_field_set.repository');
  40.             $customFieldSetRepository->upsert($this->getCustomFields(), $context);
  41.         });
  42.     }
  43.     private function createMailTemplate(InstallContext $context): void
  44.     {
  45.         $template $this->getAgrilityMailTemplate();
  46.         $context->getContext()->scope(Context::SYSTEM_SCOPE, function (Context $context) use ($template): void {
  47.             try {
  48.                 $mailTemplateRepository $this->container->get('mail_template.repository');
  49.                 $mailTemplateRepository->create($template$context);
  50.             } catch (\Exception $e) {
  51.             }
  52.         });
  53.     }
  54.     public function setAgrilitySupportEmailField()
  55.     {
  56.         $config $this->container->get(SystemConfigService::class);
  57.         $config->set('K3nAgrilityConnector.config.agrilitySupportEmailTemplate'self::AGRILITY_SUPPORT_EMAIL_TEMPLATE_ID);
  58.     }
  59.     private function getCustomFields()
  60.     {
  61.         return [
  62.             [
  63.                 'id'     => 'd9ee93f3664a6d02b27065eec61afef3',
  64.                 'name'   => 'agrility',
  65.                 'active' => true,
  66.                 'config' => [
  67.                     'label' => [
  68.                         'en-GB' => 'Agrility',
  69.                         'de-DE' => 'Agrility',
  70.                     ],
  71.                 ],
  72.                 'customFields' => [
  73.                     [
  74.                         'id'     => '46b73c7cb526e4e8a65b476ad58199c5',
  75.                         'name'   => 'agrility_farmer_id',
  76.                         'active' => true,
  77.                         'type'   => CustomFieldTypes::INT,
  78.                         'config' => [
  79.                             'label' => [
  80.                                 'en-GB' => 'Agrility FarmID',
  81.                                 'de-DE' => 'Agrility FarmID',
  82.                             ],
  83.                         ],
  84.                     ],
  85.                     [
  86.                         'id'     => 'a2a1f1b634a4062e30b03410ae3dc9ca',
  87.                         'name'   => 'agrility_creation_date',
  88.                         'active' => true,
  89.                         'type'   => CustomFieldTypes::DATETIME,
  90.                         'config' => [
  91.                             'label' => [
  92.                                 'en-GB' => 'Agrility Created At',
  93.                                 'de-DE' => 'Agrility Created At',
  94.                             ],
  95.                         ],
  96.                     ],
  97.                 ],
  98.                 'relations' => [
  99.                     [
  100.                         'id' => 'a6e4a78b7ba265ca4eec577b87d42095',
  101.                         'entityName' => 'customer',
  102.                     ],
  103.                 ],
  104.             ],
  105.         ];
  106.     }
  107.     private function getAgrilityMailTemplate()
  108.     {
  109.         return [
  110.             [
  111.                 'id'               => self::AGRILITY_SUPPORT_EMAIL_TEMPLATE_ID,
  112.                 'systemDefault'    => false,
  113.                 'mailTemplateType' => self::AGRILITY_SUPPORT_EMAIL_TEMPLATE,
  114.                 'description'      => 'Agrility support email',
  115.                 'subject'          => 'Report problem with Agrility API',
  116.                 'senderName'       => '{{ salesChannel.name }}',
  117.                 'contentHtml'      => '<div>
  118.                 <br/>
  119.                 <p>
  120.                     Hello Admin, <br/>
  121.                     <br/>
  122.                     This message is automatically generated.
  123.                     <br/>
  124.                     An error occurred while using the Agrilty API.
  125.                     <br/>
  126.                     Please check.
  127.                     <br/>
  128.                     Error details:
  129.                     <br/>
  130.                 </p>
  131.                 <p>
  132.                     <code>
  133.                         {{ errorDetail }}
  134.                     </code>
  135.                 </p>
  136.             </div>',
  137.                 'contentPlain' => 'Dear,
  138. This message is automatically generated.
  139. An error occurred while using the Agrilty API.
  140. Please check.
  141. Error details:
  142. {{ errorDetail }}
  143. ',
  144.                 'translations' => [
  145.                     'de-DE' => [
  146.                         'description' => 'Agrility-Support-E-Mail',
  147.                         'senderName'  => '{{ salesChannel.name }}',
  148.                         'subject'     => 'Problem mit der Agrility-API melden',
  149.                         'contentHtml' => '<div>
  150.                 <br/>
  151.                 <p>
  152.                     Hallo Admin, <br/>
  153.                     <br/>
  154.                     Diese Nachricht wird automatisch generiert.
  155.                     <br/>
  156.                     Bei der Verwendung der Agrilty-API ist ein Fehler aufgetreten.
  157.                     <br/>
  158.                     Fehlerdetails:
  159.                     <br/>
  160.                 </p>
  161.                 <p>
  162.                     <code>
  163.                         {{ errorDetail }}
  164.                     </code>
  165.                 </p>
  166.             </div>',
  167.                         'contentPlain' => 'Hallo Admin,
  168. Diese Nachricht wird automatisch generiert.
  169. Bei der Verwendung der Agrilty-API ist ein Fehler aufgetreten.
  170. Fehlerdetails:
  171. {{ errorDetail }}',
  172.                     ],
  173.                     'en-GB' => [
  174.                         'description' => 'Agrility support email',
  175.                         'senderName'  => '{{ salesChannel.name }}',
  176.                         'subject'     => 'Report problem with Agrility API',
  177.                         'contentHtml' => '<div>
  178.                 <br/>
  179.                 <p>
  180.                     Hello Admin, <br/>
  181.                     <br/>
  182.                     This message is automatically generated.
  183.                     <br/>
  184.                     An error occurred while using the Agrilty API.
  185.                     <br/>
  186.                     Please check.
  187.                     <br/>
  188.                     Error details:
  189.                     <br/>
  190.                 </p>
  191.                <p>
  192.                     <code>
  193.                         {{ errorDetail }}
  194.                     </code>
  195.                 </p>
  196.             </div>',
  197.                         'contentPlain' => 'Hello Admin,
  198. This message is automatically generated.
  199. An error occurred while using the Agrilty API.
  200. Please check.
  201. Error details:
  202. {{ errorDetail }}',
  203.                     ],
  204.                 ],
  205.             ],
  206.         ];
  207.     }
  208. }