Created
December 16, 2019 13:14
-
-
Save jkrzefski/7a6cf3b75cbef67c5a24c316b6d6e3a3 to your computer and use it in GitHub Desktop.
Workaround for broken mail context in Shopware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" ?> | |
<container xmlns="http://symfony.com/schema/dic/services" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | |
<!-- Add some generic project services here --> | |
<services> | |
<service id="App\EventListener\FixBackendMailModule"> | |
<tag name="shopware.event_subscriber"/> | |
<argument type="service" id="models"/> | |
</service> | |
<service id="App\EventListener\TemplateMailFactory"> | |
<tag name="shopware.event_subscriber"/> | |
</service> | |
</services> | |
</container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\Components; | |
use Doctrine\DBAL\Types\ConversionException; | |
use Enlight_Components_Mail; | |
use Shopware\Components\Model\ModelManager; | |
use Shopware\Models\Mail\Mail; | |
use Shopware_Components_StringCompiler; | |
use Shopware_Components_TemplateMail; | |
class TemplateMail extends Shopware_Components_TemplateMail | |
{ | |
/** | |
* @var Shopware_Components_TemplateMail | |
*/ | |
private $templateMail; | |
public function __construct(Shopware_Components_TemplateMail $templateMail) | |
{ | |
$this->templateMail = $templateMail; | |
} | |
public function createMail($mailModel, $context = [], $shop = null, $overrideConfig = []) | |
{ | |
try { | |
return $this->templateMail->createMail($mailModel, $context, $shop, $overrideConfig); | |
} catch (ConversionException $exception) { | |
$this->resetMailContext((string) $mailModel); | |
return $this->templateMail->createMail($mailModel, $context, $shop, $overrideConfig); | |
} | |
} | |
public function setModelManager(ModelManager $modelManager) | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function getModelManager() | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function setShop($shop) | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function getShop() | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function getTranslationReader() | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function setTranslationReader($translationReader) | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function setStringCompiler(Shopware_Components_StringCompiler $stringCompiler) | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function getStringCompiler() | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
public function loadValues(Enlight_Components_Mail $mail, Mail $mailModel, $overrideConfig = []) | |
{ | |
return call_user_func_array([$this->templateMail, __FUNCTION__], func_get_args()); | |
} | |
private function resetMailContext(string $mailModel): void | |
{ | |
$this->templateMail->getModelManager()->getConnection()->update('s_core_config_mails', [ | |
'context' => null, | |
], [ | |
'name' => $mailModel, | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\EventListener; | |
use Doctrine\DBAL\Types\ConversionException; | |
use Doctrine\ORM\OptimisticLockException; | |
use Doctrine\ORM\ORMException; | |
use Doctrine\ORM\TransactionRequiredException; | |
use Enlight\Event\SubscriberInterface; | |
use PDO; | |
use Shopware\Components\Model\ModelManager; | |
use Shopware\Models\Mail\Mail; | |
class FixBackendMailModule implements SubscriberInterface | |
{ | |
/** | |
* @var ModelManager | |
*/ | |
private $modelManager; | |
public function __construct(ModelManager $modelManager) | |
{ | |
$this->modelManager = $modelManager; | |
} | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
'Enlight_Controller_Action_Backend_Mail_getMails' => 'fix', | |
]; | |
} | |
public function fix() | |
{ | |
try { | |
$this->modelManager->getRepository(Mail::class)->findAll(); | |
} catch (ConversionException $exception) { | |
$mailIds = $this->getMailIds(); | |
foreach ($mailIds as $mailId) { | |
try { | |
$this->modelManager->find(Mail::class, (int) $mailId); | |
} catch (ConversionException $exception) { | |
$this->resetMailContext((int) $mailId); | |
} catch (OptimisticLockException | TransactionRequiredException | ORMException $exception) { | |
continue; | |
} | |
} | |
} | |
} | |
private function getMailIds(): array | |
{ | |
return (array) $this->modelManager->getConnection()->createQueryBuilder() | |
->select('id') | |
->from('s_core_config_mails') | |
->execute() | |
->fetchAll(PDO::FETCH_COLUMN) | |
; | |
} | |
private function resetMailContext(int $id) | |
{ | |
$this->modelManager->getConnection()->update('s_core_config_mails', [ | |
'context' => null, | |
], [ | |
'id' => $id, | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
namespace App\EventListener; | |
use App\Components\TemplateMail; | |
use Enlight\Event\SubscriberInterface; | |
use Enlight_Event_EventArgs; | |
use Shopware\Components\DependencyInjection\Container; | |
class TemplateMailFactory implements SubscriberInterface | |
{ | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
'Enlight_Bootstrap_AfterInitResource_templatemail' => 'factory', | |
]; | |
} | |
public function factory(Enlight_Event_EventArgs $args) | |
{ | |
/** @var Container $container */ | |
$container = $args->getSubject(); | |
$service = $container->get('templatemail'); | |
$container->set('templatemail', new TemplateMail($service)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment