diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index 7c2d2ac..c7f1b25 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -17,6 +17,7 @@ class CategoryController extends AbstractController public function index(): Response { return $this->render('category/categories.html.twig', [ +// 'controller_name' => 'CategoryController', 'categories' => $this->getDoctrine()->getRepository(Category::class)->findAll() ]); } @@ -39,6 +40,7 @@ class CategoryController extends AbstractController return $this->redirectToRoute('categories'); } return $this->render('category/categories-form.html.twig', [ +// 'controller_name' => 'CategoryController', 'form' => $form->createView(), 'title' => 'Add new category' ]); @@ -60,11 +62,11 @@ class CategoryController extends AbstractController $form = $this->createForm(CategoryType::class, $cat); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $cat = $form->getData(); $manager->flush(); return $this->redirectToRoute('categories'); } return $this->render('category/categories-form.html.twig', [ +// 'controller_name' => 'CategoryController', 'form' => $form->createView(), 'title' => 'Edit '.$cat->getName() ]); @@ -90,7 +92,8 @@ class CategoryController extends AbstractController public function categorySummary(): Response { 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() ]); } } diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index fdec3b4..672ed56 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -15,6 +15,7 @@ class CommentController extends AbstractController public function index(): Response { return $this->render('comment/comments.html.twig', [ +// 'controller_name' => 'CommentController', 'comments' => $this->getDoctrine()->getRepository(Comment::class)->findAll() ]); } @@ -56,6 +57,7 @@ class CommentController extends AbstractController public function recentComment(): Response { 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) ]); } diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 834391a..c87be4f 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -2,6 +2,7 @@ namespace App\Controller; +use App\Entity\Category; use App\Entity\Comment; use App\Entity\Post; use App\Form\CommentType; @@ -23,15 +24,10 @@ class PostController extends AbstractController public function index(int $page = 0): Response { $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', [ +// 'controller_name' => 'HomeController', // not use on template 'posts' => $repo->getPublished($page*5, 5), - 'pages' => $pages-1, + 'pages' => round(count($repo->getPublished())/5, 0, PHP_ROUND_HALF_UP)-1, 'page' => $page ]); } @@ -44,10 +40,13 @@ class PostController extends AbstractController */ public function post(Request $request, string $slug): Response { + /** @var Post $post */ $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"); } + $form = $this->commentFormGenerator($post); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { @@ -59,6 +58,7 @@ class PostController extends AbstractController $form = $this->commentFormGenerator($post); } return $this->render('post/index.html.twig', [ +// 'controller_name' => 'PostController', // not use on template 'post' => $post, 'form' => $form->createView() ]); @@ -70,6 +70,7 @@ class PostController extends AbstractController public function posts(): Response { 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')) ]); } @@ -93,6 +94,7 @@ class PostController extends AbstractController return $this->redirectToRoute('posts'); } return $this->render('post/posts-form.html.twig', [ +// 'controller_name' => 'PostController', // not use on template 'form' => $form->createView(), 'title' => 'Add new post' ]); @@ -121,6 +123,7 @@ class PostController extends AbstractController return $this->redirectToRoute('posts'); } return $this->render('post/posts-form.html.twig', [ +// 'controller_name' => 'PostController', // not use on template 'form' => $form->createView(), 'title' => 'Edit '.$post->getTitle() ]); diff --git a/src/Repository/CategoryRepository.php b/src/Repository/CategoryRepository.php index a12b228..c17a2de 100644 --- a/src/Repository/CategoryRepository.php +++ b/src/Repository/CategoryRepository.php @@ -19,6 +19,20 @@ class CategoryRepository extends ServiceEntityRepository 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 // */