src/Controller/Front/BlogController.php line 168
<?php
namespace App\Controller\Front;
use App\Entity\Animal;
use App\Entity\BlogCategory;
use App\Entity\BlogKeyword;
use App\Entity\BlogNote;
use App\Entity\Role;
use App\Entity\Rotator;
use App\Entity\RotatorPlace;
use App\Entity\RotatorType;
use App\Entity\User;
use App\Repository\AnimalRepository;
use App\Repository\BlogAuthorRepository;
use App\Repository\BlogCategoryRepository;
use App\Repository\BlogKeywordRepository;
use App\Repository\BlogNoteRepository;
use App\Repository\RoleRepository;
use App\Repository\RotatorRepository;
use App\Repository\UserRepository;
use App\Service\BlogService;
use Doctrine\Persistence\ManagerRegistry;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class BlogController extends AbstractController
{
#[Route('/autorzy')]
#[Route('/front/blog')]
public function indexAction(Request $request, ManagerRegistry $doctrine)
{
$roles = $doctrine->getRepository(Role::class)->findBy(['role' => 'ROLE_AUTHOR']);
$blogAuthors = $doctrine->getRepository(User::class)->findAllqb('id', 'DESC', $roles);
return $this->render(
'front/blog/index.html.twig',
[
'blogAuthors' => $blogAuthors->getQuery()->getResult(),
]
);
}
#[Route('/autor/{firstName}_{lastName}/{id}', name: 'base_front_blog_authors')]
public function notesAction(Request $request, ManagerRegistry $doctrine, $id)
{
$blogNotes = $doctrine->getRepository(BlogNote::class)->findBy(['author' => $id, 'isPublished' => true]);
$author = $doctrine->getRepository(User::class)->find($id);
$anotherAuthors = $doctrine
->getRepository(BlogNote::class)
->findMostPostedAuthors(2, $id);
$rotator = $doctrine->getRepository(Rotator::class)->findOneByTypeAndPlace(
RotatorType::TYPE_BANNER,
RotatorPlace::TYPE_AUTHOR
);
return $this->render(
'front/blog/notes.html.twig',
[
'author' => $author,
'anotherAuthors' => $anotherAuthors,
'blogNotes' => $blogNotes,
'blogRotator' => $rotator,
]
);
}
#[Route('/blog', name: 'base_front_blog_notes')]
#[Route('/front/blog/notesList')]
public function notesListAction(
Request $request,
ManagerRegistry $doctrine)
{
$categories = $doctrine
->getRepository(BlogCategory::class)
->getCategoriesForBlog();
$blogNotes = $doctrine
->getRepository(BlogNote::class)
->findBlogPostsForList($categories);
$bestAuthors = $doctrine
->getRepository(BlogNote::class)
->findMostPostedAuthors();
return $this->render(
'front/blog/notesList.html.twig',
[
'blogNotes' => $blogNotes,
'bestAuthors' => $bestAuthors,
]
);
}
#[Route('/blog/popular', name: 'base_front_blog_popular')]
#[Route('/front/blog/popular')]
public function popularNotesAction(
Request $request,
ManagerRegistry $doctrine)
{
$categories = $doctrine
->getRepository(BlogCategory::class)
->getCategoriesForBlog();
$blogNotes = $doctrine
->getRepository(BlogNote::class)
->findBlogPostsPopularForList($categories);
$bestAuthors = $doctrine
->getRepository(BlogNote::class)
->findMostPostedAuthors();
return $this->render(
'front/blog/notesList.html.twig',
[
'blogNotes' => $blogNotes,
'bestAuthors' => $bestAuthors,
]
);
}
#[Route('/blog/{id}', name: 'base_front_blog_notesingle')]
public function noteSingleAction(Request $request, ManagerRegistry $doctrine, BlogService $blogService, $id)
{
$isAdmin = $this->isGranted('ROLE_ADMIN');
if(!$isAdmin) {
$blogNote = $doctrine->getRepository(BlogNote::class)->findOneBy(['id' => $id, 'isPublished' => true]);
} else {
$blogNote = $doctrine->getRepository(BlogNote::class)->findOneBy(['id' => $id]);
}
if (!$blogNote) {
throw $this->createNotFoundException('Wpis nie istnieje');
}
$blogService->incrementPv($blogNote);
$nextNotesAuthor = [];
if (!is_null($blogNote->getAuthor())) {
$nextNotesAuthor = $doctrine->getRepository(BlogNote::class)->findNextNotesFromAuthor($blogNote);
}
$nextNotesCategory = $doctrine->getRepository(BlogNote::class)->findNextNotesFromCategory($blogNote);
$animals = $doctrine->getRepository(Animal::class)->findBy(['isPublished' => true], ['id' => 'DESC'], 5);
return $this->render(
'front/blog/noteSingle.html.twig',
[
'blogNote' => $blogNote,
'categories' => $doctrine->getRepository(BlogCategory::class)->getCategoriesForBlog(),
'nextNotesCategory' => $nextNotesCategory,
'nextNotesAuthor' => $nextNotesAuthor,
'animals' => $animals,
]
);
}
#[Route('blog/tag/{word}', name: 'base_front_blog_noteslistwithkeyword')]
public function notesListWithKeywordAction(ManagerRegistry $doctrine, $word)
{
$keywords = $doctrine->getRepository(BlogKeyword::class)->findBy(
['name' => $word],
['publishedDate' => 'DESC']
);
$blogNotes = $doctrine->getRepository(BlogNote::class)->findBlogPostsForKeyword($keywords);
$bestAuthors = $doctrine->getRepository(BlogNote::class)->findMostPostedAuthors();
return $this->render(
'front/blog/notesListWithKeyword.html.twig',
[
'blogNotes' => $blogNotes,
'bestAuthors' => $bestAuthors,
'keyword' => $word,
]
);
}
#[Route('/blog/kategoria/{id}/{slug}', name: 'base_front_blog_notesincategory')]
public function notesInCategoryAction($id, ManagerRegistry $doctrine, $slug)
{
$blogNotes = $doctrine->getRepository(BlogNote::class)
->findByCategories($id);
$bestAuthors = $doctrine->getRepository(BlogNote::class)->findMostPostedAuthors();
$category = $doctrine->getRepository(BlogCategory::class)->find($id);
return $this->render(
'front/blog/notesInCategory.html.twig',
[
'blogNotes' => $blogNotes,
'bestAuthors' => $bestAuthors,
'category' => $category,
]
);
}
#[Route('/jak-pomoc')]
public function howToHelpNotesList(Request $request)
{
return $this->render(
'front/blog/howToHelpNotesList.html.twig',
[
'blogNotes' => $this->getAllPostsFromHowToHelpCategory(),
]
);
}
#[Route('/jak-pomoc/{id}')]
public function howToHelpSingleAction(ManagerRegistry $doctrine, $id)
{
$blogNote = $doctrine->getRepository(BlogNote::class)->findOneBy(['id' => $id, 'isPublished' => true]);
if (empty($blogNote)) {
return new Response('', 404);
}
if ($blogNote->getCategory()->getType() != BlogCategory::TYPE_HOW_TO_HELP) {
return new RedirectResponse($this->generateUrl('base_front_blog_notesingle', ['id' => $id]));
} else {
return $this->render(
'front/blog/howToHelpSingle.html.twig',
[
'blogNote' => $blogNote,
'blogNotes' => $this->getAllPostsFromHowToHelpCategory(),
]
);
}
}
private function getAllPostsFromHowToHelpCategory(ManagerRegistry $doctrine)
{
$category = $doctrine->getRepository(BlogCategory::class)->findOneBy(['type' => BlogCategory::TYPE_HOW_TO_HELP]);
return $doctrine->getRepository(BlogNote::class)->getAllPostsInHowToHelpCategory($category);
}
}