130 lines
5.5KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Lc\ShopBundle\Form\Backend\Ticket\TicketMessageType;
  7. use Lc\ShopBundle\Form\Backend\Ticket\TicketStatusType;
  8. use Lc\ShopBundle\Model\Ticket;
  9. use Lc\ShopBundle\Services\UtilsManager;
  10. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class TicketController extends AdminController
  15. {
  16. protected $ticketUtils;
  17. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
  18. {
  19. $this->ticketUtils = $utilsManager->getTicketUtils();
  20. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);
  21. }
  22. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  23. {
  24. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  25. if ($this->filtersForm->get('status')->getData() === null) {
  26. $queryBuilder->andWhere('entity.status LIKE :open OR entity.status LIKE :beingprocessed');
  27. $queryBuilder->setParameter('open', Ticket::TICKET_STATUS_OPEN);
  28. $queryBuilder->setParameter('beingprocessed', Ticket::TICKET_STATUS_BEING_PROCESSED);
  29. }
  30. return $queryBuilder;
  31. }
  32. public function showAction()
  33. {
  34. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  35. $id = $this->request->query->get('id');
  36. $easyadmin = $this->request->attributes->get('easyadmin');
  37. $entity = $easyadmin['item'];
  38. $fields = $this->entity['show']['fields'];
  39. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  40. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  41. 'deleteForm' => $deleteForm,
  42. 'fields' => $fields,
  43. 'entity' => $entity,
  44. ]);
  45. $parameters = [
  46. 'entity' => $entity,
  47. 'fields' => $fields,
  48. 'delete_form' => $deleteForm->createView(),
  49. ];
  50. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  51. }
  52. /**
  53. * The method that is executed when the user performs a 'show' action on an entity.
  54. *
  55. * @return Response
  56. */
  57. public function renderTicketTemplate($actionName, $templatePath, array $parameters = [])
  58. {
  59. if ($actionName == 'show') {
  60. $parameters['form_add_ticket_message'] = $this->createCustomForm(TicketMessageType::class, 'newMessageTicket', $parameters, false)->createView();
  61. $parameters['form_ticket_status'] = $this->createCustomForm(TicketStatusType::class, 'ticketStatus', $parameters)->createView();
  62. }
  63. return parent::renderTemplate($actionName, $templatePath, $parameters);
  64. }
  65. public function ticketStatusAction(){
  66. $easyadmin = $this->request->attributes->get('easyadmin');
  67. $ticket = $easyadmin['item'];
  68. $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
  69. $formTicketStatusForm->handleRequest($this->request);
  70. if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
  71. $this->em->persist($ticket);
  72. $this->em->flush();
  73. $this->utils->addFlash('success', 'success.ticket.statusChange');
  74. } else {
  75. $this->utils->addFlash('error', $formTicketStatusForm->getErrors());
  76. }
  77. $response['flashMessages'] = $this->utils->getFlashMessages();
  78. return new Response(json_encode($response));
  79. }
  80. public function newMessageTicketAction(){
  81. $easyadmin = $this->request->attributes->get('easyadmin');
  82. $ticket = $easyadmin['item'];
  83. $formAddTicketMessage = $this->createForm(TicketMessageType::class);
  84. $formAddTicketMessage->handleRequest($this->request);
  85. if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
  86. $this->ticketUtils->createTicketMessage(array(
  87. 'ticket'=>$ticket,
  88. 'message'=> $formAddTicketMessage->get('message')->getData(),
  89. 'answerByAdmin'=>true
  90. ));
  91. $this->utils->addFlash('success', 'success.ticket.addMessage');
  92. } else {
  93. $this->utils->addFlash('error', $formAddTicketMessage->getErrors());
  94. }
  95. return $this->redirectToRoute('easyadmin', [
  96. 'action' => 'show',
  97. 'entity' => 'Ticket',
  98. 'id' => $ticket->getId()
  99. ]);
  100. }
  101. }