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.

255 lines
8.9KB

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