2021-03-29 13:58:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Controller\Blog;
|
|
|
|
|
|
|
|
use App\Core\Annotation\UrlGenerator;
|
|
|
|
use App\Core\Controller\Site\PageController;
|
2021-03-30 13:40:38 +02:00
|
|
|
use App\Core\Manager\EntityManager;
|
2021-03-29 13:58:05 +02:00
|
|
|
use App\Core\Site\SiteRequest;
|
2021-03-30 13:40:38 +02:00
|
|
|
use App\Core\Site\SiteStore;
|
2021-03-29 13:58:05 +02:00
|
|
|
use App\Entity\Blog\Category;
|
|
|
|
use App\Entity\Blog\Post;
|
2021-03-30 13:40:38 +02:00
|
|
|
use App\Factory\Blog\CommentFactory;
|
|
|
|
use App\Form\Blog\UserCommentType;
|
2021-03-30 20:40:20 +02:00
|
|
|
use App\Markdown\Parser\Post as PostParser;
|
2021-03-29 13:58:05 +02:00
|
|
|
use App\Repository\Blog\PostRepositoryQuery;
|
|
|
|
use App\UrlGenerator\PostGenerator;
|
2021-03-29 22:45:51 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-03-30 13:40:38 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2021-03-30 20:40:20 +02:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2021-03-29 13:58:05 +02:00
|
|
|
|
|
|
|
class PostController extends PageController
|
|
|
|
{
|
|
|
|
protected PostRepositoryQuery $postQuery;
|
|
|
|
|
|
|
|
public function __construct(PostRepositoryQuery $postQuery, SiteRequest $siteRequest, SiteStore $siteStore)
|
|
|
|
{
|
|
|
|
parent::__construct($siteRequest, $siteStore);
|
|
|
|
|
|
|
|
$this->postQuery = $postQuery;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @UrlGenerator(service=PostGenerator::class, method="post")
|
|
|
|
*/
|
2021-03-30 13:40:38 +02:00
|
|
|
public function post(
|
|
|
|
Post $post,
|
|
|
|
string $slug,
|
|
|
|
CommentFactory $commentFactory,
|
|
|
|
Request $request,
|
|
|
|
EntityManager $entityManager
|
|
|
|
): Response {
|
2021-03-29 13:58:05 +02:00
|
|
|
if (Post::DRAFT === $post->getStatus() && !$this->getUser()) {
|
|
|
|
throw $this->createNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($slug !== $post->getSlug()) {
|
|
|
|
return $this->redirectToRoute(
|
|
|
|
$this->siteRequest->getNode()->getRouteName(),
|
|
|
|
[
|
|
|
|
'post' => $post->getId(),
|
|
|
|
'slug' => $post->getSlug(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-30 13:40:38 +02:00
|
|
|
$form = $this->createForm(UserCommentType::class, $commentFactory->create($post));
|
|
|
|
|
|
|
|
if ($request->isMethod('POST')) {
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isValid()) {
|
|
|
|
$data = $request->request->get($form->getName());
|
|
|
|
$parentCommentId = (int) $data['parentCommentId'];
|
|
|
|
|
|
|
|
foreach ($post->getComments(['id' => $parentCommentId]) as $comment) {
|
|
|
|
$form->getData()->setParentComment($comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
$entityManager->create($form->getData());
|
|
|
|
|
|
|
|
$this->addFlash('success', 'Commentaire publié !');
|
|
|
|
|
|
|
|
return $this->redirect($request->getUri());
|
|
|
|
}
|
|
|
|
$this->addFlash('error', 'Le formulaire n\'est pas valide.');
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:48:55 +01:00
|
|
|
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
|
2021-03-29 13:58:05 +02:00
|
|
|
'post' => $post,
|
2021-03-30 13:40:38 +02:00
|
|
|
'form' => $form->createView(),
|
2021-03-29 13:58:05 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function posts(int $page = 1): Response
|
|
|
|
{
|
|
|
|
$entities = $this->createQuery()
|
|
|
|
->paginate($page, 5)
|
|
|
|
;
|
|
|
|
|
2021-11-23 22:48:55 +01:00
|
|
|
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
|
2021-03-29 13:58:05 +02:00
|
|
|
'pager' => $entities,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-03-29 22:45:51 +02:00
|
|
|
public function search(Request $request, int $page = 1): Response
|
|
|
|
{
|
|
|
|
$query = $request->query->get('query');
|
2021-03-30 21:16:52 +02:00
|
|
|
$tag = $request->query->get('tag');
|
2021-03-29 22:45:51 +02:00
|
|
|
|
2021-03-30 21:16:52 +02:00
|
|
|
if ($query || $tag) {
|
2021-03-29 22:45:51 +02:00
|
|
|
$entities = $this->createQuery()
|
2021-03-30 21:16:52 +02:00
|
|
|
->search($query, $tag)
|
2021-03-29 22:45:51 +02:00
|
|
|
->paginate($page, 5)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:48:55 +01:00
|
|
|
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
|
2021-03-29 22:45:51 +02:00
|
|
|
'pager' => $entities ?? null,
|
|
|
|
'query' => $query,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:58:05 +02:00
|
|
|
/**
|
|
|
|
* @UrlGenerator(service=PostGenerator::class, method="category")
|
|
|
|
*/
|
|
|
|
public function category(Category $category, string $slug, int $page = 1): Response
|
|
|
|
{
|
|
|
|
$entities = $this->createQuery()
|
|
|
|
->inCategory($category)
|
|
|
|
->paginate($page, 5)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (!$category->getIsActive() && !$this->getUser()) {
|
|
|
|
throw $this->createNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($slug !== $category->getSlug()) {
|
|
|
|
return $this->redirectToRoute(
|
|
|
|
$this->siteRequest->getNode()->getRouteName(),
|
|
|
|
[
|
|
|
|
'category' => $category->getId(),
|
|
|
|
'slug' => $category->getSlug(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-23 22:48:55 +01:00
|
|
|
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
|
2021-03-29 13:58:05 +02:00
|
|
|
'category' => $category,
|
|
|
|
'pager' => $entities,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tag(string $tag, int $page = 1): Response
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createQuery(): PostRepositoryQuery
|
|
|
|
{
|
|
|
|
return $this->postQuery->create()
|
|
|
|
->orderBy('.publishedAt', 'DESC')
|
|
|
|
->published()
|
|
|
|
;
|
|
|
|
}
|
2021-03-30 20:40:20 +02:00
|
|
|
|
|
|
|
public function rss(PostParser $parser): Response
|
|
|
|
{
|
|
|
|
$entities = $this->createQuery()->paginate(1, 20);
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
$items[] = [
|
|
|
|
'title' => $entity->getTitle(),
|
|
|
|
'guid' => $entity->getId(),
|
|
|
|
'date' => $entity->getPublishedAt(),
|
|
|
|
'description' => $parser->transformMarkdown($entity->getContent()),
|
|
|
|
'categories' => call_user_func(function () use ($entity) {
|
|
|
|
foreach ($entity->getCategories() as $category) {
|
|
|
|
if ($category->getIsActive()) {
|
|
|
|
yield $category->getTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
'link' => $this->generateUrl('blog_menu_post', [
|
|
|
|
'post' => $entity->getId(),
|
|
|
|
'slug' => $entity->getSlug(),
|
|
|
|
], UrlGeneratorInterface::ABSOLUTE_URL),
|
2021-04-12 22:11:19 +02:00
|
|
|
'linkGemini' => sprintf('gemini://deblan.io/posts/%d.gmi', $entity->getId()),
|
2021-03-30 20:40:20 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-04-11 23:17:39 +02:00
|
|
|
$response = new Response();
|
|
|
|
$response->headers->set('Content-Type', 'text/xml');
|
|
|
|
|
|
|
|
return $this->defaultRender($this->siteRequest->getPage()->getTemplate(), [
|
|
|
|
'items' => $items,
|
|
|
|
], $response);
|
2021-04-09 15:34:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function jsonApi(PostParser $parser): Response
|
|
|
|
{
|
|
|
|
$entities = $this->createQuery()->paginate(1, 30);
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach ($entities as $entity) {
|
|
|
|
$items[] = [
|
|
|
|
'id' => $entity->getId(),
|
|
|
|
'title' => $entity->getTitle(),
|
|
|
|
'date' => $entity->getPublishedAt()->format('Y-m-d H:i:s'),
|
|
|
|
'link' => $this->generateUrl('blog_menu_post', [
|
|
|
|
'post' => $entity->getId(),
|
|
|
|
'slug' => $entity->getSlug(),
|
|
|
|
], UrlGeneratorInterface::ABSOLUTE_URL),
|
2021-04-09 15:47:40 +02:00
|
|
|
'content' => $entity->getContent(),
|
2021-04-09 15:34:46 +02:00
|
|
|
'categories' => call_user_func(function () use ($entity) {
|
|
|
|
foreach ($entity->getCategories() as $category) {
|
|
|
|
if ($category->getIsActive()) {
|
|
|
|
yield $category->getTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
2021-03-30 20:40:20 +02:00
|
|
|
|
2021-04-09 15:34:46 +02:00
|
|
|
return $this->json($items);
|
2021-03-30 20:40:20 +02:00
|
|
|
}
|
2021-03-29 13:58:05 +02:00
|
|
|
}
|