vendor/uvdesk/core-framework/Controller/CustomerXHR.php line 31

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
  11. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  12. use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents;
  13. class CustomerXHR extends AbstractController
  14. {
  15.     private $userService;
  16.     private $eventDispatcher;
  17.     private $translator;
  18.     private $entityManager;
  19.     public function __construct(UserService $userServiceEventDispatcherInterface $eventDispatcherTranslatorInterface $translatorEntityManagerInterface $entityManager)
  20.     {
  21.         $this->userService $userService;
  22.         $this->eventDispatcher $eventDispatcher;
  23.         $this->translator $translator;
  24.         $this->entityManager $entityManager;
  25.     }
  26.     public function listCustomersXHR(Request $requestContainerInterface $container)
  27.     {
  28.         if (! $this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_CUSTOMER')) {
  29.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  30.         }
  31.         $json = array();
  32.         if ($request->isXmlHttpRequest()) {
  33.             $repository $this->entityManager->getRepository(User::class);
  34.             $json =  $repository->getAllCustomer($request->query$container);
  35.         }
  36.         $response = new Response(json_encode($json));
  37.         $response->headers->set('Content-Type''application/json');
  38.         return $response;
  39.     }
  40.     public function removeCustomerXHR(Request $request)
  41.     {
  42.         if (! $this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_CUSTOMER')) {
  43.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  44.         }
  45.         $json = array();
  46.         if ($request->getMethod() == "DELETE") {
  47.             $em $this->entityManager;
  48.             $id $request->attributes->get('customerId');
  49.             $user $em->getRepository(User::class)->findOneBy(['id' => $id]);
  50.             if ($user) {
  51.                 $this->userService->removeCustomer($user);
  52.                 // Trigger customer created event
  53.                 $event = new CoreWorkflowEvents\Customer\Delete();
  54.                 $event
  55.                     ->setUser($user);
  56.                 $this->eventDispatcher->dispatch($event'uvdesk.automation.workflow.execute');
  57.                 $json['alertClass']   = 'success';
  58.                 $json['alertMessage'] = $this->translator->trans('Success ! Customer removed successfully.');
  59.             } else {
  60.                 $json['alertClass']   = 'danger';
  61.                 $json['alertMessage'] = $this->translator->trans('Error ! Invalid customer id.');
  62.                 $json['statusCode']   = Response::HTTP_NOT_FOUND;
  63.             }
  64.         }
  65.         $response = new Response(json_encode($json));
  66.         $response->headers->set('Content-Type''application/json');
  67.         return $response;
  68.     }
  69. }