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