1
0
Fork 0

some fixes

This commit is contained in:
VALENTIN 2021-01-22 12:56:37 +00:00
parent 86f4accd78
commit d903c2c051
4 changed files with 32 additions and 10 deletions

View file

@ -17,6 +17,7 @@ class CategoryController extends AbstractController
public function index(): Response public function index(): Response
{ {
return $this->render('category/categories.html.twig', [ return $this->render('category/categories.html.twig', [
// 'controller_name' => 'CategoryController',
'categories' => $this->getDoctrine()->getRepository(Category::class)->findAll() 'categories' => $this->getDoctrine()->getRepository(Category::class)->findAll()
]); ]);
} }
@ -39,6 +40,7 @@ class CategoryController extends AbstractController
return $this->redirectToRoute('categories'); return $this->redirectToRoute('categories');
} }
return $this->render('category/categories-form.html.twig', [ return $this->render('category/categories-form.html.twig', [
// 'controller_name' => 'CategoryController',
'form' => $form->createView(), 'form' => $form->createView(),
'title' => 'Add new category' 'title' => 'Add new category'
]); ]);
@ -60,11 +62,11 @@ class CategoryController extends AbstractController
$form = $this->createForm(CategoryType::class, $cat); $form = $this->createForm(CategoryType::class, $cat);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
$cat = $form->getData();
$manager->flush(); $manager->flush();
return $this->redirectToRoute('categories'); return $this->redirectToRoute('categories');
} }
return $this->render('category/categories-form.html.twig', [ return $this->render('category/categories-form.html.twig', [
// 'controller_name' => 'CategoryController',
'form' => $form->createView(), 'form' => $form->createView(),
'title' => 'Edit '.$cat->getName() 'title' => 'Edit '.$cat->getName()
]); ]);
@ -90,7 +92,8 @@ class CategoryController extends AbstractController
public function categorySummary(): Response public function categorySummary(): Response
{ {
return $this->render('category/category_summary.html.twig', [ return $this->render('category/category_summary.html.twig', [
'Categories' => array_filter($this->getDoctrine()->getRepository(Category::class)->findAll(), static function ($c) {return count($c->getPosts())>=1;}) // 'controller_name' => 'CategoryController',
'Categories' => $this->getDoctrine()->getRepository(Category::class)->getCategorySummary()
]); ]);
} }
} }

View file

@ -15,6 +15,7 @@ class CommentController extends AbstractController
public function index(): Response public function index(): Response
{ {
return $this->render('comment/comments.html.twig', [ return $this->render('comment/comments.html.twig', [
// 'controller_name' => 'CommentController',
'comments' => $this->getDoctrine()->getRepository(Comment::class)->findAll() 'comments' => $this->getDoctrine()->getRepository(Comment::class)->findAll()
]); ]);
} }
@ -56,6 +57,7 @@ class CommentController extends AbstractController
public function recentComment(): Response public function recentComment(): Response
{ {
return $this->render('comment/recent_comment.html.twig', [ return $this->render('comment/recent_comment.html.twig', [
// 'controller_name' => 'CommentController',
'comments' => $this->getDoctrine()->getRepository(Comment::class)->findBy(array('valid' => true), array('createdAt' => 'DESC'), 5, 0) 'comments' => $this->getDoctrine()->getRepository(Comment::class)->findBy(array('valid' => true), array('createdAt' => 'DESC'), 5, 0)
]); ]);
} }

View file

@ -2,6 +2,7 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Category;
use App\Entity\Comment; use App\Entity\Comment;
use App\Entity\Post; use App\Entity\Post;
use App\Form\CommentType; use App\Form\CommentType;
@ -23,15 +24,10 @@ class PostController extends AbstractController
public function index(int $page = 0): Response public function index(int $page = 0): Response
{ {
$repo = $this->getDoctrine()->getRepository(Post::class); $repo = $this->getDoctrine()->getRepository(Post::class);
$pages = (int) round(count($repo->getPublished())/5, 0, PHP_ROUND_HALF_UP);
if ($page < 0) {
$page = 0;
} else if ($page > $pages) {
$page = $pages;
}
return $this->render('home/index.html.twig', [ return $this->render('home/index.html.twig', [
// 'controller_name' => 'HomeController', // not use on template
'posts' => $repo->getPublished($page*5, 5), 'posts' => $repo->getPublished($page*5, 5),
'pages' => $pages-1, 'pages' => round(count($repo->getPublished())/5, 0, PHP_ROUND_HALF_UP)-1,
'page' => $page 'page' => $page
]); ]);
} }
@ -44,10 +40,13 @@ class PostController extends AbstractController
*/ */
public function post(Request $request, string $slug): Response public function post(Request $request, string $slug): Response
{ {
/** @var Post $post */
$post = $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug)); $post = $this->getDoctrine()->getRepository(Post::class)->findOneBy(array('slug' => $slug));
if (!$post) {
if (null === $post || $post->getPublishedAt() > new \DateTime()) {
throw $this->createNotFoundException("Post not found"); throw $this->createNotFoundException("Post not found");
} }
$form = $this->commentFormGenerator($post); $form = $this->commentFormGenerator($post);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { if ($form->isSubmitted() && $form->isValid()) {
@ -59,6 +58,7 @@ class PostController extends AbstractController
$form = $this->commentFormGenerator($post); $form = $this->commentFormGenerator($post);
} }
return $this->render('post/index.html.twig', [ return $this->render('post/index.html.twig', [
// 'controller_name' => 'PostController', // not use on template
'post' => $post, 'post' => $post,
'form' => $form->createView() 'form' => $form->createView()
]); ]);
@ -70,6 +70,7 @@ class PostController extends AbstractController
public function posts(): Response public function posts(): Response
{ {
return $this->render('post/posts.html.twig', [ return $this->render('post/posts.html.twig', [
// 'controller_name' => 'PostController', // not use on template
'posts' => $this->getDoctrine()->getRepository(Post::class)->findBy(array(), array('publishedAt' => 'DESC')) 'posts' => $this->getDoctrine()->getRepository(Post::class)->findBy(array(), array('publishedAt' => 'DESC'))
]); ]);
} }
@ -93,6 +94,7 @@ class PostController extends AbstractController
return $this->redirectToRoute('posts'); return $this->redirectToRoute('posts');
} }
return $this->render('post/posts-form.html.twig', [ return $this->render('post/posts-form.html.twig', [
// 'controller_name' => 'PostController', // not use on template
'form' => $form->createView(), 'form' => $form->createView(),
'title' => 'Add new post' 'title' => 'Add new post'
]); ]);
@ -121,6 +123,7 @@ class PostController extends AbstractController
return $this->redirectToRoute('posts'); return $this->redirectToRoute('posts');
} }
return $this->render('post/posts-form.html.twig', [ return $this->render('post/posts-form.html.twig', [
// 'controller_name' => 'PostController', // not use on template
'form' => $form->createView(), 'form' => $form->createView(),
'title' => 'Edit '.$post->getTitle() 'title' => 'Edit '.$post->getTitle()
]); ]);

View file

@ -19,6 +19,20 @@ class CategoryRepository extends ServiceEntityRepository
parent::__construct($registry, Category::class); parent::__construct($registry, Category::class);
} }
/**
* @return Category[]
*/
public function getCategorySummary(): array
{
$qb = $this->createQueryBuilder('c')
->addSelect('p')
->join('c.posts', 'p')
->where('p.publishedAt <= :now')
->setParameter('now', new \DateTime());
return $qb->getQuery()->getResult();
}
// /** // /**
// * @return Category[] Returns an array of Category objects // * @return Category[] Returns an array of Category objects
// */ // */