<?php
namespace App\Controller;
use App\Entity\Chercheur;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
use DateTimeZone;
use DateTime;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Cocur\Slugify\Slugify;
use Symfony\Component\HttpFoundation\File\Exception\FileException;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request,MailerInterface $mailer, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response
{
$user = new Chercheur();
$user->setRoles(['ROLE_CHERCHEUR']);
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setDateinscription(new DateTime('now', new DateTimeZone('Africa/Tunis')));
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$photo = $form->get('photo')->getData();
if ( $photo) {
$originalLogoname = pathinfo( $photo->getClientOriginalName(), PATHINFO_FILENAME);
$slugify = new Slugify();
$a= $slugify->slugify($originalLogoname, ['separator' => '-']);
$newLogoname = $a.'-'.uniqid().'.'.$photo->guessExtension();
// Move the file to the directory where brochures are stored
try {
$photo->move(
$this->getParameter('chercheur_directory'),
$newLogoname
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$user->setPhoto($newLogoname);
}
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
$emailMessage = (new TemplatedEmail())
->from(new Address('rfmi@smart-it-partner.com', 'RFMI'))
->to($user->getEmail())
->subject('Welcome! Registration successfully completed for RFMI 2026')
->htmlTemplate('registration/email_Inscri.html.twig')
->context([
'nom' => $user->getNom(),
'prenom' => $user->getNom(),
'email_chercheur' => $user->getEmail(),
'password' => $form->get('plainPassword')->getData(),
]);
$mailer->send($emailMessage);
return $this->redirectToRoute('app_login');
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
}