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 $container, EntityManagerInterface $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. public function getRegisteredWorkflowEvents()
  55. {
  56. return $this->registeredWorkflowEvents;
  57. }
  58. public function getRegisteredWorkflowActions()
  59. {
  60. return $this->registeredWorkflowActions;
  61. }
  62. public function executeWorkflow(Event $event)
  63. {
  64. $workflowCollection = $this->entityManager->getRepository(Workflow::class)->getEventWorkflows($event::getId());
  65. /*
  66. @NOTICE: Events 'uvdesk.agent.forgot_password', 'uvdesk.customer.forgot_password' will be deprecated
  67. onwards uvdesk/automation-bundle:1.1.2 and uvdesk/core-framework:1.1.3 releases and will be
  68. completely removed with the next major release.
  69. From uvdesk/core-framework:1.1.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::getId()) {
  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. if (!empty($workflowCollection)) {
  93. foreach ($workflowCollection as $workflow) {
  94. $totalConditions = 0;
  95. $totalEvaluatedConditions = 0;
  96. foreach ($this->evaluateWorkflowConditions($workflow) as $workflowCondition) {
  97. $totalEvaluatedConditions++;
  98. if (isset($workflowCondition['type']) && $this->checkCondition($workflowCondition, $event)) {
  99. $totalConditions++;
  100. }
  101. if (isset($workflowCondition['or'])) {
  102. foreach ($workflowCondition['or'] as $orCondition) {
  103. if ($this->checkCondition($orCondition, $event)) {
  104. $totalConditions++;
  105. }
  106. }
  107. }
  108. }
  109. if ($totalEvaluatedConditions == 0 || $totalConditions >= $totalEvaluatedConditions) {
  110. $this->applyWorkflowActions($workflow, $event);
  111. }
  112. }
  113. }
  114. }
  115. private function evaluateWorkflowConditions(Workflow $workflow)
  116. {
  117. $index = -1;
  118. $workflowConditions = [];
  119. if ($workflow->getConditions() == null) {
  120. return $workflowConditions;
  121. }
  122. foreach ($workflow->getConditions() as $condition) {
  123. if (!empty($condition['operation']) && $condition['operation'] != "&&") {
  124. if (!isset($finalConditions[$index]['or'])) {
  125. $finalConditions[$index]['or'] = [];
  126. }
  127. $workflowConditions[$index]['or'][] = $condition;
  128. } else {
  129. $index++;
  130. $workflowConditions[] = $condition;
  131. }
  132. }
  133. return $workflowConditions;
  134. }
  135. private function applyWorkflowActions(Workflow $workflow, Event $event)
  136. {
  137. foreach ($workflow->getActions() as $attributes) {
  138. if (empty($attributes['type'])) {
  139. continue;
  140. }
  141. foreach ($this->getRegisteredWorkflowActions() as $workflowAction) {
  142. if ($workflowAction->getId() == $attributes['type']) {
  143. $workflowAction->applyAction($this->container, $event, isset($attributes['value']) ? $attributes['value'] : '');
  144. }
  145. }
  146. }
  147. }
  148. public function checkCondition($condition, Event $event)
  149. {
  150. $entity = null;
  151. switch (true) {
  152. case $event instanceof WorkflowEvents\EmailActivity:
  153. $entity = $event->getResolvedEmailHeaders();
  154. break;
  155. case $event instanceof WorkflowEvents\TicketActivity:
  156. $entity = $event->getTicket();
  157. break;
  158. case $event instanceof WorkflowEvents\AgentActivity:
  159. case $event instanceof WorkflowEvents\CustomerActivity:
  160. case $event instanceof WorkflowEvents\UserActivity:
  161. $entity = $event->getUser();
  162. break;
  163. default:
  164. break;
  165. }
  166. if (empty($entity)) {
  167. return false;
  168. }
  169. switch ($condition['type']) {
  170. case 'from_mail':
  171. if (isset($condition['value'])) {
  172. if ($entity instanceof Ticket) {
  173. return $this->match($condition['match'], $entity->getCustomer()->getEmail(), $condition['value']);
  174. } else if (is_array($entity) && !empty($entity['from'])) {
  175. return $this->match($condition['match'], $entity['from'], $condition['value']);
  176. }
  177. }
  178. break;
  179. case 'to_mail':
  180. if (isset($condition['value']) && $entity instanceof Ticket && $entity->getMailboxEmail()) {
  181. return $this->match($condition['match'], $entity->getMailboxEmail(), $condition['value']);
  182. }
  183. break;
  184. case 'subject':
  185. if (isset($condition['value']) && ($entity instanceof Ticket || $entity instanceof Task)) {
  186. return $this->match($condition['match'], $entity->getSubject(), $condition['value']);
  187. }
  188. break;
  189. case 'description':
  190. if (isset($condition['value']) && $entity instanceof Ticket) {
  191. $reply = $entity->createdThread->getMessage();
  192. $reply = rtrim(strip_tags($reply), "\n" );
  193. return $this->match($condition['match'], rtrim($reply), $condition['value']);
  194. }
  195. break;
  196. case 'subject_or_description':
  197. if (isset($condition['value']) && $entity instanceof Ticket) {
  198. $flag = $this->match($condition['match'], $entity->getSubject(), $condition['value']);
  199. $createThread = $this->container->get('ticket.service')->getCreateReply($entity->getId(),false);
  200. if (!$flag) {
  201. $createThread = $this->container->get('ticket.service')->getCreateReply($entity->getId(),false);
  202. $createThread['reply'] = rtrim(strip_tags($createThread['reply']), "\n" );
  203. $flag = $this->match($condition['match'],$createThread['reply'],$condition['value']);
  204. }
  205. return $flag;
  206. }
  207. break;
  208. case 'TicketPriority':
  209. if (isset($condition['value']) && ($entity instanceof Ticket)) {
  210. return $this->match($condition['match'], $entity->getPriority()->getId(), $condition['value']);
  211. }
  212. break;
  213. case 'TicketType':
  214. if (isset($condition['value']) && $entity instanceof Ticket) {
  215. $typeId = $entity->getType() ? $entity->getType()->getId() : 0;
  216. return $this->match($condition['match'], $typeId, $condition['value']);
  217. }
  218. break;
  219. case 'TicketStatus':
  220. if (isset($condition['value']) && $entity instanceof Ticket) {
  221. return $this->match($condition['match'], $entity->getStatus()->getId(), $condition['value']);
  222. }
  223. break;
  224. case 'stage':
  225. if (isset($condition['value']) && $entity instanceof Task) {
  226. return $this->match($condition['match'], $entity->getStage()->getId(), $condition['value']);
  227. }
  228. break;
  229. case 'source':
  230. if (isset($condition['value']) && $entity instanceof Ticket) {
  231. return $this->match($condition['match'], $entity->getSource(), $condition['value']);
  232. }
  233. break;
  234. case 'created':
  235. if (isset($condition['value']) && ($entity instanceof Ticket || $entity instanceof Task)) {
  236. $date = date_format($entity->getCreatedAt(), "d-m-Y h:ia");
  237. return $this->match($condition['match'], $date, $condition['value']);
  238. }
  239. break;
  240. case 'agent':
  241. if (isset($condition['value']) && $entity instanceof Ticket && $entity->getAgent()) {
  242. 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']));
  243. }
  244. break;
  245. case 'group':
  246. if (isset($condition['value']) && $entity instanceof Ticket) {
  247. $groupId = $entity->getSupportGroup() ? $entity->getSupportGroup()->getId() : 0;
  248. return $this->match($condition['match'], $groupId, $condition['value']);
  249. }
  250. break;
  251. case 'team':
  252. if (isset($condition['value']) && $entity instanceof Ticket) {
  253. $subGroupId = $entity->getSupportTeam() ? $entity->getSupportTeam()->getId() : 0;
  254. return $this->match($condition['match'], $subGroupId, $condition['value']);
  255. }
  256. break;
  257. case 'customer_name':
  258. if (isset($condition['value']) && $entity instanceof Ticket) {
  259. $lastThread = $this->container->get('ticket.service')->getTicketLastThread($entity->getId());
  260. return $this->match($condition['match'], $entity->getCustomer()->getFullName(), $condition['value']);
  261. }
  262. break;
  263. case 'customer_email':
  264. if (isset($condition['value']) && $entity instanceof Ticket) {
  265. return $this->match($condition['match'], $entity->getCustomer()->getEmail(), $condition['value']);
  266. }
  267. break;
  268. case strpos($condition['type'], 'customFields[') == 0:
  269. $value = null;
  270. $ticketCfValues = $entity->getCustomFieldValues()->getValues();
  271. foreach ($ticketCfValues as $cfValue) {
  272. $mainCf = $cfValue->getTicketCustomFieldsValues();
  273. if ($condition['type'] == 'customFields[' . $mainCf->getId() . ']' ) {
  274. if (in_array($mainCf->getFieldType(), ['select', 'radio', 'checkbox'])) {
  275. $value = json_decode($cfValue->getValue(), true);
  276. } else {
  277. $value = trim($cfValue->getValue(), '"');
  278. }
  279. break;
  280. }
  281. }
  282. if (isset($condition['value']) && $entity instanceof Ticket) {
  283. return $this->match($condition['match'], !empty($value) ? $value : '', $condition['value']);
  284. }
  285. break;
  286. default:
  287. break;
  288. }
  289. return false;
  290. }
  291. public function match($condition, $haystack, $needle)
  292. {
  293. // Filter tags
  294. if ('string' == gettype($haystack)) {
  295. $haystack = strip_tags($haystack);
  296. }
  297. switch ($condition) {
  298. case 'is':
  299. return is_array($haystack) ? in_array($needle, $haystack) : $haystack == $needle;
  300. case 'isNot':
  301. return is_array($haystack) ? !in_array($needle, $haystack) : $haystack != $needle;
  302. case 'contains':
  303. return strripos($haystack,$needle) !== false ? true : false;
  304. case 'notContains':
  305. return strripos($haystack,$needle) === false ? true : false;
  306. case 'startWith':
  307. return $needle === "" || strripos($haystack, $needle, -strlen($haystack)) !== FALSE;
  308. case 'endWith':
  309. return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && stripos($haystack, $needle, $temp) !== FALSE);
  310. case 'before':
  311. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  312. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  313. return $createdTimeStamp < $conditionTimeStamp ? true : false;
  314. case 'beforeOn':
  315. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  316. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  317. return ($createdTimeStamp < $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true : false;
  318. case 'after':
  319. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  320. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  321. return $createdTimeStamp > $conditionTimeStamp ? true : false;
  322. case 'afterOn':
  323. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  324. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  325. return $createdTimeStamp > $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp ? true : false;
  326. case 'beforeDateTime':
  327. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  328. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  329. return $createdTimeStamp < $conditionTimeStamp ? true : false;
  330. case 'beforeDateTimeOn':
  331. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  332. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  333. return ($createdTimeStamp < $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true : false;
  334. case 'afterDateTime':
  335. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  336. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  337. return $createdTimeStamp > $conditionTimeStamp ? true : false;
  338. case 'afterDateTimeOn':
  339. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  340. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  341. return $createdTimeStamp > $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp ? true : false;
  342. case 'beforeTime':
  343. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  344. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  345. return $createdTimeStamp < $conditionTimeStamp ? true : false;
  346. case 'beforeTimeOn':
  347. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  348. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  349. return ($createdTimeStamp < $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true : false;
  350. case 'afterTime':
  351. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  352. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  353. return $createdTimeStamp > $conditionTimeStamp ? true : false;
  354. case 'afterTimeOn':
  355. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  356. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  357. return $createdTimeStamp > $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp ? true : false;
  358. case 'greaterThan':
  359. return !is_array($haystack) && $needle > $haystack;
  360. case 'lessThan':
  361. return !is_array($haystack) && $needle < $haystack;
  362. default:
  363. break;
  364. }
  365. return false;
  366. }
  367. }