init
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\CustomerMessage\Controller\Adminhtml\Attachment;
|
||||
|
||||
use Esun\CustomerMessage\Model\AttachmentStorage;
|
||||
use Esun\CustomerMessage\Model\ConversationService;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\Result\RawFactory;
|
||||
|
||||
class View extends Action implements HttpGetActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_CustomerMessage::messages';
|
||||
|
||||
public function __construct(Context $context, private readonly ConversationService $conversations, private readonly AttachmentStorage $storage, private readonly RawFactory $rawFactory)
|
||||
{
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$result = $this->rawFactory->create();
|
||||
try {
|
||||
$attachment = $this->conversations->getAttachmentForAdmin((int)$this->getRequest()->getParam('id'));
|
||||
if (!$attachment) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(__('The requested image is not available.'));
|
||||
}
|
||||
$filename = str_replace(["\r", "\n"], '', (string)$attachment['original_name']);
|
||||
return $result->setHeader('Content-Type', (string)$attachment['mime_type'], true)
|
||||
->setHeader('Content-Length', (string)$attachment['file_size'], true)
|
||||
->setHeader('Content-Disposition', 'inline; filename="' . addcslashes($filename, '"\\') . '"', true)
|
||||
->setHeader('X-Content-Type-Options', 'nosniff', true)
|
||||
->setContents((string)file_get_contents($this->storage->getAbsolutePath((string)$attachment['storage_path'])));
|
||||
} catch (\Throwable) {
|
||||
return $result->setHttpResponseCode(404)->setContents('Not found');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace Esun\CustomerMessage\Controller\Adminhtml\Message;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
|
||||
class Delete extends Action implements HttpPostActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE='Esun_CustomerMessage::messages';
|
||||
public function __construct(Action\Context $context, private readonly ResourceConnection $resource)
|
||||
{
|
||||
parent::__construct($context);
|
||||
} public function execute()
|
||||
{
|
||||
$id=(int)$this->getRequest()->getParam('message_id');
|
||||
if ($id) {
|
||||
$this->resource->getConnection()->delete($this->resource->getTableName('esun_customer_message'), ['message_id=?'=>$id]);
|
||||
$this->messageManager->addSuccessMessage(__('The message was deleted.'));
|
||||
|
||||
} return $this->resultRedirectFactory->create()->setPath('*/*/index');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace Esun\CustomerMessage\Controller\Adminhtml\Message;
|
||||
|
||||
use Esun\CustomerMessage\Model\ConversationService;
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Backend\App\Action\Context;
|
||||
use Magento\Framework\App\Action\HttpGetActionInterface;
|
||||
use Magento\Framework\Controller\ResultFactory;
|
||||
|
||||
class Index extends Action implements HttpGetActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE='Esun_CustomerMessage::messages';
|
||||
public function __construct(Context $context, private readonly ConversationService $conversations)
|
||||
{
|
||||
parent::__construct($context);
|
||||
}
|
||||
public function execute()
|
||||
{
|
||||
$threadId = (int)$this->getRequest()->getParam('thread_id');
|
||||
if (!$threadId) {
|
||||
$orderId = (int)$this->getRequest()->getParam('order_id');
|
||||
$thread = $orderId
|
||||
? $this->conversations->getOrderThreadForAdmin($orderId)
|
||||
: null;
|
||||
$threadId = $thread ? (int)$thread['thread_id'] : 0;
|
||||
}
|
||||
|
||||
if ($threadId && $this->conversations->getThreadForAdmin($threadId)) {
|
||||
$this->conversations->markAdminRead($threadId);
|
||||
}
|
||||
|
||||
$page = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
|
||||
$page->setActiveMenu(self::ADMIN_RESOURCE);
|
||||
$page->getConfig()->getTitle()->prepend(__('Customer Messages'));
|
||||
return $page;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace Esun\CustomerMessage\Controller\Adminhtml\Message;
|
||||
|
||||
use Magento\Backend\App\Action;
|
||||
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory;
|
||||
use Magento\Framework\App\Action\HttpPostActionInterface;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
|
||||
class Save extends Action implements HttpPostActionInterface
|
||||
{
|
||||
public const ADMIN_RESOURCE = 'Esun_CustomerMessage::messages';
|
||||
|
||||
public function __construct(
|
||||
Action\Context $context,
|
||||
private readonly ResourceConnection $resource,
|
||||
private readonly CollectionFactory $customers
|
||||
) {
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$result = $this->resultRedirectFactory->create()->setPath('*/*/index');
|
||||
try {
|
||||
$title = trim((string)$this->getRequest()->getParam('title'));
|
||||
$content = trim((string)$this->getRequest()->getParam('content'));
|
||||
$rawEmails = preg_split('/[\s,;]+/', (string)$this->getRequest()->getParam('customer_emails'));
|
||||
$emails = array_values(array_unique(array_filter(array_map('trim', $rawEmails ?: []))));
|
||||
if ($title === '' || $content === '' || !$emails) {
|
||||
throw new LocalizedException(__('Title, content and at least one customer email are required.'));
|
||||
}
|
||||
$customerIds = $this->customers->create()->addFieldToFilter('email', ['in' => $emails])->getAllIds();
|
||||
if (!$customerIds) {
|
||||
throw new LocalizedException(__('No matching customers were found.'));
|
||||
}
|
||||
$connection = $this->resource->getConnection();
|
||||
$connection->beginTransaction();
|
||||
$connection->insert($this->resource->getTableName('esun_customer_message'), [
|
||||
'title' => $title,
|
||||
'content' => $content,
|
||||
'message_type' => (string)$this->getRequest()->getParam('message_type', 'notice'),
|
||||
'store_id' => (int)$this->getRequest()->getParam('store_id', 0),
|
||||
'is_active' => 1,
|
||||
'expires_at' => trim((string)$this->getRequest()->getParam('expires_at')) ?: null,
|
||||
]);
|
||||
$messageId = (int)$connection->lastInsertId();
|
||||
foreach ($customerIds as $customerId) {
|
||||
$connection->insert($this->resource->getTableName('esun_customer_message_recipient'), [
|
||||
'message_id' => $messageId,
|
||||
'customer_id' => (int)$customerId,
|
||||
]);
|
||||
}
|
||||
$connection->commit();
|
||||
$this->messageManager->addSuccessMessage(__('The message was sent to %1 customer(s).', count($customerIds)));
|
||||
} catch (\Throwable $e) {
|
||||
if ($this->resource->getConnection()->getTransactionLevel() > 0) {
|
||||
$this->resource->getConnection()->rollBack();
|
||||
}
|
||||
$this->messageManager->addErrorMessage(
|
||||
$e instanceof LocalizedException ? $e->getMessage() : __('The message could not be saved.')
|
||||
);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user