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

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