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

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