vendor/uvdesk/automation-bundle/Controller/Automations/WorkflowXHR.php line 30

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\AutomationBundle\Controller\Automations;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  9. use Webkul\UVDesk\AutomationBundle\Entity;
  10. use Webkul\UVDesk\AutomationBundle\EventListener\WorkflowListener;
  11. use Webkul\UVDesk\CoreFrameworkBundle\Services\TicketService;
  12. class WorkflowXHR extends AbstractController
  13. {
  14. private $userService;
  15. private $translator;
  16. private $workflowListnerService;
  17. private $ticketService;
  18. public function __construct(UserService $userService, WorkflowListener $workflowListnerService, TicketService $ticketService,TranslatorInterface $translator)
  19. {
  20. $this->userService = $userService;
  21. $this->ticketService = $ticketService;
  22. $this->workflowListnerService = $workflowListnerService;
  23. $this->translator = $translator;
  24. }
  25. public function workflowsListXhr(Request $request, ContainerInterface $container)
  26. {
  27. if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC')) {
  28. return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  29. }
  30. $json = [];
  31. $repository = $this->getDoctrine()->getRepository(Entity\Workflow::class);
  32. $json = $repository->getWorkflows($request->query, $container);
  33. $response = new Response(json_encode($json));
  34. $response->headers->set('Content-Type', 'application/json');
  35. return $response;
  36. }
  37. public function WorkflowsxhrAction(Request $request)
  38. {
  39. if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_WORKFLOW_AUTOMATIC')) {
  40. return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  41. }
  42. $json = [];
  43. $error = false;
  44. if($request->isXmlHttpRequest()){
  45. if($request->getMethod() == 'POST'){
  46. $em = $this->getDoctrine()->getManager();
  47. //sort order update
  48. $workflows = $em->getRepository(Entity\Workflow::class)->findAll();
  49. $sortOrders = $request->request->get('orders');
  50. if(count($workflows)) {
  51. foreach ($workflows as $id => $workflow) {
  52. if(!empty($sortOrders[$workflow->getId()])) {
  53. $workflow->setSortOrder($sortOrders[$workflow->getId()]);
  54. $em->persist($workflow);
  55. } else {
  56. $error = true;
  57. break;
  58. }
  59. }
  60. $em->flush();
  61. }
  62. if(!$error) {
  63. $json['alertClass'] = 'success';
  64. $json['alertMessage'] = $this->translator->trans('Success! Order has been updated successfully.');
  65. }
  66. }
  67. elseif($request->getMethod() == 'DELETE') {
  68. //$this->isAuthorized(self::ROLE_REQUIRED_AUTO);
  69. $em = $this->getDoctrine()->getManager();
  70. $id = $request->attributes->get('id');
  71. //$workFlow = $this->getWorkflow($id, 'Events');
  72. $workFlow = $em->getRepository(Entity\Workflow::class)
  73. ->findOneBy(array('id' => $id));
  74. if (!empty($workFlow)) {
  75. $em->remove($workFlow);
  76. $em->flush();
  77. } else {
  78. $error = true;
  79. }
  80. if (!$error) {
  81. $json['alertClass'] = 'success';
  82. $json['alertMessage'] = $this->translator->trans('Success! Workflow has been removed successfully.');
  83. }
  84. }
  85. }
  86. if($error){
  87. $json['alertClass'] = 'danger';
  88. $json['alertMessage'] = $this->translator->trans('Warning! You are not allowed to perform this action.');
  89. }
  90. $response = new Response(json_encode($json));
  91. $response->headers->set('Content-Type', 'application/json');
  92. return $response;
  93. }
  94. public function getWorkflowConditionOptionsXHR($entity, Request $request)
  95. {
  96. $error = false;
  97. $json = $results = array();
  98. $supportedConditions = ['TicketPriority', 'TicketType', 'TicketStatus', 'source', 'agent', 'group','team', 'agent_name', 'agent_email', 'stage'];
  99. if (!$request->isXmlHttpRequest()) {
  100. throw new Exception('', 404);
  101. } else {
  102. if ($request->getMethod() != 'GET' || !in_array($entity, $supportedConditions)) {
  103. throw new Exception('', 404);
  104. }
  105. }
  106. switch ($entity) {
  107. case 'team':
  108. $json = json_encode($this->userService->getSupportTeams());
  109. break;
  110. case 'group':
  111. $json = $this->userService->getSupportGroups();
  112. break;
  113. case 'stage':
  114. $json = $this->get('task.service')->getStages();
  115. break;
  116. case 'TicketType':
  117. $json = $this->ticketService->getTypes();
  118. break;
  119. case 'agent':
  120. case 'agent_name':
  121. $defaultAgent = ['id' => 'actionPerformingAgent', 'name' => 'Action Performing Agent'];
  122. $agentList = $this->userService->getAgentPartialDataCollection();
  123. array_push($agentList, $defaultAgent);
  124. $json = json_encode(array_map(function($item) {
  125. return [
  126. 'id' => $item['id'],
  127. 'name' => $item['name'],
  128. ];
  129. }, $agentList));
  130. break;
  131. case 'agent_email':
  132. $json = json_encode(array_map(function($item) {
  133. return [
  134. 'id' => $result['id'],
  135. 'name' => $result['email'],
  136. ];
  137. }, $this->userService->getAgentsPartialDetails()));
  138. break;
  139. case 'source':
  140. $allSources = $this->ticketService->getAllSources();
  141. $results = [];
  142. foreach($allSources as $key => $source) {
  143. $results[] = [
  144. 'id' => $key,
  145. 'name' => $source,
  146. ];
  147. };
  148. $json = json_encode($results);
  149. $results = [];
  150. break;
  151. case 'TicketStatus':
  152. case 'TicketPriority':
  153. $json = json_encode(array_map(function($item) {
  154. return [
  155. 'id' => $item->getId(),
  156. 'name' => $item->getCode(),
  157. ];
  158. }, $this->getDoctrine()->getRepository("Webkul\\UVDesk\\CoreFrameworkBundle\\Entity\\" . ucfirst($entity))->findAll()));
  159. break;
  160. default:
  161. $json = [];
  162. break;
  163. }
  164. // if (!empty($results)) {
  165. // $ignoredArray = ['__initializer__', '__cloner__', '__isInitialized__', 'description', 'color', 'company', 'createdAt', 'users', 'isActive'];
  166. // $json = $this->getSerializeObj($ignoredArray)->serialize($results, 'json');
  167. // }
  168. return new Response(is_array($json) ? json_encode($json) : $json, 200, ['Content-Type' => 'application/json']);
  169. }
  170. public function getWorkflowActionOptionsXHR($entity, Request $request, ContainerInterface $container)
  171. {
  172. foreach ($this->workflowListnerService->getRegisteredWorkflowActions() as $workflowAction) {
  173. if ($workflowAction->getId() == $entity) {
  174. $options = $workflowAction->getOptions($container);
  175. if (!empty($options)) {
  176. return new Response(json_encode($options), 200, ['Content-Type' => 'application/json']);
  177. }
  178. break;
  179. }
  180. }
  181. return new Response(json_encode([
  182. 'alertClass' => 'danger',
  183. 'alertMessage' => 'Warning! You are not allowed to perform this action.',
  184. ]), 200, ['Content-Type' => 'application/json']);
  185. }
  186. }