vendor/uvdesk/automation-bundle/Controller/Automations/Workflow.php line 32

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. use Webkul\UVDesk\AutomationBundle\Form\DefaultForm;
  7. use Webkul\UVDesk\AutomationBundle\Entity;
  8. class Workflow extends Controller
  9. {
  10.     const ROLE_REQUIRED_AUTO 'ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC';
  11.     const ROLE_REQUIRED_MANUAL 'ROLE_AGENT_MANAGE_WORKFLOW_MANUAL';
  12.     const LIMIT 20;
  13.     const WORKFLOW_MANUAL 0;
  14.     const WORKFLOW_AUTOMATIC 1;
  15.     const NAME_LENGTH 100;
  16.     const DESCRIPTION_LENGTH 200;
  17.     
  18.     public function listWorkflowCollection(Request $request)
  19.     {
  20.         if (!$this->get('user.service')->isAccessAuthorized('ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC')) {
  21.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  22.         }
  23.         return $this->render('@UVDeskAutomation//Workflow//workflowList.html.twig');
  24.     }
  25.     // Creating workflow
  26.     public function createWorkflow(Request $request)
  27.     {
  28.         if (!$this->get('user.service')->isAccessAuthorized('ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC')) {
  29.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  30.         }
  31.         $error $formData $formerror = [];
  32.         $entityManager $this->getDoctrine()->getManager();
  33.         $workflowEventType = (!empty($workflow) && $workflow->getWorkflowEvents()[0]) ? current(explode('.'$workflow->getWorkflowEvents()[0]->getEvent())) : false;
  34.         $form $this->createForm(DefaultForm::class);
  35.         if($request->request->all()) {
  36.             $form->submit($request->request->all());
  37.         }
  38.         if ($form->isSubmitted()) {
  39.             $formData $request->request;
  40.             $workflowClass 'Webkul\UVDesk\AutomationBundle\Entity\Workflow';
  41.             $workflowActionsArray $request->request->get('actions');
  42.             if (strlen($formData->get('description')) > self::DESCRIPTION_LENGTH) {
  43.                 $error['description'] = $this->translate('Warning! Please add valid Description! Length must not be greater than %desc%', ['%desc%' => self::DESCRIPTION_LENGTH]);
  44.             }
  45.             if (!empty($workflowActionsArray)) {
  46.                 foreach ($workflowActionsArray as $key => $action) {
  47.                     if (!$action['type']) {
  48.                         unset($workflowActionsArray[$key]);
  49.                     }
  50.                 }
  51.             }
  52.             if (empty($workflowActionsArray)) {
  53.                 $error['actions'] = $this->translate('Warning! Please add valid Actions!');
  54.             }
  55.             // Remove blank values from arrays
  56.             $workflowEventsArray $request->request->get('events');
  57.             if (!empty($workflowEventsArray)) {
  58.                 foreach ($workflowEventsArray as $key => $event) {
  59.                     if (!$event['event']) {
  60.                         unset($workflowEventsArray[$key]);
  61.                     }
  62.                 }
  63.             }
  64.             if (empty($workflowEventsArray)) {
  65.                 $error['events'] = $this->translate('Warning! Please add valid Events!');
  66.             }
  67.             $workflowConditionsArray $request->request->get('conditions');
  68.             if ($workflowConditionsArray) {
  69.                 foreach ($workflowConditionsArray as $key => $condition) {
  70.                     if (!$condition['type']) {
  71.                         unset($workflowConditionsArray[$key]);
  72.                     }
  73.                 }
  74.             }
  75.           
  76.             if (empty($error)) {
  77.                 // Check if new workflow and old one belong to the same class
  78.                 if (!empty($workflow) && $workflow instanceof $workflowClass) {
  79.                     $newWorkflow $workflow;
  80.                 } else {
  81.                     $newWorkflow = new $workflowClass;
  82.                     if (!empty($workflow)) {
  83.                         $entityManager->remove($workflow);
  84.                         $entityManager->flush();
  85.                     }
  86.                 }
  87.                 $newWorkflow->setName($formData->get('name'));
  88.                 $newWorkflow->setDescription($formData->get('description'));
  89.                 $newWorkflow->setStatus($formData->get('status') == 'on' true false);
  90.                 $newWorkflow->setActions($workflowActionsArray);
  91.                 $newWorkflow->setDateAdded(new \Datetime);
  92.                 $newWorkflow->setDateUpdated(new \Datetime);
  93.                 
  94.                 $formDataGetEvents array_unique($formData->get('events'), SORT_REGULAR);
  95.                 if ($newWorkflow->getWorkflowEvents()) {
  96.                     foreach ($newWorkflow->getWorkflowEvents() as $newWorkflowEvent) {
  97.                         if ($thisKey array_search(['event' => current($exNewEventEvent explode('.'$newWorkflowEvent->getEvent())), 'trigger' => end($exNewEventEvent)], $formDataGetEvents)) {
  98.                             unset($formDataGetEvents[$thisKey]);
  99.                         } else {
  100.                             $entityManager->remove($newWorkflowEvent);
  101.                             $entityManager->flush();
  102.                         }
  103.                     }
  104.                 }
  105.                 $newWorkflow->setConditions($workflowConditionsArray);
  106.                 $entityManager->persist($newWorkflow);
  107.                 $entityManager->flush();
  108.                 foreach ($formDataGetEvents as $eventEvents) {
  109.                     $event = new Entity\WorkflowEvents;
  110.                     $event->setEvent($eventEvents['trigger']);
  111.                     $event->setWorkflow($newWorkflow);
  112.                     $event->setEventId($newWorkflow->getId());
  113.                     $entityManager->persist($event);
  114.                     $entityManager->flush();
  115.                 }
  116.                 $this->addFlash('success'$request->attributes->get('id')
  117.                     ? $this->get('translator')->trans('Success! Workflow has been updated successfully.')
  118.                     :  $this->get('translator')->trans('Success! Workflow has been added successfully.')
  119.                 );
  120.                 return $this->redirectToRoute('helpdesk_member_workflow_collection');
  121.             }
  122.             $formData = [
  123.                 'type' => $request->request->get('type'),
  124.                 'name' => $request->request->get('name'),
  125.                 'description' => $request->request->get('description'),
  126.                 'status' => $request->request->get('status'),
  127.                 'events' => $request->request->get('events'),
  128.                 'actions' => $request->request->get('actions'),
  129.                 'conditions' => $request->request->get('conditions'),
  130.             ];
  131.         }
  132.       
  133.         return $this->render('@UVDeskAutomation//Workflow//createWorkflow.html.twig', array(
  134.             'form' => $form->createView(),
  135.             'error' => $error,
  136.             'formerror' => $formerror,
  137.             'formData' => $formData,
  138.         ));
  139.     }
  140.     public function editWorkflow(Request $request)
  141.     {
  142.         if (!$this->get('user.service')->isAccessAuthorized('ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC')) {
  143.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  144.         }
  145.         $error $formData $formerror = [];
  146.         $entityManager $this->getDoctrine()->getManager();
  147.         if ($request->attributes->get('id')) {
  148.             $workflow $entityManager->getRepository('UVDeskAutomationBundle:Workflow')->findOneById($request->attributes->get('id'));
  149.             if (!empty($workflow)) {
  150.                 $formData = [
  151.                     'type' => self::WORKFLOW_AUTOMATIC,
  152.                     'name' => $workflow->getName(),
  153.                     'description' => $workflow->getDescription(),
  154.                     'status' => $workflow->getStatus(),
  155.                     'actions' => $workflow->getActions(),
  156.                     'conditions' => $workflow->getConditions(),
  157.                     'events' => [],
  158.                 ];
  159.                 foreach ($workflow->getWorkflowEvents() as $event) {
  160.                     $eventDefinition $this->get('uvdesk.automations.workflows')->getRegisteredWorkflowEvent($event->getEvent());
  161.                     if (!empty($eventDefinition)) {
  162.                         $formData['events'][] = [
  163.                             'event' => $eventDefinition->getFunctionalGroup(),
  164.                             'trigger' => $eventDefinition->getId(),
  165.                         ];
  166.                     }
  167.                 }
  168.             } else {
  169.                 // Workflow not found
  170.                 $this->noResultFound();
  171.             }
  172.         }
  173.         $workflowEventType = (!empty($workflow) && $workflow->getWorkflowEvents()[0]) ? current(explode('.'$workflow->getWorkflowEvents()[0]->getEvent())) : false;
  174.         $form $this->createForm(DefaultForm::class);
  175.         if($request->request->all()) {
  176.             $form->submit($request->request->all());
  177.         }
  178.         if ($form->isSubmitted()) {
  179.             $formData $request->request;
  180.             
  181.             $workflowClass 'Webkul\UVDesk\AutomationBundle\Entity\Workflow';
  182.             $workflowActionsArray $request->request->get('actions');
  183.             if (strlen($formData->get('description')) > self::DESCRIPTION_LENGTH) {
  184.                 $error['description'] = $this->translate('Warning! Please add valid Description! Length must not be greater than %desc%', ['%desc%' => self::DESCRIPTION_LENGTH]);
  185.             }
  186.             if (!empty($workflowActionsArray)) {
  187.                 foreach ($workflowActionsArray as $key => $action) {
  188.                     if (!$action['type']) {
  189.                         unset($workflowActionsArray[$key]);
  190.                     }
  191.                 }
  192.             }
  193.             if (empty($workflowActionsArray)) {
  194.                 $error['actions'] = $this->translate('Warning! Please add valid Actions!');
  195.             }
  196.             // Check Authorization for Automatic Workflow
  197.             // $this->isAuthorized(self::ROLE_REQUIRED_AUTO);
  198.             // Remove blank values from arrays
  199.             $workflowEventsArray $request->request->get('events');
  200.             if (!empty($workflowEventsArray)) {
  201.                 foreach ($workflowEventsArray as $key => $event) {
  202.                     if (!$event['event']) {
  203.                         unset($workflowEventsArray[$key]);
  204.                     }
  205.                 }
  206.             }
  207.             if (empty($workflowEventsArray)) {
  208.                 $error['events'][] = $this->translate('Warning! Please add valid Events!');
  209.             }
  210.             /*
  211.                 @NOTICE: Events 'uvdesk.agent.forgot_password', 'uvdesk.customer.forgot_password' will be deprecated 
  212.                 onwards uvdesk/automation-bundle:1.0.2 and uvdesk/core-framework:1.0.3 releases and will be 
  213.                 completely removed with the next major release.
  214.             */
  215.             foreach ($workflowEventsArray as $eventAttributes) {
  216.                 if ($eventAttributes['event'] == 'agent' && $eventAttributes['trigger'] == 'uvdesk.user.forgot_password') {
  217.                     $error['events'][] = $this->translate('"Agent Forgot Password" has been deprecated. Please use the "User Forgot Password" event instead.');
  218.                 } else if ($eventAttributes['event'] == 'customer' && $eventAttributes['trigger'] == 'uvdesk.user.forgot_password') {
  219.                     $error['events'][] = $this->translate('"Customer Forgot Password" has been deprecated. Please use the "User Forgot Password" event instead.');
  220.                 }
  221.             }
  222.             $workflowConditionsArray $request->request->get('conditions');
  223.             if ($workflowConditionsArray) {
  224.                 foreach ($workflowConditionsArray as $key => $condition) {
  225.                     if (!$condition['type']) {
  226.                         unset($workflowConditionsArray[$key]);
  227.                     }
  228.                 }
  229.             }
  230.             if (empty($error)) {
  231.                 // Check if new workflow and old one belong to the same class
  232.                 if (!empty($workflow) && $workflow instanceof $workflowClass) {
  233.                     $newWorkflow $workflow;
  234.                 } else {
  235.                     $newWorkflow = new $workflowClass;
  236.                     if (!empty($workflow)) {
  237.                         $entityManager->remove($workflow);
  238.                         $entityManager->flush();
  239.                     }
  240.                 }
  241.                 $newWorkflow->setName($formData->get('name'));
  242.                 $newWorkflow->setDescription($formData->get('description'));
  243.                 $newWorkflow->setStatus($formData->get('status') == 'on' true false);
  244.                 $newWorkflow->setActions($workflowActionsArray);
  245.                 $newWorkflow->setDateAdded(new \Datetime);
  246.                 $newWorkflow->setDateUpdated(new \Datetime);
  247.                 $formDataGetEvents array_unique($formData->get('events'), SORT_REGULAR);
  248.                 if ($newWorkflow->getWorkflowEvents()) {
  249.                     foreach ($newWorkflow->getWorkflowEvents() as $newWorkflowEvent) {
  250.                         if ($thisKey array_search([
  251.                             'event' => current($exNewEventEvent explode('.'$newWorkflowEvent->getEvent())), 
  252.                             'trigger' => end($exNewEventEvent)
  253.                         ], $formDataGetEvents)) {
  254.                             unset($formDataGetEvents[$thisKey]);
  255.                         } else {
  256.                             $entityManager->remove($newWorkflowEvent);
  257.                             $entityManager->flush();
  258.                         }
  259.                     }
  260.                 }
  261.                 $newWorkflow->setConditions($workflowConditionsArray);
  262.                 $entityManager->persist($newWorkflow);
  263.                 $entityManager->flush();
  264.                 foreach ($formDataGetEvents as $eventEvents) {
  265.                     $event = new Entity\WorkflowEvents;
  266.                     $event->setEvent($eventEvents['trigger']);
  267.                     $event->setWorkflow($newWorkflow);
  268.                     $event->setEventId($newWorkflow->getId());
  269.                     $entityManager->persist($event);
  270.                     $entityManager->flush();
  271.                 }
  272.                 $this->addFlash('success'$request->attributes->get('id')
  273.                     ? $this->get('translator')->trans('Success! Workflow has been updated successfully.')
  274.                     :  $this->get('translator')->trans('Success! Workflow has been added successfully.')
  275.                 );
  276.                 return $this->redirectToRoute('helpdesk_member_workflow_collection');
  277.             } else {
  278.                 if (!empty($error['events'])) {
  279.                     foreach ($error['events'] as $message) {
  280.                         $this->addFlash('warning'$this->get('translator')->trans("Events: " $message));
  281.                     }
  282.                 }
  283.             }
  284.             $formData = [
  285.                 'type' => $request->request->get('type'),
  286.                 'name' => $request->request->get('name'),
  287.                 'description' => $request->request->get('description'),
  288.                 'status' => $request->request->get('status'),
  289.                 'events' => $request->request->get('events'),
  290.                 'actions' => $request->request->get('actions'),
  291.                 'conditions' => $request->request->get('conditions'),
  292.             ];
  293.         }
  294.       
  295.         return $this->render('@UVDeskAutomation//Workflow//editWorkflow.html.twig', array(
  296.             'form' => $form->createView(),
  297.             'error' => $error,
  298.             'formerror' => $formerror,
  299.             'formData' => $formData,
  300.         ));
  301.     }
  302.     //Remove Workflow
  303.     public function deleteWorkflow(Request $request)
  304.     {
  305.     } 
  306.     public function translate($string,$params = array())
  307.     {
  308.         return $this->get('translator')->trans($string,$params);
  309.     }
  310. }