linkmobility4oxid/src/Application/Controller/Admin/AdminOrder.php

120 regels
3.5 KiB
PHP

2022-07-04 13:28:58 +02:00
<?php
/**
2022-07-13 13:21:52 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
2022-07-04 13:28:58 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-07-13 13:21:52 +02:00
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link https://www.oxidmodule.com
2022-07-04 13:28:58 +02:00
*/
2022-07-13 13:21:52 +02:00
declare(strict_types=1);
2022-07-04 13:28:58 +02:00
namespace D3\Linkmobility4OXID\Application\Controller\Admin;
use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException;
use D3\Linkmobility4OXID\Application\Model\Exceptions\successfullySentException;
2022-07-04 13:28:58 +02:00
use D3\Linkmobility4OXID\Application\Model\OrderRecipients;
use D3\Linkmobility4OXID\Application\Model\Sms;
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\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->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());
2022-07-04 16:33:49 +02:00
try {
$sms = oxNew( Sms::class, $messageBody );
if ( $sms->sendOrderMessage( $order ) ) {
$this->setRemark( $sms->getRecipientsList(), $sms->getMessage() );
Registry::getUtilsView()->addErrorToDisplay(
oxNew(successfullySentException::class, $sms->getResponse()->getSmsCount() )
);
2022-07-04 16:33:49 +02:00
} else {
Registry::getUtilsView()->addErrorToDisplay(
sprintf(
Registry::getLang()->translateString( 'D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND' ),
$sms->getResponse()->getErrorMessage()
)
);
2022-07-04 16:33:49 +02:00
}
} catch (noRecipientFoundException $e) {
Registry::getUtilsView()->addErrorToDisplay($e);
2022-07-04 13:28:58 +02:00
}
}
/**
* @param $messageBody
*
* @throws Exception
*/
protected function setRemark( $recipients, $messageBody )
2022-07-04 13:28:58 +02:00
{
$remark = oxNew( Remark::class );
$remark->assign( [
'oxtype' => AdminUser::REMARK_IDENT,
'oxparentid' => $this->order->getUser()->getId(),
'oxtext' => $recipients.PHP_EOL.$messageBody
2022-07-04 13:28:58 +02:00
] );
$remark->save();
}
}