vendor/uvdesk/automation-bundle/Controller/Automations/WorkflowXHR.php line 30

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\AutomationBundle\Controller\Automations;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  9. use Webkul\UVDesk\AutomationBundle\Entity;
  10. use Webkul\UVDesk\AutomationBundle\EventListener\WorkflowListener;
  11. use Webkul\UVDesk\CoreFrameworkBundle\Services\TicketService;
  12. class WorkflowXHR extends AbstractController
  13. {
  14.     private $userService;
  15.     private $translator;
  16.     private $workflowListnerService;
  17.     private $ticketService;
  18.     public function __construct(UserService $userServiceWorkflowListener $workflowListnerServiceTicketService $ticketService,TranslatorInterface $translator)
  19.     {
  20.         $this->userService $userService;
  21.         $this->ticketService $ticketService;
  22.         $this->workflowListnerService $workflowListnerService;
  23.         $this->translator $translator;
  24.     }
  25.     public function workflowsListXhr(Request $requestContainerInterface $container)
  26.     {
  27.         if (! $this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC')) {
  28.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  29.         }
  30.         $json = [];
  31.         $repository $this->getDoctrine()->getRepository(Entity\Workflow::class);
  32.         $json $repository->getWorkflows($request->query$container);
  33.         $response = new Response(json_encode($json));
  34.         $response->headers->set('Content-Type''application/json');
  35.         return $response;
  36.     }
  37.     public function WorkflowsxhrAction(Request $request)
  38.     {
  39.         $json = [];
  40.         if (! $this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC')) {
  41.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  42.         }
  43.         if ($request->isXmlHttpRequest()){
  44.             if ($request->getMethod() == 'POST') {
  45.                 $em $this->getDoctrine()->getManager();
  46.                 //sort order update
  47.                 $workflows $em->getRepository(Entity\Workflow::class)->findAll();
  48.                 $sortOrders $request->request->get('orders');
  49.                 if (count($workflows)) {
  50.                     foreach ($workflows as $id => $workflow) {
  51.                         if (empty($sortOrders[$workflow->getId()])) {
  52.                             $error true;
  53.                             break;
  54.                         }
  55.                         $workflow->setSortOrder($sortOrders[$workflow->getId()]);
  56.                         $em->persist($workflow);
  57.                     }
  58.                     $em->flush();
  59.                     $json['alertClass'] = 'success';
  60.                     $json['alertMessage'] = $this->translator->trans('Success! Order has been updated successfully.');
  61.                 }
  62.             } elseif ($request->getMethod() == 'DELETE') {
  63.                 $em $this->getDoctrine()->getManager();
  64.                 $id $request->attributes->get('id');
  65.                 $workFlow $em->getRepository(Entity\Workflow::class)->findOneBy(array('id' => $id));
  66.                 if (empty($workFlow)) {
  67.                     $json['alertClass'] = 'danger';
  68.                     $json['alertMessage'] = $this->translator->trans('Warning! No workflow found.');
  69.                 } else {
  70.                     $em->remove($workFlow);
  71.                     $em->flush();
  72.                     $json['alertClass'] = 'success';
  73.                     $json['alertMessage'] = $this->translator->trans('Success! Workflow has been removed successfully.');
  74.                 }
  75.             }
  76.         }
  77.         $response = new Response(json_encode($json));
  78.         $response->headers->set('Content-Type''application/json');
  79.         return $response;
  80.     }
  81.     public function getWorkflowConditionOptionsXHR($entityRequest $request)
  82.     {
  83.         $json $results = array();
  84.         $supportedConditions = [
  85.             'team',
  86.             'stage',
  87.             'agent',
  88.             'group',
  89.             'source',
  90.             'agent_name',
  91.             'agent_email',
  92.             'TicketType',
  93.             'TicketPriority',
  94.             'TicketStatus',
  95.         ];
  96.         if (
  97.             ! $request->isXmlHttpRequest()
  98.             || ($request->getMethod() != 'GET'
  99.             || !in_array($entity$supportedConditions))
  100.         ) {
  101.             throw new Exception(''404);
  102.         }
  103.         switch ($entity) {
  104.             case 'team':
  105.                 $json json_encode($this->userService->getSupportTeams());
  106.                 break;
  107.             case 'group':
  108.                 $json $this->userService->getSupportGroups();
  109.                 break;
  110.             case 'stage':
  111.                 $json $this->get('task.service')->getStages();
  112.                 break;
  113.             case 'TicketType':
  114.                 $json $this->ticketService->getTypes();
  115.                 break;
  116.             case 'agent':
  117.             case 'agent_name':
  118.                 $defaultAgent = ['id' => 'actionPerformingAgent''name' => 'Action Performing Agent'];
  119.                 $agentList $this->userService->getAgentPartialDataCollection();
  120.                 array_push($agentList$defaultAgent);
  121.                 $json json_encode(array_map(function($item) {
  122.                     return [
  123.                         'id'   => $item['id'],
  124.                         'name' => $item['name'],
  125.                     ];
  126.                 }, $agentList));
  127.                 break;
  128.             case 'agent_email':
  129.                 $json json_encode(array_map(function($item) {
  130.                     return [
  131.                         'id'   => $result['id'],
  132.                         'name' => $result['email'],
  133.                     ];
  134.                 }, $this->userService->getAgentsPartialDetails()));
  135.                 break;
  136.             case 'source':
  137.                 $allSources $this->ticketService->getAllSources();
  138.                 $results = [];
  139.                 foreach ($allSources as $key => $source) {
  140.                     $results[] = [
  141.                                 'id'   => $key,
  142.                                 'name' => $source,
  143.                     ];
  144.                 };
  145.                 $json json_encode($results);
  146.                 $results = [];
  147.                 break;
  148.             case 'TicketStatus':
  149.             case 'TicketPriority':
  150.                 $json json_encode(array_map(function($item) {
  151.                     return [
  152.                         'id'   => $item->getId(),
  153.                         'name' => $item->getCode(),
  154.                     ];
  155.                 }, $this->getDoctrine()->getRepository("Webkul\\UVDesk\\CoreFrameworkBundle\\Entity\\" ucfirst($entity))->findAll()));
  156.                 break;
  157.             default:
  158.                 $json = [];
  159.                 break;
  160.         }
  161.         return new Response(is_array($json) ? json_encode($json) : $json200, ['Content-Type' => 'application/json']);
  162.     }
  163.     public function getWorkflowActionOptionsXHR($entityRequest $requestContainerInterface $container)
  164.     {
  165.         foreach ($this->workflowListnerService->getRegisteredWorkflowActions() as $workflowAction) {
  166.             if ($workflowAction->getId() == $entity) {
  167.                 $options $workflowAction->getOptions($container);
  168.                 if (!empty($options)) {
  169.                     return new Response(json_encode($options), 200, ['Content-Type' => 'application/json']);
  170.                 }
  171.                 break;
  172.             }
  173.         }
  174.         return new Response(json_encode([
  175.             'alertClass'   => 'danger',
  176.             'alertMessage' => 'Warning! You are not allowed to perform this action.',
  177.         ]), 200, ['Content-Type' => 'application/json']);
  178.     }
  179. }