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

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\AbstractController;
  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. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  14. use Webkul\UVDesk\CoreFrameworkBundle\FileSystem\FileSystem;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. use Webkul\UVDesk\CoreFrameworkBundle\Services\FileUploadService;
  17. use Symfony\Component\Filesystem\Filesystem as Fileservice;
  18. Class Customer extends AbstractController
  19. {
  20.     private $translator;
  21.     private $fileSystem;
  22.     private $passwordEncoder;
  23.     private $fileUploadService;
  24.     public function __construct(TranslatorInterface $translatorUserPasswordEncoderInterface $passwordEncoderFileSystem $fileSystemFileUploadService $fileUploadService)
  25.     {
  26.         $this->translator $translator;
  27.         $this->fileSystem $fileSystem;
  28.         $this->passwordEncoder $passwordEncoder;
  29.         $this->fileUploadService $fileUploadService;
  30.     }
  31.     protected function redirectUserToLogin()
  32.     {
  33.         $authChecker $this->container->get('security.authorization_checker');
  34.         if($authChecker->isGranted('ROLE_CUSTOMER'))
  35.             return true;
  36.     }
  37.     protected function isWebsiteActive()
  38.     {
  39.         $entityManager $this->getDoctrine()->getManager();
  40.         $website $entityManager->getRepository(CoreWebsite::class)->findOneByCode('knowledgebase');
  41.   
  42.         if (!empty($website)) {
  43.             $knowledgebaseWebsite $entityManager->getRepository(KnowledgebaseWebsite::class)->findOneBy(['website' => $website->getId(), 'status' => 1]);
  44.             
  45.             if (!empty($knowledgebaseWebsite) && true == $knowledgebaseWebsite->getIsActive()) {
  46.                 return true;
  47.             }
  48.         }
  49.         $this->noResultFound();
  50.     }
  51.     protected function noResultFound()
  52.     {
  53.         throw new NotFoundHttpException('Permission Denied !');
  54.     }
  55.     protected function isLoginDisabled()
  56.     {
  57.         $entityManager $this->getDoctrine()->getManager();
  58.         $website $entityManager->getRepository('UVDeskCoreFrameworkBundle:Website')->findOneByCode('knowledgebase');
  59.         if (!empty($website)) {
  60.             $configuration $entityManager->getRepository('UVDeskSupportCenterBundle:KnowledgebaseWebsite')->findOneBy([
  61.                 'website' => $website->getId(),
  62.                 'isActive' => 1,
  63.             ]);
  64.             if (!empty($configuration) && $configuration->getDisableCustomerLogin()) {
  65.                 return true;
  66.             }
  67.         }
  68.         return false;
  69.     }
  70.     public function login(Request $request)
  71.     {
  72.         $this->isWebsiteActive();
  73.         if ($this->redirectUserToLogin()) {
  74.             return $this->redirect($this->generateUrl('helpdesk_customer_ticket_collection')); // Replace with Dashboard route
  75.         }
  76.         /** check disabled customer login **/
  77.         if($this->isLoginDisabled()) {
  78.             $this->addFlash('warning'$this->translator->trans('Warning ! Customer Login disabled by admin.') );
  79.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  80.         }
  81.         $session $request->getSession();
  82.         $error $session->get(Security::AUTHENTICATION_ERROR);
  83.         $session->remove(Security::AUTHENTICATION_ERROR);
  84.         return $this->render('@UVDeskSupportCenter/Knowledgebase/login.html.twig', [
  85.             'searchDisable' => true,
  86.             'last_username' => $session->get(Security::LAST_USERNAME),
  87.             'error'         => $error,
  88.             'breadcrumbs' => [
  89.                 [
  90.                     'label' => $this->translator->trans('Support Center'),
  91.                     'url' => $this->generateUrl('helpdesk_knowledgebase')
  92.                 ], [
  93.                     'label' => $this->translator->trans('Sign In'),
  94.                     'url' => '#'
  95.                 ]
  96.             ]
  97.         ]);
  98.     }
  99.     public function Account(Request $request)
  100.     {
  101.         $this->isWebsiteActive();
  102.         $em $this->getDoctrine()->getManager();
  103.         $user $this->getUser();
  104.         $errors = [];
  105.         if ($request->getMethod() == 'POST') {
  106.             $data     $request->request->all();
  107.             $dataFiles $request->files->get('user_form');
  108.             $data $data['user_form'];
  109.             // Profile upload validation
  110.             $validMimeType = ['image/jpeg''image/png''image/jpg'];
  111.             if (isset($dataFiles['profileImage'])) {
  112.                 if (!in_array($dataFiles['profileImage']->getMimeType(), $validMimeType)) {
  113.                     $this->addFlash('warning'$this->translator->trans('Error ! Profile image is not valid, please upload a valid format'));
  114.                     return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  115.                 }
  116.             }
  117.             $checkUser $em->getRepository('UVDeskCoreFrameworkBundle:User')->findOneBy(array('email'=>$data['email']));
  118.             $errorFlag 0;
  119.             if ($checkUser) {
  120.                 if($checkUser->getId() != $user->getId())
  121.                     $errorFlag 1;
  122.             }
  123.             if (!$errorFlag) {
  124.                 $password $user->getPassword();
  125.                 $form $this->createForm(UserProfile::class, $user);
  126.                 $form->handleRequest($request);
  127.                 $form->submit($data);
  128.                 if ($form->isValid()) {
  129.                     if ($data != null && (!empty($data['password']['first']))) {
  130.                         $encodedPassword $this->passwordEncoder->encodePassword($user$data['password']['first']);
  131.                         if (!empty($encodedPassword) ) {
  132.                             $user->setPassword($encodedPassword);
  133.                         }
  134.                     } else {
  135.                         $user->setPassword($password);
  136.                     }
  137.                     $user->setFirstName($data['firstName']);
  138.                     $user->setLastName($data['lastName']);
  139.                     $user->setEmail($data['email']);
  140.                     $user->setTimeZone($data['timezone']);
  141.                     $user->setTimeFormat($data['timeformat']);
  142.                     
  143.                     $em->persist($user);
  144.                     $em->flush();
  145.                     $userInstance $em->getRepository('UVDeskCoreFrameworkBundle:UserInstance')->findOneBy(array('user' => $user->getId()));
  146.                     if (isset($dataFiles['profileImage'])) {
  147.                         $previousImage $userInstance->getProfileImagePath();
  148.                         if($previousImage != null){
  149.                             $image str_replace("\\","/",$this->getParameter('kernel.project_dir').'/public'.$previousImage);
  150.                             $check $this->fileUploadService->fileRemoveFromFolder($image); 
  151.                         }
  152.                         $assetDetails $this->fileSystem->getUploadManager()->uploadFile($dataFiles['profileImage'], 'profile');
  153.                         $userInstance->setProfileImagePath($assetDetails['path']);
  154.                     }
  155.                     // Removed profile image from database and path
  156.                     $fileService = new Fileservice;
  157.                     if ($request->get('removeImage') == 'on') {
  158.                         if ($userInstance->getProfileImagePath()) {
  159.                             $fileService->remove($this->getParameter('kernel.project_dir').'/public'.$userInstance->getProfileImagePath());
  160.                         }
  161.                         $userInstance $userInstance->setProfileImagePath(null);
  162.                     }
  163.                     $userInstance  $userInstance->setContactNumber($data['contactNumber']);
  164.                     $em->persist($userInstance);
  165.                     $em->flush();
  166.                     $this->addFlash('success'$this->translator->trans('Success ! Profile updated successfully.'));
  167.                     return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  168.                 } else {
  169.                     $errors $form->getErrors();
  170.                     dump($errors);
  171.                     die;
  172.                     $errors $this->getFormErrors($form);
  173.                 }
  174.             } else {
  175.                 $this->addFlash('warning'$this->translator->trans('Error ! User with same email is already exist.'));
  176.                 return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  177.             }
  178.         }
  179.         return $this->render('@UVDeskSupportCenter/Knowledgebase/customerAccount.html.twig', [
  180.             'searchDisable' => true,
  181.             'user' => $user,
  182.         ]);
  183.     }
  184.     public function searchArticle(Request $request)
  185.     {
  186.         $this->isWebsiteActive();
  187.         $searchQuery $request->query->get('s');
  188.         if (empty($searchQuery)) {
  189.             return $this->redirect($this->generateUrl('helpdesk_customer_ticket_collection'));
  190.         }
  191.         $articleCollection $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Article')->getArticleBySearch($request);
  192.         return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  193.             'search' => $searchQuery,
  194.             'articles' => $articleCollection,
  195.             'breadcrumbs' => [
  196.                 ['label' => $this->translator->trans('Support Center'), 'url' => $this->generateUrl('helpdesk_knowledgebase')],
  197.                 ['label' => $searchQuery'url' => '#'],
  198.             ],
  199.         ]);
  200.     }
  201. }