src/Controller/AuthorController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Article;
  4. use App\Entity\User;
  5. use App\Service\ArticleEntityService;
  6. use App\Service\MediaEntityService;
  7. use App\Service\UserEntityService;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12.  * Class AuthorController.
  13.  */
  14. class AuthorController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/author/{author}/articles/{name}/{page}", name="author.articles", methods={"GET"}, defaults={"page": 1})
  18.      */
  19.     public function articles(User $author$name$pageArticleEntityService $articleEntityServiceMediaEntityService $mediaEntityServiceRequest $request)
  20.     {
  21.         if (!$author) {
  22.             return $this->json([
  23.                 'error' => 'tag.not_found',
  24.                 'message' => 'Tag not found',
  25.             ]);
  26.         }
  27.         $page = (int) $page;
  28.         $page max(1$page);
  29.         $limit 50;
  30.         $articles $articleEntityService->getPublishedArticlesByAuthor$authorstrtolower($name) === "la rédaction""La Rédaction":null9$page);
  31.         $authorAvatar $author->getAvatar() ? $mediaEntityService->format($author->getAvatar()) : null;
  32.         if( strpos(strtolower($name), "la rédaction") !== false ){
  33.             $count_articles = (int) $articleEntityService->countArticlesRedaction('la rédaction');
  34.         }else{
  35.             $count_articles = (int) $articleEntityService->countArticlesByAuthor($author);
  36.         }
  37.         $pages_number ceil($count_articles 9);
  38.         $isLastPage $page === $pages_number;
  39.         if ($request->isXmlHttpRequest()) {
  40.             $articles $articleEntityService->getPublishedArticlesByAuthor($author ,strtolower($name)=== "la rédaction""La Rédaction":null$page 91);
  41.             return $this->render('themes/theme-5/author/result-articles.html.twig', [
  42.                 'author_name' => strtolower($name),
  43.                 'author' => $author,
  44.                 'articles' => $articles,
  45.                 'page' => $page,
  46.                 'is_last_page' => $isLastPage,
  47.                 'authoravatar' => $authorAvatar,
  48.                 'count_articles' => $count_articles,
  49.             ]);
  50.         }
  51.         return $this->render('author/articles.html.twig', [
  52.             'author_name' => strtolower($name),
  53.             'author' => $author,
  54.             'articles' => $articles,
  55.             'page' => $page,
  56.             'is_last_page' => $isLastPage,
  57.             'authoravatar' => $authorAvatar,
  58.             'count_articles' => $count_articles,
  59.         ]);
  60.     }
  61.     /**
  62.      *@Route("/authors/{page}", name="author.liste", methods={"GET"}, defaults={"page": 1})
  63.      */
  64.     public function listeAuthor($pageRequest $requestUserEntityService $userEntityService)
  65.     {
  66.         $page = (int) $page;
  67.         $page max(1$page);
  68.         $authors = [];
  69.         $screen_type $request->query->get('limit');
  70.         $all_articles $this->getDoctrine()->getRepository(Article::class)->getAllArticles();
  71.         foreach ($all_articles as $author_email) {
  72.             $u $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $author_email['author_email']]);
  73.             if (!empty($u)) {
  74.                 $authors[] = $u;
  75.             }
  76.         }
  77.         if ($request->isXmlHttpRequest()) {
  78.             $value $request->query->get('search');
  79.             $screen_type $request->query->get('limit');
  80.             if ($value) {
  81.                 $u $this->getDoctrine()->getRepository(User::class)->SearchByValue($value);
  82.                 $authorsSearch $userEntityService->formatAll($u);
  83.                 $pages_number ceil(\count($authorsSearch) / $screen_type);
  84.                 $isLastPage $page === $pages_number;
  85.                 return $this->render('themes/theme-5/author/_partials/search-author.html.twig', [
  86.                     'authors' => $authorsSearch,
  87.                     'page' => $page,
  88.                     'isLastPage' => $isLastPage,
  89.                 ]);
  90.             }
  91.         }
  92.         if ($request->isXmlHttpRequest()) {
  93.             $screen_type $request->query->get('limit');
  94.             $auth $userEntityService->formatAll($authors);
  95.             $autheur = \array_slice($auth0$screen_type $page);
  96.             $pages_number ceil(\count($auth) / $screen_type);
  97.             $isLastPage $page === $pages_number;
  98.             return $this->render('themes/theme-5/author/_partials/author.html.twig', [
  99.                 'authors' => $autheur,
  100.                 'page' => $page,
  101.                 'isLastPage' => $isLastPage,
  102.             ]);
  103.         }
  104.         $authors $userEntityService->formatAll($authors);
  105.         $pages_number ceil(\count($authors) / 9);
  106.         $isLastPage $page === $pages_number;
  107.         return $this->render('author/authors.html.twig', [
  108.             'authors' => \array_slice($authors09),
  109.             'page' => $page,
  110.             'isLastPage' => $isLastPage,
  111.         ]);
  112.     }
  113.     /**
  114.      *@Route("/load-authors", name="authors-more", methods={"GET"})
  115.      */
  116.     public function loadMoreAuthor(ArticleEntityService $articleEntityServiceRequest $requestUserEntityService $userEntityService)
  117.     {
  118.         $authors = [];
  119.         $isReached_limit false;
  120.         $html '';
  121.         $all_articles $this->getDoctrine()->getRepository(Article::class)->getAllArticles();
  122.         foreach ($all_articles as $author_email) {
  123.             $u $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $author_email['author_email']]);
  124.             if (!empty($u)) {
  125.                 $authors[] = $u;
  126.             }
  127.         }
  128.         if ($request->get('id') && '' !== $request->get('id') && null !== $request->get('id')) {
  129.             $search $request->get('id');
  130.             $limit $request->get('limit');
  131.             $all_authors array_filter(
  132.                 $authors,
  133.                 function ($obj) use ($search) {
  134.                     return false !== mb_strpos(mb_strtolower($obj->getFirstname()), mb_strtolower($search))
  135.                         || false !== mb_strpos(mb_strtolower($obj->getLastname()), mb_strtolower($search))
  136.                         || false !== mb_strpos(mb_strtolower($obj->getLastname().' '.$obj->getFirstname()), mb_strtolower($search))
  137.                         || false !== mb_strpos(mb_strtolower($obj->getFirstname().' '.$obj->getLastname()), mb_strtolower($search))
  138.                     ;
  139.                 }
  140.             );
  141.             $count_filtred = \count($all_authors);
  142.             if ($limit >= $count_filtred) {
  143.                 $isReached_limit true;
  144.             }
  145.             $all_authors = \array_slice($all_authors0$limit);
  146.         } else {
  147.             $limit $request->get('limit');
  148.             $count_authors = \count($authors);
  149.             if ($limit >= $count_authors) {
  150.                 $isReached_limit true;
  151.             }
  152.             $all_authors = \array_slice($authors0$limit);
  153.         }
  154.         $authors $userEntityService->formatAll($all_authors);
  155.         if (!empty($authors)) {
  156.             $html $this->render('', [
  157.                 'authors' => $authors,
  158.             ])->getContent();
  159.         }
  160.         return $this->json(['html' => $html'isReached' => $isReached_limit], 200);
  161.     }
  162. }