vendor/uvdesk/automation-bundle/EventListener/WorkflowListener.php line 74

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\AutomationBundle\EventListener;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\EventDispatcher\GenericEvent;
  5. use Webkul\UVDesk\AutomationBundle\Entity\Workflow;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Ticket;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Webkul\UVDesk\AutomationBundle\Workflow\Event as WorkflowEvent;
  9. use Webkul\UVDesk\AutomationBundle\Workflow\Action as WorkflowAction;
  10. class WorkflowListener
  11. {
  12.     private $container;
  13.     private $entityManager;
  14.     private $registeredWorkflowEvents = [];
  15.     private $registeredWorkflowActions = [];
  16.     public function __construct(ContainerInterface $containerEntityManagerInterface $entityManager)
  17.     {
  18.         $this->container $container;
  19.         $this->entityManager $entityManager;
  20.     }
  21.     public function registerWorkflowEvent(WorkflowEvent $serviceTag)
  22.     {
  23.         $this->registeredWorkflowEvents[] = $serviceTag;
  24.     }
  25.     public function registerWorkflowAction(WorkflowAction $serviceTag)
  26.     {
  27.         $this->registeredWorkflowActions[] = $serviceTag;
  28.     }
  29.     public function getRegisteredWorkflowEvent($eventId)
  30.     {
  31.         foreach ($this->registeredWorkflowEvents as $workflowDefinition) {
  32.             if ($workflowDefinition->getId() == $eventId) {
  33.                 /*
  34.                     @NOTICE: Events 'uvdesk.agent.forgot_password', 'uvdesk.customer.forgot_password' will be deprecated 
  35.                     onwards uvdesk/automation-bundle:1.0.2 and uvdesk/core-framework:1.0.3 releases and will be 
  36.                     completely removed with the next major release.
  37.                     Both the events have been mapped to return the 'uvdesk.user.forgot_password' id, so we need to 
  38.                     return the correct definition.
  39.                 */
  40.                 if ('uvdesk.user.forgot_password' == $eventId) {
  41.                     if (
  42.                         $workflowDefinition instanceof \Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events\Agent\ForgotPassword 
  43.                         || $workflowDefinition instanceof \Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events\Customer\ForgotPassword
  44.                     ) {
  45.                         continue;
  46.                     }
  47.                 }
  48.                 return $workflowDefinition;
  49.             }
  50.         }
  51.         return null;
  52.     }
  53.     
  54.     public function getRegisteredWorkflowEvents()
  55.     {
  56.         return $this->registeredWorkflowEvents;
  57.     }
  58.     public function getRegisteredWorkflowActions()
  59.     {
  60.         return $this->registeredWorkflowActions;
  61.     }
  62.     public function executeWorkflow(GenericEvent $event)
  63.     {
  64.         $workflowCollection $this->entityManager->getRepository(Workflow::class)->getEventWorkflows($event->getSubject());
  65.         /*
  66.             @NOTICE: Events 'uvdesk.agent.forgot_password', 'uvdesk.customer.forgot_password' will be deprecated 
  67.             onwards uvdesk/automation-bundle:1.0.2 and uvdesk/core-framework:1.0.3 releases and will be 
  68.             completely removed with the next major release.
  69.             From uvdesk/core-framework:1.0.3 onwards, instead of the above mentioned events, the one being 
  70.             triggered will be 'uvdesk.user.forgot_password'. Since there still might be older workflows 
  71.             configured to work on either of the two deprecated events, we will need to make an educated guess 
  72.             which one to use (if any) if there's none found for the actual event.
  73.         */
  74.         if (empty($workflowCollection) && 'uvdesk.user.forgot_password' == $event->getSubject()) {
  75.             $user $event->getArgument('entity');
  76.             if (!empty($user) && $user instanceof \Webkul\UVDesk\CoreFrameworkBundle\Entity\User) {
  77.                 $agentForgotPasswordWorkflows $this->entityManager->getRepository(Workflow::class)->getEventWorkflows('uvdesk.agent.forgot_password');
  78.                 $customerForgotPasswordWorkflows $this->entityManager->getRepository(Workflow::class)->getEventWorkflows('uvdesk.customer.forgot_password');
  79.                 if (!empty($agentForgotPasswordWorkflows) || !empty($customerForgotPasswordWorkflows)) {
  80.                     $agentInstance $user->getAgentInstance();
  81.                     $customerInstance $user->getCustomerInstance();
  82.                     if (!empty($customerForgotPasswordWorkflows) && !empty($customerInstance)) {
  83.                         // Resort to uvdesk.customer.forgot_password workflows
  84.                         $workflowCollection $customerForgotPasswordWorkflows;
  85.                     } else if (!empty($agentForgotPasswordWorkflows) && !empty($agentInstance)) {
  86.                         // Resort to uvdesk.agent.forgot_password workflows
  87.                         $workflowCollection $agentForgotPasswordWorkflows;
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.         
  93.         if (!empty($workflowCollection)) {
  94.             foreach ($workflowCollection as $workflow) {
  95.                 $totalConditions 0;
  96.                 $totalEvaluatedConditions 0;
  97.                 foreach ($this->evaluateWorkflowConditions($workflow) as $workflowCondition) {
  98.                     $totalEvaluatedConditions++;
  99.                     if (isset($workflowCondition['type']) && $this->checkCondition($workflowCondition$event->getArgument('entity'))) {
  100.                         $totalConditions++;
  101.                     }
  102.                     if (isset($workflowCondition['or'])) {
  103.                         foreach ($workflowCondition['or'] as $orCondition) {
  104.                             if ($this->checkCondition($orCondition$event->getArgument('entity'))) {
  105.                                 $totalConditions++;
  106.                             }
  107.                         }
  108.                     }
  109.                 }
  110.                 if ($totalEvaluatedConditions == || $totalConditions >= $totalEvaluatedConditions) {
  111.                     $this->applyWorkflowActions($workflow$event->getArgument('entity'), $event->hasArgument('thread') ? $event->getArgument('thread') : null);
  112.                 }
  113.             }
  114.         }
  115.     }
  116.     private function evaluateWorkflowConditions(Workflow $workflow)
  117.     {
  118.         $index = -1;
  119.         $workflowConditions = [];
  120.         if ($workflow->getConditions() == null) {
  121.             return $workflowConditions;
  122.         }
  123.         foreach ($workflow->getConditions() as $condition) {
  124.             if (!empty($condition['operation']) && $condition['operation'] != "&&") {
  125.                 if (!isset($finalConditions[$index]['or'])) {
  126.                     $finalConditions[$index]['or'] = [];
  127.                 }
  128.                 $workflowConditions[$index]['or'][] = $condition;
  129.             } else {
  130.                 $index++;
  131.                 $workflowConditions[] = $condition;
  132.             }
  133.         }
  134.         return $workflowConditions;
  135.     }
  136.     private function applyWorkflowActions(Workflow $workflow$entity$thread null)
  137.     {
  138.         foreach ($workflow->getActions() as $attributes) {
  139.             if (empty($attributes['type'])) {
  140.                 continue;
  141.             }
  142.             foreach ($this->getRegisteredWorkflowActions() as $workflowAction) {
  143.                 if ($workflowAction->getId() == $attributes['type']) {
  144.                     $workflowAction->applyAction($this->container$entity, isset($attributes['value']) ? $attributes['value'] : ''$thread);
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     public function checkCondition($condition$entity)
  150.     {
  151.         switch ($condition['type']) {
  152.             case 'from_mail':
  153.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  154.                     return $this->match($condition['match'], $entity->getCustomer()->getEmail(), $condition['value']);
  155.                 }
  156.                 break;
  157.             case 'to_mail':
  158.                 if (isset($condition['value']) && $entity instanceof Ticket && $entity->getMailboxEmail()) {
  159.                     return $this->match($condition['match'], $entity->getMailboxEmail(), $condition['value']);
  160.                 }
  161.                 
  162.                 break;
  163.             case 'subject':
  164.                 if (isset($condition['value']) && ($entity instanceof Ticket || $entity instanceof Task)) {
  165.                     return $this->match($condition['match'], $entity->getSubject(), $condition['value']);
  166.                 }
  167.                 break;
  168.             case 'description':
  169.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  170.                     $reply $entity->createdThread->getMessage();
  171.                     $reply rtrim(strip_tags($reply), "\n" );
  172.                     return $this->match($condition['match'], rtrim($reply), $condition['value']);
  173.                 }
  174.                 break;
  175.             case 'subject_or_description':
  176.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  177.                     $flag $this->match($condition['match'], $entity->getSubject(), $condition['value']);
  178.                     $createThread $this->container->get('ticket.service')->getCreateReply($entity->getId(),false);
  179.                     
  180.                     if (!$flag) {
  181.                         $createThread $this->container->get('ticket.service')->getCreateReply($entity->getId(),false);
  182.                         $createThread['reply'] = rtrim(strip_tags($createThread['reply']), "\n" );
  183.                         $flag $this->match($condition['match'],$createThread['reply'],$condition['value']);
  184.                     }
  185.                     return $flag;
  186.                 }
  187.                 break;
  188.             case 'TicketPriority':
  189.                 if (isset($condition['value']) && ($entity instanceof Ticket)) {
  190.                     return $this->match($condition['match'], $entity->getPriority()->getId(), $condition['value']);
  191.                 }
  192.                 break;
  193.             case 'TicketType':
  194.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  195.                     $typeId $entity->getType() ? $entity->getType()->getId() : 0;
  196.                     return $this->match($condition['match'], $typeId$condition['value']);
  197.                 }
  198.                 break;
  199.             case 'TicketStatus':
  200.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  201.                     return $this->match($condition['match'], $entity->getStatus()->getId(), $condition['value']);
  202.                 }
  203.                 break;
  204.             case 'stage':
  205.                 if (isset($condition['value']) && $entity instanceof Task) {
  206.                     return $this->match($condition['match'], $entity->getStage()->getId(), $condition['value']);
  207.                 }
  208.                 break;
  209.             case 'source':
  210.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  211.                     return $this->match($condition['match'], $entity->getSource(), $condition['value']);
  212.                 }
  213.                 break;
  214.             case 'created':
  215.                 if (isset($condition['value']) && ($entity instanceof Ticket || $entity instanceof Task)) {
  216.                     $date date_format($entity->getCreatedAt(), "d-m-Y h:ia");
  217.                     return $this->match($condition['match'], $date$condition['value']);
  218.                 }
  219.                 break;
  220.             case 'agent':
  221.                 if (isset($condition['value']) && $entity instanceof Ticket && $entity->getAgent()) {
  222.                     return $this->match($condition['match'], $entity->getAgent()->getId(), (($condition['value'] == 'actionPerformingAgent') ? ($this->container->get('user.service')->getCurrentUser() ? $this->container->get('user.service')->getCurrentUser()->getId() : 0) : $condition['value']));
  223.                 }
  224.                 break;
  225.             case 'group':
  226.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  227.                     $groupId $entity->getSupportGroup() ? $entity->getSupportGroup()->getId() : 0;
  228.                     return $this->match($condition['match'], $groupId$condition['value']);
  229.                 }
  230.                 break;
  231.             case 'team':
  232.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  233.                     $subGroupId $entity->getSupportTeam() ? $entity->getSupportTeam()->getId() : 0;
  234.                     return $this->match($condition['match'], $subGroupId$condition['value']);
  235.                 }
  236.                 break;
  237.             case 'customer_name':
  238.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  239.                     $lastThread $this->container->get('ticket.service')->getTicketLastThread($entity->getId());
  240.                     return $this->match($condition['match'], $entity->getCustomer()->getFullName(), $condition['value']);
  241.                 }
  242.                 
  243.                 break;
  244.             case 'customer_email':
  245.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  246.                     return $this->match($condition['match'], $entity->getCustomer()->getEmail(), $condition['value']);
  247.                 }
  248.                 break;
  249.             case strpos($condition['type'], 'customFields[') == 0:
  250.                 $value null;
  251.                 $ticketCfValues $entity->getCustomFieldValues()->getValues();
  252.                 
  253.                 foreach ($ticketCfValues as $cfValue) {
  254.                     $mainCf $cfValue->getTicketCustomFieldsValues();
  255.                     
  256.                     if ($condition['type'] == 'customFields[' $mainCf->getId() . ']' ) {
  257.                         if (in_array($mainCf->getFieldType(), ['select''radio''checkbox'])) {
  258.                            $value json_decode($cfValue->getValue(), true);
  259.                         } else {
  260.                            $value trim($cfValue->getValue(), '"');
  261.                         }
  262.                         
  263.                         break;
  264.                     }
  265.                 }
  266.                 if (isset($condition['value']) && $entity instanceof Ticket) {
  267.                     return $this->match($condition['match'], !empty($value) ? $value ''$condition['value']);
  268.                 }
  269.                 break;
  270.             default:
  271.                 break;
  272.         }
  273.         return false;
  274.     }
  275.     public function match($condition$haystack$needle)
  276.     {
  277.         // Filter tags
  278.         if ('string' == gettype($haystack)) {
  279.             $haystack strip_tags($haystack);
  280.         }
  281.         switch ($condition) {
  282.             case 'is':
  283.                 return is_array($haystack) ? in_array($needle$haystack) : $haystack == $needle;
  284.             case 'isNot':
  285.                 return is_array($haystack) ? !in_array($needle$haystack) : $haystack != $needle;
  286.             case 'contains':
  287.                 return strripos($haystack,$needle) !== false true false;
  288.             case 'notContains':
  289.                 return strripos($haystack,$needle) === false true false;
  290.             case 'startWith':
  291.                 return $needle === "" || strripos($haystack$needle, -strlen($haystack)) !== FALSE;
  292.             case 'endWith':
  293.                 return $needle === "" || (($temp strlen($haystack) - strlen($needle)) >= && stripos($haystack$needle$temp) !== FALSE);
  294.             case 'before':
  295.                 $createdTimeStamp date('Y-m-d'strtotime($haystack));
  296.                 $conditionTimeStamp date('Y-m-d'strtotime($needle " 23:59:59"));
  297.                 return $createdTimeStamp $conditionTimeStamp true false;
  298.             case 'beforeOn':
  299.                 $createdTimeStamp date('Y-m-d'strtotime($haystack));
  300.                 $conditionTimeStamp date('Y-m-d'strtotime($needle " 23:59:59"));
  301.                 return ($createdTimeStamp $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true false;
  302.             case 'after':
  303.                 $createdTimeStamp date('Y-m-d'strtotime($haystack));
  304.                 $conditionTimeStamp date('Y-m-d'strtotime($needle " 23:59:59"));
  305.                 return $createdTimeStamp $conditionTimeStamp true false;
  306.             case 'afterOn':
  307.                 $createdTimeStamp date('Y-m-d'strtotime($haystack));
  308.                 $conditionTimeStamp date('Y-m-d'strtotime($needle " 23:59:59"));
  309.                 
  310.                 return $createdTimeStamp $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp true false;
  311.             case 'beforeDateTime':
  312.                 $createdTimeStamp date('Y-m-d h:i:s'strtotime($haystack));
  313.                 $conditionTimeStamp date('Y-m-d h:i:s'strtotime($needle));
  314.                 return $createdTimeStamp $conditionTimeStamp true false;
  315.             case 'beforeDateTimeOn':
  316.                 $createdTimeStamp date('Y-m-d h:i:s'strtotime($haystack));
  317.                 $conditionTimeStamp date('Y-m-d h:i:s'strtotime($needle));
  318.                 return ($createdTimeStamp $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true false;
  319.             case 'afterDateTime':
  320.                 $createdTimeStamp date('Y-m-d h:i:s'strtotime($haystack));
  321.                 $conditionTimeStamp date('Y-m-d h:i:s'strtotime($needle));
  322.                 return $createdTimeStamp $conditionTimeStamp true false;
  323.             case 'afterDateTimeOn':
  324.                 $createdTimeStamp date('Y-m-d h:i:s'strtotime($haystack));
  325.                 $conditionTimeStamp date('Y-m-d h:i:s'strtotime($needle));
  326.                 return $createdTimeStamp $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp true false;
  327.             case 'beforeTime':
  328.                 $createdTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $haystack));
  329.                 $conditionTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $needle));
  330.                 return $createdTimeStamp $conditionTimeStamp true false;
  331.             case 'beforeTimeOn':
  332.                 $createdTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $haystack));
  333.                 $conditionTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $needle));
  334.                 return ($createdTimeStamp $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true false;
  335.             case 'afterTime':
  336.                 $createdTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $haystack));
  337.                 $conditionTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $needle));
  338.                 return $createdTimeStamp $conditionTimeStamp true false;
  339.             case 'afterTimeOn':
  340.                 $createdTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $haystack));
  341.                 $conditionTimeStamp date('Y-m-d H:i A'strtotime('2017-01-01' $needle));
  342.                 return $createdTimeStamp $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp true false;
  343.             case 'greaterThan':
  344.                 return !is_array($haystack) && $needle $haystack;
  345.             case 'lessThan':
  346.                 return !is_array($haystack) && $needle $haystack;
  347.             default:
  348.                 break;
  349.         }
  350.         return false;
  351.     }
  352. }