Files
2026-09-15 10:35:56 +08:00

72 lines
2.5 KiB
PHP

<?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');
}
}