diff --git a/src/Application/Controller/Admin/AdminOrder.php b/src/Application/Controller/Admin/AdminOrder.php new file mode 100644 index 0000000..358b4c6 --- /dev/null +++ b/src/Application/Controller/Admin/AdminOrder.php @@ -0,0 +1,119 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\Linkmobility4OXID\Application\Controller\Admin; + +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; +use D3\Linkmobility4OXID\Application\Model\OrderRecipients; +use D3\Linkmobility4OXID\Application\Model\Sms; +use D3\Linkmobility4OXID\Application\Model\UserRecipients; +use D3\LinkmobilityClient\ValueObject\Recipient; +use Exception; +use OxidEsales\Eshop\Application\Controller\Admin\AdminController; +use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Application\Model\Remark; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Registry; + +class AdminOrder extends AdminController +{ + protected $_sThisTemplate = 'd3adminorder.tpl'; + + /** + * @var Sms + */ + protected $sms; + + /** + * @var Order + */ + protected $order; + + public function __construct() + { + $this->order = $order = oxNew(Order::class); + $order->load($this->getEditObjectId()); + + $this->sms = oxNew(Sms::class); + + $this->addTplParam('recipient', $this->getRecipientFromCurrentOrder()); + + parent::__construct(); + } + + /** + * @return Recipient|false + */ + public function getRecipientFromCurrentOrder() + { + try { + return oxNew( OrderRecipients::class, $this->order )->getSmsRecipient(); + } catch (noRecipientFoundException $e) { + Registry::getUtilsView()->addErrorToDisplay( + Registry::getLang()->translateString($e->getMessage()) + ); + } + return false; + } + + /** + * @throws Exception + */ + public function send() + { + $messageBody = Registry::getRequest()->getRequestEscapedParameter( 'messagebody' ); + + if (strlen($messageBody) <= 1) { + Registry::getUtilsView()->addErrorToDisplay( + Registry::getLang()->translateString('D3LM_EXC_MESSAGE_NO_LENGTH') + ); + return; + } + + $order = oxNew(Order::class); + $order->load($this->getEditObjectId()); + + $sms = oxNew(Sms::class); + if ($sms->sendOrderMessage($order, $messageBody)) { + $this->setRemark( $messageBody ); + Registry::getUtilsView()->addErrorToDisplay( + sprintf( + Registry::getLang()->translateString('D3LM_EXC_SMS_SUCC_SENT'), + $sms->getResponse()->getSmsCount() + ) + ); + } else { + Registry::getUtilsView()->addErrorToDisplay( + Registry::getLang()->translateString('D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND') + ); + } + } + + /** + * @param $messageBody + * + * @throws Exception + */ + protected function setRemark( $messageBody ) + { + $remark = oxNew( Remark::class ); + $remark->assign( [ + 'oxtype' => 'LMSMS', + 'oxparentid' => $this->getEditObjectId(), + 'oxtext' => $messageBody + ] ); + $remark->save(); + } +} \ No newline at end of file diff --git a/src/Application/Model/OrderRecipients.php b/src/Application/Model/OrderRecipients.php new file mode 100644 index 0000000..c3daf1f --- /dev/null +++ b/src/Application/Model/OrderRecipients.php @@ -0,0 +1,67 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\Linkmobility4OXID\Application\Model; + +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; +use D3\LinkmobilityClient\ValueObject\Recipient; +use OxidEsales\Eshop\Application\Model\Country; +use OxidEsales\Eshop\Application\Model\Order; +use OxidEsales\Eshop\Application\Model\User; + +class OrderRecipients +{ + /** + * @var Order + */ + private $order; + + public function __construct(Order $order) + { + $this->order = $order; + } + + /** + * @return Recipient + * @throws noRecipientFoundException + */ + public function getSmsRecipient(): Recipient + { + foreach ($this->getSmsRecipientFields() as $fieldName) + { + $content = trim($this->order->getFieldData($fieldName)); + if (strlen($content)) { + + $country = oxNew(Country::class); + $country->load($this->order->getUser()->getFieldData('oxcountryid')); + + return oxNew(Recipient::class, $content, $country->getFieldData('oxisoalpha2')); + } + } + + throw oxNew(noRecipientFoundException::class); + } + + /** + * @return string[] + */ + public function getSmsRecipientFields(): array + { + return [ + 'oxdelfon', + 'oxbillfon' + ]; + } +} \ No newline at end of file diff --git a/src/Application/Model/Sms.php b/src/Application/Model/Sms.php index 2b6b63c..515207d 100644 --- a/src/Application/Model/Sms.php +++ b/src/Application/Model/Sms.php @@ -23,6 +23,7 @@ use D3\LinkmobilityClient\Request\RequestInterface; use D3\LinkmobilityClient\Response\ResponseInterface; use D3\LinkmobilityClient\ValueObject\Sender; use GuzzleHttp\Exception\GuzzleException; +use OxidEsales\Eshop\Application\Model\Order; use OxidEsales\Eshop\Application\Model\User; use OxidEsales\Eshop\Core\Registry; @@ -51,6 +52,27 @@ class Sms return false; } + /** + * @param Order $order + * @param $message + * + * @return bool + */ + public function sendOrderMessage(Order $order, $message): bool + { + try { + return $this->sendCustomRecipientMessage( + [ oxNew( OrderRecipients::class, $order )->getSmsRecipient() ], + $message + ); + } catch (noRecipientFoundException $e) { + Registry::getLogger()->warning($e->getMessage()); + Registry::getUtilsView()->addErrorToDisplay($e); + } + + return false; + } + /** * @param array $recipientsArray * @param $message diff --git a/src/metadata.php b/src/metadata.php index 3a81fdc..d3c3650 100644 --- a/src/metadata.php +++ b/src/metadata.php @@ -3,6 +3,7 @@ * Metadata version */ +use D3\Linkmobility4OXID\Application\Controller\Admin\AdminOrder; use D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser; $sMetadataVersion = '2.1'; @@ -29,10 +30,12 @@ $aModule = [ \OxidEsales\Eshop\Application\Controller\ContactController::class => \D3\Linkmobility4OXID\Modules\Application\Controller\ContactController::class ], 'controllers' => [ - 'd3linkmobility_user' => AdminUser::class + 'd3linkmobility_user' => AdminUser::class, + 'd3linkmobility_order' => AdminOrder::class ], 'templates' => [ - 'd3adminuser.tpl' => 'd3/linkmobility/Application/views/admin/tpl/adminuser.tpl' + 'd3adminuser.tpl' => 'd3/linkmobility/Application/views/admin/tpl/adminuser.tpl', + 'd3adminorder.tpl' => 'd3/linkmobility/Application/views/admin/tpl/adminuser.tpl' ], 'events' => [], 'settings' => [