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ů.

TicketUtils.php 7.5KB

před 4 roky
před 4 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 getTicketsByUser($user){
  26. $ticketRepo = $this->em->getRepository(TicketInterface::class);
  27. return $ticketRepo->findBy(array('user'=>$user));
  28. }
  29. public function createTicket($params): TicketInterface
  30. {
  31. $classTicket = $this->em->getClassMetadata(TicketInterface::class)->getName() ;
  32. $ticket = new $classTicket ;
  33. $ticket->setMerchant($this->merchantUtils->getMerchantCurrent()) ;
  34. $ticket->setStatus(1) ;
  35. if(isset($params['user'])) {
  36. $ticket->setUser($params['user']) ;
  37. $email = $params['user']->getEmail() ;
  38. $firstname = $params['user']->getFirstname() ;
  39. }
  40. else {
  41. $ticket->setVisitorFirstname($params['visitorFirstname']) ;
  42. $ticket->setVisitorLastname($params['visitorLastname']) ;
  43. $ticket->setVisitorEmail($params['visitorEmail']) ;
  44. $ticket->setVisitorToken(uniqid()) ;
  45. $email = $params['visitorEmail'] ;
  46. $firstname = $params['visitorFirstname'] ;
  47. }
  48. $ticket->setStatus(Ticket::TICKET_STATUS_OPEN) ;
  49. $ticket->setType($params['type']) ;
  50. if(isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
  51. $ticket->setOrderShop($params['orderShop']) ;
  52. }
  53. $ticket->setSubject($params['subject']) ;
  54. $this->em->persist($ticket);
  55. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  56. $ticketMessage = new $classTicketMessage ;
  57. $ticketMessage->setStatus(1) ;
  58. $ticketMessage->setTicket($ticket) ;
  59. $ticketMessage->setMessage($params['message']) ;
  60. if(isset($params['createByAdmin']) && $params['createByAdmin'])$ticketMessage->setAnswerByAdmin(true);
  61. $this->em->persist($ticketMessage);
  62. $this->em->flush() ;
  63. if(isset($params['createByAdmin']) && $params['createByAdmin']) {
  64. // envoi email au client
  65. $this->mailUtils->send([
  66. MailUtils::SUBJECT => 'Vous avez reçu un nouveau message',
  67. MailUtils::TO_EMAIL => $email,
  68. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new-by-admin',
  69. MailUtils::CONTENT_DATA => [
  70. 'firstname' => $firstname,
  71. 'ticket' => $ticket
  72. ],
  73. ]);
  74. }else{
  75. $this->mailUtils->send([
  76. MailUtils::SUBJECT => 'Nouvelle demande',
  77. MailUtils::TO_EMAIL => $email,
  78. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
  79. MailUtils::CONTENT_DATA => [
  80. 'firstname' => $firstname,
  81. 'ticket' => $ticket
  82. ],
  83. ]);
  84. }
  85. $this->notifyAdmin($ticket, $ticketMessage);
  86. return $ticket ;
  87. }
  88. public function createTicketMessage($params)
  89. {
  90. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  91. $ticketMessage = new $classTicketMessage ;
  92. $ticket = $params['ticket'] ;
  93. $ticketMessage->setStatus(1) ;
  94. $ticketMessage->setTicket($ticket) ;
  95. $ticketMessage->setMessage($params['message']) ;
  96. if(isset($params['answerByAdmin']) && $params['answerByAdmin']) {
  97. $ticketMessage->setAnswerByAdmin($params['answerByAdmin']) ;
  98. // envoi email au client
  99. $this->mailUtils->send([
  100. MailUtils::SUBJECT => 'Réponse à votre demande',
  101. MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail() : $ticket->getVisitorEmail(),
  102. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
  103. MailUtils::CONTENT_DATA => [
  104. 'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname() : $ticket->getVisitorFirstname(),
  105. 'ticket' => $ticket
  106. ],
  107. ]) ;
  108. }
  109. $this->em->persist($ticketMessage);
  110. if(isset($params['closeTicket']) && $params['closeTicket']) {
  111. $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED) ;
  112. }
  113. $ticket->setUpdatedAt(new \DateTime()) ;
  114. $this->em->persist($ticket);
  115. $this->em->flush() ;
  116. return $ticketMessage ;
  117. }
  118. public function notifyAdmin($ticket, $ticketMessage){
  119. $userRepo = $this->em->getRepository(UserInterface::class);
  120. $usersToNotify = $userRepo->findByTicketTypesNotification($ticket->getType());
  121. foreach ($usersToNotify as $userToNotify){
  122. if($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)){
  123. // envoi email au client
  124. $this->mailUtils->send([
  125. MailUtils::SUBJECT => 'Nouveau ticket sur placedulocal',
  126. MailUtils::TO_EMAIL => $userToNotify->getEmail(),
  127. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-notification',
  128. MailUtils::CONTENT_DATA => [
  129. 'firstname' => $userToNotify->getFirstname(),
  130. 'ticket' => $ticket,
  131. 'ticketMessage' => $ticketMessage
  132. ],
  133. ]) ;
  134. }
  135. }
  136. }
  137. }