Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

223 lines
9.6KB

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