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

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