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

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