src/Controller/ArticlesController.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ArticleDetails;
  4. use App\Entity\Articles;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class ArticlesController extends AbstractController
  12. {
  13.     private $em;
  14.     private $encryptor;
  15.     public function __construct(EntityManagerInterface $emEncryptor $encryptor)
  16.     {
  17.         $this->em $em;
  18.         $this->encryptor $encryptor;
  19.     }
  20.     #[Route('/article/{pageId}'name'articles_page')]
  21.     public function articlesAction(Request $request): Response
  22.     {
  23.         $article $this->em->getRepository(Articles::class)->findByPageId($request->get('pageId'));
  24.         return $this->render('frontend/articles.html.twig', [
  25.             'articles' => $article,
  26.         ]);
  27.     }
  28.     #[Route('/support/authors/{articleId}'name'support_authors')]
  29.     public function articleAuthorsAction(Request $request): Response
  30.     {
  31.         $authors $this->em->getRepository(ArticleDetails::class)->findUsersByld($request->get('articleId'));
  32.         $response '';
  33.         $count count($authors);
  34.         if($count 3){
  35.             $remaining $count 3;
  36.             $response 'and ' $remaining ' others';
  37.         }
  38.         return new Response($response);
  39.     }
  40.     #[Route('/article-list/authors/{articleDetailId}/{showAuthors}'name'article_list_authors')]
  41.     public function articleListAuthorsAction(Request $request): Response
  42.     {
  43.         $articleDetails $this->em->getRepository(ArticleDetails::class)->find($request->get('articleDetailId'));
  44.         $lastUpdated $this->em->getRepository(ArticleDetails::class)->findByLastUpdated($request->get('articleDetailId'));
  45.         $response '';
  46.         if($request->get('showAuthors') == 1) {
  47.             $response 'Written By ';
  48.             $authors = [];
  49.             foreach ($articleDetails as $articleDetail) {
  50.                 $authors[] = $this->encryptor->decrypt($articleDetail->getUser()->getFirstName()) . ' ' $this->encryptor->decrypt($articleDetail->getUser()->getLastName());
  51.             }
  52.             $separator ', ';
  53.             if(count($authors) <= 2){
  54.                 $separator ' & ';
  55.             }
  56.             $response .= implode($separator$authors);
  57.         }
  58.         if(array_key_exists(0$lastUpdated)) {
  59.             $modified $lastUpdated[0]->getModified()->format('Y-m-d H:i:s');
  60.             $response .= '<br>Updated ' $this->timeAgo($modified);
  61.         }
  62.         return new Response($response);
  63.     }
  64.     #[Route('/article/{pageId}/{articleId}'name'article_list_page')]
  65.     public function articleListAction(Request $request): Response
  66.     {
  67.         $articleId $request->get('articleId');
  68.         $article $this->em->getRepository(Articles::class)->find($articleId);
  69.         return $this->render('frontend/articles_list.html.twig', [
  70.             'article' => $article,
  71.         ]);
  72.     }
  73.     #[Route('/article/{pageId}/{articleId}/{articleDetailId}'name'article_details_page')]
  74.     public function articleDetailsAction(Request $request): Response
  75.     {
  76.         $articleDetailId $request->get('articleDetailId');
  77.         $articleDetails $this->em->getRepository(ArticleDetails::class)->find($articleDetailId);
  78.         return $this->render('frontend/article_details.html.twig', [
  79.             'articleDetails' => $articleDetails,
  80.         ]);
  81.     }
  82.     public function timeAgo($date) {
  83.         $timestamp strtotime($date);
  84.         $strTime = ["second""minute""hour""day""month""year"];
  85.         $length = ["60","60","24","30","12","10"];
  86.         $currentTime time();
  87.         if($currentTime >= $timestamp) {
  88.             $diff time() - $timestamp;
  89.             for($i 0$diff >= $length[$i] && $i count($length) - 1$i++) {
  90.                 $diff $diff $length[$i];
  91.             }
  92.             $diff round($diff);
  93.             return $diff " " $strTime[$i] . "s ago ";
  94.         }
  95.         return '';
  96.     }
  97. }