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

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