45 lines
1.9 KiB
PHP
45 lines
1.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\CustomerMessage\Controller\Adminhtml\Thread;
|
|
|
|
use Esun\CustomerMessage\Model\ConversationService;
|
|
use Magento\Backend\App\Action;
|
|
use Magento\Backend\App\Action\Context;
|
|
use Magento\Framework\App\Action\HttpPostActionInterface;
|
|
use Magento\Framework\Data\Form\FormKey\Validator;
|
|
|
|
class Reply extends Action implements HttpPostActionInterface
|
|
{
|
|
public const ADMIN_RESOURCE = 'Esun_CustomerMessage::messages';
|
|
|
|
public function __construct(Context $context, private readonly Validator $formKey, private readonly ConversationService $conversations)
|
|
{
|
|
parent::__construct($context);
|
|
}
|
|
|
|
public function execute()
|
|
{
|
|
$threadId = (int)$this->getRequest()->getParam('thread_id');
|
|
$orderId = (int)$this->getRequest()->getParam('order_id');
|
|
$redirect = $this->resultRedirectFactory->create();
|
|
if (!$this->formKey->validate($this->getRequest())) {
|
|
$this->messageManager->addErrorMessage(__('Your session has expired. Please try again.'));
|
|
return $redirect->setPath('esun_customer_message/message/index', $threadId ? ['thread_id' => $threadId] : ['order_id' => $orderId]);
|
|
}
|
|
try {
|
|
$threadId = $this->conversations->sendAsAdmin(
|
|
(int)$this->_auth->getUser()->getId(),
|
|
(string)$this->getRequest()->getParam('content'),
|
|
(array)$this->getRequest()->getFiles('images', []),
|
|
$threadId,
|
|
$orderId
|
|
);
|
|
$this->messageManager->addSuccessMessage(__('The reply has been sent.'));
|
|
} catch (\Throwable $exception) {
|
|
$this->messageManager->addErrorMessage($exception instanceof \Magento\Framework\Exception\LocalizedException ? $exception->getMessage() : __('The reply could not be sent.'));
|
|
}
|
|
return $redirect->setPath('esun_customer_message/message/index', ['thread_id' => $threadId]);
|
|
}
|
|
}
|