src/Controller/TicketController.php line 185

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Category;
  4. use App\Entity\Comment;
  5. use App\Entity\Contact;
  6. use App\Entity\Customer;
  7. use App\Entity\Status;
  8. use App\Entity\Ticket;
  9. use App\Entity\TypeTicket;
  10. use App\Entity\Urgency;
  11. use App\Entity\User;
  12. use App\Form\TicketType;
  13. use App\Services\Calculate;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Form\Extension\Core\Type\DateType;
  16. use Symfony\Component\Form\Extension\Core\Type\TimeType;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Mailer\MailerInterface;
  22. use Symfony\Component\Mime\Email;
  23. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  24. class TicketController extends AbstractController
  25. {
  26. #[Route('/tickets', name: 'app_list_ticket', options: ["expose" => true])]
  27. public function index(): Response
  28. {
  29. $em = $this->getDoctrine()->getManager();
  30. $listTickets = $em->getRepository(Ticket::class)->getListTickets()->getQuery()->getResult();
  31. $listStatus = $em->getRepository(Status::class)->getListStatus()->getQuery()->getArrayResult();
  32. return $this->render('ticket/index.html.twig', [
  33. "listTickets" => $listTickets,
  34. "listStatus" => $listStatus
  35. ]);
  36. }
  37. #[Route('/tickets/ajouter', name: 'app_add_ticket', options: ["expose" => true])]
  38. public function add(Request $request, Calculate $calculate, MailerInterface $mailer): Response
  39. {
  40. $em = $this->getDoctrine()->getManager();
  41. $ticket = new Ticket();
  42. $ticket->setCreator($this->getUser());
  43. $ticket->setUrgency($em->getRepository(Urgency::class)->find(1)); // NORMAL
  44. $ticket->setStatus($em->getRepository(Status::class)->find(4)); // EN ATTENTE / IN PROGRESS
  45. if(isset($_GET['id'])){
  46. $customer = $em->getRepository(Customer::class)->find($_GET['id']);
  47. $ticket->setCustomer($customer);
  48. }
  49. $form = $this->createForm(TicketType::class, $ticket);
  50. if ($request->isMethod('POST')
  51. && $form->handleRequest($request)->isValid()) {
  52. $ticket = $form->getData();
  53. $ticket->setTime($calculate->transformTimerToTimeFunction($ticket->getTimer()->format('H:i:s')));
  54. $ticket->setTimeRecipient($calculate->transformTimerToTimeFunction($ticket->getTimerRecipient()->format('H:i:s')));
  55. $em->persist($ticket);
  56. $em->flush();
  57. $request->getSession()->getFlashBag()->add('success', 'Le ticket a été ajouté avec succès.');
  58. $this->sendTicketCreatedEmail($this->getUser(), $ticket, $mailer, 'Création du ticket', "Un nouveau ticket a été créé.");
  59. return $this->redirectToRoute('app_list_ticket');
  60. }
  61. return $this->render('ticket/add.html.twig', [
  62. "form" => $form->createView(),
  63. "idCustomer" => $_GET['id'] ?? 0
  64. ]);
  65. }
  66. #[Route('/tickets/{id}/modifier', name: 'app_edit_ticket')]
  67. public function edit(Request $request, Ticket $ticket, Calculate $calculate, MailerInterface $mailer): Response
  68. {
  69. $em = $this->getDoctrine()->getManager();
  70. $form = $this->createForm(TicketType::class, $ticket);
  71. // ADD INFORMATION EDITION
  72. $form->add('updatedAtTimeRecipient', DateType::class, array(
  73. 'label' => 'Date de mise à jour du temps',
  74. 'widget' => 'single_text',
  75. 'required' => false,
  76. ))->add('timerRecipient', TimeType::class, array(
  77. 'label' => 'Temps (H:m:s)',
  78. 'attr' => array(
  79. 'class' => 'timer-call',
  80. 'step' => 1,
  81. ),
  82. 'widget' => 'single_text',
  83. 'with_seconds' => true,
  84. 'required' => false,
  85. ));
  86. if ($request->isMethod('POST')
  87. && $form->handleRequest($request)->isValid()) {
  88. $ticket = $form->getData();
  89. $ticket->setTime($calculate->transformTimerToTimeFunction($ticket->getTimer()->format('H:i:s')));
  90. $ticket->setTimeRecipient($calculate->transformTimerToTimeFunction($ticket->getTimerRecipient()->format('H:i:s')));
  91. $em->flush();
  92. $request->getSession()->getFlashBag()->add('success', 'Le ticket a été modifié avec succès.');
  93. $this->sendTicketCreatedEmail($this->getUser(), $ticket, $mailer, 'Modification du ticket', "Un ticket a été modifié.");
  94. return $this->redirectToRoute('app_list_ticket');
  95. }
  96. return $this->render('ticket/edit.html.twig', [
  97. "form" => $form->createView(),
  98. "ticket" => $ticket
  99. ]);
  100. }
  101. #[Route('/tickets/{id}/informations', name: 'app_view_ticket', options: ["expose" => true])]
  102. public function view(Ticket $ticket): Response
  103. {
  104. $em = $this->getDoctrine()->getManager();
  105. $listComments = $em->getRepository(Comment::class)->findBy(
  106. array(
  107. "ticket" => $ticket
  108. ),
  109. array(
  110. "id" => "DESC"
  111. )
  112. );
  113. $response = new JsonResponse ();
  114. return $response->setData($this->render('ticket/view.html.twig', array(
  115. "ticket" => $ticket,
  116. "listComments" => $listComments
  117. ))->getContent());
  118. }
  119. #[Route('/tickets/ajax', name: 'app_list_ajax_ticket', options: ["expose" => true])]
  120. public function listAjax(): Response
  121. {
  122. $em = $this->getDoctrine()->getManager();
  123. switch ($_POST['type']){
  124. /* case 'status3': // EN COURS
  125. $listTickets = $em->getRepository(Ticket::class)->getListTicketsByRecipientAndStatus($this->getUser()->getId(), [3])->getQuery()->getResult();
  126. break;
  127. case 'status4': // ATTENTE
  128. $listTickets = $em->getRepository(Ticket::class)->getListTicketsByRecipientAndStatus($this->getUser()->getId(), [4])->getQuery()->getResult();
  129. break;*/
  130. case 'ticketOpened':
  131. $listTickets = $em->getRepository(Ticket::class)->findOpened([3,4])->getQuery()->getArrayResult();
  132. break;
  133. case 'ticketForMe':
  134. $listTickets = $em->getRepository(Ticket::class)->findByMe($this->getUser()->getId(), [3,4])->getQuery()->getArrayResult();
  135. break;
  136. case 'ticketForGroup':
  137. $listTickets = $em->getRepository(Ticket::class)->findByGroup($this->getUser()->getCategory()->getId(), [3,4])->getQuery()->getArrayResult();
  138. break;
  139. case 'ticketOfCustomer':
  140. $listTickets = $em->getRepository(Ticket::class)->getListTicketsByCustomer($_POST['idCustomer'])->getQuery()->getResult();
  141. break;
  142. case 'allTickets':
  143. $listTickets = $em->getRepository(Ticket::class)->getListTickets()->getQuery()->getResult();
  144. break;
  145. }
  146. $response = new JsonResponse ();
  147. return $response->setData($this->render('ticket/ajaxTable.html.twig', array(
  148. "listTickets" => $listTickets,
  149. "fullTable" => $_POST['fullTable'] ?? "true"
  150. ))->getContent());
  151. }
  152. #[Route('/tickets/data/mode-call/ajax', name: 'app_mode_call_data_list_ajax', options: ["expose" => true])]
  153. public function listDatasAjax(): JsonResponse
  154. {
  155. $em = $this->getDoctrine()->getManager();
  156. $listCustomers = $em->getRepository(Customer::class)->getListCustomers()->getQuery()->getArrayResult();
  157. $listUsers = $em->getRepository(User::class)->getListUsers()->getQuery()->getArrayResult();
  158. $listCategories = $em->getRepository(Category::class)->getListCategories()->getQuery()->getArrayResult();
  159. $listTypesTicket = $em->getRepository(TypeTicket::class)-> getListTypesTicket()->getQuery()->getArrayResult();
  160. $response = new JsonResponse ();
  161. return $response->setData(
  162. array(
  163. "customers" => $listCustomers,
  164. "users" => $listUsers,
  165. "categories" => $listCategories,
  166. "typesTicket" => $listTypesTicket
  167. )
  168. );
  169. }
  170. #[Route('/tickets/ajouter/ajax', name: 'app_add_ticket_ajax', options: ["expose" => true])]
  171. public function addAjax(Request $request, Calculate $calculate, MailerInterface $mailer): Response
  172. {
  173. $em = $this->getDoctrine()->getManager();
  174. $ticket = new Ticket();
  175. $ticket->setCreator($this->getUser());
  176. $ticket->setUrgency($em->getRepository(Urgency::class)->find(1)); // NORMAL
  177. $ticket->setStatus($em->getRepository(Status::class)->find(4)); // EN ATTENTE / IN PROGRESS
  178. $ticket->setTimer(new \DateTime($_POST['timer']));
  179. $ticket->setTime($calculate->transformTimerToTimeFunction($ticket->getTimer()->format('H:i:s')));
  180. $ticket->setCustomer($em->getRepository(Customer::class)->find($_POST['idCustomer']));
  181. $ticket->setContact($em->getRepository(Contact::class)->find($_POST['idContact']));
  182. $ticket->setRecipient($em->getRepository(User::class)->find($_POST['idRecipient']));
  183. $ticket->setCategory($em->getRepository(Category::class)->find($_POST['idCategory']));
  184. foreach ($_POST['idsTypesTicket'] as $idType) {
  185. $ticket->addTypesTicket($em->getRepository(TypeTicket::class)->find($idType));
  186. }
  187. $ticket->setObject($_POST['object']);
  188. $em->persist($ticket);
  189. $em->flush();
  190. $request->getSession()->getFlashBag()->add('success', 'Le ticket a été ajouté avec succès.');
  191. $this->sendTicketCreatedEmail($this->getUser(), $ticket, $mailer, 'Création du ticket', "Un nouveau ticket a été créé.");
  192. $response = new JsonResponse ();
  193. return $response->setData('true');
  194. }
  195. #[Route('/tickets/archive/{id}', name: 'app_archive_ticket_ajax', options: ["expose" => true])]
  196. public function archive(Ticket $ticket): Response
  197. {
  198. $em = $this->getDoctrine()->getManager();
  199. $ticket->setArchived(1);
  200. $ticket->setNumFacture($_POST['numArchive']);
  201. $em->flush();
  202. $response = new JsonResponse ();
  203. return $response->setData('true');
  204. }
  205. #[Route('/tickets/unarchive/{id}', name: 'app_unarchive_ticket_ajax', options: ["expose" => true])]
  206. public function unarchive(Ticket $ticket): Response
  207. {
  208. $em = $this->getDoctrine()->getManager();
  209. $ticket->setArchived(0);
  210. $ticket->setNumFacture('');
  211. $em->flush();
  212. $response = new JsonResponse ();
  213. return $response->setData('true');
  214. }
  215. private function sendTicketCreatedEmail(User $user, Ticket $ticket, MailerInterface $mailer, string $subject, string $text): void
  216. {
  217. if($_ENV['APP_MAIL'] === 'false') return;
  218. if (in_array('ROLE_CUSTOMER', $user->getRoles())) {
  219. $email = (new Email())
  220. ->from($_ENV['APP_MAIL_FROM'])
  221. ->to($_ENV['APP_MAIL_TO'])
  222. ->subject($subject . ' #' . $ticket->getId())
  223. ->text(
  224. $text . "\n" .
  225. ($ticket->getObject() ? "Objet : " . $ticket->getObject() : '') . "\n" .
  226. "Créé par : " . $user->getUsername() . "\n" .
  227. "Lien : " . $this->generateUrl('app_view_ticket', ['id' => $ticket->getId()], UrlGeneratorInterface::ABSOLUTE_URL)
  228. );
  229. $mailer->send($email);
  230. }
  231. }
  232. /* #[Route('/tickets/regeneration', name: 'app_list_regenerate_ticket')]
  233. public function regenerate(Calculate $calculate): Response
  234. {
  235. $em = $this->getDoctrine()->getManager();
  236. $listTickets = $em->getRepository(Ticket::class)->findBy(array());
  237. foreach ($listTickets as $ticket){
  238. }
  239. $em->flush();
  240. $response = new JsonResponse ();
  241. return $response->setData('true');
  242. }*/
  243. }