init
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerMessage\Controller\Thread;
|
||||
|
||||
use Esun\CustomerMessage\Model\ConversationService;
|
||||
use Magento\Customer\Controller\AbstractAccount;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\Data\Form\FormKey\Validator;
|
||||
|
||||
class Send extends AbstractAccount implements HttpPostActionInterface
|
||||
{
|
||||
public function __construct(
|
||||
Context $context,
|
||||
private readonly Session $session,
|
||||
private readonly Validator $formKey,
|
||||
private readonly ConversationService $conversations
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$redirect = $this->resultRedirectFactory->create();
|
||||
$orderId = (int)$this->getRequest()->getParam('order_id');
|
||||
if (!$this->formKey->validate($this->getRequest())) {
|
||||
$this->messageManager->addErrorMessage(__('Your session has expired. Please try again.'));
|
||||
return $redirect->setPath($orderId ? 'customer-message/order/contact' : 'customer-message/account/index', $orderId ? ['order_id' => $orderId] : []);
|
||||
}
|
||||
try {
|
||||
$threadId = $this->conversations->sendAsCustomer(
|
||||
(int)$this->session->getCustomerId(),
|
||||
(int)$this->session->getCustomer()->getStoreId(),
|
||||
(string)$this->getRequest()->getParam('content'),
|
||||
(array)$this->getRequest()->getFiles('images', []),
|
||||
(int)$this->getRequest()->getParam('thread_id'),
|
||||
$orderId,
|
||||
(string)$this->getRequest()->getParam('subject')
|
||||
);
|
||||
$this->messageManager->addSuccessMessage(__('Your message has been sent.'));
|
||||
return $orderId
|
||||
? $redirect->setPath('customer-message/order/contact', ['order_id' => $orderId])
|
||||
: $redirect->setPath('customer-message/account/index', ['thread_id' => $threadId]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->messageManager->addErrorMessage($exception instanceof \Magento\Framework\Exception\LocalizedException ? $exception->getMessage() : __('The message could not be sent.'));
|
||||
return $redirect->setPath($orderId ? 'customer-message/order/contact' : 'customer-message/account/index', $orderId ? ['order_id' => $orderId] : []);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user