From 8d92d3f9fc43538fd611cf93eb01ae4c6a1b0951 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 6 Aug 2025 12:20:05 +0200 Subject: [PATCH] update organisation --- src/Controller/OrganizationController.php | 42 ++++++++++++++++++++++- src/Controller/UserController.php | 2 +- src/Form/OrganizationForm.php | 2 +- templates/organization/edit.html.twig | 21 ++++++++++++ templates/organization/show.html.twig | 8 +++-- 5 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 templates/organization/edit.html.twig diff --git a/src/Controller/OrganizationController.php b/src/Controller/OrganizationController.php index ccbb3b8..4320576 100644 --- a/src/Controller/OrganizationController.php +++ b/src/Controller/OrganizationController.php @@ -90,7 +90,7 @@ class OrganizationController extends AbstractController ]); } - #[Route('/{id}', name: 'show',requirements: ['id' => '\d+'], methods: ['GET'])] + #[Route('/{id}', name: 'show', requirements: ['id' => '\d+'], methods: ['GET'])] public function show(int $id, ActionService $actionService): Response { if ($this->isGranted('ROLE_ADMIN')) { @@ -137,6 +137,46 @@ class OrganizationController extends AbstractController ]); } + #[Route('/edit/{id}', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET', 'POST'])] + public function edit(Request $request): Response + { + $id = $request->attributes->get('id'); + if (!$this->isGranted('ROLE_SUPER_ADMIN')) { + throw $this->createNotFoundException(self::ACCESS_DENIED); + } + $organization = $this->entityManager->getRepository(Organizations::class)->find($id); + if (!$organization) { + throw $this->createNotFoundException(self::NOT_FOUND); + } + $form = $this->createForm(OrganizationForm::class, $organization); + $form->handleRequest($request); + if ($form->isSubmitted() && $form->isValid()) { + $logoFile = $form->get('logoUrl')->getData(); + + if ($logoFile) { + $currentDate = (new \DateTime())->format('Y-m-d'); + $organizationName = preg_replace('/[^a-zA-Z0-9]/', '_', $organization->getName()); + $extension = $logoFile->guessExtension(); + $newFilename = $currentDate . '_' . $organizationName . '.' . $extension; + // Move the file to the directory where logos are stored + $logoFile->move( + $this->getParameter('logos_directory'), + $newFilename + ); + + // Update the 'logoUrl' property to store the file name + $organization->setLogoUrl($newFilename); + } + $this->entityManager->persist($organization); + $this->entityManager->flush(); + $this->addFlash('success', 'Organization updated successfully'); + return $this->redirectToRoute('organization_index'); + } + return $this->render('organization/edit.html.twig', [ + 'form' => $form->createView(), + 'organization' => $organization, + ]); + } } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 3ac6989..31d7f83 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -148,7 +148,7 @@ class UserController extends AbstractController /** * GET /user/{id}/edit - Show form to edit user */ - #[Route('/{id}/edit', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET', 'PUT', 'POST'])] + #[Route('/edit/{id}', name: 'edit', requirements: ['id' => '\d+'], methods: ['GET', 'PUT', 'POST'])] public function edit(int $id, EntityManagerInterface $entityManager, Request $request): Response { //Handle access control diff --git a/src/Form/OrganizationForm.php b/src/Form/OrganizationForm.php index e2d1b32..571b773 100644 --- a/src/Form/OrganizationForm.php +++ b/src/Form/OrganizationForm.php @@ -22,7 +22,7 @@ class OrganizationForm extends AbstractType ->add('logoUrl', FileType::class, [ 'required' => false, 'label' => 'Logo', - 'mapped' => true, // Important if the entity property is not directly mapped + 'mapped' => false, // Important if the entity property is not directly mapped 'attr' => ['accept' => 'image/*'], ]); } diff --git a/templates/organization/edit.html.twig b/templates/organization/edit.html.twig new file mode 100644 index 0000000..be0f350 --- /dev/null +++ b/templates/organization/edit.html.twig @@ -0,0 +1,21 @@ +{% extends 'base.html.twig' %} + +{% block body %} +
+
+
+

Modifier l'organisation

+ {% if is_granted("ROLE_SUPER_ADMIN") %} +{# Supprimer#} + {% endif %} +
+
+ + {{ form_start(form, {'action': path('organization_edit', {'id': organization.id}), 'method': 'PUT'}) }} + {{ form_widget(form) }} + + {{ form_end(form) }} +
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/organization/show.html.twig b/templates/organization/show.html.twig index bc0b401..a60df7d 100644 --- a/templates/organization/show.html.twig +++ b/templates/organization/show.html.twig @@ -5,11 +5,13 @@

{% if organization.logoUrl %} - Organization logo + Organization logo {% endif %} {{ organization.name|title }} - Dashboard

- {% if is_granted("ROLE_SUER_ADMIN") %} - {# Désactiver #} + {% if is_granted("ROLE_SUPER_ADMIN") %} + Gérer mon + organisation {% endif %}
{# USER ROW #}