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

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 logout(Request $request)
  70.     {
  71.         return;
  72.     }
  73.     public function login(Request $request)
  74.     {
  75.         $this->isWebsiteActive();
  76.         if ($this->redirectUserToLogin()) {
  77.             return $this->redirect($this->generateUrl('helpdesk_customer_ticket_collection')); // Replace with Dashboard route
  78.         }
  79.         /** check disabled customer login **/
  80.         if($this->isLoginDisabled()) {
  81.             $this->addFlash('warning'$this->translator->trans('Warning ! Customer Login disabled by admin.') );
  82.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  83.         }
  84.         $session $request->getSession();
  85.         $error $session->get(Security::AUTHENTICATION_ERROR);
  86.         $session->remove(Security::AUTHENTICATION_ERROR);
  87.         return $this->render('@UVDeskSupportCenter/Knowledgebase/login.html.twig', [
  88.             'searchDisable' => true,
  89.             'last_username' => $session->get(Security::LAST_USERNAME),
  90.             'error'         => $error,
  91.             'breadcrumbs' => [
  92.                 [
  93.                     'label' => $this->translator->trans('Support Center'),
  94.                     'url' => $this->generateUrl('helpdesk_knowledgebase')
  95.                 ], [
  96.                     'label' => $this->translator->trans('Sign In'),
  97.                     'url' => '#'
  98.                 ]
  99.             ]
  100.         ]);
  101.     }
  102.     public function Account(Request $request)
  103.     {
  104.         $this->isWebsiteActive();
  105.         $em $this->getDoctrine()->getManager();
  106.         $user $this->getUser();
  107.         $errors = [];
  108.         if ($request->getMethod() == 'POST') {
  109.             $data     $request->request->all();
  110.             $dataFiles $request->files->get('user_form');
  111.             $data $data['user_form'];
  112.             // Profile upload validation
  113.             $validMimeType = ['image/jpeg''image/png''image/jpg'];
  114.             if (isset($dataFiles['profileImage'])) {
  115.                 if (!in_array($dataFiles['profileImage']->getMimeType(), $validMimeType)) {
  116.                     $this->addFlash('warning'$this->translator->trans('Error ! Profile image is not valid, please upload a valid format'));
  117.                     return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  118.                 }
  119.             }
  120.             $checkUser $em->getRepository(CoreEntites\User::class)->findOneBy(array('email'=>$data['email']));
  121.             $errorFlag 0;
  122.             if ($checkUser) {
  123.                 if($checkUser->getId() != $user->getId())
  124.                     $errorFlag 1;
  125.             }
  126.             if (!$errorFlag) {
  127.                 $password $user->getPassword();
  128.                 $form $this->createForm(UserProfile::class, $user);
  129.                 $form->handleRequest($request);
  130.                 $form->submit($data);
  131.                 if ($form->isValid()) {
  132.                     if ($data != null && (!empty($data['password']['first']))) {
  133.                         $encodedPassword $this->passwordEncoder->encodePassword($user$data['password']['first']);
  134.                         if (!empty($encodedPassword) ) {
  135.                             $user->setPassword($encodedPassword);
  136.                         }
  137.                     } else {
  138.                         $user->setPassword($password);
  139.                     }
  140.                     $user->setFirstName($data['firstName']);
  141.                     $user->setLastName($data['lastName']);
  142.                     $user->setEmail($data['email']);
  143.                     $user->setTimeZone($data['timezone']);
  144.                     $user->setTimeFormat($data['timeformat']);
  145.                     
  146.                     $em->persist($user);
  147.                     $em->flush();
  148.                     $userInstance $em->getRepository(CoreEntites\UserInstance::class)->findOneBy(array('user' => $user->getId()));
  149.                     if (isset($dataFiles['profileImage'])) {
  150.                         $previousImage $userInstance->getProfileImagePath();
  151.                         if($previousImage != null){
  152.                             $image str_replace("\\","/",$this->getParameter('kernel.project_dir').'/public'.$previousImage);
  153.                             $check $this->fileUploadService->fileRemoveFromFolder($image); 
  154.                         }
  155.                         $assetDetails $this->fileSystem->getUploadManager()->uploadFile($dataFiles['profileImage'], 'profile');
  156.                         $userInstance->setProfileImagePath($assetDetails['path']);
  157.                     }
  158.                     // Removed profile image from database and path
  159.                     $fileService = new Fileservice;
  160.                     if ($request->get('removeImage') == 'on') {
  161.                         if ($userInstance->getProfileImagePath()) {
  162.                             $fileService->remove($this->getParameter('kernel.project_dir').'/public'.$userInstance->getProfileImagePath());
  163.                         }
  164.                         $userInstance $userInstance->setProfileImagePath(null);
  165.                     }
  166.                     $userInstance  $userInstance->setContactNumber($data['contactNumber']);
  167.                     $em->persist($userInstance);
  168.                     $em->flush();
  169.                     $this->addFlash('success'$this->translator->trans('Success ! Profile updated successfully.'));
  170.                     return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  171.                 } else {
  172.                     $errors $form->getErrors();
  173.                     dump($errors);
  174.                     die;
  175.                     $errors $this->getFormErrors($form);
  176.                 }
  177.             } else {
  178.                 $this->addFlash('warning'$this->translator->trans('Error ! User with same email is already exist.'));
  179.                 return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  180.             }
  181.         }
  182.         return $this->render('@UVDeskSupportCenter/Knowledgebase/customerAccount.html.twig', [
  183.             'searchDisable' => true,
  184.             'user' => $user,
  185.         ]);
  186.     }
  187.     public function searchArticle(Request $request)
  188.     {
  189.         $this->isWebsiteActive();
  190.         $searchQuery $request->query->get('s');
  191.         if (empty($searchQuery)) {
  192.             return $this->redirect($this->generateUrl('helpdesk_customer_ticket_collection'));
  193.         }
  194.         $articleCollection $this->getDoctrine()->getRepository(SupportEntites\Article::class)->getArticleBySearch($request);
  195.         return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  196.             'search' => $searchQuery,
  197.             'articles' => $articleCollection,
  198.             'breadcrumbs' => [
  199.                 ['label' => $this->translator->trans('Support Center'), 'url' => $this->generateUrl('helpdesk_knowledgebase')],
  200.                 ['label' => $searchQuery'url' => '#'],
  201.             ],
  202.         ]);
  203.     }
  204. }