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