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

137 lines
4.3 KiB
PHP

<?php
namespace KupShop\CommentsBundle\Email;
use KupShop\CommentsBundle\Comment;
use KupShop\CommentsBundle\Util\CommentsUtil;
use KupShop\I18nBundle\Translations\ArticlesTranslation;
use KupShop\I18nBundle\Translations\ProductsTranslation;
use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Email\BaseEmail;
use KupShop\KupShopBundle\Util\StringUtil;
use Query\Operator;
use Query\Translation;
use Symfony\Component\Routing\Router;
class CommentEmail extends BaseEmail
{
protected static $priority = -7;
protected static $type = 'COMMENT';
protected static $simpleComment = false;
/** @var Comment */
protected $comment;
private $commentsUtil;
private $contextManager;
public function __construct(LanguageContext $languageContext, ContextManager $contextManager, CommentsUtil $commentsUtil)
{
parent::__construct($languageContext);
$this->commentsUtil = $commentsUtil;
$this->contextManager = $contextManager;
}
public function setComment(Comment $comment): self
{
$this->comment = $comment;
return $this;
}
public static function getPlaceholders()
{
$placeholders = [
'KOMENTAR' => [
'text' => 'Komentář',
],
'ODKAZ' => [
'text' => 'Odkaz',
],
];
return [self::$type => $placeholders] + (parent::getPlaceholders() ?? []);
}
public function replacePlaceholdersItem($placeholder)
{
switch ($placeholder) {
case 'KOMENTAR':
$smarty = createSmarty(false, true);
$smarty->assign('comment', $this->comment);
$smarty->assign('simpleComment', static::$simpleComment);
return $smarty->fetch('email/email_comment.tpl');
case 'ODKAZ':
if ($this->comment->getIdProduct()) {
return path('products', [
'id' => $this->comment->getIdProduct(),
'name' => StringUtil::slugify($this->getObjectTitle()),
], Router::ABSOLUTE_URL);
}
return path('kupshop_content_articles_article_1', [
'IDa' => $this->comment->getIdArticle(),
'slug' => StringUtil::slugify($this->getObjectTitle()),
], Router::ABSOLUTE_URL);
}
return parent::replacePlaceholdersItem($placeholder);
}
public function testEmail(): array
{
$comment = $this->getDummyComment();
$this->setComment($comment);
return $this->contextManager->activateContexts([LanguageContext::class => $comment->getIdLanguage()], function () {
return parent::testEmail();
});
}
protected function getObjectTitle(): string
{
if ($this->comment->getIdProduct()) {
$title = sqlQueryBuilder()->select('p.title')
->from('products', 'p')
->where(Operator::equals(['p.id' => $this->comment->getIdProduct()]))
->andWhere(Translation::coalesceTranslatedFields(ProductsTranslation::class))
->execute()->fetchColumn();
} else {
$title = sqlQueryBuilder()->select('a.title')
->from('articles', 'a')
->where(Operator::equals(['a.id' => $this->comment->getIdArticle()]))
->andWhere(Translation::coalesceTranslatedFields(ArticlesTranslation::class))
->execute()->fetchColumn();
}
return $title ? $title : '';
}
private function getDummyComment(): Comment
{
$comment1 = (new Comment())
->setId(1)
->setIdLanguage('cs')
->setIdParent(null)
->setIdProduct((int) returnSQLResult('SELECT MAX(id) FROM products'))
->setIdUser((int) returnSQLResult('SELECT MAX(id) FROM users'))
->setContent('Testovaci komentar')
->setStatus(Comment::STATUS_CONFIRMED)
->setDateAdd('2020-07-16 12:00:00');
$comment2 = clone $comment1;
$comment2->setId(2)
->setIdParent(1)
->setParent($comment1);
$comment1->setChildren([$comment2]);
return $comment2;
}
}