Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

210 lines
9.0KB

  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\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. class TicketUtils
  15. {
  16. protected $em ;
  17. protected $merchantUtils ;
  18. protected $mailUtils ;
  19. protected $authorizationChecker ;
  20. protected $parameterBag ;
  21. public function __construct(
  22. EntityManagerInterface $em,
  23. MerchantUtilsInterface $merchantUtils,
  24. MailUtils $mailUtils,
  25. AuthorizationCheckerInterface $authorizationChecker,
  26. ParameterBagInterface $parameterBag
  27. ) {
  28. $this->em = $em ;
  29. $this->merchantUtils = $merchantUtils ;
  30. $this->mailUtils = $mailUtils ;
  31. $this->authorizationChecker = $authorizationChecker ;
  32. $this->parameterBag = $parameterBag ;
  33. }
  34. public function getTicketsByUser($user){
  35. $ticketRepo = $this->em->getRepository(TicketInterface::class);
  36. return $ticketRepo->findBy(array('user'=>$user));
  37. }
  38. public function uploadImageTicketMessage($formTicket)
  39. {
  40. $image = $formTicket->get('image')->getData();
  41. if ($image) {
  42. $originalFilename = pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME);
  43. $newFilename = uniqid().'.' . $image->guessExtension();
  44. try {
  45. $image->move(
  46. $this->parameterBag->get('app.ticket_images_directory'),
  47. $newFilename
  48. );
  49. }
  50. catch (FileException $e) {
  51. throw new \ErrorException("Une erreur est survenue lors de l'upload du fichier.");
  52. }
  53. return $this->parameterBag->get('app.ticket_images_subdirectory').$newFilename ;
  54. }
  55. return false ;
  56. }
  57. public function createTicket($params): TicketInterface
  58. {
  59. $classTicket = $this->em->getClassMetadata(TicketInterface::class)->getName() ;
  60. $ticket = new $classTicket ;
  61. $ticket->setMerchant($this->merchantUtils->getMerchantCurrent()) ;
  62. $ticket->setStatus(1) ;
  63. if(isset($params['user'])) {
  64. $ticket->setUser($params['user']) ;
  65. $email = $params['user']->getEmail() ;
  66. $firstname = $params['user']->getFirstname() ;
  67. }
  68. else {
  69. $ticket->setVisitorFirstname($params['visitorFirstname']) ;
  70. $ticket->setVisitorLastname($params['visitorLastname']) ;
  71. $ticket->setVisitorEmail($params['visitorEmail']) ;
  72. $ticket->setVisitorToken(uniqid()) ;
  73. $email = $params['visitorEmail'] ;
  74. $firstname = $params['visitorFirstname'] ;
  75. }
  76. $ticket->setStatus(Ticket::TICKET_STATUS_OPEN) ;
  77. $ticket->setType($params['type']) ;
  78. if(isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
  79. $ticket->setOrderShop($params['orderShop']) ;
  80. }
  81. $ticket->setSubject($params['subject']) ;
  82. $this->em->persist($ticket);
  83. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  84. $ticketMessage = new $classTicketMessage ;
  85. $ticketMessage->setStatus(1) ;
  86. $ticketMessage->setTicket($ticket) ;
  87. $ticketMessage->setMessage($params['message']) ;
  88. if(isset($params['imageFilename']) && $params['imageFilename']) {
  89. $ticketMessage->setImageFilename($params['imageFilename']);
  90. }
  91. if(isset($params['createByAdmin']) && $params['createByAdmin']) {
  92. $ticketMessage->setAnswerByAdmin(true);
  93. }
  94. $this->em->persist($ticketMessage);
  95. $this->em->flush() ;
  96. if(isset($params['createByAdmin']) && $params['createByAdmin']) {
  97. // envoi email au client
  98. $this->mailUtils->send([
  99. MailUtils::SUBJECT => 'Vous avez reçu un nouveau message',
  100. MailUtils::TO_EMAIL => $email,
  101. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new-by-admin',
  102. MailUtils::CONTENT_DATA => [
  103. 'firstname' => $firstname,
  104. 'ticket' => $ticket
  105. ],
  106. ]);
  107. }else{
  108. $this->mailUtils->send([
  109. MailUtils::SUBJECT => 'Nouvelle demande',
  110. MailUtils::TO_EMAIL => $email,
  111. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
  112. MailUtils::CONTENT_DATA => [
  113. 'firstname' => $firstname,
  114. 'ticket' => $ticket
  115. ],
  116. ]);
  117. }
  118. $this->notifyAdmin($ticket, $ticketMessage);
  119. return $ticket ;
  120. }
  121. public function createTicketMessage($params)
  122. {
  123. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName() ;
  124. $ticketMessage = new $classTicketMessage ;
  125. $ticket = $params['ticket'] ;
  126. $ticketMessage->setStatus(1) ;
  127. $ticketMessage->setTicket($ticket) ;
  128. $ticketMessage->setMessage($params['message']) ;
  129. if(isset($params['answerByAdmin']) && $params['answerByAdmin']) {
  130. $ticketMessage->setAnswerByAdmin($params['answerByAdmin']) ;
  131. // envoi email au client
  132. $this->mailUtils->send([
  133. MailUtils::SUBJECT => 'Réponse à votre demande',
  134. MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail() : $ticket->getVisitorEmail(),
  135. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
  136. MailUtils::CONTENT_DATA => [
  137. 'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname() : $ticket->getVisitorFirstname(),
  138. 'ticket' => $ticket
  139. ],
  140. ]) ;
  141. }
  142. $this->em->persist($ticketMessage);
  143. if(isset($params['closeTicket']) && $params['closeTicket']) {
  144. $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED) ;
  145. }
  146. $ticket->setUpdatedAt(new \DateTime()) ;
  147. $this->em->persist($ticket);
  148. $this->em->flush() ;
  149. return $ticketMessage ;
  150. }
  151. public function notifyAdmin($ticket, $ticketMessage){
  152. $userRepo = $this->em->getRepository(UserInterface::class);
  153. $usersToNotify = $userRepo->findByTicketTypesNotification($ticket->getType());
  154. foreach ($usersToNotify as $userToNotify){
  155. if($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)){
  156. // envoi email au client
  157. $this->mailUtils->send([
  158. MailUtils::SUBJECT => 'Nouveau ticket sur placedulocal',
  159. MailUtils::TO_EMAIL => $userToNotify->getEmail(),
  160. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-notification',
  161. MailUtils::CONTENT_DATA => [
  162. 'firstname' => $userToNotify->getFirstname(),
  163. 'ticket' => $ticket,
  164. 'ticketMessage' => $ticketMessage
  165. ],
  166. ]) ;
  167. }
  168. }
  169. }
  170. }