vendor/uvdesk/api-bundle/API/Threads.php line 154

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\ApiBundle\API;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Webkul\UVDesk\CoreFrameworkBundle\Entity as CoreFrameworkBundleEntity;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
  11. class Threads extends AbstractController
  12. {
  13.     /** Ticket Reply
  14.      * @param Request $request
  15.      */
  16.     public function saveThread(Request $request$ticketidContainerInterface $containerEntityManagerInterface $entityManager)
  17.     {
  18.         $data $request->request->all() ?: json_decode($request->getContent(), true);
  19.         if (
  20.             ! isset($data['threadType'])
  21.             || ! isset($data['message'])
  22.             || ! isset($data['actAsType'])
  23.         ) {
  24.             $json['error'] = 'missing fields';
  25.             $json['description'] = 'required: (threadType: reply|forward|note) , message, actAsType (customer|agent)';
  26.             return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  27.         }
  28.         $ticket $entityManager->getRepository(CoreFrameworkBundleEntity\Ticket::class)->findOneById($ticketid);
  29.         // Check for empty ticket
  30.         if (empty($ticket)) {
  31.             $json['error'] = "Error! No such ticket with ticket id exist";
  32.             return new JsonResponse($jsonResponse::HTTP_NOT_FOUND);
  33.         } else if ('POST' != $request->getMethod()) {
  34.             $json['error'] = "Error! invalid request method";
  35.             return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  36.         }
  37.         // Check if message content is empty
  38.         $parsedMessage trim(strip_tags($data['message'], '<img>'));
  39.         $parsedMessage str_replace('&nbsp;'''$parsedMessage);
  40.         $parsedMessage str_replace(' '''$parsedMessage);
  41.         if (null == $parsedMessage) {
  42.             $json['error'] = "Warning ! Reply content cannot be left blank.";
  43.             return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  44.         }
  45.         $actAsType strtolower($data['actAsType']);
  46.         $user $entityManager->getRepository(CoreFrameworkBundleEntity\User::class)->findOneByEmail($data['actAsEmail']);
  47.         if (! $user) {
  48.             $json['error'] = 'Error! no user found.';
  49.             return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  50.         }
  51.         if ($actAsType == 'agent') {
  52.             $data['user'] = isset($user) && $user $user $container->get('user.service')->getCurrentUser();
  53.         } else {
  54.             $data['user'] = $user;
  55.         }
  56.         $attachments $request->files->get('attachments');
  57.         if (! empty($attachments)) {
  58.             $attachments is_array($attachments) ? $attachments : [$attachments];
  59.         }
  60.         $threadDetails = [
  61.             'user'          => $data['user'],
  62.             'createdBy'     => $actAsType,
  63.             'source'        => 'api',
  64.             'threadType'    => strtolower($data['threadType']),
  65.             'message'       => str_replace(['&lt;script&gt;''&lt;/script&gt;'], ''$data['message']),
  66.             'attachments'   => $attachments
  67.         ];
  68.         if (! empty($data['status'])) {
  69.             $ticketStatus =  $entityManager->getRepository(CoreFrameworkBundleEntity\TicketStatus::class)->findOneByCode($data['status']);
  70.             $ticket->setStatus($ticketStatus);
  71.         }
  72.         if (isset($data['to'])) {
  73.             $threadDetails['to'] = $data['to'];
  74.         }
  75.         if (isset($data['cc'])) {
  76.             $threadDetails['cc'] = $data['cc'];
  77.         }
  78.         if (isset($data['cccol'])) {
  79.             $threadDetails['cccol'] = $data['cccol'];
  80.         }
  81.         if (isset($data['bcc'])) {
  82.             $threadDetails['bcc'] = $data['bcc'];
  83.         }
  84.         $customer $entityManager->getRepository(CoreFrameworkBundleEntity\UserInstance::class)->findOneBy(array('user' => $user->getId(), 'supportRole' => 4));
  85.         if (
  86.             ! empty($customer)
  87.             && $threadDetails['createdBy'] == 'customer'
  88.             && $threadDetails['threadType'] == 'note'
  89.         ) {
  90.             $json['success'] = "success', Can't add note user account.";
  91.             return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  92.         }
  93.         if (
  94.             ! empty($customer)
  95.             && $threadDetails['createdBy'] == 'customer'
  96.             && $threadDetails['threadType'] == 'forward'
  97.         ) {
  98.             $json['success'] = "success', Can't forward ticket to user account.";
  99.             return new JsonResponse($jsonResponse::HTTP_BAD_REQUEST);
  100.         }
  101.         // Create Thread
  102.         $thread $container->get('ticket.service')->createThread($ticket$threadDetails);
  103.         // Check for thread types
  104.         switch ($thread->getThreadType()) {
  105.             case 'note':
  106.                 $event = new CoreWorkflowEvents\Ticket\Note();
  107.                 $event
  108.                     ->setTicket($ticket)
  109.                     ->setThread($thread)
  110.                 ;
  111.                 $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  112.                 $json['success'] = "success', Note added to ticket successfully.";
  113.                 return new JsonResponse($jsonResponse::HTTP_OK);
  114.                 break;
  115.             case 'reply':
  116.                 if ($thread->getCreatedBy() == 'customer') {
  117.                     $event = new CoreWorkflowEvents\Ticket\CustomerReply();
  118.                     $event
  119.                         ->setTicket($ticket)
  120.                         ->setThread($thread)
  121.                     ;
  122.                 } else {
  123.                     $event = new CoreWorkflowEvents\Ticket\AgentReply();
  124.                     $event
  125.                         ->setTicket($ticket)
  126.                         ->setThread($thread)
  127.                     ;
  128.                 }
  129.                 $container->get('event_dispatcher')->dispatch($event'uvdesk.automation.workflow.execute');
  130.                 $json['success'] = "success', Reply added to ticket successfully..";
  131.                 $json['threadId'] = $thread->getId();
  132.                 $json['thread'] = $threadDetails;
  133.                 return new JsonResponse($jsonResponse::HTTP_OK);
  134.                 break;
  135.             case 'forward':
  136.                 // Prepare headers
  137.                 $headers = ['References' => $ticket->getReferenceIds()];
  138.                 if (null != $ticket->currentThread->getMessageId()) {
  139.                     $headers['In-Reply-To'] = $ticket->currentThread->getMessageId();
  140.                 }
  141.                 // Prepare attachments
  142.                 $attachments $entityManager->getRepository(CoreFrameworkBundleEntity\Attachment::class)->findByThread($thread);
  143.                 $projectDir $container->get('kernel')->getProjectDir();
  144.                 $attachments array_map(function ($attachment) use ($projectDir) {
  145.                     return str_replace('//''/'$projectDir "/public" $attachment->getPath());
  146.                 }, $attachments);
  147.                 // Forward thread to users
  148.                 try {
  149.                     $messageId $container->get('email.service')->sendMail($params['subject'] ?? ("Forward: " $ticket->getSubject()), $thread->getMessage(), $thread->getReplyTo(), $headers$ticket->getMailboxEmail(), $attachments ?? [], $thread->getCc() ?: [], $thread->getBcc() ?: []);
  150.                     if (! empty($messageId)) {
  151.                         $thread->setMessageId($messageId);
  152.                         $entityManager->persist($thread);
  153.                         $entityManager->flush();
  154.                     }
  155.                 } catch (\Exception $e) {
  156.                     // Do nothing ...
  157.                     // @TODO: Log exception
  158.                 }
  159.                 $json['success'] = "success', Reply added to the ticket and forwarded successfully.";
  160.                 return new JsonResponse($jsonResponse::HTTP_OK);
  161.                 break;
  162.             default:
  163.                 break;
  164.         }
  165.     }
  166. }