From 7db6fce4017fefaa61efa5d64629a19c7e0d3b1b Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 4 Jul 2022 09:13:36 +0200 Subject: [PATCH] add backend controller for users --- .../Controller/Admin/AdminUser.php | 117 ++++++++++++++++++ .../Exceptions/noRecipientFoundException.php | 10 +- src/Application/Model/Sms.php | 41 +++++- .../views/admin/de/d3Linkmobility_lang.php | 13 +- src/Application/views/admin/tpl/adminuser.tpl | 51 ++++++++ .../Application/Model/RequestFactory.php | 38 ++++++ src/menu.xml | 10 ++ src/metadata.php | 15 ++- 8 files changed, 286 insertions(+), 9 deletions(-) create mode 100644 src/Application/Controller/Admin/AdminUser.php create mode 100644 src/Application/views/admin/tpl/adminuser.tpl create mode 100644 src/Modules/Application/Model/RequestFactory.php create mode 100644 src/menu.xml diff --git a/src/Application/Controller/Admin/AdminUser.php b/src/Application/Controller/Admin/AdminUser.php new file mode 100644 index 0000000..d586d04 --- /dev/null +++ b/src/Application/Controller/Admin/AdminUser.php @@ -0,0 +1,117 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\Linkmobility4OXID\Application\Controller\Admin; + +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; +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\Remark; +use OxidEsales\Eshop\Application\Model\User; +use OxidEsales\Eshop\Core\Registry; + +class AdminUser extends AdminController +{ + protected $_sThisTemplate = 'd3adminuser.tpl'; + + /** + * @var Sms + */ + protected $sms; + + /** + * @var User + */ + protected $user; + + public function __construct() + { + $this->user = $user = oxNew(User::class); + $user->load($this->getEditObjectId()); + + $this->sms = oxNew(Sms::class); + + $this->addTplParam('recipient', $this->getRecipientFromCurrentUser()); + + parent::__construct(); + } + + /** + * @return Recipient|false + */ + public function getRecipientFromCurrentUser() + { + try { + return oxNew( UserRecipients::class, $this->user )->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; + } + + $user = oxNew(User::class); + $user->load($this->getEditObjectId()); + + $sms = oxNew(Sms::class); + if ($sms->sendUserAccountMessage($user, $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/Exceptions/noRecipientFoundException.php b/src/Application/Model/Exceptions/noRecipientFoundException.php index f5d8147..e6f906e 100644 --- a/src/Application/Model/Exceptions/noRecipientFoundException.php +++ b/src/Application/Model/Exceptions/noRecipientFoundException.php @@ -15,7 +15,13 @@ namespace D3\Linkmobility4OXID\Application\Model\Exceptions; -class noRecipientFoundException implements abortSendingExceptionInterface -{ +use Exception; +use OxidEsales\Eshop\Core\Exception\StandardException; +class noRecipientFoundException extends StandardException implements abortSendingExceptionInterface +{ + public function __construct( $sMessage = "D3LM_EXC_NO_RECIPIENT_SET", $iCode = 0, Exception $previous = null ) + { + parent::__construct( $sMessage, $iCode, $previous ); + } } \ No newline at end of file diff --git a/src/Application/Model/Sms.php b/src/Application/Model/Sms.php index eced071..2b6b63c 100644 --- a/src/Application/Model/Sms.php +++ b/src/Application/Model/Sms.php @@ -16,9 +16,11 @@ namespace D3\Linkmobility4OXID\Application\Model; use D3\Linkmobility4OXID\Application\Model\Exceptions\abortSendingExceptionInterface; +use D3\Linkmobility4OXID\Application\Model\Exceptions\noRecipientFoundException; +use D3\Linkmobility4OXID\Modules\Application\Model\RequestFactory; use D3\LinkmobilityClient\Exceptions\ApiException; use D3\LinkmobilityClient\Request\RequestInterface; -use D3\LinkmobilityClient\SMS\RequestFactory; +use D3\LinkmobilityClient\Response\ResponseInterface; use D3\LinkmobilityClient\ValueObject\Sender; use GuzzleHttp\Exception\GuzzleException; use OxidEsales\Eshop\Application\Model\User; @@ -26,6 +28,8 @@ use OxidEsales\Eshop\Core\Registry; class Sms { + private $response; + /** * @param User $user * @param $message @@ -33,6 +37,27 @@ class Sms * @return bool */ public function sendUserAccountMessage(User $user, $message): bool + { + try { + return $this->sendCustomRecipientMessage( + [ oxNew( UserRecipients::class, $user )->getSmsRecipient() ], + $message + ); + } catch (noRecipientFoundException $e) { + Registry::getLogger()->warning($e->getMessage()); + Registry::getUtilsView()->addErrorToDisplay($e); + } + + return false; + } + + /** + * @param array $recipientsArray + * @param $message + * + * @return bool + */ + public function sendCustomRecipientMessage(array $recipientsArray, $message): bool { try { $configuration = oxNew( Configuration::class ); @@ -42,9 +67,13 @@ class Sms $request->setTestMode( $configuration->getTestMode() )->setMethod( RequestInterface::METHOD_POST )->setSenderAddress( oxNew( Sender::class, $configuration->getSmsSenderNumber(), $configuration->getSmsSenderCountry() ) )->setSenderAddressType( RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL ); $recipientsList = $request->getRecipientsList(); - $recipientsList->add(oxNew( UserRecipients::class, $user )->getSmsRecipient()); + foreach ($recipientsArray as $recipient) { + $recipientsList->add( $recipient ); + } $response = $client->request( $request ); + $this->response = $response; + return $response->isSuccessful(); } catch (abortSendingExceptionInterface $e) { Registry::getLogger()->warning($e->getMessage()); @@ -62,4 +91,12 @@ class Sms return false; } + + /** + * @return ResponseInterface|null + */ + public function getResponse() + { + return $this->response; + } } \ No newline at end of file diff --git a/src/Application/views/admin/de/d3Linkmobility_lang.php b/src/Application/views/admin/de/d3Linkmobility_lang.php index 2116900..6035d1a 100644 --- a/src/Application/views/admin/de/d3Linkmobility_lang.php +++ b/src/Application/views/admin/de/d3Linkmobility_lang.php @@ -24,5 +24,16 @@ $aLang = [ 'SHOP_MODULE_GROUP_d3linkmobility_general' => 'Grundeinstellungen', 'SHOP_MODULE_d3linkmobility_debug' => 'Debug-Modus', - 'SHOP_MODULE_d3linkmobility_apitoken' => 'API-Token' + 'SHOP_MODULE_d3linkmobility_apitoken' => 'API-Token', + + 'D3LM_ADMIN_USER_RECIPIENT' => 'Empfängernummer', + 'D3LM_ADMIN_USER_MESSAGE' => 'Nachricht', + 'D3LM_ADMIN_SEND' => 'versenden', + + 'D3LM_EXC_MESSAGE_NO_LENGTH' => 'Die Mitteilung muss Inhalt haben', + 'D3LM_EXC_SMS_SUCC_SENT' => 'Die Mitteilung wurde erfolgreich versendet. (%1$s Nachricht(en) verwendet)', + 'D3LM_EXC_MESSAGE_UNEXPECTED_ERR_SEND' => 'Beim Versenden der Nachricht(en) ist ein unerwarteter Fehler aufgetreten.', + 'D3LM_EXC_NO_RECIPIENT_SET' => 'Kein (verwendbarer) Empfänger gesetzt.', + + 'tbcluser_linkmobility' => 'SMS-Versand' ]; diff --git a/src/Application/views/admin/tpl/adminuser.tpl b/src/Application/views/admin/tpl/adminuser.tpl new file mode 100644 index 0000000..5a68cb9 --- /dev/null +++ b/src/Application/views/admin/tpl/adminuser.tpl @@ -0,0 +1,51 @@ +[{include file="headitem.tpl" title="GENERAL_ADMIN_TITLE"|oxmultilangassign}] + +
+ [{$oViewConf->getHiddenSid()}] + + +
+ +[{if $recipient}] +
+ [{$oViewConf->getHiddenSid()}] + + + + + + + + + +
+ + + + + + + + + + + + + +
+ + + +
+ + + +
+
+ +
+
+
+[{/if}] + +[{include file="bottomitem.tpl"}] \ No newline at end of file diff --git a/src/Modules/Application/Model/RequestFactory.php b/src/Modules/Application/Model/RequestFactory.php new file mode 100644 index 0000000..acb992b --- /dev/null +++ b/src/Modules/Application/Model/RequestFactory.php @@ -0,0 +1,38 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\Linkmobility4OXID\Modules\Application\Model; + +use D3\Linkmobility4OXID\Application\Model\Configuration; +use D3\LinkmobilityClient\Request\RequestInterface; +use D3\LinkmobilityClient\SMS\SmsRequestInterface; +use D3\LinkmobilityClient\ValueObject\Sender; + +class RequestFactory extends \D3\LinkmobilityClient\SMS\RequestFactory +{ + public function getSmsRequest(): SmsRequestInterface + { + $configuration = oxNew( Configuration::class ); + + $request = parent::getSmsRequest(); + $request->setTestMode($configuration->getTestMode()) + ->setSenderAddress( + oxNew( Sender::class, $configuration->getSmsSenderNumber(), $configuration->getSmsSenderCountry() ) + ) + ->setSenderAddressType( RequestInterface::SENDERADDRESSTYPE_INTERNATIONAL ); + + return $request; + } +} \ No newline at end of file diff --git a/src/menu.xml b/src/menu.xml new file mode 100644 index 0000000..dee132b --- /dev/null +++ b/src/menu.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/src/metadata.php b/src/metadata.php index 75d8982..3a81fdc 100644 --- a/src/metadata.php +++ b/src/metadata.php @@ -2,9 +2,12 @@ /** * Metadata version */ + +use D3\Linkmobility4OXID\Application\Controller\Admin\AdminUser; + $sMetadataVersion = '2.1'; -$sModuleId = 'd3linkmobility'; -$sD3Logo = '(D3) '; +$sModuleId = 'd3linkmobility'; +$sD3Logo = '(D3) '; /** * Module information @@ -25,8 +28,12 @@ $aModule = [ \OxidEsales\Eshop\Application\Controller\StartController::class => \D3\Linkmobility4OXID\Modules\Application\Controller\StartController::class, \OxidEsales\Eshop\Application\Controller\ContactController::class => \D3\Linkmobility4OXID\Modules\Application\Controller\ContactController::class ], - 'controllers' => [], - 'templates' => [], + 'controllers' => [ + 'd3linkmobility_user' => AdminUser::class + ], + 'templates' => [ + 'd3adminuser.tpl' => 'd3/linkmobility/Application/views/admin/tpl/adminuser.tpl' + ], 'events' => [], 'settings' => [ [