<?php
// src/Controller/BUTicketController.php
namespace App\Controller;
use App\Entity\Category;
use App\Entity\ExternalUser;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\HttpFoundation\{Request, Response};
use App\Service\{BUTicketService, SpecialCharService, FileUploader, MailerService};
class BUTicketController extends CoreController
{
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => BUTicket::class,
'required' => true,
]);
}
/**
* @Route("/service-de-la-documentation/ticket", name="app_form_bu")
*/
public function handleForm(Request $request, ManagerRegistry $doctrine, FileUploader $fileUploader, BUTicketService $buService, MailerService $mailer): Response
{
// Getting all the data we need that is stored in the session
$session = $this->requestStack->getSession();
extract($this->handleUrlParams($session));
// Gets the data we need for the form
$choices = $this->getFormData($session, $doctrine);
$choices["departmentId"] = $sector;
extract($this->createFormUtils('BUTicket', $choices));
// Inspects the given request and calls submit() if the form was submitted
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// uses the service to validate the submitted data
$errors = $buService->validateTicket($form, $ticket);
// If there are any error in the array
// We return the form with the errors
if (count($errors) > 0) {
$errorArray = [];
foreach($errors as $error) {
$errorArray[$error->getPropertyPath()] = $error->getMessage();
}
return $this->renderForm('form.html.twig', [
'form' => $form,
"sector" => $department,
"service" => $service,
"errors" => $errorArray,
"headerLink" => 'app_department_3'
]);
}
// 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
$isUserInDB = $this->doctrine->getRepository(ExternalUser::class)->findByRealId($ticket->getEmail());
// We use the service to prepare and send the ticket to the WebService
$response = $buService->prepareTicket($ticket);
// if $response contains the id of the ticket
if (is_numeric($response)) {
// Sends an email to the author of the ticket
if (empty($isUserInDB)) {
$content = $mailer->prepareEmailContent($response, $ticket, false);
}
else {
$content = $mailer->prepareEmailContent($response, $ticket, true);
}
MailerService::sendEmail($ticket->getEmail(), 'BU Unîmes : votre question - réponse', $content);
// Retrieves the files that's been submitted
$files = $form->getData()['ticket']['attachment'];
// If there is a least one file
if ($files) {
$userId = $this->getUserId($session, $form);
// Instanciates the File class, moves it in the target directory and then adds it to the db
$fileUploader->handleFiles($files, $userId, $response, $sector);
// TODO : erreur non testée !
$fileError = $session->get('file-error');
if ($fileError) {
$session->remove('error-file');
return $this->render('errors.html.twig', $fileError);
}
}
}
// if response is an array, it means the addTicket() failed and returned us the exception data in an array
else if (is_array($response)) {
return $this->render('errors.html.twig', $response['error-data'], $response['error']);
}
return $this->redirectToRoute('app_success_3', [
"sector" => $sector,
"service" => $serviceId
]);
}
return $this->renderForm('form.html.twig', [
'form' => $form,
"sector" => $department,
"service" => $service,
"headerLink" => 'app_department_3'
]);
}
/**
* Allows to fetch some data accordingly to the user and the requested service
*
* @param object $session
* @return array
*/
private function getFormData($session, $doctrine) {
// extracting all the data stored in the session
extract($session->all());
// Declaring the data for the status options and category options
$choices = array("categories" => ['-- Faire un choix --' => 'null']);
// We need the sub-categories of the "Question ? Réponse !" category
$subcategories = $doctrine->getRepository(Category::class)->findByParentId($service);
foreach ($subcategories as $category) {
$key = SpecialCharService::setHomeCardsSpecialChars($category);
$choices['categories'][$key] = $category->getId();
}
// If the user is set, we merge them to the choices array
if (isset($user)) {
$choices["user"] = $user;
}
return $choices;
}
}