From 71ffcefab9e781e4314e74a2b2ca2fe5bb265733 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 16 Jan 2021 19:17:36 +0100 Subject: [PATCH] Categories administration --- assets/scss/app.scss | 7 ++ src/Controller/CategoryController.php | 70 ++++++++++++++++++++ src/Controller/PostController.php | 2 +- src/Form/CategoryType.php | 29 ++++++++ templates/category/categories-form.html.twig | 17 +++++ templates/category/categories.html.twig | 19 +++++- templates/post/posts.html.twig | 2 +- 7 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 src/Form/CategoryType.php create mode 100644 templates/category/categories-form.html.twig diff --git a/assets/scss/app.scss b/assets/scss/app.scss index e6cf8be..e13df79 100644 --- a/assets/scss/app.scss +++ b/assets/scss/app.scss @@ -18,3 +18,10 @@ height: 100%; z-index: 7; } + +.floating-btn { + position:fixed; + bottom: 45px; + right: 24px; + z-index: 100; +} diff --git a/src/Controller/CategoryController.php b/src/Controller/CategoryController.php index 46cd11b..0d805cc 100644 --- a/src/Controller/CategoryController.php +++ b/src/Controller/CategoryController.php @@ -3,7 +3,9 @@ namespace App\Controller; use App\Entity\Category; +use App\Form\CategoryType; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; @@ -20,6 +22,74 @@ class CategoryController extends AbstractController ]); } + /** + * @Route("/admin/categories/add", name="category-add") + * @param Request $request + * @return Response + */ + public function add(Request $request): Response + { + $cat = new Category(); + $form = $this->createForm(CategoryType::class, $cat); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $cat = $form->getData(); + $manager = $this->getDoctrine()->getManager(); + $manager->persist($cat); + $manager->flush(); + return $this->redirectToRoute('categories'); + } + return $this->render('category/categories-form.html.twig', [ + 'controller_name' => 'CategoryController', + 'form' => $form->createView(), + 'title' => 'Add new category' + ]); + } + + /** + * @Route("/admin/categories/edit/{id}", name="category-edit") + * @param Request $request + * @param int $id + * @return Response + */ + public function edit(Request $request, int $id): Response + { + $manager = $this->getDoctrine()->getManager(); + $cat = $manager->getRepository(Category::class)->find($id); + if (!$cat) { + throw $this->createNotFoundException("Category not found"); + } + $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() + ]); + } + + /** + * @Route("/admin/categories/remove/{id}", name="category-remove") + * @param int $id + * @return Response + */ + public function remove(int $id): Response + { + $manager = $this->getDoctrine()->getManager(); + $cat = $manager->getRepository(Category::class)->find($id); + if (!$cat) { + throw $this->createNotFoundException("Category not found"); + } + $manager->remove($cat); + $manager->flush(); + return $this->redirectToRoute('categories'); + } + public function categorySummary(): Response { return $this->render('category/category_summary.html.twig', [ diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index 7446268..92a8c04 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -76,7 +76,7 @@ class PostController extends AbstractController 'controller_name' => 'PostController', 'form' => $form->createView(), 'title' => 'Add new post' - ]); + ]); } /** diff --git a/src/Form/CategoryType.php b/src/Form/CategoryType.php new file mode 100644 index 0000000..d6f263c --- /dev/null +++ b/src/Form/CategoryType.php @@ -0,0 +1,29 @@ +add('name', TextType::class, ['attr' => ['class' => 'form-control']]) + ->add('save', SubmitType::class, ['attr' => ['class' => 'btn btn-primary btn-block']]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => Category::class, + ]); + } +} diff --git a/templates/category/categories-form.html.twig b/templates/category/categories-form.html.twig new file mode 100644 index 0000000..fad8200 --- /dev/null +++ b/templates/category/categories-form.html.twig @@ -0,0 +1,17 @@ +{% extends 'admin/base-admin.html.twig' %} +{% block title %}{{ title }}{% endblock %} + +{% block body %} +
+

{{ title }}

+ {{ form_start(form) }} + {{ form_errors(form) }} +
+ {{ form_widget(form.name) }} + {{ form_label(form.name, null, {'label_attr': {'class': 'form-label'}}) }} +
+ + {{ form_row(form.save) }} + {{ form_end(form) }} +
+{% endblock %} diff --git a/templates/category/categories.html.twig b/templates/category/categories.html.twig index 2b9eca2..1d0c4cf 100644 --- a/templates/category/categories.html.twig +++ b/templates/category/categories.html.twig @@ -1,7 +1,22 @@ {% extends 'admin/base-admin.html.twig' %} -{% block title %}Hello AdminController!{% endblock %} +{% block title %}Administration - Categories{% endblock %} {% block body %} - +
+ + + +
+
+ {% for category in categories %} +
+
+
{{ category.name }}
+ Delete + Edit +
+
+ {% endfor %} +
{% endblock %} diff --git a/templates/post/posts.html.twig b/templates/post/posts.html.twig index 1d9cb7a..467b755 100644 --- a/templates/post/posts.html.twig +++ b/templates/post/posts.html.twig @@ -3,7 +3,7 @@ {% block title %}Administration - Posts{% endblock %} {% block body %} -
+