vendor/uvdesk/core-framework/Controller/TicketXHR.php line 516

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Webkul\UVDesk\CoreFrameworkBundle\Entity as CoreFrameworkBundleEntities;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportLabel;
  7. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Thread;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Ticket;
  9. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Tag;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Entity\TicketType;
  11. use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportRole;
  12. use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
  13. use Webkul\UVDesk\CoreFrameworkBundle\Entity\TicketPriority;
  14. use Webkul\UVDesk\CoreFrameworkBundle\Entity\TicketStatus;
  15. use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportGroup;
  16. use Webkul\UVDesk\CoreFrameworkBundle\Entity\SupportTeam;
  17. use Webkul\UVDesk\CoreFrameworkBundle\Entity\TicketLabel;
  18. use Symfony\Component\EventDispatcher\GenericEvent;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
  21. use Webkul\UVDesk\CoreFrameworkBundle\Form as CoreFrameworkBundleForms;
  22. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  23. use Webkul\UVDesk\CoreFrameworkBundle\DataProxies as CoreFrameworkBundleDataProxies;
  24. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  25. use Symfony\Contracts\Translation\TranslatorInterface;
  26. use Webkul\UVDesk\CoreFrameworkBundle\Services\UVDeskService;
  27. use Webkul\UVDesk\CoreFrameworkBundle\Services\TicketService;
  28. use Webkul\UVDesk\CoreFrameworkBundle\Services\EmailService;
  29. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  30. use Symfony\Component\DependencyInjection\ContainerInterface;
  31. use Webkul\UVDesk\SupportCenterBundle\Entity\ArticleTags;
  32. class TicketXHR extends AbstractController
  33. {
  34.     private $userService;
  35.     private $translator;
  36.     private $eventDispatcher;
  37.     private $ticketService;
  38.     private $emailService;
  39.     public function __construct(UserService $userServiceTranslatorInterface $translatorTicketService $ticketServiceEmailService $emailServiceEventDispatcherInterface $eventDispatcher)
  40.     {
  41.         $this->userService $userService;
  42.         $this->emailService $emailService;
  43.         $this->translator $translator;
  44.         $this->ticketService $ticketService;
  45.         $this->eventDispatcher $eventDispatcher;
  46.     }
  47.     public function loadTicketXHR($ticketId)
  48.     {
  49.         $entityManager $this->getDoctrine()->getManager();
  50.         $request $this->container->get('request_stack')->getCurrentRequest();
  51.     }
  52.     public function bookmarkTicketXHR()
  53.     {
  54.         $entityManager $this->getDoctrine()->getManager();
  55.         $request $this->container->get('request_stack')->getCurrentRequest();
  56.         $requestContent json_decode($request->getContent(), true);
  57.         $ticket $entityManager->getRepository(Ticket::class)->findOneById($requestContent['id']);
  58.         // Process only if user have ticket access
  59.         if (false == $this->ticketService->isTicketAccessGranted($ticket)) {
  60.             throw new \Exception('Access Denied'403);
  61.         }
  62.         if (!empty($ticket)) {
  63.             $ticket->setIsStarred(!$ticket->getIsStarred());
  64.             $entityManager->persist($ticket);
  65.             $entityManager->flush();
  66.             return new Response(json_encode(['alertClass' => 'success']), 200, ['Content-Type' => 'application/json']);
  67.         }
  68.         return new Response(json_encode([]), 404, ['Content-Type' => 'application/json']);
  69.     }
  70.     public function ticketLabelXHR(Request $request)
  71.     {
  72.         $method $request->getMethod();
  73.         $content $request->getContent();
  74.         $em $this->getDoctrine()->getManager();
  75.         if($method == "POST") {
  76.             $data json_decode($contenttrue);
  77.             if($data['name'] != "") {
  78.                 $label = new SupportLabel();
  79.                 $label->setName($data['name']);
  80.                 if(isset($data['colorCode']))
  81.                     $label->setColorCode($data['colorCode']);
  82.                 $label->setUser($this->userService->getCurrentUser());
  83.                 $em->persist($label);
  84.                 $em->flush();
  85.                 $json['alertClass'] = 'success';
  86.                 $json['alertMessage'] = $this->translator->trans('Success ! Label created successfully.');
  87.                 $json['label'] = json_encode([
  88.                     'id' => $label->getId(),
  89.                     'name' => $label->getName(),
  90.                     'colorCode' => $label->getColorCode(),
  91.                     'labelUser' => $label->getUser()->getId(),
  92.                 ]);
  93.             } else {
  94.                 $json['alertClass'] = 'danger';
  95.                 $json['alertMessage'] = $this->translator->trans('Error ! Label name can not be blank.');
  96.             }
  97.         } elseif($method == "PUT") {
  98.             $data json_decode($contenttrue);
  99.             $label $em->getRepository(SupportLabel::class)->findOneBy(array('id' => $request->attributes->get('ticketLabelId')));
  100.             if($label) {
  101.                 $label->setName($data['name']);
  102.                 if(!empty($data['colorCode'])) {
  103.                     $label->setColorCode($data['colorCode']);
  104.                 }
  105.                 $em->persist($label);
  106.                 $em->flush();
  107.                 $json['label'] = json_encode([
  108.                     'id' => $label->getId(),
  109.                     'name' => $label->getName(),
  110.                     'colorCode' => $label->getColorCode(),
  111.                     'labelUser' => $label->getUser()->getId(),
  112.                 ]);
  113.                 $json['alertClass'] = 'success';
  114.                 $json['alertMessage'] = $this->translator->trans('Success ! Label updated successfully.');
  115.             } else {
  116.                 $json['alertClass'] = 'danger';
  117.                 $json['alertMessage'] = $this->translator->trans('Error ! Invalid label id.');
  118.             }
  119.         } elseif($method == "DELETE") {
  120.             $label $em->getRepository(SupportLabel::class)->findOneBy(array('id' => $request->attributes->get('ticketLabelId')));
  121.             if($label) {
  122.                 $em->remove($label);
  123.                 $em->flush();
  124.                 $json['alertClass'] = 'success';
  125.                 $json['alertMessage'] = $this->translator->trans('Success ! Label removed successfully.');
  126.             } else {
  127.                 $json['alertClass'] = 'danger';
  128.                 $json['alertMessage'] = $this->translator->trans('Error ! Invalid label id.');
  129.             }
  130.         }
  131.         return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
  132.     }
  133.     public function updateTicketDetails(Request $request)
  134.     {
  135.         $ticketId $request->attributes->get('ticketId');
  136.         $entityManager $this->getDoctrine()->getManager();
  137.         $ticket $entityManager->getRepository(Ticket::class)->find($ticketId);
  138.         if (!$ticket)
  139.             $this->noResultFound();
  140.         // Proceed only if user has access to the resource
  141.         if (false == $this->ticketService->isTicketAccessGranted($ticket)) {
  142.             throw new \Exception('Access Denied'403);
  143.         }
  144.         $error false;
  145.         $message '';
  146.         if ($request->request->get('subject') == '') {
  147.             $error true;
  148.             $message $this->translator->trans('Error! Subject field is mandatory');
  149.         } elseif ($request->request->get('reply') == '') {
  150.             $error true;
  151.             $message $this->translator->trans('Error! Reply field is mandatory');
  152.         }
  153.         if (!$error) {
  154.             $ticket->setSubject($request->request->get('subject'));
  155.             $createThread $this->ticketService->getCreateReply($ticket->getId(), false);
  156.             $createThread $entityManager->getRepository(Thread::class)->find($createThread['id']);
  157.             $createThread->setMessage($request->request->get('reply'));
  158.             $entityManager->persist($createThread);
  159.             $entityManager->persist($ticket);
  160.             $entityManager->flush();
  161.             $this->addFlash('success'$this->translator->trans('Success ! Ticket has been updated successfully.'));
  162.         } else {
  163.             $this->addFlash('warning'$message);
  164.         }
  165.         return $this->redirect($this->generateUrl('helpdesk_member_ticket', ['ticketId'=> $ticketId] ));
  166.     }
  167.     public function updateTicketAttributes($ticketId)
  168.     {
  169.         // @TODO: Ticket Voter
  170.         // $this->denyAccessUnlessGranted('VIEW', $ticket);
  171.         $entityManager $this->getDoctrine()->getManager();
  172.         $request $this->container->get('request_stack')->getCurrentRequest();
  173.         $requestContent $request->request->all() ?: json_decode($request->getContent(), true);
  174.         $ticketId =  $ticketId != $ticketId $requestContent['ticketId'];
  175.         $ticket $entityManager->getRepository(Ticket::class)->findOneById($ticketId);
  176.         // Proceed only if user has access to the resource
  177.         if (false == $this->ticketService->isTicketAccessGranted($ticket)) {
  178.             throw new \Exception('Access Denied'403);
  179.         }
  180.         // Validate request integrity
  181.         if (empty($ticket)) {
  182.             $responseContent = [
  183.                 'alertClass' => 'danger',
  184.                 'alertMessage' => $this->translator->trans('Unable to retrieve details for ticket #%ticketId%.', [
  185.                     '%ticketId%' => $ticketId,
  186.                 ]),
  187.             ];
  188.             return new Response(json_encode($responseContent), 200, ['Content-Type' => 'application/json']);
  189.         } else if (!isset($requestContent['attribute'])) {
  190.             $responseContent = [
  191.                 'alertClass' => 'danger',
  192.                 'alertMessage' => $this->translator->trans('Insufficient details provided.'),
  193.             ];
  194.             return new Response(json_encode($responseContent), 400, ['Content-Type' => 'application/json']);
  195.         }
  196.         // Update attribute
  197.         switch ($requestContent['attribute']) {
  198.             case 'agent':
  199.                 $agent $entityManager->getRepository(User::class)->findOneById($requestContent['value']);
  200.                 if (empty($agent)) {
  201.                     // User does not exist
  202.                     return new Response(json_encode([
  203.                         'alertClass' => 'danger',
  204.                         'alertMessage' => $this->translator->trans('Unable to retrieve agent details'),
  205.                     ]), 404, ['Content-Type' => 'application/json']);
  206.                 } else {
  207.                     // Check if an agent instance exists for the user
  208.                     $agentInstance $agent->getAgentInstance();
  209.                     if (empty($agentInstance)) {
  210.                         // Agent does not exist
  211.                         return new Response(json_encode([
  212.                             'alertClass' => 'danger',
  213.                             'alertMessage' => $this->translator->trans('Unable to retrieve agent details'),
  214.                         ]), 404, ['Content-Type' => 'application/json']);
  215.                     }
  216.                 }
  217.                 $agentDetails $agentInstance->getPartialDetails();
  218.                 // Check if ticket is already assigned to the agent
  219.                 if ($ticket->getAgent() && $agent->getId() === $ticket->getAgent()->getId()) {
  220.                     return new Response(json_encode([
  221.                         'alertClass' => 'success',
  222.                         'alertMessage' => $this->translator->trans('Ticket already assigned to %agent%', [
  223.                             '%agent%' => $agentDetails['name'],
  224.                         ]),
  225.                     ]), 200, ['Content-Type' => 'application/json']);
  226.                 } else {
  227.                     $ticket->setAgent($agent);
  228.                     $entityManager->persist($ticket);
  229.                     $entityManager->flush();
  230.                     // Trigger Agent Assign event
  231.                     $event = new GenericEvent(CoreWorkflowEvents\Ticket\Agent::getId(), [
  232.                         'entity' => $ticket,
  233.                     ]);
  234.                     $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  235.                     return new Response(json_encode([
  236.                         'alertClass' => 'success',
  237.                         'alertMessage' => $this->translator->trans('Ticket successfully assigned to %agent%', [
  238.                             '%agent%' => $agentDetails['name'],
  239.                         ]),
  240.                     ]), 200, ['Content-Type' => 'application/json']);
  241.                 }
  242.                 break;
  243.             case 'status':
  244.                 $ticketStatus $entityManager->getRepository(TicketStatus::class)->findOneById((int) $requestContent['value']);
  245.                 if (empty($ticketStatus)) {
  246.                     // Selected ticket status does not exist
  247.                     return new Response(json_encode([
  248.                         'alertClass' => 'danger',
  249.                         'alertMessage' => $this->translator->trans('Unable to retrieve status details'),
  250.                     ]), 404, ['Content-Type' => 'application/json']);
  251.                 }
  252.                 if ($ticketStatus->getId() === $ticket->getStatus()->getId()) {
  253.                     return new Response(json_encode([
  254.                         'alertClass' => 'success',
  255.                         'alertMessage' => $this->translator->trans('Ticket status already set to %status%', [
  256.                             '%status%' => $ticketStatus->getDescription()
  257.                         ]),
  258.                     ]), 200, ['Content-Type' => 'application/json']);
  259.                 } else {
  260.                     $ticket->setStatus($ticketStatus);
  261.                     $entityManager->persist($ticket);
  262.                     $entityManager->flush();
  263.                     // Trigger ticket status event
  264.                     $event = new GenericEvent(CoreWorkflowEvents\Ticket\Status::getId(), [
  265.                         'entity' => $ticket,
  266.                     ]);
  267.                     $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  268.                     return new Response(json_encode([
  269.                         'alertClass' => 'success',
  270.                         'alertMessage' => $this->translator->trans('Ticket status update to %status%', [
  271.                             '%status%' => $ticketStatus->getDescription()
  272.                         ]),
  273.                     ]), 200, ['Content-Type' => 'application/json']);
  274.                 }
  275.                 break;
  276.             case 'priority':
  277.                 // $this->isAuthorized('ROLE_AGENT_UPDATE_TICKET_PRIORITY');
  278.                 $ticketPriority $entityManager->getRepository(TicketPriority::class)->findOneById($requestContent['value']);
  279.                 if (empty($ticketPriority)) {
  280.                     // Selected ticket priority does not exist
  281.                     return new Response(json_encode([
  282.                         'alertClass' => 'danger',
  283.                         'alertMessage' => $this->translator->trans('Unable to retrieve priority details'),
  284.                     ]), 404, ['Content-Type' => 'application/json']);
  285.                 }
  286.                 if ($ticketPriority->getId() === $ticket->getPriority()->getId()) {
  287.                     return new Response(json_encode([
  288.                         'alertClass' => 'success',
  289.                         'alertMessage' => $this->translator->trans('Ticket priority already set to %priority%', [
  290.                             '%priority%' => $ticketPriority->getDescription()
  291.                         ]),
  292.                     ]), 200, ['Content-Type' => 'application/json']);
  293.                 } else {
  294.                     $ticket->setPriority($ticketPriority);
  295.                     $entityManager->persist($ticket);
  296.                     $entityManager->flush();
  297.                     // Trigger ticket Priority event
  298.                     $event = new GenericEvent(CoreWorkflowEvents\Ticket\Priority::getId(), [
  299.                         'entity' => $ticket,
  300.                     ]);
  301.                     $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  302.                     return new Response(json_encode([
  303.                         'alertClass' => 'success',
  304.                         'alertMessage' => $this->translator->trans('Ticket priority updated to %priority%', [
  305.                             '%priority%' => $ticketPriority->getDescription()
  306.                         ]),
  307.                     ]), 200, ['Content-Type' => 'application/json']);
  308.                 }
  309.                 break;
  310.             case 'group':
  311.                 $supportGroup $entityManager->getRepository(SupportGroup::class)->findOneById($requestContent['value']);
  312.                 if (empty($supportGroup)) {
  313.                     if ($requestContent['value'] == "") {
  314.                         if ($ticket->getSupportGroup() != null) {
  315.                             $ticket->setSupportGroup(null);
  316.                             $entityManager->persist($ticket);
  317.                             $entityManager->flush();
  318.                         }
  319.                         $responseCode 200;
  320.                         $response = [
  321.                             'alertClass' => 'success',
  322.                             'alertMessage' => $this->translator->trans('Ticket support group updated successfully'),
  323.                         ];
  324.                     } else {
  325.                         $responseCode 404;
  326.                         $response = [
  327.                             'alertClass' => 'danger',
  328.                             'alertMessage' => $this->translator->trans('Unable to retrieve support group details'),
  329.                         ];
  330.                     }
  331.                     return new Response(json_encode($response), $responseCode, ['Content-Type' => 'application/json']);;
  332.                 }
  333.                 if ($ticket->getSupportGroup() != null && $supportGroup->getId() === $ticket->getSupportGroup()->getId()) {
  334.                     return new Response(json_encode([
  335.                         'alertClass' => 'success',
  336.                         'alertMessage' => 'Ticket already assigned to support group ' $supportGroup->getName(),
  337.                     ]), 200, ['Content-Type' => 'application/json']);
  338.                 } else {
  339.                     $ticket->setSupportGroup($supportGroup);
  340.                     $entityManager->persist($ticket);
  341.                     $entityManager->flush();
  342.                     // Trigger Support group event
  343.                     $event = new GenericEvent(CoreWorkflowEvents\Ticket\Group::getId(), [
  344.                         'entity' => $ticket,
  345.                     ]);
  346.                     $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  347.                     return new Response(json_encode([
  348.                         'alertClass' => 'success',
  349.                         'alertMessage' => $this->translator->trans('Ticket assigned to support group '). $supportGroup->getName(),
  350.                     ]), 200, ['Content-Type' => 'application/json']);
  351.                 }
  352.                 break;
  353.             case 'team':
  354.                 $supportTeam $entityManager->getRepository(SupportTeam::class)->findOneById($requestContent['value']);
  355.                 if (empty($supportTeam)) {
  356.                     if ($requestContent['value'] == "") {
  357.                         if ($ticket->getSupportTeam() != null) {
  358.                             $ticket->setSupportTeam(null);
  359.                             $entityManager->persist($ticket);
  360.                             $entityManager->flush();
  361.                         }
  362.                         $responseCode 200;
  363.                         $response = [
  364.                             'alertClass' => 'success',
  365.                             'alertMessage' => $this->translator->trans('Ticket support team updated successfully'),
  366.                         ];
  367.                     } else {
  368.                         $responseCode 404;
  369.                         $response = [
  370.                             'alertClass' => 'danger',
  371.                             'alertMessage' => $this->translator->trans('Unable to retrieve support team details'),
  372.                         ];
  373.                     }
  374.                     return new Response(json_encode($response), $responseCode, ['Content-Type' => 'application/json']);;
  375.                 }
  376.                 if ($ticket->getSupportTeam() != null && $supportTeam->getId() === $ticket->getSupportTeam()->getId()) {
  377.                     return new Response(json_encode([
  378.                         'alertClass' => 'success',
  379.                         'alertMessage' => 'Ticket already assigned to support team ' $supportTeam->getName(),
  380.                     ]), 200, ['Content-Type' => 'application/json']);
  381.                 } else {
  382.                     $ticket->setSupportTeam($supportTeam);
  383.                     $entityManager->persist($ticket);
  384.                     $entityManager->flush();
  385.                     // Trigger ticket delete event
  386.                     $event = new GenericEvent(CoreWorkflowEvents\Ticket\Team::getId(), [
  387.                         'entity' => $ticket,
  388.                     ]);
  389.                     $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  390.                     return new Response(json_encode([
  391.                         'alertClass' => 'success',
  392.                         'alertMessage' => 'Ticket assigned to support team ' $supportTeam->getName(),
  393.                     ]), 200, ['Content-Type' => 'application/json']);
  394.                 }
  395.                 break;
  396.             case 'type':
  397.                 // $this->isAuthorized('ROLE_AGENT_UPDATE_TICKET_TYPE');
  398.                 $ticketType $entityManager->getRepository(TicketType::class)->findOneById($requestContent['value']);
  399.                 if (empty($ticketType)) {
  400.                     // Selected ticket priority does not exist
  401.                     return new Response(json_encode([
  402.                         'alertClass' => 'danger',
  403.                         'alertMessage' => 'Unable to retrieve ticket type details',
  404.                     ]), 404, ['Content-Type' => 'application/json']);
  405.                 }
  406.                 if (null != $ticket->getType() && $ticketType->getId() === $ticket->getType()->getId()) {
  407.                     return new Response(json_encode([
  408.                         'alertClass' => 'success',
  409.                         'alertMessage' => 'Ticket type already set to ' $ticketType->getDescription(),
  410.                     ]), 200, ['Content-Type' => 'application/json']);
  411.                 } else {
  412.                     $ticket->setType($ticketType);
  413.                     $entityManager->persist($ticket);
  414.                     $entityManager->flush();
  415.                     // Trigger ticket delete event
  416.                     $event = new GenericEvent(CoreWorkflowEvents\Ticket\Type::getId(), [
  417.                         'entity' => $ticket,
  418.                     ]);
  419.                     $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  420.                     return new Response(json_encode([
  421.                         'alertClass' => 'success',
  422.                         'alertMessage' => 'Ticket type updated to ' $ticketType->getDescription(),
  423.                     ]), 200, ['Content-Type' => 'application/json']);
  424.                 }
  425.                 break;
  426.             case 'label':
  427.                 $label $entityManager->getRepository(SupportLabel::class)->find($requestContent['labelId']);
  428.                 if($label) {
  429.                     $ticket->removeSupportLabel($label);
  430.                     $entityManager->persist($ticket);
  431.                     $entityManager->flush();
  432.                     return new Response(json_encode([
  433.                         'alertClass' => 'success',
  434.                         'alertMessage' => $this->translator->trans('Success ! Ticket to label removed successfully.'),
  435.                     ]), 200, ['Content-Type' => 'application/json']);
  436.                 }
  437.                 break;
  438.             default:
  439.                 break;
  440.         }
  441.         return new Response(json_encode([]), 400, ['Content-Type' => 'application/json']);
  442.     }
  443.     public function listTicketCollectionXHR(Request $request)
  444.     {
  445.         if ($request->isXmlHttpRequest()) {
  446.             $paginationResponse $this->ticketService->paginateMembersTicketCollection($request);
  447.             return new Response(json_encode($paginationResponse), 200, ['Content-Type' => 'application/json']);
  448.         }
  449.         return new Response(json_encode([]), 404, ['Content-Type' => 'application/json']);
  450.     }
  451.     public function updateTicketCollectionXHR(Request $request)
  452.     {
  453.         if ($request->isXmlHttpRequest()) {
  454.             $massResponse $this->ticketService->massXhrUpdate($request);
  455.             return new Response(json_encode($massResponse), 200, ['Content-Type' => 'application/json']);
  456.         }
  457.         return new Response(json_encode([]), 404);
  458.     }
  459.     public function loadTicketFilterOptionsXHR(Request $request)
  460.     {
  461.         return new Response(json_encode([]), 404);
  462.     }
  463.     public function saveTicketLabel(Request $request)
  464.     {
  465.         $entityManager $this->getDoctrine()->getManager();
  466.         $request $this->container->get('request_stack')->getCurrentRequest();
  467.         $requestContent json_decode($request->getContent(), true);
  468.         $ticket $entityManager->getRepository(Ticket::class)->findOneById($requestContent['ticketId']);
  469.         // Process only if user have ticket access
  470.         if (false == $this->ticketService->isTicketAccessGranted($ticket)) {
  471.             throw new \Exception('Access Denied'403);
  472.         }
  473.         if ('POST' == $request->getMethod()) {
  474.             $responseContent = [];
  475.             $user $this->userService->getSessionUser();
  476.             $supportLabel $entityManager->getRepository(SupportLabel::class)->findOneBy([
  477.                 'user' => $user->getId(),
  478.                 'name' => $requestContent['name'],
  479.             ]);
  480.             if (empty($supportLabel)) {
  481.                 $supportLabel = new SupportLabel();
  482.                 $supportLabel->setName($requestContent['name']);
  483.                 $supportLabel->setUser($user);
  484.                 $entityManager->persist($supportLabel);
  485.                 $entityManager->flush();
  486.             }
  487.             $ticketLabelCollection $ticket->getSupportLabels()->toArray();
  488.             if (empty($ticketLabelCollection)) {
  489.                 $ticket->addSupportLabel($supportLabel);
  490.                 $entityManager->persist($ticket);
  491.                 $entityManager->flush();
  492.                 $responseContent['alertClass'] = 'success';
  493.                 $responseContent['alertMessage'] = $this->translator->trans(
  494.                     'Label %label% added to ticket successfully', [
  495.                     '%label%' => $supportLabel->getName(),
  496.                 ]);
  497.             } else {
  498.                 $isLabelAlreadyAdded false;
  499.                 foreach ($ticketLabelCollection as $ticketLabel) {
  500.                     if ($supportLabel->getId() == $ticketLabel->getId()) {
  501.                         $isLabelAlreadyAdded true;
  502.                         break;
  503.                     }
  504.                 }
  505.                 if (false == $isLabelAlreadyAdded) {
  506.                     $ticket->addSupportLabel($supportLabel);
  507.                     $entityManager->persist($ticket);
  508.                     $entityManager->flush();
  509.                     $responseContent['alertClass'] = 'success';
  510.                     $responseContent['alertMessage'] = $this->translator->trans(
  511.                         'Label %label% added to ticket successfully', [
  512.                         '%label%' => $supportLabel->getName(),
  513.                     ]);
  514.                 } else {
  515.                     $responseContent['alertClass'] = 'warning';
  516.                     $responseContent['alertMessage'] = $this->translator->trans(
  517.                         'Label %label% already added to ticket', [
  518.                         '%label%' => $supportLabel->getName(),
  519.                     ]);
  520.                 }
  521.             }
  522.             $responseContent['label'] = [
  523.                 'id' => $supportLabel->getId(),
  524.                 'name' => $supportLabel->getName(),
  525.                 'color' => $supportLabel->getColorCode(),
  526.             ];
  527.             return new Response(json_encode($responseContent), 200, ['Content-Type' => 'application/json']);
  528.         }
  529.         return new Response(json_encode([]), 404, ['Content-Type' => 'application/json']);
  530.     }
  531.     public function getLabels($request null)
  532.     {
  533.         static $labels;
  534.         if (null !== $labels)
  535.             return $labels;
  536.         $qb $this->em->createQueryBuilder();
  537.         $qb->select('tl')->from(TicketLabel::class, 'tl')
  538.             ->andwhere('tl.labelUser = :labelUserId')
  539.             ->andwhere('tl.company = :companyId')
  540.             ->setParameter('labelUserId'$this->getUser()->getId())
  541.             ->setParameter('companyId'$this->getCompany()->getId());
  542.         if($request) {
  543.             $qb->andwhere("tl.name LIKE :labelName");
  544.             $qb->setParameter('labelName''%'.urldecode($request->query->get('query')).'%');
  545.         }
  546.         return $labels $qb->getQuery()->getArrayResult();
  547.     }
  548.     public function loadTicketSearchFilterOptions(Request $request)
  549.     {
  550.         if (true === $request->isXmlHttpRequest()) {
  551.             switch ($request->query->get('type')) {
  552.                 case 'agent':
  553.                     $filtersResponse $this->userService->getAgentPartialDataCollection($request);
  554.                     break;
  555.                 case 'customer':
  556.                     $filtersResponse $this->userService->getCustomersPartial($request);
  557.                     break;
  558.                 case 'group':
  559.                     $filtersResponse $this->userService->getSupportGroups($request);
  560.                     break;
  561.                 case 'team':
  562.                     $filtersResponse $this->userService->getSupportTeams($request);
  563.                     break;
  564.                 case 'tag':
  565.                     $filtersResponse $this->ticketService->getTicketTags($request);
  566.                     break;
  567.                 case 'label':
  568.                     $searchTerm $request->query->get('query');
  569.                     $entityManager $this->getDoctrine()->getManager();
  570.                     $supportLabelQuery $entityManager->createQueryBuilder()->select('supportLabel')
  571.                         ->from(SupportLabel::class, 'supportLabel')
  572.                         ->where('supportLabel.user = :user')->setParameter('user'$this->userService->getSessionUser());
  573.                     if (!empty($searchTerm)) {
  574.                         $supportLabelQuery->andWhere('supportLabel.name LIKE :labelName')->setParameter('labelName''%' urldecode($searchTerm) . '%');
  575.                     }
  576.                     $supportLabelCollection $supportLabelQuery->getQuery()->getArrayResult();
  577.                     return new Response(json_encode($supportLabelCollection), 200, ['Content-Type' => 'application/json']);
  578.                     break;
  579.                 default:
  580.                     break;
  581.             }
  582.         }
  583.         return new Response(json_encode([]), 404, ['Content-Type' => 'application/json']);
  584.     }
  585.     public function loadTicketCollectionSearchFilterOptionsXHR(Request $request)
  586.     {
  587.         $json = [];
  588.         if ($request->isXmlHttpRequest()) {
  589.             if ($request->query->get('type') == 'agent') {
  590.                 $json $this->userService->getAgentsPartialDetails($request);
  591.             } elseif ($request->query->get('type') == 'customer') {
  592.                 $json $this->userService->getCustomersPartial($request);
  593.             } elseif ($request->query->get('type') == 'group') {
  594.                 $json $this->userService->getSupportGroups($request);
  595.             } elseif ($request->query->get('type') == 'team') {
  596.                 $json $this->userService->getSupportTeams($request);
  597.             } elseif ($request->query->get('type') == 'tag') {
  598.                 $json $this->ticketService->getTicketTags($request);
  599.             } elseif ($request->query->get('type') == 'label') {
  600.                 $json $this->ticketService->getLabels($request);
  601.             }
  602.         }
  603.         return new Response(json_encode($json), 200, ['Content-Type' => 'application/json']);
  604.     }
  605.     public function listTicketTypeCollectionXHR(Request $request)
  606.     {
  607.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_TICKET_TYPE')) {
  608.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  609.         }
  610.         if (true === $request->isXmlHttpRequest()) {
  611.             $paginationResponse $this->ticketService->paginateMembersTicketTypeCollection($request);
  612.             return new Response(json_encode($paginationResponse), 200, ['Content-Type' => 'application/json']);
  613.         }
  614.         return new Response(json_encode([]), 404, ['Content-Type' => 'application/json']);
  615.     }
  616.     public function removeTicketTypeXHR($typeIdRequest $request)
  617.     {
  618.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_TICKET_TYPE')) {
  619.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  620.         }
  621.         $json = [];
  622.         if($request->getMethod() == "DELETE") {
  623.             $em $this->getDoctrine()->getManager();
  624.             $id $request->attributes->get('typeId');
  625.             $type $em->getRepository(TicketType::class)->find($id);
  626.             // $this->get('event.manager')->trigger([
  627.             //             'event' => 'type.deleted',
  628.             //             'entity' => $type
  629.             //         ]);
  630.             $em->remove($type);
  631.             $em->flush();
  632.             $json['alertClass'] = 'success';
  633.             $json['alertMessage'] = $this->translator->trans('Success ! Type removed successfully.');
  634.         }
  635.         $response = new Response(json_encode($json));
  636.         $response->headers->set('Content-Type''application/json');
  637.         return $response;
  638.     }
  639.     public function listTagCollectionXHR(Request $request)
  640.     {
  641.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_TAG')) {
  642.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  643.         }
  644.         if (true === $request->isXmlHttpRequest()) {
  645.             $paginationResponse $this->ticketService->paginateMembersTagCollection($request);
  646.             return new Response(json_encode($paginationResponse), 200, ['Content-Type' => 'application/json']);
  647.         }
  648.         return new Response(json_encode([]), 404, ['Content-Type' => 'application/json']);
  649.     }
  650.     public function applyTicketPreparedResponseXHR(Request $request)
  651.     {
  652.         $id $request->attributes->get('id');
  653.         $ticketId $request->attributes->get('ticketId');
  654.         $ticket $this->getDoctrine()->getManager()->getRepository(Ticket::class)->findOneById($ticketId);
  655.         // Process only if user have ticket access
  656.         if (false == $this->ticketService->isTicketAccessGranted($ticket)) {
  657.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  658.         }
  659.         $event = new GenericEvent($id, [
  660.             'entity' =>  $ticket
  661.         ]);
  662.         $this->eventDispatcher->dispatch($event'uvdesk.automation.prepared_response.execute');
  663.         $this->addFlash('success'$this->translator->trans('Success ! Prepared Response applied successfully.'));
  664.         return $this->redirect($this->generateUrl('helpdesk_member_ticket',['ticketId' => $ticketId]));
  665.     }
  666.     public function loadTicketSavedReplies(Request $request)
  667.     {
  668.         $json = array();
  669.         $data $request->query->all();
  670.         if ($request->isXmlHttpRequest()) {
  671.             $json['message'] = $this->ticketService->getSavedReplyContent($data['id'],$data['ticketId']);
  672.         }
  673.         $response = new Response(json_encode($json));
  674.         return $response;
  675.     }
  676.     public function createTicketTagXHR(Request $request)
  677.     {
  678.         $json = [];
  679.         $content json_decode($request->getContent(), true);
  680.         $em $this->getDoctrine()->getManager();
  681.         $ticket $em->getRepository(Ticket::class)->find($content['ticketId']);
  682.         // Process only if user have ticket access
  683.         if (false == $this->ticketService->isTicketAccessGranted($ticket)) {
  684.             throw new \Exception('Access Denied'403);
  685.         }
  686.         if($request->getMethod() == "POST") {
  687.             $tag = new CoreFrameworkBundleEntities\Tag();
  688.             if ($content['name'] != "") {
  689.                 $checkTag $em->getRepository(Tag::class)->findOneBy(array('name' => $content['name']));
  690.                 if(!$checkTag) {
  691.                     $tag->setName($content['name']);
  692.                     $em->persist($tag);
  693.                     $em->flush();
  694.                     //$json['tag'] = json_decode($this->objectSerializer($tag));
  695.                     $ticket->addSupportTag($tag);
  696.                 } else {
  697.                     //$json['tag'] = json_decode($this->objectSerializer($checkTag));
  698.                     $ticket->addSupportTag($checkTag);
  699.                 }
  700.                 $em->persist($ticket);
  701.                 $em->flush();
  702.                 $json['alertClass'] = 'success';
  703.                 $json['alertMessage'] = $this->translator->trans('Success ! Tag added successfully.');
  704.             } else {
  705.                 $json['alertClass'] = 'danger';
  706.                 $json['alertMessage'] = $this->translator->trans('Please enter tag name.');
  707.             }
  708.         } elseif($request->getMethod() == "DELETE") {
  709.             $tag $em->getRepository(Tag::class)->findOneBy(array('id' => $request->attributes->get('id')));
  710.             if($tag) {
  711.                 $articles $em->getRepository(ArticleTags::class)->findOneBy(array('tagId' => $tag->getId()));
  712.                 if($articles)
  713.                     foreach ($articles as $entry) {
  714.                         $em->remove($entry);
  715.                     }
  716.                 $ticket->removeSupportTag($tag);
  717.                 $em->persist($ticket);
  718.                 $em->flush();
  719.                 $json['alertClass'] = 'success';
  720.                 $json['alertMessage'] = $this->translator->trans('Success ! Tag unassigned successfully.');
  721.             } else {
  722.                 $json['alertClass'] = 'danger';
  723.                 $json['alertMessage'] = $this->translator->trans('Error ! Invalid tag.');
  724.             }
  725.         }
  726.         $response = new Response(json_encode($json));
  727.         $response->headers->set('Content-Type''application/json');
  728.         return $response;
  729.     }
  730.     public function getSearchFilterOptionsXhr(Request $request)
  731.     {
  732.         $json = [];
  733.         if ($request->isXmlHttpRequest()) {
  734.             if($request->query->get('type') == 'agent') {
  735.                 $json $this->userService->getAgentsPartialDetails($request);
  736.             } elseif($request->query->get('type') == 'customer') {
  737.                 $json $this->userService->getCustomersPartial($request);
  738.             } elseif($request->query->get('type') == 'group') {
  739.                 $json $this->userService->getSupportGroups($request);
  740.             } elseif($request->query->get('type') == 'team') {
  741.                 $json $this->userService->getSupportTeams($request);
  742.             } elseif($request->query->get('type') == 'tag') {
  743.                 $json $this->ticketService->getTicketTags($request);
  744.             } elseif($request->query->get('type') == 'label') {
  745.                 $json $this->ticketService->getLabels($request);
  746.             }
  747.         }
  748.         $response = new Response(json_encode($json));
  749.         $response->headers->set('Content-Type''application/json');
  750.         return $response;
  751.     }
  752.     public function updateCollaboratorXHR(Request $request)
  753.     {
  754.         $json = [];
  755.         $content json_decode($request->getContent(), true);
  756.         $em $this->getDoctrine()->getManager();
  757.         $ticket $em->getRepository(Ticket::class)->find($content['ticketId']);
  758.         // Process only if user have ticket access
  759.         if (false == $this->ticketService->isTicketAccessGranted($ticket)) {
  760.             throw new \Exception('Access Denied'403);
  761.         }
  762.         if($request->getMethod() == "POST") {
  763.             if($content['email'] == $ticket->getCustomer()->getEmail()) {
  764.                 $json['alertClass'] = 'danger';
  765.                 $json['alertMessage'] = $this->translator->trans('Error ! Customer can not be added as collaborator.');
  766.             } else {
  767.                 $data = array(
  768.                     'from' => $content['email'],
  769.                     'firstName' => ($firstName ucfirst(current(explode('@'$content['email'])))),
  770.                     'lastName' => ' ',
  771.                     'role' => 4,
  772.                 );
  773.                 $supportRole $em->getRepository(SupportRole::class)->findOneByCode('ROLE_CUSTOMER');
  774.                 $collaborator $this->userService->createUserInstance($data['from'], $data['firstName'], $supportRole$extras = ["active" => true]);
  775.                 $checkTicket $em->getRepository(Ticket::class)->isTicketCollaborator($ticket$content['email']);
  776.                 if (!$checkTicket) {
  777.                     $ticket->addCollaborator($collaborator);
  778.                     $em->persist($ticket);
  779.                     $em->flush();
  780.                     $ticket->lastCollaborator $collaborator;
  781.                     if ($collaborator->getCustomerInstance())
  782.                         $json['collaborator'] = $collaborator->getCustomerInstance()->getPartialDetails();
  783.                     else
  784.                         $json['collaborator'] = $collaborator->getAgentInstance()->getPartialDetails();
  785.                     $event = new GenericEvent(CoreWorkflowEvents\Ticket\Collaborator::getId(), [
  786.                         'entity' => $ticket,
  787.                     ]);
  788.                     $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  789.                     $json['alertClass'] = 'success';
  790.                     $json['alertMessage'] = $this->translator->trans('Success ! Collaborator added successfully.');
  791.                 } else {
  792.                     $json['alertClass'] = 'danger';
  793.                     $message "Collaborator is already added.";
  794.                     $json['alertMessage'] = $this->translator->trans('Error ! ' $message);
  795.                 }
  796.             }
  797.         } elseif($request->getMethod() == "DELETE") {
  798.             $collaborator $em->getRepository(User::class)->findOneBy(array('id' => $request->attributes->get('id')));
  799.             if($collaborator) {
  800.                 $ticket->removeCollaborator($collaborator);
  801.                 $em->persist($ticket);
  802.                 $em->flush();
  803.                 $json['alertClass'] = 'success';
  804.                 $json['alertMessage'] = $this->translator->trans('Success ! Collaborator removed successfully.');
  805.             } else {
  806.                 $json['alertClass'] = 'danger';
  807.                 $json['alertMessage'] = $this->translator->trans('Error ! Invalid Collaborator.');
  808.             }
  809.         }
  810.         $response = new Response(json_encode($json));
  811.         $response->headers->set('Content-Type''application/json');
  812.         return $response;
  813.     }
  814.     // Apply quick Response action
  815.     public function getTicketQuickViewDetailsXhr(Request $requestContainerInterface $container)
  816.     {
  817.         $json = [];
  818.         if ($request->isXmlHttpRequest()) {
  819.             $ticketId $request->query->get('ticketId');
  820.             $json $this->getDoctrine()->getRepository(Ticket::class)->getTicketDetails($request->query$container);
  821.         }
  822.         $response = new Response(json_encode($json));
  823.         $response->headers->set('Content-Type''application/json');
  824.         return $response;
  825.     }
  826.     public function updateTicketTagXHR(Request $request$tagId)
  827.     {
  828.         $content json_decode($request->getContent(), true);
  829.         $entityManager $this->getDoctrine()->getManager();
  830.         if (isset($content['name']) && $content['name'] != "") {
  831.             $checkTag $entityManager->getRepository(Tag::class)->findOneBy(array('id' => $tagId));
  832.             if($checkTag) {
  833.                 $checkTag->setName($content['name']);
  834.                 $entityManager->persist($checkTag);
  835.                 $entityManager->flush();
  836.             }
  837.             $json['alertClass'] = 'success';
  838.             $json['alertMessage'] = $this->translator->trans('Success ! Tag updated successfully.');
  839.         }
  840.         $response = new Response(json_encode($json));
  841.         $response->headers->set('Content-Type''application/json');
  842.         return $response;
  843.     }
  844.     public function removeTicketTagXHR($tagId)
  845.     {
  846.         $entityManager $this->getDoctrine()->getManager();
  847.         $checkTag $entityManager->getRepository(Tag::class)->findOneBy(array('id' => $tagId));
  848.         if($checkTag) {
  849.             $entityManager->remove($checkTag);
  850.             $entityManager->flush();
  851.             $json['alertClass'] = 'success';
  852.             $json['alertMessage'] = $this->translator->trans('Success ! Tag removed successfully.');
  853.         }
  854.         $response = new Response(json_encode($json));
  855.         $response->headers->set('Content-Type''application/json');
  856.         return $response;
  857.     }
  858. }