You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
8.8KB

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