Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

164 lines
7.3KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use App\Entity\TicketMessage;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Context\OrderShopInterface;
  7. use Lc\ShopBundle\Context\TicketInterface;
  8. use Lc\ShopBundle\Context\TicketMessageInterface;
  9. use Lc\ShopBundle\Context\UserInterface;
  10. use Lc\ShopBundle\Model\Ticket;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. class TicketUtils
  13. {
  14. protected $em ;
  15. protected $merchantUtils ;
  16. protected $mailUtils ;
  17. protected $authorizationChecker ;
  18. public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils, MailUtils $mailUtils, AuthorizationCheckerInterface $authorizationChecker)
  19. {
  20. $this->em = $em ;
  21. $this->merchantUtils = $merchantUtils ;
  22. $this->mailUtils = $mailUtils ;
  23. $this->authorizationChecker = $authorizationChecker ;
  24. }
  25. public function createTicket($params): TicketInterface
  26. {
  27. $classTicket = $this->em->getClassMetadata(TicketInterface::class)->getName() ;
  28. $ticket = new $classTicket ;
  29. $ticket->setMerchant($this->merchantUtils->getMerchantCurrent()) ;
  30. $ticket->setStatus(1) ;
  31. if(isset($params['user'])) {
  32. $ticket->setUser($params['user']) ;
  33. $email = $params['user']->getEmail() ;
  34. $firstname = $params['user']->getFirstname() ;
  35. }
  36. else {
  37. $ticket->setVisitorFirstname($params['visitorFirstname']) ;
  38. $ticket->setVisitorLastname($params['visitorLastname']) ;
  39. $ticket->setVisitorEmail($params['visitorEmail']) ;
  40. $ticket->setVisitorToken(uniqid()) ;
  41. $email = $params['visitorEmail'] ;
  42. $firstname = $params['visitorFirstname'] ;
  43. }
  44. $ticket->setStatus(Ticket::TICKET_STATUS_OPEN) ;
  45. $ticket->setType($params['type']) ;
  46. if(isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
  47. $ticket->setOrderShop($params['orderShop']) ;
  48. }
  49. $ticket->setSubject($params['subject']) ;
  50. $this->em->persist($ticket);
  51. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  52. $ticketMessage = new $classTicketMessage ;
  53. $ticketMessage->setStatus(1) ;
  54. $ticketMessage->setTicket($ticket) ;
  55. $ticketMessage->setMessage($params['message']) ;
  56. if(isset($params['createByAdmin']) && $params['createByAdmin'])$ticketMessage->setAnswerByAdmin(true);
  57. $this->em->persist($ticketMessage);
  58. $this->em->flush() ;
  59. if(isset($params['createByAdmin']) && $params['createByAdmin']) {
  60. // envoi email au client
  61. $this->mailUtils->send([
  62. MailUtils::SUBJECT => 'Vous avez reçu un nouveau message',
  63. MailUtils::TO_EMAIL => $email,
  64. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new-by-admin',
  65. MailUtils::CONTENT_DATA => [
  66. 'firstname' => $firstname,
  67. 'ticket' => $ticket
  68. ],
  69. ]);
  70. }else{
  71. $this->mailUtils->send([
  72. MailUtils::SUBJECT => 'Nouvelle demande',
  73. MailUtils::TO_EMAIL => $email,
  74. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
  75. MailUtils::CONTENT_DATA => [
  76. 'firstname' => $firstname,
  77. 'ticket' => $ticket
  78. ],
  79. ]);
  80. }
  81. $this->notifyAdmin($ticket, $ticketMessage);
  82. return $ticket ;
  83. }
  84. public function createTicketMessage($params)
  85. {
  86. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  87. $ticketMessage = new $classTicketMessage ;
  88. $ticket = $params['ticket'] ;
  89. $ticketMessage->setStatus(1) ;
  90. $ticketMessage->setTicket($ticket) ;
  91. $ticketMessage->setMessage($params['message']) ;
  92. if(isset($params['answerByAdmin']) && $params['answerByAdmin']) {
  93. $ticketMessage->setAnswerByAdmin($params['answerByAdmin']) ;
  94. // envoi email au client
  95. $this->mailUtils->send([
  96. MailUtils::SUBJECT => 'Réponse à votre demande',
  97. MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail() : $ticket->getVisitorEmail(),
  98. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
  99. MailUtils::CONTENT_DATA => [
  100. 'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname() : $ticket->getVisitorFirstname(),
  101. 'ticket' => $ticket
  102. ],
  103. ]) ;
  104. }
  105. $this->em->persist($ticketMessage);
  106. if(isset($params['closeTicket']) && $params['closeTicket']) {
  107. $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED) ;
  108. }
  109. $ticket->setUpdatedAt(new \DateTime()) ;
  110. $this->em->persist($ticket);
  111. $this->em->flush() ;
  112. return $ticketMessage ;
  113. }
  114. public function notifyAdmin($ticket, $ticketMessage){
  115. $userRepo = $this->em->getRepository(UserInterface::class);
  116. $usersToNotify = $userRepo->findByTicketTypesNotification($ticket->getType());
  117. foreach ($usersToNotify as $userToNotify){
  118. if($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)){
  119. // envoi email au client
  120. $this->mailUtils->send([
  121. MailUtils::SUBJECT => 'Nouveau ticket sur placedulocal',
  122. MailUtils::TO_EMAIL => $userToNotify->getEmail(),
  123. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-notification',
  124. MailUtils::CONTENT_DATA => [
  125. 'firstname' => $userToNotify->getFirstname(),
  126. 'ticket' => $ticket,
  127. 'ticketMessage' => $ticketMessage
  128. ],
  129. ]) ;
  130. }
  131. }
  132. }
  133. }