From 8ecd1171322a18adff9ea76ee167213adbcaffc7 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sun, 7 Aug 2022 00:45:00 +0200 Subject: [PATCH] initial --- .../d3linkmobility_ordermanager_action.php | 76 ++++++- .../Model/Exceptions/emptyMesageException.php | 27 +++ .../d3linkmobility_ordermanager_sender.php | 202 +++++++++++++++++- .../de/d3_linkmobility_ordermanager_lang.php | 13 +- .../d3ordermanager_conf_linkmobility.php | 2 +- src/metadata.php | 2 +- 6 files changed, 297 insertions(+), 25 deletions(-) rename src/Application/Model/{ => Actions}/d3linkmobility_ordermanager_action.php (71%) create mode 100644 src/Application/Model/Exceptions/emptyMesageException.php diff --git a/src/Application/Model/d3linkmobility_ordermanager_action.php b/src/Application/Model/Actions/d3linkmobility_ordermanager_action.php similarity index 71% rename from src/Application/Model/d3linkmobility_ordermanager_action.php rename to src/Application/Model/Actions/d3linkmobility_ordermanager_action.php index 940f470..bd1f62c 100644 --- a/src/Application/Model/d3linkmobility_ordermanager_action.php +++ b/src/Application/Model/Actions/d3linkmobility_ordermanager_action.php @@ -13,8 +13,11 @@ declare(strict_types=1); -namespace D3\Linkmobility4Ordermanager\Application\Model; +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\d3ShopCompatibilityAdapterException; use D3\Ordermanager\Application\Model\Actions\d3ordermanager_action_abstract; @@ -25,6 +28,8 @@ use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; use OxidEsales\Eshop\Core\Exception\DatabaseException; use OxidEsales\Eshop\Core\Exception\StandardException; +use OxidEsales\Eshop\Core\Language; +use OxidEsales\Eshop\Core\Registry; class d3linkmobility_ordermanager_action extends d3ordermanager_action_abstract { @@ -56,8 +61,11 @@ class d3linkmobility_ordermanager_action extends d3ordermanager_action_abstract return 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ERR_NOVALIDSOURCE'; } elseif ($this->hasRequiredValuesTplSource(false)) { return 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ERR_NOVALIDTPL'; - } else { + } elseif ($this->hasRequiredValuesCmsSource(false)) { return 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ERR_NOVALIDCMS'; + } else { + + return 'foo'; } } @@ -74,21 +82,18 @@ class d3linkmobility_ordermanager_action extends d3ordermanager_action_abstract if (false == $this->isExecutable()) { return; } - $this->throwUnvalidConfigurationException(); startProfile(__METHOD__); /** @var Language $oLang */ - $oLang = d3GetModCfgDIC()->get('d3ox.ordermanager.'.Language::class); - + $oLang = oxNew(Language::class); $this->getManager()->getRemarkHandler()->addNote( sprintf( $oLang->translateString('D3_ORDERMANAGER_JOBDESC_SENDLMMESSAGE', null, true), $this->getRecipientDescription() ) ); - $this->startExecution(); stopProfile(__METHOD__); @@ -115,6 +120,33 @@ class d3linkmobility_ordermanager_action extends d3ordermanager_action_abstract return implode(', ', $aMailDesc); } + /** + * @return bool + */ + public function hasRequiredValues(): bool + { + return $this->hasRequiredValuesNoSource(true) && + ( + $this->hasRequiredValuesTplSource(true) || + $this->hasRequiredValuesCmsSource(true) + ) && $this->hasRequiredValuesRecipient(); + } + + /** + * @param bool $blExpected + * + * @return bool + */ + protected function hasRequiredValuesNoSource(bool $blExpected): bool + { + $source = (string) $this->getManager()->getValue( 'sLinkMobilityMessageFromSource' ); + + $return = strlen(trim($source)) && + in_array(trim($source), [self::SOURCE_CMS, self::SOURCE_TEMPLATE]); + + return $blExpected ? $return : false === $return; + } + /** * @param bool $blExpected * @@ -166,6 +198,21 @@ class d3linkmobility_ordermanager_action extends d3ordermanager_action_abstract } } + /** + * @return bool + */ + protected function hasRequiredValuesRecipient(): bool + { + $toCust = (bool) $this->getManager()->getValue( 'blLinkMobilityMessageToCustomer' ); + $toCustom = (bool) $this->getManager()->getValue( 'blLinkMobilityMessageToCustom' ); + $toCustomAddress = (string) $this->getManager()->getValue( 'sLinkMobilityMessageToCustomAddress' ); + + return $toCust || ( + $toCustom && + (bool) strlen( trim( $toCustomAddress ) ) + ); + } + /** * @return bool * @throws DBALException @@ -188,15 +235,26 @@ class d3linkmobility_ordermanager_action extends d3ordermanager_action_abstract */ public function startExecution() { - if ($this->canExecuteMethod() && $this->hasRequiredValues()) { - $this->getSendClass()->sendOrderManagerSms($this->getManager(), $this->getItem()); + try { + if ($this->canExecuteMethod() && $this->hasRequiredValues()) { + dumpvar(__METHOD__ . __LINE__ . PHP_EOL); + $this->getSendClass()->sendOrderManagerSms($this->getManager(), $this->getItem()); + } + } catch (emptyMesageException $e) { + Registry::getLogger()->error($e->getMessage()); + } catch (noRecipientFoundException $e) { + Registry::getLogger()->info( + $this->getManager()->getFieldData('oxtitle')." => ". + $this->getItem()->getFieldData('oxordernr').": ". + $e->getMessage() + ); } } /** * @return d3linkmobility_ordermanager_sender */ - public function getSendClass() + public function getSendClass(): d3linkmobility_ordermanager_sender { /** @var d3linkmobility_ordermanager_sender $mailer */ $sender = oxNew(d3linkmobility_ordermanager_sender::class); diff --git a/src/Application/Model/Exceptions/emptyMesageException.php b/src/Application/Model/Exceptions/emptyMesageException.php new file mode 100644 index 0000000..7cacf32 --- /dev/null +++ b/src/Application/Model/Exceptions/emptyMesageException.php @@ -0,0 +1,27 @@ + + * @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); + } +} \ 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 33281dc..85a7e07 100644 --- a/src/Application/Model/d3linkmobility_ordermanager_sender.php +++ b/src/Application/Model/d3linkmobility_ordermanager_sender.php @@ -15,11 +15,30 @@ 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; use D3\LinkmobilityClient\ValueObject\Recipient; +use D3\ModCfg\Application\Model\d3str; +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\d3ordermanager_renderererrorhandler; +use D3\OxidServiceBridges\Internal\Framework\Module\Path\ModulePathResolverBridgeInterface; +use OxidEsales\Eshop\Application\Model\Basket; +use OxidEsales\Eshop\Application\Model\Content; use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Application\Model\Payment; +use OxidEsales\Eshop\Core\Email; +use OxidEsales\Eshop\Core\Exception\ArticleException; +use OxidEsales\Eshop\Core\Exception\ArticleInputException; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\EshopCommunity\Core\Controller\BaseController; +use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; +use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateEngineInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererBridgeInterface; +use OxidEsales\EshopCommunity\Internal\Framework\Templating\TemplateRendererInterface; class d3linkmobility_ordermanager_sender { @@ -29,43 +48,206 @@ class d3linkmobility_ordermanager_sender /** @var Order */ protected $item; + /** + * @param Manager $manager + * @param Order $item + * @return void + * @throws ArticleException + * @throws ArticleInputException + * @throws noRecipientFoundException + * @throws d3ParameterNotFoundException + * @throws emptyMesageException + */ public function sendOrderManagerSms(d3ordermanager $manager, Order $item) { $this->setManager($manager); $this->setItem($item); - +dumpvar(__METHOD__.__LINE__.PHP_EOL); + dumpvar($this->getMessageBody()); + dumpvar($this->getRecipients()); +die(); $sms = oxNew(Sms::class, $this->getMessageBody()); $sms->sendCustomRecipientMessage($this->getRecipients()); } /** * @return string + * @throws ArticleException + * @throws ArticleInputException + * @throws d3ParameterNotFoundException + * @throws emptyMesageException */ protected function getMessageBody(): string { - return ''; + $oManager = $this->getManager(); + + $viewData = []; + + $blTplFromAdmin = $oManager->getValue('sLinkMobilityMessageFromTheme') == 'admin'; + + $oConfig = Registry::getConfig(); + $oConfig->setAdminMode($blTplFromAdmin); + + /** @var TemplateRendererInterface $renderer */ + $renderer = ContainerFactory::getInstance()->getContainer() + ->get(TemplateRendererBridgeInterface::class) + ->getTemplateRenderer(); + $templateEngine = $renderer->getTemplateEngine(); + + /** @var $oBasket Basket */ + $oBasket = $oManager->getCurrentItem()->d3getOrderBasket4OrderManager($oManager); + + $oPayment = oxNew(Payment::class); + $oPayment->loadInLang($oManager->getCurrentItem()->getFieldData('oxlang'), $oBasket->getPaymentId()); + + $oManager->getCurrentItem()->d3setBasket4OrderManager($oBasket); + $oManager->getCurrentItem()->d3setPayment4OrderManager($oPayment); + + $oShop = Registry::getConfig()->getActiveShop(); + + $viewData["oShop"] = $oShop; + $viewData["oViewConf"] = (oxNew(BaseController::class))->getViewConfig(); + $viewData["oOrder"] = $oManager->getCurrentItem(); + $viewData["oUser"] = $oManager->getCurrentItem()->getOrderUser(); + $viewData["shopTemplateDir"] = $oConfig->getTemplateDir(false); + $viewData["charset"] = Registry::getLang()->translateString("charset"); + + $viewData["shop"] = $oShop; + $viewData["order"] = $oManager->getCurrentItem(); + $viewData["user"] = $oManager->getCurrentItem()->getOrderUser(); + $viewData["payment"] = $oPayment; + $viewData["oDelSet"] = $oManager->getCurrentItem()->getDelSet(); + $viewData["currency"] = $oManager->getCurrentItem()->getOrderCurrency(); + $viewData["basket"] = $oBasket; + $viewData["oEmailView"] = oxNew(Email::class); + + // ToDo: check in TWIG and change to a generic solution (e.g. path names in template name) + // Smarty only + if (method_exists($templateEngine, '__set')) { + $templateEngine->__set( 'template_dir', $this->getTemplateDir4OrderManager( $oManager ) ); + } + + foreach ($viewData as $id => $value) { + $templateEngine->addGlobal($id, $value); + } + + $content = $this->_d3GenerateOrderManagerMessageContent($templateEngine); + $oConfig->setAdminMode(true); + + return $content; + } + + /** + * @param TemplateEngineInterface $templateEngine + * @return string + * @throws d3ParameterNotFoundException + * @throws emptyMesageException + */ + protected function _d3GenerateOrderManagerMessageContent(TemplateEngineInterface $templateEngine) + { + $iOrderLangId = $this->getManager()->getCurrentItem()->getFieldData('oxlang'); + $oLang = Registry::getLang(); + $iCurrentTplLang = $oLang->getTplLanguage(); + $iCurrentBaseLang = $oLang->getBaseLanguage(); + $oLang->setTplLanguage($iOrderLangId); + $oLang->setBaseLanguage($iOrderLangId); + + $iCurrentCurrency = Registry::getConfig()->getShopCurrency(); + $iOrderCurr = $this->getManager()->getCurrentItem()->getOrderCurrency()->id; + Registry::getConfig()->setActShopCurrency($iOrderCurr); + + set_error_handler( + [d3GetModCfgDIC()->get(d3ordermanager_renderererrorhandler::class), 'd3HandleTemplateEngineErrors'] + ); + + if ($this->getManager()->getValue('sLinkMobilityMessageFromSource') == 'cms') { + $oUtilsView = Registry::getUtilsView(); + $oContent = oxNew(Content::class); + $oContent->loadInLang($iOrderLangId, $this->getManager()->getValue('sLinkMobilityMessageFromContentname')); + + $content = $oUtilsView->getRenderedContent( + $oContent->getFieldData('oxcontent'), + $templateEngine->getGlobals(), + $oContent->getId() . 'oxcontent' + ); + } elseif ($this->getManager()->getValue('sLinkMobilityMessageFromSource') == 'template') { + $content = $templateEngine->render($this->getManager()->getValue('sLinkMobilityMessageFromTemplatename')); + } + + if (false === is_string($content) || false === (bool) strlen($content)) { + throw oxNew(emptyMesageException::class, 'message content is empty', $this->getManager()->getFieldData('oxtitle')); + } + + restore_error_handler(); + + $oLang->setTplLanguage($iCurrentTplLang); + $oLang->setBaseLanguage($iCurrentBaseLang); + Registry::getConfig()->setActShopCurrency($iCurrentCurrency); + + return $content; + } + + /** + * @param Manager $oManager + * @return string + */ + public function getTemplateDir4OrderManager( Manager $oManager ): string + { + if ($oManager->getValue('sLinkMobilityMessageFromTheme') == 'module') { + $sModuleId = $oManager->getValue('sLinkMobilityMessageFromModulePath'); + /** @var ModulePathResolverBridgeInterface $pathResolverBridge */ + $pathResolverBridge = ContainerFactory::getInstance()->getContainer()->get(ModulePathResolverBridgeInterface::class); + $sModulePath = $pathResolverBridge->getFullModulePathFromConfiguration( + $sModuleId, + Registry::getConfig()->getShopId() + ); + $sPath = (oxNew(d3str::class))->untrailingslashit($sModulePath); + } else { + $blAdmin = $oManager->getValue('sLinkMobilityMessageFromTheme') == 'admin'; + $sPath = Registry::getConfig()->getTemplateDir($blAdmin); + } + return $sPath; } /** * @return array - * @throws \D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException - * @throws \D3\ModCfg\Application\Model\Exception\d3ParameterNotFoundException + * @throws noRecipientFoundException */ protected function getRecipients(): array { - $aEditedValues = $this->getManager()->getEditedValues(); - $recipients = []; - if ($aEditedValues && $aEditedValues['blLinkMobilityMessageToCustomer']) { - $recipients[] = oxNew(OrderRecipients::class, $this->getItem())->getSmsRecipient(); + if ((bool) $this->getManager()->getValue('blLinkMobilityMessageToCustomer')) { + $recipients[] = (oxNew(OrderRecipients::class, $this->getItem()))->getSmsRecipient(); } - if ($aEditedValues && $aEditedValues['blLinkMobilityMessageToCustom']) { - $recipients[] = oxNew(Recipient::class, 'number', 'DE'); + if ((bool) $this->getManager()->getValue('blLinkMobilityMessageToCustom') && + strlen(trim($this->getManager()->getValue('sLinkMobilityMessageToCustomAddress'))) + ) { + foreach ($this->extractCustomAddresses() as $phoneNumber => $countryId) { + $recipients[] = oxNew(Recipient::class, $phoneNumber, $countryId); + } } return $recipients; } + /** + * @return array + */ + protected function extractCustomAddresses(): array + { + $addresses = []; + $customAddresses = trim($this->getManager()->getValue('sLinkMobilityMessageToCustomAddress')); + + if (strlen($customAddresses)) { + foreach (explode(';', $customAddresses) as $addressGroups) { + list($phoneNumber, $countryId) = explode('@', trim($addressGroups)); + $addresses[trim($phoneNumber)] = trim($countryId); + } + } + + return $addresses; + } + /** * @return Order */ diff --git a/src/Application/views/admin/de/d3_linkmobility_ordermanager_lang.php b/src/Application/views/admin/de/d3_linkmobility_ordermanager_lang.php index df72e5c..b7a8f78 100644 --- a/src/Application/views/admin/de/d3_linkmobility_ordermanager_lang.php +++ b/src/Application/views/admin/de/d3_linkmobility_ordermanager_lang.php @@ -20,12 +20,11 @@ $sLangName = 'Deutsch'; $aLang = array( 'charset' => 'UTF-8', - 'D3_LINKMOBILITY_ORDERMANAGER_ACTION' => 'Mobile Messages (über LINK Mobility) senden', + 'D3_LINKMOBILITY_ORDERMANAGER_ACTION' => 'SMS senden (via LINK Mobility)', - 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ToUseLMSettings' => 'Der Nachrichtenempfänger wird aus den Kundendaten an den Bestellungen ermittelt.', - 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ToUseLMSettings_DESC' => 'Welche Daten der Bestellung hierfür verwendet werden, setzen Sie in den Einstellungen des LINK Mobility Moduls unter "Erweiterungen -> Module -> Einstell.".', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ToUseLMSettings' => '', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ToUseLMSettings_DESC' => '', - 'D3_ORDERMANAGER_ACTION_MAILSEND' => 'E-Mail senden', 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_FROM1' => '', 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_FROMSUBJECT' => 'Betreff-Template', 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_FROMCMS_SUBJECT' => 'Der CMS-Titel ist gleichzeitig der Mail-Betreff.', @@ -38,6 +37,12 @@ $aLang = array( 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_FROMTEMPLATE_DESC' => 'Geben Sie den vollständigen Templatenamen (inkl. Ordner ab tpl-Ordner und Dateiendung) an', 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_FROMCMS' => 'aus Kundeninformation', 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_FROMCMS_SOURCE' => 'CMS-Eintrag', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_FROM2' => 'an', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_TOCUSTOMER' => 'Kunde', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_TOCUSTOMER_DESC' => 'Der Nachrichtenempfänger wird aus den Kundendaten an den Bestellungen ermittelt. Welche Daten der Bestellung hierfür verwendet werden, setzen Sie in den Einstellungen des LINK Mobility Moduls unter "Erweiterungen -> Module -> Einstell.".', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_TOOWNER' => 'Shopbetreiber', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_TOMAIL' => 'folgende Mobilfunknummer(n)', + 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_TOMAIL_DESC' => '

Geben Sie in dem Eingabefeld eine oder mehrere gültige Mobilfunknummern und deren Herkunftsland in folgendem Format an und aktivieren Sie die Option mit dem Häkchenfeld:

017112345678@DE; 015212345678@AT', 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ERR_NOVALIDSOURCE' => 'keine gültige Inhaltsquelle gesetzt', 'D3_ORDERMANAGER_ACTION_LINKMOBILITYMESSAGE_ERR_NOVALIDTPL' => 'keine gültigen Templatedaten gesetzt', diff --git a/src/Modules/Ordermanager/Application/Model/d3ordermanager_conf_linkmobility.php b/src/Modules/Ordermanager/Application/Model/d3ordermanager_conf_linkmobility.php index 218f51b..8030035 100644 --- a/src/Modules/Ordermanager/Application/Model/d3ordermanager_conf_linkmobility.php +++ b/src/Modules/Ordermanager/Application/Model/d3ordermanager_conf_linkmobility.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace D3\Linkmobility4Ordermanager\Modules\Ordermanager\Application\Model; -use D3\Linkmobility4Ordermanager\Application\Model\d3linkmobility_ordermanager_action; +use D3\Linkmobility4Ordermanager\Application\Model\Actions\d3linkmobility_ordermanager_action; use OxidEsales\Eshop\Core\Registry; use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory; use OxidEsales\EshopCommunity\Internal\Framework\Module\State\ModuleStateServiceInterface; diff --git a/src/metadata.php b/src/metadata.php index 7e3ba67..ea1dbe4 100644 --- a/src/metadata.php +++ b/src/metadata.php @@ -43,6 +43,6 @@ $aModule = [ 'd3linkmobility_ordermanager_action.tpl' => 'd3/linkmobility4ordermanager/Application/views/admin/tpl/d3linkmobility_ordermanager_action.tpl' ], 'events' => [], - 'blocks' => [], + 'blocks' => [], 'settings' => [] ];