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

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