Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

223 lines
9.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\Context\OrderShopInterface;
  7. use Lc\ShopBundle\Context\UserInterface;
  8. use Lc\ShopBundle\Form\Backend\Ticket\TicketMessageType;
  9. use Lc\ShopBundle\Form\Backend\Ticket\TicketStatusType;
  10. use Lc\ShopBundle\Model\Ticket;
  11. use Lc\ShopBundle\Services\UtilsManager;
  12. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Security;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class TicketController extends AdminController
  17. {
  18. protected $ticketUtils;
  19. protected $userUtils;
  20. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
  21. {
  22. $this->ticketUtils = $utilsManager->getTicketUtils();
  23. $this->userUtils = $utilsManager->getUserUtils();
  24. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);
  25. }
  26. public function persistTicketEntity($entity, $form)
  27. {
  28. $entity->setStatus(Ticket::TICKET_STATUS_OPEN);
  29. $ticket = $this->ticketUtils->createTicket([
  30. 'user' => $entity->getUser(),
  31. 'type' => $entity->getType(),
  32. 'orderShop' => $entity->getOrderShop(),
  33. 'subject' => $entity->getSubject(),
  34. 'message' => $form->get('message')->getData(),
  35. 'createByAdmin' => true,
  36. ]);
  37. }
  38. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  39. {
  40. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  41. if ($this->filtersForm->get('status')->getData() === null) {
  42. $queryBuilder->andWhere('entity.status LIKE :open OR entity.status LIKE :beingprocessed');
  43. $queryBuilder->setParameter('open', Ticket::TICKET_STATUS_OPEN);
  44. $queryBuilder->setParameter('beingprocessed', Ticket::TICKET_STATUS_BEING_PROCESSED);
  45. }
  46. return $queryBuilder;
  47. }
  48. public function showAction()
  49. {
  50. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  51. $id = $this->request->query->get('id');
  52. $easyadmin = $this->request->attributes->get('easyadmin');
  53. $entity = $easyadmin['item'];
  54. $fields = $this->entity['show']['fields'];
  55. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  56. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  57. 'deleteForm' => $deleteForm,
  58. 'fields' => $fields,
  59. 'entity' => $entity,
  60. ]);
  61. $parameters = [
  62. 'entity' => $entity,
  63. 'fields' => $fields,
  64. 'delete_form' => $deleteForm->createView(),
  65. ];
  66. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  67. }
  68. /**
  69. * The method that is executed when the user performs a 'show' action on an entity.
  70. *
  71. * @return Response
  72. */
  73. public function renderTicketTemplate($actionName, $templatePath, array $parameters = [])
  74. {
  75. if ($actionName == 'show') {
  76. $parameters['form_add_ticket_message'] = $this->createCustomForm(TicketMessageType::class, 'newMessageTicket', $parameters, false)->createView();
  77. $parameters['form_ticket_status'] = $this->createCustomForm(TicketStatusType::class, 'ticketStatus', $parameters)->createView();
  78. }
  79. return parent::renderTemplate($actionName, $templatePath, $parameters);
  80. }
  81. public function ticketStatusAction()
  82. {
  83. $easyadmin = $this->request->attributes->get('easyadmin');
  84. $ticket = $easyadmin['item'];
  85. $formTicketStatusForm = $this->createForm(TicketStatusType::class, $ticket);
  86. $formTicketStatusForm->handleRequest($this->request);
  87. if ($formTicketStatusForm->isSubmitted() && $formTicketStatusForm->isValid()) {
  88. $this->em->persist($ticket);
  89. $this->em->flush();
  90. $this->utils->addFlash('success', 'success.ticket.statusChange');
  91. } else {
  92. $this->utils->addFlash('error', $formTicketStatusForm->getErrors());
  93. }
  94. $response['flashMessages'] = $this->utils->getFlashMessages();
  95. return new Response(json_encode($response));
  96. }
  97. public function newMessageTicketAction()
  98. {
  99. $easyadmin = $this->request->attributes->get('easyadmin');
  100. $ticket = $easyadmin['item'];
  101. $formAddTicketMessage = $this->createForm(TicketMessageType::class);
  102. $formAddTicketMessage->handleRequest($this->request);
  103. if ($formAddTicketMessage->isSubmitted() && $formAddTicketMessage->isValid()) {
  104. $this->ticketUtils->createTicketMessage(array(
  105. 'ticket' => $ticket,
  106. 'message' => $formAddTicketMessage->get('message')->getData(),
  107. 'answerByAdmin' => true
  108. ));
  109. $this->utils->addFlash('success', 'success.ticket.addMessage');
  110. } else {
  111. $this->utils->addFlash('error', $formAddTicketMessage->getErrors());
  112. }
  113. return $this->redirectToRoute('easyadmin', [
  114. 'action' => 'show',
  115. 'entity' => 'Ticket',
  116. 'id' => $ticket->getId()
  117. ]);
  118. }
  119. public function newAction()
  120. {
  121. $this->dispatch(EasyAdminEvents::PRE_NEW);
  122. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  123. $easyadmin = $this->request->attributes->get('easyadmin');
  124. $easyadmin['item'] = $entity;
  125. $this->request->attributes->set('easyadmin', $easyadmin);
  126. $fields = $this->entity['new']['fields'];
  127. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  128. if ($this->request->request->get('user')) {
  129. $userRepo = $this->em->getRepository(UserInterface::class);
  130. $newForm->get('user')->setData($userRepo->find($this->request->request->get('user')));
  131. }
  132. if ($this->request->request->get('orderId')) {
  133. $newForm->get('orderId')->setData($this->request->request->get('orderId'));
  134. }
  135. $newForm->handleRequest($this->request);
  136. if ($newForm->isSubmitted() && $newForm->isValid()) {
  137. if ($newForm->get('orderId')->getData()) {
  138. $orderShopRepo = $this->em->getRepository(OrderShopInterface::class);
  139. $orderShop = $orderShopRepo->find($newForm->get('orderId')->getData());
  140. if($orderShop)$entity->setOrderShop($orderShop);
  141. }
  142. $this->processUploadedFiles($newForm);
  143. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  144. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  145. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  146. $this->utils->addFlash('success', 'success.ticket.addMessage');
  147. if ($this->request->isXmlHttpRequest()) {
  148. $response['type'] = 'success';
  149. $response['flashMessages'] = $this->utils->getFlashMessages();
  150. return new Response(json_encode($response));
  151. } else {
  152. return $this->redirectToReferrer();
  153. }
  154. }
  155. $this->dispatch(EasyAdminEvents::POST_NEW, [
  156. 'entity_fields' => $fields,
  157. 'form' => $newForm,
  158. 'entity' => $entity,
  159. ]);
  160. $parameters = [
  161. 'form' => $newForm->createView(),
  162. 'entity_fields' => $fields,
  163. 'entity' => $entity,
  164. ];
  165. if ($this->request->isXmlHttpRequest()) {
  166. $response['modal'] = $this->renderView('@LcShop/backend/ticket/modal/create.html.twig', $parameters);
  167. return new Response(json_encode($response));
  168. } else {
  169. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  170. }
  171. }
  172. }