Files
kupshop/bundles/KupShop/KupShopBundle/Email/UserMessagesTrait.php
2025-08-02 16:30:27 +02:00

89 lines
2.5 KiB
PHP

<?php
namespace KupShop\KupShopBundle\Email;
use KupShop\I18nBundle\Translations\EmailsTranslation;
use KupShop\KupShopBundle\Exception\UserMessageException;
use Query\Translation;
trait UserMessagesTrait
{
protected string $user_message;
protected array $user_messages_types = [];
private function fetchMessages($languageID)
{
if (!$languageID) {
$languageID = $this->languageContext->getActiveId();
}
if (isset($this->emails[$languageID])) {
return $this->emails[$languageID];
}
$this->emails[$languageID] = sqlQueryBuilder()
->select('e.*')
->from('emails', 'e')
->where('e.type=:type')
->setParameter('type', self::getType())
->andWhere(Translation::coalesceTranslatedFields(
EmailsTranslation::class,
null,
$languageID
))
->orderBy('e.position')
->execute()
->fetchAllAssociative();
return $this->emails[$languageID];
}
public function getMessagesByStatus($status, $languageID = null): array
{
$messages = $this->getMessages($languageID);
if (!$this->user_messages_types) {
foreach ($messages as &$message) {
if (!is_null($message['order_status'])) {
$this->user_messages_types[$message['order_status']][] = $message;
}
}
}
if (!empty($this->user_messages_types[$status])) {
return $this->user_messages_types[$status];
}
return [];
}
public function setUserMessage(string $message): void
{
$this->user_message = $message;
}
public function getEmailTemplate($languageID = null): array
{
$messages = $this->getMessages($languageID);
if ($this->user_message) {
foreach ($messages as $message) {
if ($message['name'] == $this->user_message) {
return $message;
}
}
}
throw new UserMessageException(
sprintf('Error during user message send! User message "%s" not found!', $this->user_message ?: ''),
[
'user_message' => $this->user_message,
'messages' => $messages,
'language' => $this->languageContext->getActiveId(),
'id_object' => $this->getObjectId(),
'type' => self::getType(),
]
);
}
}