src/webkul/uvdesk/automation-bundle/EventListener/WorkflowListener.php line 76

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\Entity as CoreEntities;
  12. class WorkflowListener
  13. {
  14. private $container;
  15. private $entityManager;
  16. private $registeredWorkflowEvents = [];
  17. private $registeredWorkflowActions = [];
  18. public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager)
  19. {
  20. $this->container = $container;
  21. $this->entityManager = $entityManager;
  22. }
  23. public function registerWorkflowEvent(Event $serviceTag)
  24. {
  25. $this->registeredWorkflowEvents[] = $serviceTag;
  26. }
  27. public function registerWorkflowAction(Action $serviceTag)
  28. {
  29. $this->registeredWorkflowActions[] = $serviceTag;
  30. }
  31. public function getRegisteredWorkflowEvent($eventId)
  32. {
  33. foreach ($this->registeredWorkflowEvents as $workflowDefinition) {
  34. if ($workflowDefinition->getId() == $eventId) {
  35. /*
  36. @NOTICE: Events 'uvdesk.agent.forgot_password', 'uvdesk.customer.forgot_password' will be deprecated
  37. onwards uvdesk/automation-bundle:1.1.2 and uvdesk/core-framework:1.1.3 releases and will be
  38. completely removed with the next major release.
  39. Both the events have been mapped to return the 'uvdesk.user.forgot_password' id, so we need to
  40. return the correct definition.
  41. */
  42. if ('uvdesk.user.forgot_password' == $eventId) {
  43. if (
  44. $workflowDefinition instanceof \Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events\Agent\ForgotPassword
  45. || $workflowDefinition instanceof \Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events\Customer\ForgotPassword
  46. ) {
  47. continue;
  48. }
  49. }
  50. return $workflowDefinition;
  51. }
  52. }
  53. return null;
  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. 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)) {
  100. $totalConditions++;
  101. }
  102. if (isset($workflowCondition['or'])) {
  103. foreach ($workflowCondition['or'] as $orCondition) {
  104. if ($this->checkCondition($orCondition, $event)) {
  105. $totalConditions++;
  106. }
  107. }
  108. }
  109. }
  110. if ($totalEvaluatedConditions == 0 || $totalConditions >= $totalEvaluatedConditions) {
  111. $this->applyWorkflowActions($workflow, $event);
  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, Event $event)
  137. {
  138. // dd($this->getRegisteredWorkflowActions());
  139. foreach ($workflow->getActions() as $attributes) {
  140. if (empty($attributes['type'])) {
  141. continue;
  142. }
  143. foreach ($this->getRegisteredWorkflowActions() as $workflowAction) {
  144. // dd($workflowAction->getId() , $attributes['type']);
  145. if ($workflowAction->getId() == $attributes['type']) {
  146. // dd($workflowAction->getId() , $attributes['type'], $event instanceof CoreEntities\Ticket);
  147. $workflowAction->applyAction($this->container, $event, isset($attributes['value']) ? $attributes['value'] : '');
  148. // dd($event);
  149. }
  150. }
  151. }
  152. }
  153. public function checkCondition($condition, Event $event)
  154. {
  155. $entity = null;
  156. switch (true) {
  157. case $event instanceof WorkflowEvents\EmailActivity:
  158. $entity = $event->getResolvedEmailHeaders();
  159. break;
  160. case $event instanceof WorkflowEvents\TicketActivity:
  161. $entity = $event->getTicket();
  162. break;
  163. case $event instanceof WorkflowEvents\AgentActivity:
  164. case $event instanceof WorkflowEvents\CustomerActivity:
  165. case $event instanceof WorkflowEvents\UserActivity:
  166. $entity = $event->getUser();
  167. break;
  168. default:
  169. break;
  170. }
  171. if (empty($entity)) {
  172. return false;
  173. }
  174. switch ($condition['type']) {
  175. case 'from_mail':
  176. if (isset($condition['value'])) {
  177. if ($entity instanceof Ticket) {
  178. return $this->match($condition['match'], $entity->getCustomer()->getEmail(), $condition['value']);
  179. } else if (is_array($entity) && !empty($entity['from'])) {
  180. return $this->match($condition['match'], $entity['from'], $condition['value']);
  181. }
  182. }
  183. break;
  184. case 'to_mail':
  185. if (isset($condition['value']) && $entity instanceof Ticket && $entity->getMailboxEmail()) {
  186. return $this->match($condition['match'], $entity->getMailboxEmail(), $condition['value']);
  187. }
  188. break;
  189. case 'subject':
  190. if (isset($condition['value']) && ($entity instanceof Ticket || $entity instanceof Task)) {
  191. return $this->match($condition['match'], $entity->getSubject(), $condition['value']);
  192. }
  193. break;
  194. case 'description':
  195. if (isset($condition['value']) && $entity instanceof Ticket) {
  196. $reply = $entity->createdThread->getMessage();
  197. $reply = rtrim(strip_tags($reply), "\n" );
  198. return $this->match($condition['match'], rtrim($reply), $condition['value']);
  199. }
  200. break;
  201. case 'subject_or_description':
  202. if (isset($condition['value']) && $entity instanceof Ticket) {
  203. $flag = $this->match($condition['match'], $entity->getSubject(), $condition['value']);
  204. $createThread = $this->container->get('ticket.service')->getCreateReply($entity->getId(),false);
  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. break;
  268. case 'customer_email':
  269. if (isset($condition['value']) && $entity instanceof Ticket) {
  270. return $this->match($condition['match'], $entity->getCustomer()->getEmail(), $condition['value']);
  271. }
  272. break;
  273. case strpos($condition['type'], 'customFields[') == 0:
  274. $value = null;
  275. $ticketCfValues = $entity->getCustomFieldValues()->getValues();
  276. foreach ($ticketCfValues as $cfValue) {
  277. $mainCf = $cfValue->getTicketCustomFieldsValues();
  278. if ($condition['type'] == 'customFields[' . $mainCf->getId() . ']' ) {
  279. if (in_array($mainCf->getFieldType(), ['select', 'radio', 'checkbox'])) {
  280. $value = json_decode($cfValue->getValue(), true);
  281. } else {
  282. $value = trim($cfValue->getValue(), '"');
  283. }
  284. break;
  285. }
  286. }
  287. if (isset($condition['value']) && $entity instanceof Ticket) {
  288. return $this->match($condition['match'], !empty($value) ? $value : '', $condition['value']);
  289. }
  290. break;
  291. default:
  292. break;
  293. }
  294. return false;
  295. }
  296. public function match($condition, $haystack, $needle)
  297. {
  298. // Filter tags
  299. if ('string' == gettype($haystack)) {
  300. $haystack = strip_tags($haystack);
  301. }
  302. switch ($condition) {
  303. case 'is':
  304. return is_array($haystack) ? in_array($needle, $haystack) : $haystack == $needle;
  305. case 'isNot':
  306. return is_array($haystack) ? !in_array($needle, $haystack) : $haystack != $needle;
  307. case 'contains':
  308. return strripos($haystack,$needle) !== false ? true : false;
  309. case 'notContains':
  310. return strripos($haystack,$needle) === false ? true : false;
  311. case 'startWith':
  312. return $needle === "" || strripos($haystack, $needle, -strlen($haystack)) !== FALSE;
  313. case 'endWith':
  314. return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && stripos($haystack, $needle, $temp) !== FALSE);
  315. case 'before':
  316. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  317. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  318. return $createdTimeStamp < $conditionTimeStamp ? true : false;
  319. case 'beforeOn':
  320. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  321. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  322. return ($createdTimeStamp < $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true : false;
  323. case 'after':
  324. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  325. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  326. return $createdTimeStamp > $conditionTimeStamp ? true : false;
  327. case 'afterOn':
  328. $createdTimeStamp = date('Y-m-d', strtotime($haystack));
  329. $conditionTimeStamp = date('Y-m-d', strtotime($needle . " 23:59:59"));
  330. return $createdTimeStamp > $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp ? true : false;
  331. case 'beforeDateTime':
  332. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  333. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  334. return $createdTimeStamp < $conditionTimeStamp ? true : false;
  335. case 'beforeDateTimeOn':
  336. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  337. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  338. return ($createdTimeStamp < $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true : false;
  339. case 'afterDateTime':
  340. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  341. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  342. return $createdTimeStamp > $conditionTimeStamp ? true : false;
  343. case 'afterDateTimeOn':
  344. $createdTimeStamp = date('Y-m-d h:i:s', strtotime($haystack));
  345. $conditionTimeStamp = date('Y-m-d h:i:s', strtotime($needle));
  346. return $createdTimeStamp > $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp ? true : false;
  347. case 'beforeTime':
  348. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  349. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  350. return $createdTimeStamp < $conditionTimeStamp ? true : false;
  351. case 'beforeTimeOn':
  352. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  353. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  354. return ($createdTimeStamp < $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp) ? true : false;
  355. case 'afterTime':
  356. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  357. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  358. return $createdTimeStamp > $conditionTimeStamp ? true : false;
  359. case 'afterTimeOn':
  360. $createdTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $haystack));
  361. $conditionTimeStamp = date('Y-m-d H:i A', strtotime('2017-01-01' . $needle));
  362. return $createdTimeStamp > $conditionTimeStamp || $createdTimeStamp == $conditionTimeStamp ? true : false;
  363. case 'greaterThan':
  364. return !is_array($haystack) && $needle > $haystack;
  365. case 'lessThan':
  366. return !is_array($haystack) && $needle < $haystack;
  367. default:
  368. break;
  369. }
  370. return false;
  371. }
  372. }