src/Controller/CommentController.php line 67

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\DTO\AppDTO;
  4. use App\Entity\User;
  5. use App\Entity\Comment;
  6. use App\Service\Cache\Cache;
  7. use App\Repository\UserRepository;
  8. use App\Repository\CommentRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Doctrine\ORM\Tools\Pagination\Paginator;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use App\Service\Paginator as ServicePaginator;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class CommentController extends AbstractASController
  16. {
  17.     public function __construct(
  18.         private EntityManagerInterface $em
  19.         private AppDTO $app
  20.         private CommentRepository $Comments,
  21.         private UserRepository $Users,
  22.     )
  23.     {        
  24.     }
  25.     
  26.     #[Route('/comments/add'name'comments_add_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  27.     #[Route('/{_locale}/comments/add'name'comments_add',  requirements: ['_locale' => '%app.langs%'])]
  28.     public function addComment(Request $request): Response
  29.     {
  30.         /** @var App\Entity\User $User */
  31.         $User $this->getUser();
  32.         $type $request->get("type");
  33.         $par = (int) $request->get('par');
  34.         $user $User $User->getId() : 0;
  35.         $author $request->get('author') ?? $User->getName();
  36.         $theme = (string) $request->get('theme');
  37.         $cont = (string) $request->get('cont');
  38.         
  39.         $Comment = new Comment();
  40.         $Comment->setType($type);
  41.         $Comment->setPar($par);
  42.         $Comment->setUser($user);
  43.         $Comment->setAuthor($author);
  44.         $Comment->setTheme($theme);
  45.         $Comment->setTstamp(time());
  46.         $Comment->setCont($cont);
  47.         $Comment->setAnswer('');
  48.         $Comment->setLang($request->getLocale());
  49.         $Comment->setVisible(0);
  50.         $this->em->persist($Comment);
  51.         $this->em->flush();
  52.         $msg $this->app->labels->get('comments-mail-1');
  53.         $subj $this->app->labels->get('comments-mail-2');
  54.             
  55.         // Func::mailhtml($sett['sitename'], Env::mail_from(), $sett['admin_email'], $subj, $msg);
  56.         $result = ["status" => "ok"];
  57.         return $this->json($result);
  58.     }
  59.     #[Route('/comments'name'comments_no_locale'defaults: ['_locale' => '%app.default_lang%'])]
  60.     #[Route('/{_locale}/comments'name'comments',  requirements: ['_locale' => '%app.langs%'])]
  61.     public function index(Request $request): Response
  62.     {
  63.         $results 50;
  64.         $start = (int) $request->query->get('start');
  65.         $this->em->getClassMetadata(User::class)->setPrimaryTable(['name' => 'user']);
  66.         
  67.         $dql "SELECT c FROM App\Entity\Comment c WHERE c.visible = 1 ORDER BY c.tstamp DESC";
  68.         $query $this->em->createQuery($dql)
  69.                        ->setFirstResult($start)
  70.                        ->setMaxResults($results);
  71.         $comments = new Paginator($query);
  72.         $cnt count($comments);
  73.         
  74.         foreach($comments as $k => $comment) {
  75.             $dql "SELECT count(o) FROM App\Entity\Order o WHERE o.user = ".$comment->getUser()." and ".time()."-o.tstamp < 60*60*24*365";
  76.             // if (empty($comment->getAuthor())) {
  77.                 $user $this->Users->find($comment->getUser());
  78.                 if ($user) {
  79.                     $comment->setAuthor($user->getName()." ".mb_strtoupper(mb_substr($user->getSurname(), 01)).".");
  80.                 }                
  81.             // }
  82.             
  83.             $comment->orders_count array_pop(($this->em->createQuery($dql)->getResult())[0]);
  84.         }
  85.         
  86.         $response $this->render('comment/list.html.twig', [
  87.             'comments' => $comments,
  88.             'paginator' => new ServicePaginator('comments'$cnt$results$start),
  89.         ]);
  90.         $response Cache::http($response$this->getParameter('app.http_cache_time'));
  91.         return $response;
  92.     }
  93. }