vendor/uvdesk/support-center-bundle/Controller/Customer.php line 65

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\Security\Core\Security;
  5. use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
  6. use Symfony\Component\EventDispatcher\GenericEvent;
  7. use Webkul\UVDesk\CoreFrameworkBundle\Form\UserProfile;
  8. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  9. use Webkul\UVDesk\CoreFrameworkBundle\Utils\TokenGenerator;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Webkul\UVDesk\SupportCenterBundle\Entity\KnowledgebaseWebsite;
  12. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Website as CoreWebsite;
  13. Class Customer extends Controller
  14. {
  15.     protected function redirectUserToLogin()
  16.     {
  17.         $authChecker $this->container->get('security.authorization_checker');
  18.         if($authChecker->isGranted('ROLE_CUSTOMER'))
  19.             return true;
  20.     }
  21.     protected function isWebsiteActive()
  22.     {
  23.         $entityManager $this->getDoctrine()->getManager();
  24.         $website $entityManager->getRepository(CoreWebsite::class)->findOneByCode('knowledgebase');
  25.   
  26.         if (!empty($website)) {
  27.             $knowledgebaseWebsite $entityManager->getRepository(KnowledgebaseWebsite::class)->findOneBy(['website' => $website->getId(), 'status' => 1]);
  28.             
  29.             if (!empty($knowledgebaseWebsite) && true == $knowledgebaseWebsite->getIsActive()) {
  30.                 return true;
  31.             }
  32.         }
  33.         $this->noResultFound();
  34.     }
  35.     protected function noResultFound()
  36.     {
  37.         throw new NotFoundHttpException('Permission Denied !');
  38.     }
  39.     protected function isLoginDisabled()
  40.     {
  41.         $entityManager $this->getDoctrine()->getManager();
  42.         $website $entityManager->getRepository('UVDeskCoreFrameworkBundle:Website')->findOneByCode('knowledgebase');
  43.         if (!empty($website)) {
  44.             $configuration $entityManager->getRepository('UVDeskSupportCenterBundle:KnowledgebaseWebsite')->findOneBy([
  45.                 'website' => $website->getId(),
  46.                 'isActive' => 1,
  47.             ]);
  48.             if (!empty($configuration) && $configuration->getDisableCustomerLogin()) {
  49.                 return true;
  50.             }
  51.         }
  52.         return false;
  53.     }
  54.     public function login(Request $request)
  55.     {
  56.          
  57.           
  58.          $this->isWebsiteActive();
  59.         if ($this->redirectUserToLogin()) {
  60.             return $this->redirect($this->generateUrl('helpdesk_customer_ticket_collection')); // Replace with Dashboard route
  61.         }
  62.         /** check disabled customer login **/
  63.         if($this->isLoginDisabled()) {
  64.             $this->addFlash('warning'$this->get('translator')->trans('Warning ! Customer Login disabled by admin.') );
  65.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  66.         }
  67.         $session $request->getSession();
  68.         $error $session->get(Security::AUTHENTICATION_ERROR);
  69.         $session->remove(Security::AUTHENTICATION_ERROR);
  70.        
  71.         
  72.         $existUser=1;
  73.         if($session->get(Security::LAST_USERNAME)!=NULL){ // cuando mando datos desde el formulario de logueo ingreso a comprobar si el usuario existe
  74.       
  75.             $em $this->getDoctrine()->getManager();
  76.             $checkUser $em->getRepository('UVDeskCoreFrameworkBundle:User')->findOneBy(array('email'=>$session->get(Security::LAST_USERNAME)));
  77.             
  78.             if(!$checkUser)
  79.                 $existUser=0;
  80.         }
  81.         $sesion=null;
  82.         if($this->get('user.service')->getSessionUser()!=null)
  83.             $sesion=true;
  84.         return $this->render('@UVDeskSupportCenter/Knowledgebase/login.html.twig', [
  85.             'searchDisable' => true,
  86.             'last_username' => $session->get(Security::LAST_USERNAME),
  87.             'error'         => $error,
  88.             'existUser'=>$existUser,
  89.             'breadcrumbs' => [
  90.                 [
  91.                     'label' => $this->get('translator')->trans('Support Center'),
  92.                     'url' => $this->generateUrl('helpdesk_knowledgebase'),
  93.                     'sesion'=>$sesion
  94.                     
  95.                 ], [
  96.                     'label' => $this->get('translator')->trans('Sign In'),
  97.                     'url' => '#','sesion'=>$sesion
  98.                 ]
  99.             ]
  100.         ]);
  101.     }
  102.     public function Account(Request $request)
  103.     {
  104.        
  105.         $this->isWebsiteActive();
  106.         $em $this->getDoctrine()->getManager();
  107.         $user $this->getUser();
  108.         $errors = [];
  109.         if ($request->getMethod() == 'POST') {
  110.             $data     $request->request->all();
  111.             if($user->getId()!=null && $user->getRoles()=="ROLE_CUSTOMER"){
  112.                 $data["user_form"]["firstName"]=$user->getFirstName();
  113.                 $data["user_form"]["email"]=$user->getEmail();
  114.                 $data["user_form"]["lastName"]=$user->getlastName();
  115.             }
  116.             $dataFiles $request->files->get('user_form');
  117.             $data $data['user_form'];
  118.             // Profile upload validation
  119.             $validMimeType = ['image/jpeg''image/png''image/jpg'];
  120.             if (isset($dataFiles['profileImage'])) {
  121.                 if (!in_array($dataFiles['profileImage']->getMimeType(), $validMimeType)) {
  122.                     $this->addFlash('warning'$this->get('translator')->trans('Error ! Profile image is not valid, please upload a valid format'));
  123.                     return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  124.                 }
  125.             }
  126.             $checkUser $em->getRepository('UVDeskCoreFrameworkBundle:User')->findOneBy(array('email'=>$data['email']));
  127.             $errorFlag 0;
  128.             if ($checkUser) {
  129.                 if($checkUser->getId() != $user->getId())
  130.                     $errorFlag 1;
  131.             }
  132.             if (!$errorFlag) {
  133.                 $password $user->getPassword();
  134.                 $form $this->createForm(UserProfile::class, $user);
  135.                 $form->handleRequest($request);
  136.                 $form->submit($data);
  137.                 if ($form->isValid()) {
  138.                     if ($data != null && (!empty($data['password']['first']))) {
  139.                         $encodedPassword $this->container->get('security.password_encoder')->encodePassword($user$data['password']['first']);
  140.                         if (!empty($encodedPassword) ) {
  141.                             $user->setPassword($encodedPassword);
  142.                         }
  143.                     } else {
  144.                         $user->setPassword($password);
  145.                     }
  146.                
  147.                     //$user->setFirstName($data['firstName']);
  148.                     //$user->setLastName($data['lastName']);
  149.                     ///$user->setEmail($data['email']);
  150.                     $user->setTimeZone($data['timezone']);
  151.                     
  152.                     $em->persist($user);
  153.                     $em->flush();
  154.                     $userInstance $em->getRepository('UVDeskCoreFrameworkBundle:UserInstance')->findOneBy(array('user' => $user->getId()));
  155.                     if (isset($dataFiles['profileImage'])) {
  156.                         $assetDetails $this->container->get('uvdesk.core.file_system.service')->getUploadManager()->uploadFile($dataFiles['profileImage'], 'profile');
  157.                         $userInstance->setProfileImagePath($assetDetails['path']);
  158.                     }
  159.                     $userInstance  $userInstance->setContactNumber($data['contactNumber']);
  160.                     $em->persist($userInstance);
  161.                     $em->flush();
  162.                     $this->addFlash('success'$this->get('translator')->trans('Success ! Profile updated successfully.'));
  163.                     return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  164.                 } else {
  165.                     $errors $form->getErrors();
  166.                     dump($errors);
  167.                     die;
  168.                     $errors $this->getFormErrors($form);
  169.                 }
  170.             } else {
  171.                 $this->addFlash('warning'$this->get('translator')->trans('Error ! User with same email is already exist.'));
  172.                 return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  173.             }
  174.         }
  175.         return $this->render('@UVDeskSupportCenter/Knowledgebase/customerAccount.html.twig', [
  176.             'searchDisable' => true,
  177.             'user' => $user,
  178.         ]);
  179.     }
  180.     public function searchArticle(Request $request)
  181.     {
  182.         $this->isWebsiteActive();
  183.         $searchQuery $request->query->get('s');
  184.         if (empty($searchQuery)) {
  185.             return $this->redirect($this->generateUrl('helpdesk_customer_ticket_collection'));
  186.         }
  187.         $articleCollection $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Article')->getArticleBySearch($request);
  188.         if($this->get('user.service')->getSessionUser()!=null)
  189.             $sesion=true;
  190.         return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  191.             'search' => $searchQuery,
  192.             'articles' => $articleCollection,
  193.             'breadcrumbs' => [
  194.                     [
  195.                     'label' => $this->get('translator')->trans('Support Center'), 
  196.                     'url' => $this->generateUrl('helpdesk_knowledgebase'),
  197.                      'sesion'=>$sesion
  198.                     ],
  199.                     [
  200.                     'label' => $searchQuery
  201.                     'url' => '#',
  202.                      'sesion'=>$sesion
  203.                     ],
  204.             ],
  205.         ]);
  206.     }
  207. }