From 368b4fbcd3d5907a988f10a87e0564c0935b4430 Mon Sep 17 00:00:00 2001 From: Ryan Prather Date: Fri, 3 Jan 2025 14:01:54 +0000 Subject: [PATCH] allow uploading profile image --- src/Controller/AdminController.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index df1b98b..57bf7ac 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -20,6 +20,7 @@ use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Http\Attribute\CurrentUser; use Symfony\Component\Security\Http\Attribute\IsGranted; +use Symfony\Component\String\Slugger\SluggerInterface; #[IsGranted('ROLE_ADMIN')] class AdminController extends AbstractController @@ -96,8 +97,11 @@ class AdminController extends AbstractController } #[Route('/add-user', name: 'app_add_user')] - public function addUser(Request $request, #[CurrentUser()] User $admin): Response - { + public function addUser( + Request $request, + #[CurrentUser()] User $admin, + SluggerInterface $slugger + ): Response { $this->denyAccessUnlessGranted('ROLE_ADMIN'); $this->msgs = $this->entityManager->getRepository(Messages::class)->getUnreadMessages($admin); $this->notificationCount = $this->entityManager->getRepository(Messages::class)->getUnreadMessageCount($admin); @@ -143,6 +147,19 @@ class AdminController extends AbstractController ->setCompany($admin->getCompany()) ; + if ($form->get('imageName')->getData()) { + /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */ + $file = $form['imageName']->getData(); + $destination = $this->getParameter('kernel.project_dir').'/public/uploads/user_images/'; + $originalFilename = pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME); + $newFilename = $slugger->slug($originalFilename).'-'.uniqid().'.'.$file->guessExtension(); + $file->move( + $destination, + $newFilename + ); + + $user->setImageName($newFilename); + } $msg = MessageFactory::createUser($admin, $user, 'Welcome', "Welcome to CM Tracker");