src/Controller/BUTicketController.php line 128

Open in your IDE?
  1. <?php 
  2. // src/Controller/BUTicketController.php
  3. namespace App\Controller;
  4. use App\Entity\Category;
  5. use App\Entity\ExternalUser;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\OptionsResolver\OptionsResolver;
  9. use Symfony\Component\HttpFoundation\{RequestResponse};
  10. use App\Service\{BUTicketServiceSpecialCharServiceFileUploaderMailerService};
  11. class BUTicketController extends CoreController
  12. {
  13.     public function configureOptions(OptionsResolver $resolver): void
  14.     {
  15.         $resolver->setDefaults([
  16.             'data_class' => BUTicket::class,
  17.             'required' => true,
  18.         ]);
  19.     }
  20.     /**
  21.     * @Route("/service-de-la-documentation/ticket", name="app_form_bu")
  22.     */
  23.     public function handleForm(Request $requestManagerRegistry $doctrineFileUploader $fileUploaderBUTicketService $buServiceMailerService $mailer): Response
  24.     {
  25.         // Getting all the data we need that is stored in the session
  26.         $session $this->requestStack->getSession();
  27.         extract($this->handleUrlParams($session));
  28.         // Gets the data we need for the form
  29.         $choices $this->getFormData($session$doctrine);
  30.         $choices["departmentId"] = $sector;
  31.         
  32.         extract($this->createFormUtils('BUTicket'$choices));
  33.         // Inspects the given request and calls submit() if the form was submitted
  34.         $form->handleRequest($request);
  35.         
  36.         if ($form->isSubmitted() && $form->isValid()) {
  37.             // uses the service to validate the submitted data
  38.             $errors $buService->validateTicket($form$ticket);
  39.             
  40.             // If there are any error in the array
  41.             // We return the form with the errors
  42.             if (count($errors) > 0) { 
  43.                 $errorArray = [];
  44.                 foreach($errors as $error) {                    
  45.                     $errorArray[$error->getPropertyPath()] = $error->getMessage();
  46.                 }
  47.                 return $this->renderForm('form.html.twig', [
  48.                     'form' => $form,
  49.                     "sector" => $department,
  50.                     "service" => $service,
  51.                     "errors" => $errorArray,
  52.                     "headerLink" => 'app_department_3'
  53.                 ]);
  54.             }
  55.             // We need to know if the user is already in the database, in order to adapt the content of the email that will be sent to them
  56.             $isUserInDB $this->doctrine->getRepository(ExternalUser::class)->findByRealId($ticket->getEmail());
  57.             
  58.             // We use the service to prepare and send the ticket to the WebService
  59.             $response $buService->prepareTicket($ticket);
  60.             // if $response contains the id of the ticket
  61.             if (is_numeric($response)) {
  62.                 // Sends an email to the author of the ticket
  63.                 if (empty($isUserInDB)) {
  64.                     $content $mailer->prepareEmailContent($response$ticketfalse);
  65.                 }
  66.                 else {
  67.                     $content $mailer->prepareEmailContent($response$tickettrue);
  68.                 }
  69.                 MailerService::sendEmail($ticket->getEmail(), 'BU Unîmes : votre question - réponse'$content);
  70.                 // Retrieves the files that's been submitted
  71.                 $files $form->getData()['ticket']['attachment'];
  72.                 // If there is a least one file
  73.                 if ($files) {
  74.                     $userId $this->getUserId($session$form);
  75.                     // Instanciates the File class, moves it in the target directory and then adds it to the db
  76.                     $fileUploader->handleFiles($files$userId$response$sector);
  77.                     // TODO : erreur non testée !
  78.                     $fileError $session->get('file-error');
  79.                     if ($fileError) {
  80.                         $session->remove('error-file');
  81.                         return $this->render('errors.html.twig'$fileError);
  82.                     }
  83.                 }
  84.             }
  85.             // if response is an array, it means the addTicket() failed and returned us the exception data in an array
  86.             else if (is_array($response)) {
  87.                 
  88.                 return $this->render('errors.html.twig'$response['error-data'], $response['error']);
  89.             }
  90.             
  91.             return $this->redirectToRoute('app_success_3', [
  92.                 "sector" => $sector,
  93.                 "service" => $serviceId
  94.             ]);
  95.         }
  96.         
  97.         return $this->renderForm('form.html.twig', [
  98.             'form' => $form,
  99.             "sector" => $department,
  100.             "service" => $service,
  101.             "headerLink" => 'app_department_3'
  102.         ]);
  103.     }
  104.     /**
  105.      * Allows to fetch some data accordingly to the user and the requested service
  106.      *
  107.      * @param object $session
  108.      * @return array
  109.      */
  110.     private function getFormData($session$doctrine) {
  111.         // extracting all the data stored in the session
  112.         extract($session->all());
  113.         // Declaring the data for the status options and category options
  114.         $choices = array("categories" => ['-- Faire un choix --' => 'null']);
  115.         // We need the sub-categories of the "Question ? Réponse !" category
  116.         $subcategories $doctrine->getRepository(Category::class)->findByParentId($service);
  117.         foreach ($subcategories as $category) {
  118.             $key SpecialCharService::setHomeCardsSpecialChars($category);
  119.             $choices['categories'][$key] = $category->getId();
  120.         }
  121.         // If the user is set, we merge them to the choices array
  122.         if (isset($user)) {
  123.             $choices["user"] = $user;
  124.         }
  125.         return $choices;
  126.     }
  127. }