vendor/uvdesk/core-framework/Workflow/Actions/Ticket/MailAgent.php line 106

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Workflow\Actions\Ticket;
  3. use Webkul\UVDesk\AutomationBundle\Workflow\FunctionalGroup;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Webkul\UVDesk\AutomationBundle\Workflow\Action as WorkflowAction;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Ticket;
  7. use Webkul\UVDesk\CoreFrameworkBundle\Entity\EmailTemplates;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Thread;
  9. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Attachment;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
  11. class MailAgent extends WorkflowAction
  12. {
  13.     public static function getId()
  14.     {
  15.         return 'uvdesk.ticket.mail_agent';
  16.     }
  17.     public static function getDescription()
  18.     {
  19.         return "Mail to agent";
  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.         $agentCollection array_map(function ($agent) {
  35.             return [
  36.                 'id' => $agent['id'],
  37.                 'name' => $agent['name'],
  38.             ];
  39.         }, $container->get('user.service')->getAgentPartialDataCollection());
  40.         array_unshift($agentCollection, [
  41.             'id' => 'responsePerforming',
  42.             'name' => 'Response Performing Agent',
  43.         ], [
  44.             'id' => 'assignedAgent',
  45.             'name' => 'Assigned Agent',
  46.         ]);
  47.         return [
  48.             'partResults' => $agentCollection,
  49.             'templates' => $emailTemplateCollection,
  50.         ];
  51.     }
  52.     public static function applyAction(ContainerInterface $container$entity$value null$thread null)
  53.     {
  54.         $entityManager $container->get('doctrine.orm.entity_manager');
  55.         if ($entity instanceof Ticket) {
  56.             $emailTemplate $entityManager->getRepository(EmailTemplates::class)->findOneById($value['value']);
  57.             $emails self::getAgentMails($value['for'], (($ticketAgent $entity->getAgent()) ? $ticketAgent->getEmail() : ''), $container);
  58.             $ticketCollaborators = []; 
  59.             if ($emails || $emailTemplate) {
  60.                 $queryBuilder $entityManager->createQueryBuilder()
  61.                     ->select('th.messageId as messageId')
  62.                     ->from(Thread::class, 'th')
  63.                     ->where('th.createdBy = :userType')->setParameter('userType''agent')
  64.                     ->orderBy('th.id''DESC')
  65.                     ->setMaxResults(1);
  66.                 
  67.                 $inReplyTo $queryBuilder->getQuery()->getOneOrNullResult();
  68.                 if (!empty($inReplyTo)) {
  69.                     $emailHeaders['In-Reply-To'] = $inReplyTo;
  70.                 }
  71.                 if (!empty($entity->getReferenceIds())) {
  72.                     $emailHeaders['References'] = $entity->getReferenceIds();
  73.                 }
  74.                 if ($thread == null) {
  75.                     $ticketId $entity->getId();
  76.                     $thread $container->get('ticket.service')->getInitialThread("$ticketId");
  77.                 }
  78.                 // Only process attachments if required in the message body
  79.                 // @TODO: Revist -> Maybe we should always include attachments if they are provided??
  80.                 $createdThread = isset($entity->createdThread) && $entity->createdThread->getThreadType() != "note" $entity->createdThread : (isset($entity->currentThread) ? $entity->currentThread "") ;
  81.                 $attachments = [];
  82.                 if ((!empty($createdThread) || !empty($thread)) && (strpos($emailTemplate->getMessage(), '{%ticket.attachments%}') !== false || strpos($emailTemplate->getMessage(), '{% ticket.attachments %}') !== false)) {
  83.                     $createdThread = !empty($createdThread) ? $createdThread $thread;
  84.                     $attachments array_map(function($attachment) use ($container) { 
  85.                         return str_replace('//''/'$container->get('kernel')->getProjectDir() . "/public" $attachment->getPath());
  86.                     }, $entityManager->getRepository(Attachment::class)->findByThread($createdThread));
  87.                 }
  88.                 $placeHolderValues $container->get('email.service')->getTicketPlaceholderValues($entity'agent');
  89.                 $subject $container->get('email.service')->processEmailSubject($emailTemplate->getSubject(), $placeHolderValues);
  90.                 $message $container->get('email.service')->processEmailContent($emailTemplate->getMessage(), $placeHolderValues);
  91.                 $thread = ($thread != null) ? $thread $createdThread;
  92.                 if ($thread != null && $thread->getThreadType() == "reply" && $thread->getCreatedBy() != "collaborator") {
  93.                     $ticketCollaborators = (($thread != null) && !empty($thread->getTicket()) && $thread != "" ) ? $thread->getTicket()->getCollaborators() : [];
  94.                 }
  95.                 if(!empty($emails) && $emails != null){
  96.                     foreach ($emails as $email) {
  97.                         if (is_array($email)) {
  98.                            $email $email['email'];
  99.                         }
  100.                         $messageId $container->get('email.service')->sendMail($subject$message$email$emailHeadersnull$attachments ?? []);
  101.                         if (!empty($messageId)) {
  102.                             $updatedReferenceIds $entity->getReferenceIds() . ' ' $messageId;            
  103.                             $entity->setReferenceIds($updatedReferenceIds);
  104.     
  105.                             $entityManager->persist($entity);
  106.                             $entityManager->flush();
  107.                         }
  108.                     }
  109.                 }
  110.                 
  111.                 if(!empty($thread) && ($thread->getCc() || $thread->getBcc()) || $ticketCollaborators != null && count($ticketCollaborators) > 0) {
  112.                     self::sendCcBccMail($container$entity$thread$subject$attachments$ticketCollaborators$message);
  113.                 }
  114.             } else {
  115.                 // Email Template/Emails Not Found. Disable Workflow/Prepared Response
  116.                 // $this->disableEvent($event, $entity);
  117.             }
  118.         } 
  119.     }
  120.     public static function getAgentMails($for$currentEmails$container)
  121.     {
  122.         $agentMails = [];
  123.         $entityManager $container->get('doctrine.orm.entity_manager');
  124.         foreach ($for as $agent) {
  125.             if ($agent == 'assignedAgent') {
  126.                 if (is_array($currentEmails)) {
  127.                     $agentMails array_merge($agentMails$currentEmails);
  128.                 } else {
  129.                     $agentMails[] = $currentEmails;
  130.                 }
  131.             } else if ($agent == 'responsePerforming' && is_object($currentUser $container->get('security.token_storage')->getToken()->getUser())) {
  132.                 // Add current user email if any
  133.                 $agentMails[] = $currentUser->getEmail();
  134.             } else if ($agent == 'baseAgent') {
  135.                 // Add selected user email if any
  136.                 if (is_array($currentEmails)) {
  137.                     $agentMails array_merge($agentMails$currentEmails);
  138.                 } else {
  139.                     $agentMails[] = $currentEmails;
  140.                 }
  141.             } else if((int)$agent) {
  142.                 $qb $entityManager->createQueryBuilder();
  143.                 $emails $qb->select('u.email')->from(User::class, 'u')
  144.                     ->andwhere("u.id = :userId")
  145.                     ->setParameter('userId'$agent)
  146.                     ->getQuery()->getResult();
  147.                 
  148.                 foreach ($emails as $email) {
  149.                     $agent $entityManager->getRepository(User::class)->findOneBy($email);
  150.                     if ($agent != null && $agent->getAgentInstance() != null) {
  151.                             $agentMails[] = $email;
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.         return array_filter($agentMails);
  157.     }
  158.     
  159.     public static function sendCcBccMail($container$entity$thread$subject$attachments$ticketCollaborators$message null)
  160.     {
  161.         $entityManager $container->get('doctrine.orm.entity_manager');
  162.         $collabrator = array();
  163.         $cc = array();
  164.         if($thread->getCc() != null){
  165.             foreach($thread->getCc() as $EmailCC){
  166.                 if($entityManager->getRepository(Ticket::class)->isTicketCollaborator($thread->getTicket(), $EmailCC) != false){
  167.                     $collabrator[] = $EmailCC;
  168.                 }else{
  169.                     $cc[] = $EmailCC;
  170.                 }
  171.            }   
  172.         }
  173.         $emailOfcollabrator = !empty($thread) && $thread->getCreatedBy() == "collaborator" $thread->getUser()->getEmail() : null;
  174.         if ($collabrator != null && !empty($collabrator) || $ticketCollaborators != null && !empty($ticketCollaborators)) {
  175.             if (count($collabrator) == && count($ticketCollaborators) > && !empty($ticketCollaborators) && empty($collabrator)) {
  176.                 foreach ($ticketCollaborators as $collaborator) {
  177.                     if (!empty($collaborator->getEmail()) && $collaborator->getEmail() != $emailOfcollabrator) {
  178.                         $collabrator[] = $collaborator->getEmail();
  179.                     }
  180.                 }
  181.             }
  182.             $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $entity->getMailboxEmail(), $attachments ?? [], $collabrator ?? [], []); 
  183.             if (!empty($messageId)) {
  184.             $updatedReferenceIds $entity->getReferenceIds() . ' ' $messageId;            
  185.             $entity->setReferenceIds($updatedReferenceIds);
  186.             $entityManager->persist($entity);
  187.             $entityManager->flush();
  188.             }
  189.            if($thread->getCc() != null && count($thread->getCc()) == count($collabrator) && $thread->getBcc() != null){
  190.             $message '<html><body style="background-image: none"><p>'.html_entity_decode($thread->getMessage()).'</p></body></html>';
  191.             $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $entity->getMailboxEmail(), $attachments ?? [], [], $thread->getBcc() ?? []);  
  192.            }
  193.         }
  194.         if($cc != null && !empty($cc)){
  195.             $message '<html><body style="background-image: none"><p>'.html_entity_decode($thread->getMessage()).'</p></body></html>';
  196.             $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $entity->getMailboxEmail(), $attachments ?? [], $cc ?? [], $thread->getBcc() ?? []);    
  197.         }
  198.            
  199.         if($thread->getBcc() != null && $thread->getCc() == null){
  200.             $message '<html><body style="background-image: none"><p>'.html_entity_decode($thread->getMessage()).'</p></body></html>';
  201.             $messageId $container->get('email.service')->sendMail($subject$messagenull, [], $entity->getMailboxEmail(), $attachments ?? [], $thread->getCc() ?? [], $thread->getBcc() ?? []);  
  202.         }
  203.     }
  204. }