From 22a5b2a25c68fd95030a5137569e22afc29901c2 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 9 Aug 2022 13:48:58 +0200 Subject: [PATCH] move message content generator to Order Manager plugin --- .../d3linkmobility_ordermanager_action.php | 4 +- .../Model/Exceptions/emptyMesageException.php | 27 -- .../Model/MessageContentGenerator.php | 319 ------------------ .../d3linkmobility_ordermanager_sender.php | 7 +- 4 files changed, 7 insertions(+), 350 deletions(-) delete mode 100644 src/Application/Model/Exceptions/emptyMesageException.php delete mode 100644 src/Application/Model/MessageContentGenerator.php diff --git a/src/Application/Model/Actions/d3linkmobility_ordermanager_action.php b/src/Application/Model/Actions/d3linkmobility_ordermanager_action.php index 0c70a47..807d93c 100644 --- a/src/Application/Model/Actions/d3linkmobility_ordermanager_action.php +++ b/src/Application/Model/Actions/d3linkmobility_ordermanager_action.php @@ -16,12 +16,12 @@ declare(strict_types=1); namespace D3\Linkmobility4Ordermanager\Application\Model\Actions; use D3\Linkmobility4Ordermanager\Application\Model\d3linkmobility_ordermanager_sender; -use D3\Linkmobility4Ordermanager\Application\Model\Exceptions\emptyMesageException; use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception; use D3\ModCfg\Application\Model\Exception\d3ParameterNotFoundException; use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException; use D3\Ordermanager\Application\Model\Actions\d3ordermanager_action_abstract; +use D3\Ordermanager\Application\Model\Exceptions\emptyMesageException; use D3\Ordermanager\Application\Model\d3ordermanager_conf; use D3\Ordermanager\Application\Model\Exceptions\d3ActionRequirementAbstract; use D3\Ordermanager\Application\Model\Exceptions\d3ordermanager_actionException; @@ -204,6 +204,8 @@ class d3linkmobility_ordermanager_action extends d3ordermanager_action_abstract } /** + * @param bool $blExpected + * * @return bool */ protected function hasRequiredValuesRecipient(bool $blExpected): bool diff --git a/src/Application/Model/Exceptions/emptyMesageException.php b/src/Application/Model/Exceptions/emptyMesageException.php deleted file mode 100644 index 693611d..0000000 --- a/src/Application/Model/Exceptions/emptyMesageException.php +++ /dev/null @@ -1,27 +0,0 @@ - - * @link https://www.oxidmodule.com - */ - -declare(strict_types=1); - -namespace D3\Linkmobility4Ordermanager\Application\Model\Exceptions; - -use OxidEsales\Eshop\Core\Exception\StandardException; - -class emptyMesageException extends StandardException -{ - public function __construct($sMessage = "not set", $iCode = 0, \Exception $previous = null) - { - $sMessage = 'empty message content in task '.$sMessage; - parent::__construct($sMessage, $iCode, $previous); - } -} diff --git a/src/Application/Model/MessageContentGenerator.php b/src/Application/Model/MessageContentGenerator.php deleted file mode 100644 index c2d7edd..0000000 --- a/src/Application/Model/MessageContentGenerator.php +++ /dev/null @@ -1,319 +0,0 @@ -setManager($manager); - $this->prepareItem($item); - $this->setItem($item); - } - - /** - * @param $tplName - * @return string - * @throws emptyMesageException - * @throws Exception - */ - public function generateFromTpl($tplName): string - { - [$iCurrentTplLang, $iCurrentBaseLang, $iCurrentCurrency] = $this->preGenerate(); - - $mail = $this->getMailer(); - $engine = $this->getTemplateEngine($mail); - $content = $engine->render($tplName); - - return $this->postGenerate($content, $iCurrentTplLang, $iCurrentBaseLang, $iCurrentCurrency); - } - - /** - * @param string $cmsIdent - * @return string - * @throws emptyMesageException - * @throws Exception - */ - public function generateFromCms(string $cmsIdent): string - { - [$iCurrentTplLang, $iCurrentBaseLang, $iCurrentCurrency] = $this->preGenerate(); - - $mail = $this->getMailer(); - $engine = $this->getTemplateEngine($mail); - - $oUtilsView = Registry::getUtilsView(); - $oContent = oxNew(Content::class); - $oContent->loadInLang( - $this->getItem()->getFieldData('oxlang'), - $cmsIdent - ); - - $content = $oUtilsView->getRenderedContent( - $oContent->getFieldData('oxcontent'), - $engine->getGlobals(), - $oContent->getId() . 'oxcontent' - ); - - return $this->postGenerate($content, $iCurrentTplLang, $iCurrentBaseLang, $iCurrentCurrency); - } - - /** - * @return array - * @throws Exception - */ - public function preGenerate(): array - { - Registry::getConfig()->setAdminMode($this->templateFrom === self::TEMPLATE_FROM_ADMIN); - $iOrderLangId = $this->getItem()->getFieldData('oxlang'); - $oLang = Registry::getLang(); - /** @var int $iCurrentTplLang */ - $iCurrentTplLang = $oLang->getTplLanguage(); - /** @var int $iCurrentBaseLang */ - $iCurrentBaseLang = $oLang->getBaseLanguage(); - $oLang->setTplLanguage($iOrderLangId); - $oLang->setBaseLanguage($iOrderLangId); - - /** @var int $iCurrentCurrency */ - $iCurrentCurrency = Registry::getConfig()->getShopCurrency(); - /** @var stdClass $oOrderCurr */ - $oOrderCurr = $this->getItem()->getOrderCurrency(); - $iOrderCurr = $oOrderCurr->id; - Registry::getConfig()->setActShopCurrency($iOrderCurr); - - set_error_handler( - [d3GetModCfgDIC()->get(d3ordermanager_renderererrorhandler::class), 'd3HandleTemplateEngineErrors'] - ); - - return array($iCurrentTplLang, $iCurrentBaseLang, $iCurrentCurrency); - } - - /** - * @param string $content - * @param int $iCurrentTplLang - * @param int $iCurrentBaseLang - * @param int $iCurrentCurrency - * @return string - */ - public function postGenerate(string $content, int $iCurrentTplLang, int $iCurrentBaseLang, int $iCurrentCurrency): string - { - restore_error_handler(); - - $oLang = Registry::getLang(); - $oLang->setTplLanguage($iCurrentTplLang); - $oLang->setBaseLanguage($iCurrentBaseLang); - Registry::getConfig()->setActShopCurrency($iCurrentCurrency); - - Registry::getConfig()->setAdminMode(true); - - if (false === (bool) strlen($content)) { - throw oxNew(emptyMesageException::class, 'message content is empty', $this->getManager()->getFieldData('oxtitle')); - } - - return $content; - } - - /** - * @param Item $item - * @return void - * @throws ArticleException - * @throws ArticleInputException - */ - protected function prepareItem(Item $item) - { - /** @var Basket $oBasket */ - $oBasket = $item->d3getOrderBasket4OrderManager($this->getManager()); - - $oPayment = oxNew(Payment::class); - $oPayment->loadInLang($item->getFieldData('oxlang'), $oBasket->getPaymentId()); - - $item->d3setBasket4OrderManager($oBasket); - $item->d3setPayment4OrderManager($oPayment); - } - - /** - * @param string $from - * @param string|null $moduleId - * @return void - */ - public function setTemplateFrom(string $from, string $moduleId = null) - { - switch ($from) { - case self::TEMPLATE_FROM_ADMIN: - $this->templateFrom = self::TEMPLATE_FROM_ADMIN; - return; - case self::TEMPLATE_FROM_FRONTEND: - $this->templateFrom = self::TEMPLATE_FROM_FRONTEND; - return; - case self::TEMPLATE_FROM_MODULE: - if (is_null($moduleId)) throw oxNew(InvalidArgumentException::class, 'missing module'); - $this->templateFrom = self::TEMPLATE_FROM_MODULE; - $this->tplModuleId = $moduleId; - return; - } - - throw oxNew(InvalidArgumentException::class, 'unknown template source'); - } - - /** - * @param Email $mail - * @return TemplateEngineInterface - */ - protected function getTemplateEngine(Email $mail): TemplateEngineInterface - { - /** @var TemplateRendererInterface $renderer */ - $renderer = ContainerFactory::getInstance()->getContainer() - ->get(TemplateRendererBridgeInterface::class) - ->getTemplateRenderer(); - $engine = $renderer->getTemplateEngine(); - - // ToDo: check in TWIG and change to a generic solution (e.g. path names in template name) - // Smarty only - if (method_exists($engine, '__set')) { - $engine->__set('template_dir', $this->getTemplateDir4OrderManager()); - } - - foreach ($mail->getViewData() as $id => $value) { - $engine->addGlobal($id, $value); - } - - return $engine; - } - - /** - * @return string - */ - public function getTemplateDir4OrderManager(): string - { - if ($this->templateFrom === self::TEMPLATE_FROM_MODULE) { - /** @var ModulePathResolverBridgeInterface $pathResolverBridge */ - $pathResolverBridge = ContainerFactory::getInstance()->getContainer()->get(ModulePathResolverBridgeInterface::class); - $sModulePath = $pathResolverBridge->getFullModulePathFromConfiguration( - $this->tplModuleId, - Registry::getConfig()->getShopId() - ); - $sPath = (oxNew(d3str::class))->untrailingslashit($sModulePath); - } else { - $blAdmin = $this->templateFrom === self::TEMPLATE_FROM_ADMIN; - $sPath = Registry::getConfig()->getTemplateDir($blAdmin); - } - return $sPath; - } - - /** - * @return d3_oxemail_ordermanager - */ - public function getMailer(): d3_oxemail_ordermanager - { - $mail = oxNew(Email::class); - - $this->setViewData($mail); - - return $mail; - } - - /** - * @param Email $mail - * @return void - */ - public function setViewData(Email $mail) - { - $oShop = $this->getShop(); - - $mail->setViewData("oShop", $oShop); - $mail->setViewData("oViewConf", (oxNew(BaseController::class))->getViewConfig()); - $mail->setViewData("oOrder", $this->getItem()); - $mail->setViewData("oUser", $this->getItem()->getOrderUser()); - $mail->setViewData("shopTemplateDir", Registry::getConfig()->getTemplateDir()); - $mail->setViewData("charset", Registry::getLang()->translateString("charset")); - - $mail->setViewData("shop", $oShop); - $mail->setViewData("order", $this->getItem()); - $mail->setViewData("user", $this->getItem()->getOrderUser()); - $mail->setViewData("payment", $this->getItem()->d3getPayment4OrderManager()); - $mail->setViewData("oDelSet", $this->getItem()->getDelSet()); - $mail->setViewData("currency", $this->getItem()->getOrderCurrency()); - $mail->setViewData("basket", $this->getItem()->d3getBasket4OrderManager()); - $mail->setViewData("oEmailView", $mail); - } - - /** - * @return Shop - */ - public function getShop(): Shop - { - return Registry::getConfig()->getActiveShop(); - } - - /** - * @return Item - */ - public function getItem(): Item - { - return $this->item; - } - - /** - * @param Item $item - */ - public function setItem(Item $item): void - { - $this->item = $item; - } - - /** - * @return Manager - */ - public function getManager(): Manager - { - return $this->manager; - } - - /** - * @param Manager $manager - */ - public function setManager(Manager $manager): void - { - $this->manager = $manager; - } -} \ No newline at end of file diff --git a/src/Application/Model/d3linkmobility_ordermanager_sender.php b/src/Application/Model/d3linkmobility_ordermanager_sender.php index d22bd29..1df97d7 100644 --- a/src/Application/Model/d3linkmobility_ordermanager_sender.php +++ b/src/Application/Model/d3linkmobility_ordermanager_sender.php @@ -15,7 +15,6 @@ declare(strict_types=1); namespace D3\Linkmobility4Ordermanager\Application\Model; -use D3\Linkmobility4Ordermanager\Application\Model\Exceptions\emptyMesageException; use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; use D3\Linkmobility4OXID\Application\Model\MessageTypes\Sms; use D3\Linkmobility4OXID\Application\Model\OrderRecipients; @@ -24,6 +23,8 @@ use D3\LinkmobilityClient\ValueObject\Recipient; use D3\ModCfg\Application\Model\Exception\d3ParameterNotFoundException; use D3\Ordermanager\Application\Model\d3ordermanager; use D3\Ordermanager\Application\Model\d3ordermanager as Manager; +use D3\Ordermanager\Application\Model\Exceptions\emptyMesageException; +use D3\Ordermanager\Application\Model\MessageContentGenerator; use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Core\Exception\ArticleException; use OxidEsales\Eshop\Core\Exception\ArticleInputException; @@ -90,7 +91,7 @@ class d3linkmobility_ordermanager_sender protected function getRecipients(): array { $recipients = []; - if ((bool) $this->getManager()->getValue('blLinkMobilityMessageToCustomer')) { + if ($this->getManager()->getValue('blLinkMobilityMessageToCustomer')) { try { $recipients[] = (oxNew(OrderRecipients::class, $this->getItem()))->getSmsRecipient(); } catch (noRecipientFoundException $e) { @@ -99,7 +100,7 @@ class d3linkmobility_ordermanager_sender $this->getManager()->getRemarkHandler()->addNote($note); } } - if ((bool) $this->getManager()->getValue('blLinkMobilityMessageToCustom') && + if ($this->getManager()->getValue('blLinkMobilityMessageToCustom') && strlen(trim($this->getManager()->getValue('sLinkMobilityMessageToCustomAddress'))) ) { foreach ($this->extractCustomAddresses() as $phoneNumber => $countryId) {