Categories administration
This commit is contained in:
parent
441443e426
commit
71ffcefab9
7 changed files with 142 additions and 4 deletions
|
@ -18,3 +18,10 @@
|
||||||
height: 100%;
|
height: 100%;
|
||||||
z-index: 7;
|
z-index: 7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.floating-btn {
|
||||||
|
position:fixed;
|
||||||
|
bottom: 45px;
|
||||||
|
right: 24px;
|
||||||
|
z-index: 100;
|
||||||
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Entity\Category;
|
use App\Entity\Category;
|
||||||
|
use App\Form\CategoryType;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
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
|
public function categorySummary(): Response
|
||||||
{
|
{
|
||||||
return $this->render('category/category_summary.html.twig', [
|
return $this->render('category/category_summary.html.twig', [
|
||||||
|
|
29
src/Form/CategoryType.php
Normal file
29
src/Form/CategoryType.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Form;
|
||||||
|
|
||||||
|
use App\Entity\Category;
|
||||||
|
use Symfony\Component\Form\AbstractType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
class CategoryType extends AbstractType
|
||||||
|
{
|
||||||
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
|
{
|
||||||
|
$builder
|
||||||
|
->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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
17
templates/category/categories-form.html.twig
Normal file
17
templates/category/categories-form.html.twig
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
{% extends 'admin/base-admin.html.twig' %}
|
||||||
|
{% block title %}{{ title }}{% endblock %}
|
||||||
|
|
||||||
|
{% block body %}
|
||||||
|
<div class="container-fluid">
|
||||||
|
<h1>{{ title }}</h1>
|
||||||
|
{{ form_start(form) }}
|
||||||
|
{{ form_errors(form) }}
|
||||||
|
<div class="form-outline">
|
||||||
|
{{ form_widget(form.name) }}
|
||||||
|
{{ form_label(form.name, null, {'label_attr': {'class': 'form-label'}}) }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{{ form_row(form.save) }}
|
||||||
|
{{ form_end(form) }}
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
|
@ -1,7 +1,22 @@
|
||||||
{% extends 'admin/base-admin.html.twig' %}
|
{% extends 'admin/base-admin.html.twig' %}
|
||||||
|
|
||||||
{% block title %}Hello AdminController!{% endblock %}
|
{% block title %}Administration - Categories{% endblock %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
<div class="floating-btn">
|
||||||
|
<a href="{{ path('category-add') }}" class="btn btn-primary btn-floating" >
|
||||||
|
<i class="fas fa-plus"></i>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<div class="container-fluid">
|
||||||
|
{% for category in categories %}
|
||||||
|
<div class="card text-center">
|
||||||
|
<div class="card-body">
|
||||||
|
<h5 class="card-title">{{ category.name }}</h5>
|
||||||
|
<a href="{{ path('category-remove', {id: category.id}) }}" class="btn btn-danger">Delete</a>
|
||||||
|
<a href="{{ path('category-edit', {id: category.id}) }}" class="btn btn-primary">Edit</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
{% block title %}Administration - Posts{% endblock %}
|
{% block title %}Administration - Posts{% endblock %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<div style="position:fixed; bottom: 45px; right: 24px; z-index: 100">
|
<div class="floating-btn">
|
||||||
<a href="{{ path('post-add') }}" class="btn btn-primary btn-floating" >
|
<a href="{{ path('post-add') }}" class="btn btn-primary btn-floating" >
|
||||||
<i class="fas fa-plus"></i>
|
<i class="fas fa-plus"></i>
|
||||||
</a>
|
</a>
|
||||||
|
|
Reference in a new issue