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 %} +