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

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