<?php
namespace App\Controller;
use App\Entity\Article;
use App\Entity\User;
use App\Service\ArticleEntityService;
use App\Service\MediaEntityService;
use App\Service\UserEntityService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
/**
* Class AuthorController.
*/
class AuthorController extends AbstractController
{
/**
* @Route("/author/{author}/articles/{name}/{page}", name="author.articles", methods={"GET"}, defaults={"page": 1})
*/
public function articles(User $author, $name, $page, ArticleEntityService $articleEntityService, MediaEntityService $mediaEntityService, Request $request)
{
if (!$author) {
return $this->json([
'error' => 'tag.not_found',
'message' => 'Tag not found',
]);
}
$page = (int) $page;
$page = max(1, $page);
$limit = 50;
$articles = $articleEntityService->getPublishedArticlesByAuthor( $author, strtolower($name) === "la rédaction"? "La Rédaction":null, 9, $page);
$authorAvatar = $author->getAvatar() ? $mediaEntityService->format($author->getAvatar()) : null;
if( strpos(strtolower($name), "la rédaction") !== false ){
$count_articles = (int) $articleEntityService->countArticlesRedaction('la rédaction');
}else{
$count_articles = (int) $articleEntityService->countArticlesByAuthor($author);
}
$pages_number = ceil($count_articles / 9);
$isLastPage = $page === $pages_number;
if ($request->isXmlHttpRequest()) {
$articles = $articleEntityService->getPublishedArticlesByAuthor($author ,strtolower($name)=== "la rédaction"? "La Rédaction":null, $page * 9, 1);
return $this->render('themes/theme-5/author/result-articles.html.twig', [
'author_name' => strtolower($name),
'author' => $author,
'articles' => $articles,
'page' => $page,
'is_last_page' => $isLastPage,
'authoravatar' => $authorAvatar,
'count_articles' => $count_articles,
]);
}
return $this->render('author/articles.html.twig', [
'author_name' => strtolower($name),
'author' => $author,
'articles' => $articles,
'page' => $page,
'is_last_page' => $isLastPage,
'authoravatar' => $authorAvatar,
'count_articles' => $count_articles,
]);
}
/**
*@Route("/authors/{page}", name="author.liste", methods={"GET"}, defaults={"page": 1})
*/
public function listeAuthor($page, Request $request, UserEntityService $userEntityService)
{
$page = (int) $page;
$page = max(1, $page);
$authors = [];
$screen_type = $request->query->get('limit');
$all_articles = $this->getDoctrine()->getRepository(Article::class)->getAllArticles();
foreach ($all_articles as $author_email) {
$u = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $author_email['author_email']]);
if (!empty($u)) {
$authors[] = $u;
}
}
if ($request->isXmlHttpRequest()) {
$value = $request->query->get('search');
$screen_type = $request->query->get('limit');
if ($value) {
$u = $this->getDoctrine()->getRepository(User::class)->SearchByValue($value);
$authorsSearch = $userEntityService->formatAll($u);
$pages_number = ceil(\count($authorsSearch) / $screen_type);
$isLastPage = $page === $pages_number;
return $this->render('themes/theme-5/author/_partials/search-author.html.twig', [
'authors' => $authorsSearch,
'page' => $page,
'isLastPage' => $isLastPage,
]);
}
}
if ($request->isXmlHttpRequest()) {
$screen_type = $request->query->get('limit');
$auth = $userEntityService->formatAll($authors);
$autheur = \array_slice($auth, 0, $screen_type * $page);
$pages_number = ceil(\count($auth) / $screen_type);
$isLastPage = $page === $pages_number;
return $this->render('themes/theme-5/author/_partials/author.html.twig', [
'authors' => $autheur,
'page' => $page,
'isLastPage' => $isLastPage,
]);
}
$authors = $userEntityService->formatAll($authors);
$pages_number = ceil(\count($authors) / 9);
$isLastPage = $page === $pages_number;
return $this->render('author/authors.html.twig', [
'authors' => \array_slice($authors, 0, 9),
'page' => $page,
'isLastPage' => $isLastPage,
]);
}
/**
*@Route("/load-authors", name="authors-more", methods={"GET"})
*/
public function loadMoreAuthor(ArticleEntityService $articleEntityService, Request $request, UserEntityService $userEntityService)
{
$authors = [];
$isReached_limit = false;
$html = '';
$all_articles = $this->getDoctrine()->getRepository(Article::class)->getAllArticles();
foreach ($all_articles as $author_email) {
$u = $this->getDoctrine()->getRepository(User::class)->findOneBy(['email' => $author_email['author_email']]);
if (!empty($u)) {
$authors[] = $u;
}
}
if ($request->get('id') && '' !== $request->get('id') && null !== $request->get('id')) {
$search = $request->get('id');
$limit = $request->get('limit');
$all_authors = array_filter(
$authors,
function ($obj) use ($search) {
return false !== mb_strpos(mb_strtolower($obj->getFirstname()), mb_strtolower($search))
|| false !== mb_strpos(mb_strtolower($obj->getLastname()), mb_strtolower($search))
|| false !== mb_strpos(mb_strtolower($obj->getLastname().' '.$obj->getFirstname()), mb_strtolower($search))
|| false !== mb_strpos(mb_strtolower($obj->getFirstname().' '.$obj->getLastname()), mb_strtolower($search))
;
}
);
$count_filtred = \count($all_authors);
if ($limit >= $count_filtred) {
$isReached_limit = true;
}
$all_authors = \array_slice($all_authors, 0, $limit);
} else {
$limit = $request->get('limit');
$count_authors = \count($authors);
if ($limit >= $count_authors) {
$isReached_limit = true;
}
$all_authors = \array_slice($authors, 0, $limit);
}
$authors = $userEntityService->formatAll($all_authors);
if (!empty($authors)) {
$html = $this->render('', [
'authors' => $authors,
])->getContent();
}
return $this->json(['html' => $html, 'isReached' => $isReached_limit], 200);
}
}