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

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