src/Controller/HomeController.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use App\Service\DlmsService;
  10. use App\Entity\User;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  13. use App\Repository\SubscriptionRepository;
  14. use App\Service\CommonService;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  18. use App\Service\NotificationService;
  19. use Symfony\Component\Lock\LockFactory;
  20. use Symfony\Component\Lock\Store\SemaphoreStore;
  21. use Doctrine\ORM\EntityManagerInterface;
  22. class HomeController extends AbstractController
  23. {
  24.     private EntityManagerInterface $entityManager;
  25.     public function __construct(EntityManagerInterface $entityManager)
  26.     {
  27.         $this->entityManager = $entityManager;
  28.     }
  29.     /**
  30.      * @Route("/home", name="home-page")
  31.      */
  32.     public function homePage(Request $request)
  33.     {    
  34.         $params = $request->query->all();
  35.         if (!empty($params)) {
  36.             return $this->redirectToRoute('external_auth', $params);
  37.         }
  38.         return $this->render('homepage/home_page.html.twig');
  39.     }
  40.     /**
  41.      * @Route("/auth/redirect", name="auth-redirect")
  42.      */
  43.     public function authRedirect(Request $request)
  44.     {    
  45.         if ($this->isGranted('ROLE_ADMIN')) {
  46.             return $this->redirectToRoute('sadmin-dashboard');
  47.         } else if ($this->isGranted('ROLE_USER')) {
  48.             return $this->redirectToRoute('dashboard-view');
  49.         } else if ($this->isGranted('ROLE_CLOUD_TRAINER') || $this->isGranted('ROLE_MASTER_TRAINER')) {
  50.             return $this->redirectToRoute('trainer-welcome');
  51.         } else if ($this->isGranted('ROLE_CUSTOMER')) {
  52.             if ($request->query->get('buymore') == 'y') {
  53.                 return $this->redirectToRoute('buy-more-quotation');
  54.             } else {
  55.                 return $this->redirectToRoute('customer-admin');
  56.             }
  57.         } else if ($this->isGranted('ROLE_BUYER')) {
  58.             return $this->redirectToRoute('buyer-dashboard');
  59.         } else if ($this->isGranted('IS_AUTHENTICATED_2FA_IN_PROGRESS')) {
  60.             return $this->redirectToRoute('2fa_login');
  61.         }
  62.         return $this->redirectToRoute('home-page');
  63.     }
  64.     
  65.     /**
  66.      * @Route("/ecommerce/verify", name="ecommerce-verify")
  67.      */
  68.     public function ecommerceVerify(Request $request, LoggerInterface $logger, DlmsService $dlmsService, TranslatorInterface $translator, NotificationService $notificationService, CommonService $commonService)
  69.     {    
  70.         if ($request->query->get('_redirect') != 'y') {
  71.             $logger->error('VERIFY ' . $request->query->get('id') . '=' . $request->headers->get('User-Agent'));
  72.             return $this->render('registration/redirect.html.twig', [
  73.                 'url' => $this->generateUrl('ecommerce-verify', [], UrlGeneratorInterface::ABSOLUTE_URL) . '?hash=' . $request->query->get('hash') . '&id=' . $request->query->get('id') . '&email=' . $request->query->get('email') . '&_redirect=y'
  74.             ]);
  75.         }
  76.         $id = (int) $request->query->get('id');
  77.         $logger->error('VERIFY redirect ' . $request->query->get('id') . '=' . $request->headers->get('User-Agent'));
  78.         $noti = $translator->trans('msg.not_verify_account');
  79.         if ($id > 0 && $dlmsService->verifyHash($id, $request->query->get('email'), $request->query->get('hash'))) {
  80.             $store = new SemaphoreStore();
  81.             $factory = new LockFactory($store);
  82.             $lock = $factory->createLock('ecommerce-verify' . $id);
  83.             $lock->acquire(true);
  84.             $found = $this->entityManager->getRepository(User::class)->find($id);
  85.             if ($found != null && $found->getActive()) {
  86.                 if ($request->query->get('email') == '' || $request->query->get('email') == $found->getEmail()) {
  87.                     if (in_array('ROLE_BUYER', $found->getRoles())) {
  88.                         $found->setVerified(true);
  89.                         $this->entityManager->flush();
  90.                         $notificationService->sendEmailNOTIF('NOTIF 001 : buyer registration', 'mail/notif/reg_buyer.html.twig', [
  91.                             'name' => $found->getFirstName() . ' ' . $found->getLastName(),
  92.                             'company' => $found->getOrganizationName(),
  93.                             'reg_date' => date('d/m/Y'),
  94.                         ]);
  95.                         $noti = $translator->trans('sign_up.verify_success_message', [
  96.                             '%url%' => $this->generateUrl('app_login')
  97.                         ]);
  98.                     } else {
  99.                         if (!$found->getVerified()) {
  100.                             $role = '';
  101.                             if (in_array('ROLE_CLOUD_TRAINER', $found->getRoles())) {
  102.                                 $role = 'cloud_trainer';
  103.                             } else if (in_array('ROLE_USER', $found->getRoles())) {
  104.                                 $role = 'user';
  105.                             }
  106.                             if ($role) {
  107.                                 $result = $dlmsService->createDlmsAccount($found, $role);
  108.                                 if ($result) {
  109.                                     $noti = $result;    
  110.                                 }
  111.                             }
  112.                         } else { // already verified
  113.                             $createdAt = $found->getCreatedAt()->getTimestamp();
  114.                             if ($createdAt > strtotime('now -7 days')) {
  115.                                 if ($commonService->checkEmailNotResentTooMuch($found->getEmail())) {
  116.                                     $isSuccess = $dlmsService->sendEmailWelcome($found);
  117.                                 } else {
  118.                                     $isSuccess = false;
  119.                                 }
  120.                                 if ($isSuccess) {
  121.                                     $noti = $translator->trans('msg.ecommerce_verify_resend');
  122.                                 } else {
  123.                                     $noti = $translator->trans('msg.ecommerce_verify_resend') . ' ' . $translator->trans('msg.mail_server_error_try_later');
  124.                                 }
  125.                             } else {
  126.                                 $noti = $translator->trans('msg.ecommerce_verify_already', ['%href%' => $this->generateUrl('forgot_password')]);
  127.                             }
  128.                         }                    
  129.                     }
  130.                 } else {
  131.                     $noti = $translator->trans('msg.invalid_link');
  132.                 }
  133.             } else {
  134.                 $noti = $translator->trans('msg.user_nonexist_deactivated');
  135.             }
  136.             $lock->release();
  137.         } else {
  138.             $noti = $translator->trans('msg.invalid_link');
  139.         }
  140.         return $this->render('registration/ecommerce/message.html.twig', [
  141.             'message' => $noti,
  142.         ]);
  143.     }
  144.     /**
  145.      * @Route("/ecommerce/verify/change", name="ecommerce-email-change-verify")
  146.      */
  147.     public function ecommerceEmailChangeVerify(Request $request, LoggerInterface $logger, DlmsService $dlmsService, TranslatorInterface $translator)
  148.     {
  149.         if ($request->query->get('_redirect') != 'y') {
  150.             $logger->error('VERIFY CHANGE ' . $request->query->get('id') . '=' . $request->headers->get('User-Agent'));
  151.             return $this->render('registration/redirect.html.twig', [
  152.                 'url' => $this->generateUrl('ecommerce-email-change-verify', [], UrlGeneratorInterface::ABSOLUTE_URL) . '?hash=' . $request->query->get('hash') . '&id=' . $request->query->get('id') . '&email=' . $request->query->get('email') . '&_redirect=y'
  153.             ]);
  154.         }
  155.         $id = (int) $request->query->get('id');
  156.         $logger->error('VERIFY CHANGE redirect ' . $request->query->get('id') . '=' . $request->headers->get('User-Agent'));
  157.         $noti = $translator->trans('msg.not_verify_account');
  158.         if ($id > 0 && $dlmsService->verifyHash($id, $request->query->get('email'), $request->query->get('hash'))) {
  159.             $entityManager = $this->entityManager;
  160.             $conn = $entityManager->getConnection();
  161.             $sql = "
  162.                 SELECT u.*, e.email as new_email, g.package_code, c.company_type, g.site_id, s.customer_id
  163.                 FROM users u
  164.                 JOIN groups g ON g.id = u.group_id
  165.                 JOIN sites s ON s.id = g.site_id
  166.                 JOIN customers c ON c.id = s.customer_id
  167.                 LEFT JOIN email_verifications e ON e.user_id = u.id
  168.                 WHERE u.id = :id
  169.             "; // for user or trainer
  170.             $stmt = $conn->prepare($sql);
  171.             $stmt->execute(['id' => $id]);
  172.             $found = $stmt->fetch();
  173.             if ( !empty($found) && $found['active'] && $found['verified'] ) {
  174.                 if ( !empty($found['new_email']) && $request->query->get('email') == $found['new_email'] ) {
  175.                     $exist = $entityManager->getRepository(User::class)->checkEmailExist($found['new_email'], $found['site_id'], $found['company_type'] == 'Contest', $found['customer_id']);
  176.                     if ($exist == 'NO_EXIST') {
  177.                         $result = $dlmsService->editDlmsAccount($found, $found, $found['package_code'], $found['new_email']);
  178.                         if (!empty($result) && $result['code'] == 201) {
  179.                             $sql = "
  180.                             DELETE FROM email_verifications
  181.                             WHERE user_id = :user_id
  182.                             ";
  183.                             $stmt = $conn->prepare($sql);
  184.                             $stmt->execute([
  185.                                 'user_id' => $found['id'], 
  186.                             ]);  
  187.                             $sql = "
  188.                             UPDATE `users` SET `email` = :email
  189.                             WHERE id = :id;
  190.                             ";
  191.                             $stmt = $conn->prepare($sql);
  192.                             $stmt->execute([
  193.                                 'email' => $found['new_email'], 
  194.                                 'id' => $found['id'], 
  195.                             ]);  
  196.                             $noti = $translator->trans('msg.change_email_success');
  197.                         } else {
  198.                             $noti = $translator->trans('msg.api_error_try_later');
  199.                         }
  200.                     } else {
  201.                         $noti = $translator->trans('msg.email_was_existed');
  202.                     }
  203.                 } else {
  204.                     $noti = $translator->trans('msg.invalid_link');
  205.                 }
  206.             } else {
  207.                 $noti = $translator->trans('msg.user_nonexist_deactivated');
  208.             }
  209.         } else {
  210.             $noti = $translator->trans('msg.invalid_link');
  211.         }
  212.         return $this->render('registration/ecommerce/message.html.twig', [
  213.             'message' => $noti,
  214.         ]);
  215.     }
  216.     /**
  217.      * @Route("/contact-for-support", name="contact-for-support")
  218.      */
  219.     public function contactForSupport(Request $request, TranslatorInterface $translator, NotificationService $notificationService, CommonService $commonService)
  220.     {   
  221.         
  222.         $alert = ['error' => null, 'success' => null];
  223.         $listMachine = [
  224.             $translator->trans('contact_support.form.list_machine.your_configuration') => '',
  225.             $translator->trans('contact_support.form.list_machine.pc') => 'pc',
  226.             $translator->trans('contact_support.form.list_machine.mac') => 'mac',
  227.         ];
  228.         $listVersion = [
  229.             $translator->trans('contact_support.form.list_version.your_version') => '',
  230.             $translator->trans('contact_support.form.list_version.cloud') => 'cloud',
  231.             $translator->trans('contact_support.form.list_version.local') => 'local',
  232.         ];
  233.         $cloudQuestion = [
  234.             $translator->trans('contact_support.form.list_question.question') => '',
  235.             $translator->trans('contact_support.form.list_question.cloud.question_1') => 'question_1',
  236.             $translator->trans('contact_support.form.list_question.cloud.question_2') => 'question_2',
  237.             $translator->trans('contact_support.form.list_question.cloud.question_3') => 'question_3',
  238.             $translator->trans('contact_support.form.list_question.cloud.question_4') => 'question_4',
  239.             $translator->trans('contact_support.form.list_question.cloud.question_5') => 'question_5',
  240.         ];
  241.         $localQuestion = [
  242.             $translator->trans('contact_support.form.list_question.question') => '',
  243.             $translator->trans('contact_support.form.list_question.local.question_1') => 'question_1',
  244.             $translator->trans('contact_support.form.list_question.local.question_2') => 'question_2',
  245.             $translator->trans('contact_support.form.list_question.local.question_3') => 'question_3',
  246.             $translator->trans('contact_support.form.list_question.local.question_4') => 'question_4',
  247.             $translator->trans('contact_support.form.list_question.local.question_5') => 'question_5',
  248.         ];
  249.         $form = $this->createFormBuilder();
  250.         $form
  251.             ->add('email', EmailType::class, array('attr' => array('maxlength' => 255, 'placeholder' => $translator->trans('email'))))
  252.             ->add('first_name', TextType::class, array('attr' => array('maxlength' => 255, 'placeholder' => $translator->trans('first_name'))))
  253.             ->add('last_name', TextType::class, array('attr' => array('maxlength' => 255, 'placeholder' => $translator->trans('last_name'))))
  254.             ->add('company_name', TextType::class, array('attr' => array('maxlength' => 255, 'placeholder' => $translator->trans('company_name'))))
  255.             ->add('phone', TextType::class, array('attr' => array('maxlength' => 255, 'placeholder' => $translator->trans('phone'))))
  256.             ->add('country', ChoiceType::class, [
  257.                     'choices'  => [$translator->trans('country') => ''] + array_combine(array_keys(SubscriptionRepository::COUNTRYCODES),array_keys(SubscriptionRepository::COUNTRYCODES)),
  258.                     'attr' => array()
  259.                 ]
  260.             )
  261.             ->add('machine', ChoiceType::class, [
  262.                     'choices'  => $listMachine,
  263.                     'attr' => array()
  264.                 ]
  265.             )
  266.             ->add('version', ChoiceType::class, [
  267.                     'choices'  => $listVersion,
  268.                     'attr' => array()
  269.                 ]
  270.             )
  271.             ->add('cloud_question', ChoiceType::class, [
  272.                     'choices'  => $cloudQuestion,
  273.                     'attr' => array(),
  274.                     'data' => ''
  275.                 ]
  276.             )
  277.             ->add('local_question', ChoiceType::class, [
  278.                     'choices'  => $localQuestion,
  279.                     'attr' => array(),
  280.                     'data' => ''
  281.                 ]
  282.             )
  283.             ->add('question', TextareaType::class, array('attr' => array('placeholder' => $translator->trans('detail_problem'))))
  284.         ;
  285.         $form = $form->getForm();
  286.         
  287.         $form->handleRequest($request);
  288.         if ($form->isSubmitted() && $form->isValid()) {
  289.             $formData = $form->getData();
  290.             $product = $request->get('product');
  291.             $predefinedQuestion = '';
  292.             if ($formData['version'] == 'cloud') {
  293.                 $predefinedQuestion = $formData['cloud_question'];
  294.             }
  295.             if ($formData['version'] == 'local') {
  296.                 $predefinedQuestion = $formData['local_question'];
  297.             }
  298.             if ( ! $commonService->checkRecaptcha($request->get('g-recaptcha-response')) ) {
  299.                 $alert['error'] = $translator->trans('msg.error_not_fill_all');
  300.             } elseif ( !empty($formData['email']) && !empty($formData['first_name']) && !empty($formData['last_name']) && !empty($formData['company_name']) && !empty($formData['phone']) && !empty($formData['country']) && !empty($product) && !empty($formData['machine']) && !empty($formData['version']) && !empty($predefinedQuestion) && !empty($formData['question'])) {
  301.                 try {
  302.                     $formData['product'] = $product;
  303.                     $formData['predefined_question'] = $predefinedQuestion;
  304.                     $notificationService->sendQuestionForSupportTeam($formData);
  305.                     return $this->render('registration/ecommerce/message.html.twig', [
  306.                         'message' => $translator->trans('msg.contact_support_success', ['%url%' => $this->generateUrl('home-page')]),
  307.                     ]);
  308.                 } catch (\Throwable $th) {
  309.                     $alert['error'] = $translator->trans('msg.error');
  310.                 }
  311.             } else {
  312.                 $alert['error'] = $translator->trans('msg.error_not_fill_all');
  313.             }
  314.         }
  315.         return $this->render('support/contact-support.html.twig', [
  316.             'form' => $form->createView(),
  317.             'alert' => $alert,
  318.         ]);
  319.     }
  320.     /**
  321.      * @Route("/blog", name="blog-page")
  322.      */
  323.     public function blog()
  324.     {
  325.         return $this->render('homepage/blog.html.twig');
  326.     }
  327. }