vendor/uvdesk/core-framework/Workflow/Actions/Ticket/MailCustomer.php line 91

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Workflow\Actions\Ticket;
  3. use Webkul\UVDesk\AutomationBundle\Workflow\Event;
  4. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Ticket;
  5. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Attachment;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Entity\EmailTemplates;
  7. use Webkul\UVDesk\AutomationBundle\Workflow\FunctionalGroup;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Webkul\UVDesk\AutomationBundle\Workflow\Events\TicketActivity;
  10. use Webkul\UVDesk\AutomationBundle\Workflow\Action as WorkflowAction;
  11. class MailCustomer extends WorkflowAction
  12. {
  13.     public static function getId()
  14.     {
  15.         return 'uvdesk.ticket.mail_customer';
  16.     }
  17.     public static function getDescription()
  18.     {
  19.         return "Mail to customer";
  20.     }
  21.     public static function getFunctionalGroup()
  22.     {
  23.         return FunctionalGroup::TICKET;
  24.     }
  25.     public static function getOptions(ContainerInterface $container)
  26.     {
  27.         $entityManager $container->get('doctrine.orm.entity_manager');
  28.         $emailTemplateCollection array_map(function ($emailTemplate) {
  29.             return [
  30.                 'id'   => $emailTemplate->getId(),
  31.                 'name' => $emailTemplate->getName(),
  32.             ];
  33.         }, $entityManager->getRepository(EmailTemplates::class)->findAll());
  34.         return $emailTemplateCollection;
  35.     }
  36.     public static function applyAction(ContainerInterface $containerEvent $event$value null$thread null)
  37.     {
  38.         $entityManager $container->get('doctrine.orm.entity_manager');
  39.         if (!$event instanceof TicketActivity) {
  40.             return;
  41.         } else {
  42.             $ticket $event->getTicket();
  43.             if (empty($ticket)) {
  44.                 return;
  45.             }
  46.         }
  47.         $currentThread = isset($ticket->currentThread) ? $ticket->currentThread '';
  48.         $createdThread = isset($ticket->createdThread) ? $ticket->createdThread '';
  49.         $emailTemplate $entityManager->getRepository(EmailTemplates::class)->findOneById($value);
  50.         if (empty($emailTemplate)) {
  51.             return;
  52.         }
  53.         // Only process attachments if required in the message body
  54.         // @TODO: Revist -> Maybe we should always include attachments if they are provided??
  55.         $attachments = [];
  56.         if (!empty($createdThread) && (strpos($emailTemplate->getMessage(), '{%ticket.attachments%}') !== false || strpos($emailTemplate->getMessage(), '{% ticket.attachments %}') !== false)) {
  57.             $attachments array_map(function ($attachment) use ($container) {
  58.                 return str_replace('//''/'$container->get('kernel')->getProjectDir() . "/public" $attachment->getPath());
  59.             }, $entityManager->getRepository(Attachment::class)->findByThread($createdThread));
  60.         }
  61.         $ticketPlaceholders $container->get('email.service')->getTicketPlaceholderValues($ticket);
  62.         $subject $container->get('email.service')->processEmailSubject($emailTemplate->getSubject(), $ticketPlaceholders);
  63.         $message $container->get('email.service')->processEmailContent($emailTemplate->getMessage(), $ticketPlaceholders);
  64.         $thread = ($thread != null) ? $thread $createdThread;
  65.         $ticketCollaborators = (($thread != null) && !empty($thread->getTicket()) && $thread != "") ? $thread->getTicket()->getCollaborators() : [];
  66.         $headers = ['References' => $ticket->getReferenceIds()];
  67.         if (!empty($thread)) {
  68.             $headers = ['References' => $ticket->getReferenceIds()];
  69.             if (!empty($currentThread) && null != $currentThread->getMessageId()) {
  70.                 $headers['In-Reply-To'] = $currentThread->getMessageId();
  71.             }
  72.             $messageId $container->get('email.service')->sendMail($subject$message$ticket->getCustomer()->getEmail(), $headers$ticket->getMailboxEmail(), $attachments ?? []);
  73.             if (!empty($messageId)) {
  74.                 $updatedReferenceIds $ticket->getReferenceIds() . ' ' $messageId;
  75.                 $ticket->setReferenceIds($updatedReferenceIds);
  76.                 $entityManager->persist($ticket);
  77.                 $entityManager->flush();
  78.             }
  79.             if ($thread->getCc() || $thread->getBcc() || $ticketCollaborators != null && count($ticketCollaborators) > 0) {
  80.                 self::sendCcBccMail($container$ticket$thread$subject$attachments$ticketCollaborators$message);
  81.             }
  82.         } else {
  83.             if (!empty($ticket->getReferenceIds())) {
  84.                 $headers = ['References' => $ticket->getReferenceIds()];
  85.             }
  86.             $message $container->get('email.service')->sendMail($subject$message$ticket->getCustomer()->getEmail(), $headers);
  87.         }
  88.     }
  89.     public static function sendCcBccMail($container$ticket$thread$subject$attachments$ticketCollaborators$message null)
  90.     {
  91.         $cc = array();
  92.         $collabrator = array();
  93.         $entityManager $container->get('doctrine.orm.entity_manager');
  94.         if ($thread->getCc() != null) {
  95.             foreach ($thread->getCc() as $EmailCC) {
  96.                 if ($entityManager->getRepository(Ticket::class)->isTicketCollaborator($thread->getTicket(), $EmailCC) != false) {
  97.                     $collabrator[] = $EmailCC;
  98.                 } else {
  99.                     $cc[] = $EmailCC;
  100.                 }
  101.             }
  102.         }
  103.         $emailOfcollabrator = !empty($thread) && $thread->getCreatedBy() == "collaborator" $thread->getUser()->getEmail() : null;
  104.         if ($collabrator != null && !empty($collabrator) || $ticketCollaborators != null && !empty($ticketCollaborators)) {
  105.             if (count($collabrator) == && count($ticketCollaborators) > && !empty($ticketCollaborators) && empty($collabrator)) {
  106.                 foreach ($ticketCollaborators as $collaborator) {
  107.                     if (!empty($collaborator->getEmail()) && $collaborator->getEmail() != $emailOfcollabrator) {
  108.                         $collabrator[] = $collaborator->getEmail();
  109.                     }
  110.                 }
  111.             }
  112.             $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $ticket->getMailboxEmail(), $attachments ?? [], $collabrator ?? [], []);
  113.             if (!empty($messageId)) {
  114.                 $updatedReferenceIds $ticket->getReferenceIds() . ' ' $messageId;
  115.                 $ticket->setReferenceIds($updatedReferenceIds);
  116.                 $entityManager->persist($ticket);
  117.                 $entityManager->flush();
  118.             }
  119.             if ($collabrator != null && $thread->getCc() != null && count($thread->getCc()) == count($collabrator) && $thread->getBcc() != null) {
  120.                 $message '<html><body style="background-image: none"><p>' html_entity_decode($thread->getMessage()) . '</p></body></html>';
  121.                 $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $ticket->getMailboxEmail(), $attachments ?? [], [], $thread->getBcc() ?? []);
  122.             }
  123.         }
  124.         if ($cc != null && !empty($cc)) {
  125.             $message '<html><body style="background-image: none"><p>' html_entity_decode($thread->getMessage()) . '</p></body></html>';
  126.             $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $ticket->getMailboxEmail(), $attachments ?? [], $cc ?? [], $thread->getBcc() ?? []);
  127.         }
  128.         if ($thread->getBcc() != null && $thread->getCc() == null) {
  129.             $message '<html><body style="background-image: none"><p>' html_entity_decode($thread->getMessage()) . '</p></body></html>';
  130.             $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $ticket->getMailboxEmail(), $attachments ?? [], $thread->getCc() ?? [], $thread->getBcc() ?? []);
  131.         }
  132.     }
  133. }