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

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 $translator, UserPasswordEncoderInterface $passwordEncoder, FileSystem $fileSystem, FileUploadService $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. if (!empty($website)) {
  41. $knowledgebaseWebsite = $entityManager->getRepository(SupportEntites\KnowledgebaseWebsite::class)->findOneBy(['website' => $website->getId(), 'status' => 1]);
  42. if (!empty($knowledgebaseWebsite) && true == $knowledgebaseWebsite->getIsActive()) {
  43. return true;
  44. }
  45. }
  46. $this->noResultFound();
  47. }
  48. protected function noResultFound()
  49. {
  50. throw new NotFoundHttpException('Permission Denied !');
  51. }
  52. protected function isLoginDisabled()
  53. {
  54. $entityManager = $this->getDoctrine()->getManager();
  55. $website = $entityManager->getRepository(CoreEntites\Website::class)->findOneByCode('knowledgebase');
  56. if (!empty($website)) {
  57. $configuration = $entityManager->getRepository(SupportEntites\KnowledgebaseWebsite::class)->findOneBy([
  58. 'website' => $website->getId(),
  59. 'isActive' => 1,
  60. ]);
  61. if (!empty($configuration) && $configuration->getDisableCustomerLogin()) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. public function login(Request $request)
  68. {
  69. $this->isWebsiteActive();
  70. if ($this->redirectUserToLogin()) {
  71. return $this->redirect($this->generateUrl('helpdesk_customer_ticket_collection')); // Replace with Dashboard route
  72. }
  73. /** check disabled customer login **/
  74. if($this->isLoginDisabled()) {
  75. $this->addFlash('warning', $this->translator->trans('Warning ! Customer Login disabled by admin.') );
  76. return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  77. }
  78. $session = $request->getSession();
  79. $error = $session->get(Security::AUTHENTICATION_ERROR);
  80. $session->remove(Security::AUTHENTICATION_ERROR);
  81. return $this->render('@UVDeskSupportCenter/Knowledgebase/login.html.twig', [
  82. 'searchDisable' => true,
  83. 'last_username' => $session->get(Security::LAST_USERNAME),
  84. 'error' => $error,
  85. 'breadcrumbs' => [
  86. [
  87. 'label' => $this->translator->trans('Support Center'),
  88. 'url' => $this->generateUrl('helpdesk_knowledgebase')
  89. ], [
  90. 'label' => $this->translator->trans('Sign In'),
  91. 'url' => '#'
  92. ]
  93. ]
  94. ]);
  95. }
  96. public function Account(Request $request)
  97. {
  98. $this->isWebsiteActive();
  99. $em = $this->getDoctrine()->getManager();
  100. $user = $this->getUser();
  101. $errors = [];
  102. if ($request->getMethod() == 'POST') {
  103. $data = $request->request->all();
  104. $dataFiles = $request->files->get('user_form');
  105. $data = $data['user_form'];
  106. // Profile upload validation
  107. $validMimeType = ['image/jpeg', 'image/png', 'image/jpg'];
  108. if (isset($dataFiles['profileImage'])) {
  109. if (!in_array($dataFiles['profileImage']->getMimeType(), $validMimeType)) {
  110. $this->addFlash('warning', $this->translator->trans('Error ! Profile image is not valid, please upload a valid format'));
  111. return $this->redirect($this->generateUrl('helpdesk_customer_account'));
  112. }
  113. }
  114. $checkUser = $em->getRepository(CoreEntites\User::class)->findOneBy(array('email'=>$data['email']));
  115. $errorFlag = 0;
  116. if ($checkUser) {
  117. if($checkUser->getId() != $user->getId())
  118. $errorFlag = 1;
  119. }
  120. if (!$errorFlag) {
  121. $password = $user->getPassword();
  122. $form = $this->createForm(UserProfile::class, $user);
  123. $form->handleRequest($request);
  124. $form->submit($data);
  125. if ($form->isValid()) {
  126. if ($data != null && (!empty($data['password']['first']))) {
  127. $encodedPassword = $this->passwordEncoder->encodePassword($user, $data['password']['first']);
  128. if (!empty($encodedPassword) ) {
  129. $user->setPassword($encodedPassword);
  130. }
  131. } else {
  132. $user->setPassword($password);
  133. }
  134. $user->setFirstName($data['firstName']);
  135. $user->setLastName($data['lastName']);
  136. $user->setEmail($data['email']);
  137. $user->setTimeZone($data['timezone']);
  138. $user->setTimeFormat($data['timeformat']);
  139. $em->persist($user);
  140. $em->flush();
  141. $userInstance = $em->getRepository(CoreEntites\UserInstance::class)->findOneBy(array('user' => $user->getId()));
  142. if (isset($dataFiles['profileImage'])) {
  143. $previousImage = $userInstance->getProfileImagePath();
  144. if($previousImage != null){
  145. $image = str_replace("\\","/",$this->getParameter('kernel.project_dir').'/public'.$previousImage);
  146. $check = $this->fileUploadService->fileRemoveFromFolder($image);
  147. }
  148. $assetDetails = $this->fileSystem->getUploadManager()->uploadFile($dataFiles['profileImage'], 'profile');
  149. $userInstance->setProfileImagePath($assetDetails['path']);
  150. }
  151. // Removed profile image from database and path
  152. $fileService = new Fileservice;
  153. if ($request->get('removeImage') == 'on') {
  154. if ($userInstance->getProfileImagePath()) {
  155. $fileService->remove($this->getParameter('kernel.project_dir').'/public'.$userInstance->getProfileImagePath());
  156. }
  157. $userInstance = $userInstance->setProfileImagePath(null);
  158. }
  159. $userInstance = $userInstance->setContactNumber($data['contactNumber']);
  160. $em->persist($userInstance);
  161. $em->flush();
  162. $this->addFlash('success', $this->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->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(SupportEntites\Article::class)->getArticleBySearch($request);
  188. return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  189. 'search' => $searchQuery,
  190. 'articles' => $articleCollection,
  191. 'breadcrumbs' => [
  192. ['label' => $this->translator->trans('Support Center'), 'url' => $this->generateUrl('helpdesk_knowledgebase')],
  193. ['label' => $searchQuery, 'url' => '#'],
  194. ],
  195. ]);
  196. }
  197. }