← Back
Editing: UserController.php
<?php namespace App\Controller; use App\Entity\Agency; use App\Entity\AgentFrequency; use App\Entity\Periode; use App\Entity\User; use App\Form\AgentType; use App\Form\UserType; use App\Repository\PeriodeRepository; use App\Repository\UserRepository; use App\Service\AgentService; use App\Service\SolutionService; use App\Service\UserService; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Routing\Annotation\Route; #[Route(path: '/user')] class UserController extends AbstractController { private UserService $userService; /** * UserController constructor. */ public function __construct(UserService $userService) { $this->userService = $userService; } #[Route(path: '/', name: 'user_index', methods: ['GET'])] public function index(AgentService $agentService, UserService $userService, EntityManagerInterface $em): Response { $agents = $userService->getAgentsForCurrentSolution(); $periodes = $em->getRepository(Periode::class)->findAll(); $total = count($agents); $parPage = 20; $nbPages = ceil($total / $parPage); return $this->render( 'user/index.html.twig', [ 'agentService' => $agentService, 'agents' => $agents, 'nbPages' => $nbPages, 'periodes' => $periodes, ] ); } #[Route(path: '/userEnableAjaxAll', name: 'user_enable_ajax_all', methods: ['GET'])] public function fastEditAll(Request $request, EntityManagerInterface $em, UserRepository $userRepository): Response { /** @var User $user */ $user = $this->getUser(); $agency = $user->getAgency(); $agents = $userRepository->findAgentsByAgency($request, $agency->getId()); if (false === boolval($request->get('val'))) { $enabled = false; } else { $enabled = true; } foreach ($agents as $agent) { $agent[0]->setEnabled($enabled); $em->persist($agent[0]); } $em->flush(); return new Response($request->get('val')); } #[Route(path: '/userEnableAjax', name: 'user_enable_ajax', methods: ['GET'])] public function userEnableAjax( Request $request, EntityManagerInterface $em, UserRepository $userRepository ): Response { if (false === boolval($request->get('val'))) { $enabled = false; } else { $enabled = true; } $user = $userRepository->find(intval($request->get('id'))); $user->setEnabled($enabled); $em->persist($user); $em->flush(); return new Response($request->get('val')); } #[Route(path: '/new/{id}', name: 'user_new', methods: ['GET', 'POST'])] public function new( Agency $agency, UserService $userService, Request $request, EntityManagerInterface $em ): Response { $user = new User(); $user->setAgency($agency); $password = $userService->randomPassword(); $user->setPlainPassword($password); $form = $this->createForm(UserType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $user->setUsername($form->getData()->getEmail()); $user->setUsernameCanonical($form->getData()->getEmail()); $em->persist($user); $em->flush(); return $this->redirectToRoute('agency_index'); } return $this->render( 'user/new.html.twig', [ 'user' => $user, 'form' => $form->createView(), ] ); } /** * @return RedirectResponse|Response */ #[Route(path: '/newAgent', name: 'user_newAgent', methods: ['GET', 'POST'])] public function newAgent( Request $request, SolutionService $solutionService, PeriodeRepository $periodeRepository, UserRepository $userRepository, EntityManagerInterface $em ) { /** @var User $currentUser */ $currentUser = $this->getUser(); $user = new User(); $user->addSolution($solutionService->getCurrent()); $user->setRoles(['ROLE_AGENT']); $user->setManager($currentUser); $user->setAgency($currentUser->getAgency()); $existingAgents = $userRepository->getExistingAgentsFromOtherSolutions($currentUser); $frequency = new AgentFrequency(); $frequency->setSolution($solutionService->getCurrent()); $frequency->setFrequencyPeriod($periodeRepository->findOneBy(['nom' => 'MOIS'])); $user->addAgentFrequency($frequency); $form = $this->createForm(AgentType::class, $user); $form->handleRequest($request); if ($form->isSubmitted()) { try { $this->userService->createUser($user); $this->userService->sendNewAccountEmail($user); $em->flush(); return $this->redirectToRoute('user_index'); } catch (\Exception $e) { $this->addFlash('danger', $e->getMessage()); return $this->redirectToRoute('user_newAgent'); } } return $this->render( 'user/newAgent.html.twig', [ 'form' => $form->createView(), 'existingAgents' => $existingAgents, ] ); } /** * @throws \Exception */ #[Route(path: '/newAgent/assoc', name: 'agent_assoc')] public function importFromExistingAgent( Request $request, UserRepository $userRepository, SolutionService $solutionService, AgentService $agentService, EntityManagerInterface $em ): RedirectResponse { $existingId = $request->request->get('existing-agent'); /** @var User $user */ $user = $this->getUser(); $agent = $userRepository->find($existingId); if (!$agent || $agent->getManager()->getId() !== $user->getId()) { throw $this->createNotFoundException('This agent does not exist'); } $agentService->setAgent($agent); $agentFrequency = $agent->getAgentFrequencyBySolution($solutionService->getCurrent()); if (null === $agentFrequency) { $agentFrequency = $agentService->createDefaultFrequency($agent); } $agent->addSolution($solutionService->getCurrent()); $agent->addAgentFrequency($agentFrequency); $em->persist($agent); $em->flush(); $this->addFlash('success', 'L\'agent a bien été importé, veuillez éditer ses informations'); return $this->redirectToRoute('agent_edit', ['id' => $agent->getId()]); } #[Route(path: '/editAgent/{id}', name: 'agent_edit')] public function editAgent(User $agent, Request $request, EntityManagerInterface $em): Response { $form = $this->createForm(AgentType::class, $agent); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->persist($agent); $em->flush(); $this->addFlash('success', 'L\'agent a bien été modifié'); } return $this->render( 'user/editAgent.html.twig', [ 'form' => $form->createView(), ] ); } #[Route(path: '/{id}', name: 'user_show', methods: ['GET'])] public function show(User $user): Response { return $this->render( 'user/show.html.twig', [ 'user' => $user, ] ); } #[Route(path: '/{id}/edit', name: 'user_edit', methods: ['GET', 'POST'])] public function edit(Request $request, User $user, EntityManagerInterface $em): Response { $form = $this->createForm(UserType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $response = $request->get('user'); $user->setUsername($response->getEmail()); $em->persist($user); $em->flush(); return $this->redirectToRoute( 'agency_index', [ 'id' => $user->getId(), ] ); } return $this->render( 'user/edit.html.twig', [ 'user' => $user, 'form' => $form->createView(), ] ); } /** * @throws \Exception */ #[Route(path: '/editAjax', name: 'user_edit_ajax', methods: ['POST'])] public function fastEdit(Request $request, UserService $userService, EntityManagerInterface $em): Response { if (!$request->get('id')) { return new Response('Utilisateur introuvable'); } $user = $em->getRepository(User::class)->find($request->get('id')); if (!$user) { return new Response('Utilisateur introuvable'); } if (/* !$request->get('frequence') || */ !$request->get('objectif')) { return new Response('Une erreur est survenue'); } $user->getAgentFrequencies()[0]->setFrequencyValue($request->get('objectif')); $user->setRappel(null !== $request->get('rappel') ? $request->get('rappel') : $user->getRappel()); $em->persist($user); $em->flush(); $result = $userService->statsByAgent($user); $percentage = $result['pourcentage']; return new JsonResponse(['id' => $user->getId(), 'percentage' => $percentage]); } #[Route(path: '/{id}/delete', name: 'user_delete', methods: ['DELETE', 'POST'])] public function delete( Request $request, User $user, SolutionService $solutionService, EntityManagerInterface $em ): Response { if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) { $user->removeSolution($solutionService->getCurrent()); $em->persist($user); if (0 === $user->getSolutions()->count()) { $em->remove($user); } $em->flush(); } return $this->redirectToRoute('user_index'); } #[Route(path: '/generate-new-password/{id}', name: 'user_send_password')] public function generateNewPassword(User $user, UserService $userService, EntityManagerInterface $em): JsonResponse { $status = 'OK'; $rawPassword = $userService->randomPassword(); $user->setPlainPassword($rawPassword); // Update database time to force lifecycle events (plain password is not persisted) $user->setUpdatedAt(new \DateTime()); $em->persist($user); $em->flush(); try { $userService->sendNewPassword($user, $rawPassword); } catch (TransportExceptionInterface $exception) { $status = 'KO'; } return new JsonResponse(['status' => $status]); } }
Save File
Cancel