From fa47dcd5b616badbdf0f00dc33ae2d7fab3b465b Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 6 Sep 2023 15:50:12 +0200 Subject: [PATCH] FIX [center][crud] use crud controller for center entity --- .../Controller/CenterController.php | 168 +----------------- .../ChillMainExtension.php | 24 +++ .../ChillMainBundle/Form/CenterType.php | 27 ++- .../views/Admin/Center/edit.html.twig | 11 ++ .../views/Admin/Center/index.html.twig | 39 ++++ .../views/Admin/Center/new.html.twig | 11 ++ .../Resources/views/Center/edit.html.twig | 23 --- .../Resources/views/Center/index.html.twig | 50 ------ .../Resources/views/Center/new.html.twig | 23 --- .../MenuBuilder/AdminUserMenuBuilder.php | 2 +- src/Bundle/ChillMainBundle/config/routes.yaml | 6 +- .../translations/messages.fr.yml | 6 + 12 files changed, 113 insertions(+), 277 deletions(-) create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Admin/Center/edit.html.twig create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Admin/Center/index.html.twig create mode 100644 src/Bundle/ChillMainBundle/Resources/views/Admin/Center/new.html.twig delete mode 100644 src/Bundle/ChillMainBundle/Resources/views/Center/edit.html.twig delete mode 100644 src/Bundle/ChillMainBundle/Resources/views/Center/index.html.twig delete mode 100644 src/Bundle/ChillMainBundle/Resources/views/Center/new.html.twig diff --git a/src/Bundle/ChillMainBundle/Controller/CenterController.php b/src/Bundle/ChillMainBundle/Controller/CenterController.php index 1cc129e5d..fef36c11b 100644 --- a/src/Bundle/ChillMainBundle/Controller/CenterController.php +++ b/src/Bundle/ChillMainBundle/Controller/CenterController.php @@ -11,178 +11,22 @@ declare(strict_types=1); namespace Chill\MainBundle\Controller; +use Chill\MainBundle\CRUD\Controller\CRUDController; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Form\CenterType; -use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Chill\MainBundle\Pagination\PaginatorInterface; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\HttpFoundation\Request; /** * Class CenterController. */ -class CenterController extends AbstractController +class CenterController extends CRUDController { - /** - * Creates a new Center entity. - */ - public function createAction(Request $request) + protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator) { - $center = new Center(); - $form = $this->createCreateForm($center); - $form->handleRequest($request); + $query->addOrderBy('e.name', 'ASC'); - if ($form->isValid()) { - $em = $this->getDoctrine()->getManager(); - $em->persist($center); - $em->flush(); - - return $this->redirect($this->generateUrl('admin_center')); - } - - return $this->render('@ChillMain/Center/new.html.twig', [ - 'entity' => $center, - 'form' => $form->createView(), - ]); - } - - /** - * Displays a form to edit an existing Center entity. - * - * @param mixed $id - */ - public function editAction($id) - { - $em = $this->getDoctrine()->getManager(); - - $center = $em->getRepository(\Chill\MainBundle\Entity\Center::class)->find($id); - - if (!$center) { - throw $this->createNotFoundException('Unable to find Center entity.'); - } - - $editForm = $this->createEditForm($center); - - return $this->render('@ChillMain/Center/edit.html.twig', [ - 'entity' => $center, - 'edit_form' => $editForm->createView(), - ]); - } - - /** - * Lists all Center entities. - */ - public function indexAction() - { - $em = $this->getDoctrine()->getManager(); - - $entities = $em->getRepository(\Chill\MainBundle\Entity\Center::class)->findAll(); - - usort($entities, fn (Center $a, Center $b) => $a->getName() <=> $b->getName()); - - return $this->render('@ChillMain/Center/index.html.twig', [ - 'entities' => $entities, - ]); - } - - /** - * Displays a form to create a new Center entity. - */ - public function newAction() - { - $center = new Center(); - $form = $this->createCreateForm($center); - - return $this->render('@ChillMain/Center/new.html.twig', [ - 'entity' => $center, - 'form' => $form->createView(), - ]); - } - - /** - * Finds and displays a Center entity. - * - * @param mixed $id - */ - public function showAction($id) - { - $em = $this->getDoctrine()->getManager(); - - $center = $em->getRepository(\Chill\MainBundle\Entity\Center::class)->find($id); - - if (!$center) { - throw $this->createNotFoundException('Unable to find Center entity.'); - } - - return $this->render('@ChillMain/Center/show.html.twig', [ - 'entity' => $center, - ]); - } - - /** - * Edits an existing Center entity. - * - * @param mixed $id - */ - public function updateAction(Request $request, $id) - { - $em = $this->getDoctrine()->getManager(); - - $center = $em->getRepository(\Chill\MainBundle\Entity\Center::class)->find($id); - - if (!$center) { - throw $this->createNotFoundException('Unable to find Center entity.'); - } - - $editForm = $this->createEditForm($center); - $editForm->handleRequest($request); - - if ($editForm->isValid()) { - $em->flush(); - - return $this->redirect($this->generateUrl('admin_center_edit', ['id' => $id])); - } - - return $this->render('@ChillMain/Center/edit.html.twig', [ - 'entity' => $center, - 'edit_form' => $editForm->createView(), - ]); - } - - /** - * Creates a form to create a Center entity. - * - * @param Center $center The entity - * - * @return \Symfony\Component\Form\Form The form - */ - private function createCreateForm(Center $center) - { - $form = $this->createForm(CenterType::class, $center, [ - 'action' => $this->generateUrl('admin_center_create'), - 'method' => 'POST', - ]); - - $form->add('submit', SubmitType::class, ['label' => 'Create']); - - return $form; - } - - /** - * Creates a form to edit a Center entity. - * - * @param Center $center The entity - * - * @return \Symfony\Component\Form\Form The form - */ - private function createEditForm(Center $center) - { - $form = $this->createForm(CenterType::class, $center, [ - 'action' => $this->generateUrl('admin_center_update', ['id' => $center->getId()]), - 'method' => 'PUT', - ]); - - $form->add('submit', SubmitType::class, ['label' => 'Update']); - - return $form; + return parent::orderQuery($action, $query, $request, $paginator); } } diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index c86a69be2..a8ac588f1 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace Chill\MainBundle\DependencyInjection; use Chill\MainBundle\Controller\AddressApiController; +use Chill\MainBundle\Controller\CenterController; use Chill\MainBundle\Controller\CivilityApiController; use Chill\MainBundle\Controller\CivilityController; use Chill\MainBundle\Controller\CountryController; @@ -44,6 +45,7 @@ use Chill\MainBundle\Doctrine\DQL\Unaccent; use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator; use Chill\MainBundle\Doctrine\Type\NativeDateIntervalType; use Chill\MainBundle\Doctrine\Type\PointType; +use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Civility; use Chill\MainBundle\Entity\Country; use Chill\MainBundle\Entity\GeographicalUnitLayer; @@ -53,6 +55,7 @@ use Chill\MainBundle\Entity\LocationType; use Chill\MainBundle\Entity\Regroupment; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\UserJob; +use Chill\MainBundle\Form\CenterType; use Chill\MainBundle\Form\CivilityType; use Chill\MainBundle\Form\CountryType; use Chill\MainBundle\Form\LanguageType; @@ -524,6 +527,27 @@ class ChillMainExtension extends Extension implements ], ], ], + [ + 'class' => Center::class, + 'name' => 'center', + 'base_path' => '/admin/center', + 'form_class' => CenterType::class, + 'controller' => CenterController::class, + 'actions' => [ + 'index' => [ + 'role' => 'ROLE_ADMIN', + 'template' => '@ChillMain/Admin/Center/index.html.twig', + ], + 'new' => [ + 'role' => 'ROLE_ADMIN', + 'template' => '@ChillMain/Admin/Center/new.html.twig', + ], + 'edit' => [ + 'role' => 'ROLE_ADMIN', + 'template' => '@ChillMain/Admin/Center/edit.html.twig', + ], + ], + ], ], 'apis' => [ [ diff --git a/src/Bundle/ChillMainBundle/Form/CenterType.php b/src/Bundle/ChillMainBundle/Form/CenterType.php index ff758ca49..ed5a496d6 100644 --- a/src/Bundle/ChillMainBundle/Form/CenterType.php +++ b/src/Bundle/ChillMainBundle/Form/CenterType.php @@ -11,7 +11,10 @@ declare(strict_types=1); namespace Chill\MainBundle\Form; +use Chill\MainBundle\Entity\Center; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -21,24 +24,18 @@ class CenterType extends AbstractType public function buildForm(FormBuilderInterface $builder, array $options) { $builder - ->add('name', TextType::class); + ->add('name', TextType::class, [ + 'label' => 'Nom', + ]); +/* ->add('isActive', CheckboxType::class, [ + 'label' => 'Actif ?', + 'required' => false, + ]);*/ } - /** - * @param OptionsResolverInterface $resolver - */ public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults([ - 'data_class' => \Chill\MainBundle\Entity\Center::class, - ]); - } - - /** - * @return string - */ - public function getBlockPrefix() - { - return 'chill_mainbundle_center'; + $resolver + ->setDefault('class', Center::class); } } diff --git a/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/edit.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/edit.html.twig new file mode 100644 index 000000000..4d55c480c --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/edit.html.twig @@ -0,0 +1,11 @@ +{% extends '@ChillMain/CRUD/Admin/index.html.twig' %} + +{% block title %} + {% include('@ChillMain/CRUD/_edit_title.html.twig') %} +{% endblock %} + +{% block admin_content %} + {% embed '@ChillMain/CRUD/_edit_content.html.twig' %} + {% block content_form_actions_save_and_show %}{% endblock %} + {% endembed %} +{% endblock admin_content %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/index.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/index.html.twig new file mode 100644 index 000000000..e5554f6d9 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/index.html.twig @@ -0,0 +1,39 @@ +{% extends '@ChillMain/CRUD/Admin/index.html.twig' %} + +{% block admin_content %} + {% embed '@ChillMain/CRUD/_index.html.twig' %} + {% block table_entities_thead_tr %} + {{ 'Label'|trans }} +{# {{ 'Active'|trans }}#} +   + {% endblock %} + + {% block table_entities_tbody %} + {% for entity in entities %} + + {{ entity.name }} +{# #} +{# {% if entity.isActive %}#} +{# #} +{# {% else %}#} +{# #} +{# {% endif %}#} +{# #} + + + + + {% endfor %} + {% endblock %} + + {% block actions_before %} +
  • + {{'Back to the admin'|trans}} +
  • + {% endblock %} + {% endembed %} +{% endblock %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/new.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/new.html.twig new file mode 100644 index 000000000..7c204dddd --- /dev/null +++ b/src/Bundle/ChillMainBundle/Resources/views/Admin/Center/new.html.twig @@ -0,0 +1,11 @@ +{% extends '@ChillMain/CRUD/Admin/index.html.twig' %} + +{% block title %} + {% include('@ChillMain/CRUD/_new_title.html.twig') %} +{% endblock %} + +{% block admin_content %} + {% embed '@ChillMain/CRUD/_new_content.html.twig' %} + {% block content_form_actions_save_and_show %}{% endblock %} + {% endembed %} +{% endblock admin_content %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Center/edit.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Center/edit.html.twig deleted file mode 100644 index 1894a1402..000000000 --- a/src/Bundle/ChillMainBundle/Resources/views/Center/edit.html.twig +++ /dev/null @@ -1,23 +0,0 @@ -{% extends '@ChillMain/Admin/layoutWithVerticalMenu.html.twig' %} - -{% block title %}{{ 'Center edit'|trans }}{% endblock %} - -{% block admin_content -%} -

    {{ 'Center edit'|trans }}

    - - {{ form_start(edit_form) }} - {{ form_row(edit_form.name) }} - - - - {{ form_end(edit_form) }} -{% endblock %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Center/index.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Center/index.html.twig deleted file mode 100644 index cb2da8a62..000000000 --- a/src/Bundle/ChillMainBundle/Resources/views/Center/index.html.twig +++ /dev/null @@ -1,50 +0,0 @@ -{% extends '@ChillMain/CRUD/Admin/index.html.twig' %} - -{% block title %}{{ 'Center list'|trans }}{% endblock %} - -{% block admin_content -%} - {% embed '@ChillMain/CRUD/_index.html.twig' %} - - {% block index_header %} -

    {{ 'Center list'|trans }}

    - {% endblock %} - - {% block filter_order %}{% endblock %} - - {% block table_entities_thead_tr %} - id - {{ 'Name'|trans }} - {{ 'Actions'|trans }} - {% endblock %} - - {% block table_entities_tbody %} - {% for entity in entities %} - - {{ entity.id }} - {{ entity.name }} - - - - - {% endfor %} - {% endblock %} - - {% block pagination %}{% endblock %} - - {% block list_actions %} - - {% endblock list_actions %} - - {% endembed %} -{% endblock %} diff --git a/src/Bundle/ChillMainBundle/Resources/views/Center/new.html.twig b/src/Bundle/ChillMainBundle/Resources/views/Center/new.html.twig deleted file mode 100644 index cc64b37c2..000000000 --- a/src/Bundle/ChillMainBundle/Resources/views/Center/new.html.twig +++ /dev/null @@ -1,23 +0,0 @@ -{% extends '@ChillMain/Admin/layoutWithVerticalMenu.html.twig' %} - -{% block title %}{{ 'Center creation'|trans }}{% endblock %} - -{% block admin_content -%} -

    {{ 'Center creation'|trans }}

    - - {{ form_start(form) }} - {{ form_row(form.name) }} - - - - {{ form_end(form) }} -{% endblock %} diff --git a/src/Bundle/ChillMainBundle/Routing/MenuBuilder/AdminUserMenuBuilder.php b/src/Bundle/ChillMainBundle/Routing/MenuBuilder/AdminUserMenuBuilder.php index 7bd23c81f..8e2ccb7c6 100644 --- a/src/Bundle/ChillMainBundle/Routing/MenuBuilder/AdminUserMenuBuilder.php +++ b/src/Bundle/ChillMainBundle/Routing/MenuBuilder/AdminUserMenuBuilder.php @@ -49,7 +49,7 @@ class AdminUserMenuBuilder implements LocalMenuBuilderInterface ]); $menu->addChild('Center list', [ - 'route' => 'admin_center', + 'route' => 'chill_crud_center_index', ])->setExtras(['order' => 1010]); $menu->addChild('Regroupements des centres', [ diff --git a/src/Bundle/ChillMainBundle/config/routes.yaml b/src/Bundle/ChillMainBundle/config/routes.yaml index d25f2aaff..7e2af7a7f 100644 --- a/src/Bundle/ChillMainBundle/config/routes.yaml +++ b/src/Bundle/ChillMainBundle/config/routes.yaml @@ -10,9 +10,9 @@ chill_main_admin_scope: resource: "@ChillMainBundle/config/routes/scope.yaml" prefix: "{_locale}/admin/scope" -chill_main_admin: - resource: "@ChillMainBundle/config/routes/center.yaml" - prefix: "{_locale}/admin/center" +#chill_main_admin: +# resource: "@ChillMainBundle/config/routes/center.yaml" +# prefix: "{_locale}/admin/center" chill_main_exports: resource: "@ChillMainBundle/config/routes/exports.yaml" diff --git a/src/Bundle/ChillMainBundle/translations/messages.fr.yml b/src/Bundle/ChillMainBundle/translations/messages.fr.yml index 6a6136a41..6cbb1fef4 100644 --- a/src/Bundle/ChillMainBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillMainBundle/translations/messages.fr.yml @@ -425,6 +425,12 @@ crud: add_new: Ajouter un regroupement title_new: Nouveau regroupement title_edit: Modifier un regroupement + center: + index: + title: Liste des centres + add_new: Ajouter un centre + title_new: Nouveau centre + title_edit: Modifier un centre No entities: Aucun élément