<?php
namespace App\Controller;
use App\DTO\AppDTO;
use App\Entity\User;
use App\Entity\Comment;
use App\Service\Cache\Cache;
use App\Repository\UserRepository;
use App\Repository\CommentRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpFoundation\Request;
use App\Service\Paginator as ServicePaginator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CommentController extends AbstractASController
{
protected EntityManagerInterface $em;
protected CacheInterface $Cache;
protected AppDTO $app;
//Repositories
protected CommentRepository $Comments;
protected UserRepository $Users;
public function __construct(EntityManagerInterface $em, CacheInterface $Cache, AppDTO $app, RequestStack $requestStack)
{
$this->requestStack = $requestStack;
$this->Cache = $Cache;
$this->em = $em;
$this->app = $app;
$this->Comments = $em->getRepository(Comment::class);
$this->Users = $em->getRepository(User::class);
}
#[Route('/comments/add', name: 'comments_add_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route('/{_locale}/comments/add', name: 'comments_add', requirements: ['_locale' => '%app.langs%'])]
public function addComment(Request $request): Response
{
/** @var App\Entity\User $User */
$User = $this->getUser();
$type = $request->get("type");
$par = (int) $request->get('par');
$user = $User ? $User->getId() : 0;
$author = $request->get('author') ?? $User->getName();
$theme = (string) $request->get('theme');
$cont = (string) $request->get('cont');
$Comment = new Comment();
$Comment->setType($type);
$Comment->setPar($par);
$Comment->setUser($user);
$Comment->setAuthor($author);
$Comment->setTheme($theme);
$Comment->setTstamp(time());
$Comment->setCont($cont);
$Comment->setAnswer('');
$Comment->setLang($request->getLocale());
$Comment->setVisible(0);
$this->em->persist($Comment);
$this->em->flush();
$msg = $this->app->labels->get('comments-mail-1');
$subj = $this->app->labels->get('comments-mail-2');
// Func::mailhtml($sett['sitename'], Env::mail_from(), $sett['admin_email'], $subj, $msg);
$result = ["status" => "ok"];
return $this->json($result);
}
#[Route('/comments', name: 'comments_no_locale', defaults: ['_locale' => '%app.default_lang%'])]
#[Route('/{_locale}/comments', name: 'comments', requirements: ['_locale' => '%app.langs%'])]
public function index(Request $request): Response
{
$results = 50;
$start = (int) $request->query->get('start');
$dql = "SELECT c FROM App\Entity\Comment c WHERE c.visible = 1 ORDER BY c.tstamp DESC";
$query = $this->em->createQuery($dql)
->setFirstResult($start)
->setMaxResults($results);
$comments = new Paginator($query);
$cnt = count($comments);
foreach($comments as $comment) {
$dql = "SELECT count(o) FROM App\Entity\Order o WHERE o.user = ".$comment->getUser()." and ".time()."-o.tstamp < 60*60*24*365";
if (empty($comment->author)) {
$user = $this->Users->find($comment->getUser());
if ($user) {
$comment->setAuthor($user->getName()." ".$user->getSurname());
}
}
$comment->orders_count = array_pop(($this->em->createQuery($dql)->getResult())[0]);
}
$response = $this->render('comment/list.html.twig', [
'comments' => $comments,
'paginator' => new ServicePaginator('comments', $cnt, $results, $start),
]);
$response = Cache::http($response, $this->getParameter('app.http_cache_time'));
return $response;
}
}