This commit is contained in:
2026-09-15 10:35:56 +08:00
commit 782f8ea7aa
4086 changed files with 631288 additions and 0 deletions
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerMessage\Block\Account;
use Esun\CustomerMessage\Model\MessageProvider;
use Esun\CustomerMessage\Model\ConversationService;
use Magento\Customer\Model\Session;
use Magento\Framework\View\Element\Template;
class Messages extends Template
{
private ?int $unreadCount = null;
public function __construct(Template\Context $context, private readonly Session $session, private readonly MessageProvider $provider, private readonly ConversationService $conversations, array $data = [])
{
parent::__construct($context, $data);
}
public function getMessages(): array
{
return $this->provider->getForCustomer((int)$this->session->getCustomerId(), (int)$this->_storeManager->getStore()->getId(), (int)($this->getData('limit') ?: 50));
}
public function getThreads(): array
{
return $this->conversations->getThreadsForCustomer((int)$this->session->getCustomerId(), (int)($this->getData('limit') ?: 100));
}
public function getUnreadCount(): int
{
if ($this->unreadCount === null) {
$customerId = (int)$this->session->getCustomerId();
$this->unreadCount = $customerId > 0
? $this->conversations->getCustomerUnreadTotal($customerId)
: 0;
}
return $this->unreadCount;
}
public function getSelectedThread(): ?array
{
if ((int)$this->getRequest()->getParam('new') === 1) {
return null;
}
$threadId = (int)$this->getRequest()->getParam('thread_id');
if ($threadId) {
return $this->conversations->getThreadForCustomer($threadId, (int)$this->session->getCustomerId());
}
$orderId = (int)$this->getRequest()->getParam('order_id');
if ($orderId) {
return $this->conversations->getOrderThreadForCustomer($orderId, (int)$this->session->getCustomerId());
}
$threads = $this->getThreads();
return $threads ? $this->conversations->getThreadForCustomer((int)$threads[0]['thread_id'], (int)$this->session->getCustomerId()) : null;
}
public function getConversationMessages(?array $thread): array
{
return $thread ? $this->conversations->getMessages((int)$thread['thread_id']) : [];
}
public function getOrderId(): int
{
return (int)$this->getRequest()->getParam('order_id');
}
public function getOrderIncrementId(): string
{
return (string)$this->getRequest()->getParam('order_increment_id');
}
}
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerMessage\Block\Account;
use Esun\CustomerMessage\Model\ConversationService;
use Magento\Customer\Block\Account\SortLink;
use Magento\Customer\Model\Session;
use Magento\Framework\App\DefaultPathInterface;
use Magento\Framework\View\Element\Template\Context;
class NavigationLink extends SortLink
{
private ?int $unreadCount = null;
public function __construct(Context $context, DefaultPathInterface $defaultPath, private readonly Session $session, private readonly ConversationService $conversations, array $data = [])
{
parent::__construct($context, $defaultPath, $data);
}
public function getUnreadCount(): int
{
if ($this->unreadCount === null) {
$customerId = (int)$this->session->getCustomerId();
$this->unreadCount = $customerId > 0
? $this->conversations->getCustomerUnreadTotal($customerId)
: 0;
}
return $this->unreadCount;
}
}
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Esun\CustomerMessage\Block\Adminhtml\Message;
use Esun\CustomerMessage\Model\ConversationService;
use Magento\Backend\Block\Template;
use Magento\Sales\Api\OrderRepositoryInterface;
class Index extends Template
{
public function __construct(Template\Context $context, private readonly ConversationService $conversations, private readonly OrderRepositoryInterface $orders, array $data = [])
{
parent::__construct($context, $data);
}
public function getThreads(): array
{
return $this->conversations->getThreadsForAdmin();
}
public function getSelectedThread(): ?array
{
$threadId = (int)$this->getRequest()->getParam('thread_id');
if ($threadId) {
return $this->conversations->getThreadForAdmin($threadId);
}
$orderId = (int)$this->getRequest()->getParam('order_id');
return $orderId ? $this->conversations->getOrderThreadForAdmin($orderId) : null;
}
public function getMessages(?array $thread): array
{
return $thread ? $this->conversations->getMessages((int)$thread['thread_id']) : [];
}
public function getOrder(): ?\Magento\Sales\Api\Data\OrderInterface
{
$orderId = (int)$this->getRequest()->getParam('order_id');
if (!$orderId) {
return null;
}
try {
return $this->orders->get($orderId);
} catch (\Throwable) {
return null;
}
}
}