Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

220 lines
8.2KB

  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. {
  36. $ticketRepo = $this->em->getRepository(TicketInterface::class);
  37. return $ticketRepo->findBy(array('user' => $user));
  38. }
  39. public function uploadImageTicketMessage($formTicket)
  40. {
  41. $image = $formTicket->get('image')->getData();
  42. if ($image) {
  43. $originalFilename = pathinfo($image->getClientOriginalName(), PATHINFO_FILENAME);
  44. $newFilename = uniqid().'.'.$image->guessExtension();
  45. try {
  46. $image->move(
  47. $this->parameterBag->get('app.ticket_images_directory'),
  48. $newFilename
  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. } else {
  68. $ticket->setVisitorFirstname($params['visitorFirstname']);
  69. $ticket->setVisitorLastname($params['visitorLastname']);
  70. $ticket->setVisitorEmail($params['visitorEmail']);
  71. $ticket->setVisitorToken(uniqid());
  72. $email = $params['visitorEmail'];
  73. $firstname = $params['visitorFirstname'];
  74. }
  75. $ticket->setStatus(Ticket::TICKET_STATUS_OPEN);
  76. $ticket->setType($params['type']);
  77. if (isset($params['orderShop']) && $params['orderShop'] && $params['orderShop'] instanceof OrderShopInterface) {
  78. $ticket->setOrderShop($params['orderShop']);
  79. }
  80. $ticket->setSubject($params['subject']);
  81. $this->em->persist($ticket);
  82. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName();
  83. $ticketMessage = new $classTicketMessage;
  84. $ticketMessage->setStatus(1);
  85. $ticketMessage->setTicket($ticket);
  86. $ticketMessage->setMessage($params['message']);
  87. if (isset($params['imageFilename']) && $params['imageFilename']) {
  88. $ticketMessage->setImageFilename($params['imageFilename']);
  89. }
  90. if (isset($params['createByAdmin']) && $params['createByAdmin']) {
  91. $ticketMessage->setAnswerByAdmin(true);
  92. }
  93. $this->em->persist($ticketMessage);
  94. $this->em->flush();
  95. if (isset($params['createByAdmin']) && $params['createByAdmin']) {
  96. // envoi email au client
  97. $this->mailUtils->send(
  98. [
  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. );
  108. } else {
  109. $this->mailUtils->send(
  110. [
  111. MailUtils::SUBJECT => 'Nouvelle demande',
  112. MailUtils::TO_EMAIL => $email,
  113. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-new',
  114. MailUtils::CONTENT_DATA => [
  115. 'firstname' => $firstname,
  116. 'ticket' => $ticket,
  117. ],
  118. ]
  119. );
  120. }
  121. $this->notifyAdmin($ticket, $ticketMessage);
  122. return $ticket;
  123. }
  124. public function createTicketMessage($params)
  125. {
  126. $classTicketMessage = $this->em->getClassMetadata(TicketMessageInterface::class)->getName();
  127. $ticketMessage = new $classTicketMessage;
  128. $ticket = $params['ticket'];
  129. $ticketMessage->setStatus(1);
  130. $ticketMessage->setTicket($ticket);
  131. $ticketMessage->setMessage($params['message']);
  132. if (isset($params['answerByAdmin']) && $params['answerByAdmin']) {
  133. $ticketMessage->setAnswerByAdmin($params['answerByAdmin']);
  134. // envoi email au client
  135. $this->mailUtils->send(
  136. [
  137. MailUtils::SUBJECT => 'Réponse à votre demande',
  138. MailUtils::TO_EMAIL => $ticket->getUser() ? $ticket->getUser()->getEmail(
  139. ) : $ticket->getVisitorEmail(),
  140. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-response',
  141. MailUtils::CONTENT_DATA => [
  142. 'firstname' => $ticket->getUser() ? $ticket->getUser()->getFirstname(
  143. ) : $ticket->getVisitorFirstname(),
  144. 'ticket' => $ticket,
  145. ],
  146. ]
  147. );
  148. }
  149. $this->em->persist($ticketMessage);
  150. if (isset($params['imageFilename']) && $params['imageFilename']) {
  151. $ticketMessage->setImageFilename($params['imageFilename']);
  152. }
  153. if (isset($params['closeTicket']) && $params['closeTicket']) {
  154. $ticket->setStatus(Ticket::TICKET_STATUS_CLOSED);
  155. }
  156. $ticket->setUpdatedAt(new \DateTime());
  157. $this->em->persist($ticket);
  158. $this->em->flush();
  159. return $ticketMessage;
  160. }
  161. public function notifyAdmin($ticket, $ticketMessage)
  162. {
  163. $userRepo = $this->em->getRepository(UserInterface::class);
  164. $usersToNotify = $userRepo->findByTicketTypesNotification($ticket->getType());
  165. foreach ($usersToNotify as $userToNotify) {
  166. if ($this->authorizationChecker->isGranted('ROLE_ADMIN', $userToNotify)) {
  167. // envoi email au client
  168. $this->mailUtils->send(
  169. [
  170. MailUtils::SUBJECT => 'Nouveau ticket sur placedulocal',
  171. MailUtils::TO_EMAIL => $userToNotify->getEmail(),
  172. MailUtils::CONTENT_TEMPLATE => 'mail/ticket-notification',
  173. MailUtils::CONTENT_DATA => [
  174. 'firstname' => $userToNotify->getFirstname(),
  175. 'ticket' => $ticket,
  176. 'ticketMessage' => $ticketMessage,
  177. ],
  178. ]
  179. );
  180. }
  181. }
  182. }
  183. }