src/Controller/DefaultController.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\Security\Core\Security;
  8. use App\Entity\Account;
  9. use App\Repository\AccountRepository;
  10. use App\Entity\JobApplication;
  11. use App\Form\JobApplicationType;
  12. use App\Repository\JobApplicationRepository;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use App\Entity\Jobs;
  15. use App\Form\JobsType;
  16. use App\Repository\JobsRepository;
  17. use App\Form\AccountType;
  18. use App\Entity\Rewards;
  19. use App\Form\RewardsType;
  20. use App\Repository\RewardsRepository;
  21. use App\Entity\Referral;
  22. use App\Repository\ReferralRepository;
  23. use App\Repository\RewardSetupRepository;
  24. use App\Repository\ProfileRepository;
  25. use App\Entity\Profile;
  26. use Symfony\Component\Security\Core\User\UserInterface;
  27. use Doctrine\Persistence\ManagerRegistry;
  28. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  29. use App\Service\FileUploaderService;
  30. use App\Entity\Employer;
  31. use App\Service\EmployerService;
  32. use App\Entity\InquiryLead;
  33. use App\Form\InquiryLead1Type;
  34. use App\Repository\InquiryLeadRepository;
  35. use Swift_SmtpTransport;
  36. use Swift_Mailer;
  37. use Swift_Message;
  38. use Twig\Environment;
  39. use Twig\Loader\FilesystemLoader;
  40. class DefaultController extends AbstractController
  41. {   
  42.     private $security;
  43.     
  44.     #[Route('/default'name'app_default')]
  45.     public function index(Request $requestInquiryLeadRepository $inquiryLeadRepository
  46.     AccountRepository $accountRepositorySecurity $securityAuthenticationUtils $authenticationUtils): Response
  47.     {
  48.     
  49.         
  50.         $siteUser $this->getUser();
  51.         $this ->security $security;
  52.         $roles null;
  53.         if ($this->security->isGranted('ROLE_EMPLOYER')) {
  54.             $roles 'role_employer';
  55.         }
  56.         if ($this->security->isGranted('ROLE_EMPLOYEE')) {
  57.             $roles =  'role_employee';
  58.         }
  59.         // get the login error if there is one
  60.         $hostName $_SERVER['HTTP_HOST'];
  61.         $baseHostName $hostName;
  62.         $baseHostOne str_replace('www.','',$baseHostName);
  63.         $hostName str_replace('dev.','',$baseHostOne);
  64.         $hostName explode('.'$hostName)[0];
  65.         $error $authenticationUtils->getLastAuthenticationError();
  66.         // last username entered by the user
  67.         $lastUsername $authenticationUtils->getLastUsername();
  68.         
  69.         // $siteUser = $this->getUser();
  70.         $inquiryLead = new InquiryLead();
  71.         $form $this->createForm(InquiryLead1Type::class, $inquiryLead);
  72.         $form->handleRequest($request);
  73.         if ($form->isSubmitted() && $form->isValid()) {
  74.             
  75.             $inquiry $form["message"]->getData();
  76.             $transport = (new Swift_SmtpTransport('smtp.sendgrid.net'587'tls'))
  77.             ->setUsername('apikey')
  78.             ->setPassword('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');
  79.         
  80.             // working!
  81.             // $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
  82.             // ->setUsername('rdpatriciatolentino@gmail.com')
  83.             // ->setPassword('tbfw qinu owdd kzgx');
  84.             $mailer = (new Swift_Mailer($transport));
  85.             
  86.           
  87.             $message = (new Swift_Message('ERS Inquiry'))
  88.                     ->setFrom(['no-reply@recruitday.com'])
  89.                     ->setTo(['rdpatriciatolentino@gmail.com'])
  90.                     ->setBody($inquiry)
  91.                     ->setContentType('text/html');
  92.             $result $mailer->send($message);
  93.             $inquiryLeadRepository->save($inquiryLeadtrue);       
  94.             $this->addFlash('success''Thank you for your message. Our representatitve will contact you as soon as possible.');
  95.             
  96.         }
  97.         if ($siteUser) {
  98.             return $this->renderForm('inquiry_lead/new.html.twig', [
  99.                 'inquiry_lead' => $inquiryLead,
  100.                 'form' => $form,
  101.                 'siteUser' => $siteUser,
  102.                 'roles' => $roles
  103.             ]);
  104.         } 
  105.         if (!$siteUser) {
  106.             return $this->renderForm('inquiry_lead/new.html.twig', [
  107.                 'inquiry_lead' => $inquiryLead,
  108.                 'form' => $form,
  109.                 'siteUser' => $siteUser,
  110.                 'roles' => $roles,
  111.                 'hostName' => $hostName
  112.             ]);
  113.         } 
  114.     
  115.     }  
  116.     //    #[Route('/default', name: 'app_default')]
  117.     //    public function index( AccountRepository $accountRepository, Security $security, AuthenticationUtils $authenticationUtils): Response
  118.     //    {
  119.     //     $siteUser = $this->getUser();
  120.     //     $this ->security = $security;
  121.     //     $roles = null;
  122.     //     if ($this->security->isGranted('ROLE_EMPLOYER')) {
  123.     //         $roles = 'role_employer';
  124.     //     }
  125.     //     if ($this->security->isGranted('ROLE_EMPLOYEE')) {
  126.     //         $roles =  'role_employee';
  127.     //     }
  128.     //     // get the login error if there is one
  129.     //     $hostName = $_SERVER['HTTP_HOST'];
  130.     //     $baseHostName = $hostName;
  131.     //     $baseHostOne = str_replace('www.','',$baseHostName);
  132.     //     $hostName = str_replace('dev.','',$baseHostOne);
  133.     //     $hostName = explode('.', $hostName)[0];
  134.     //     $error = $authenticationUtils->getLastAuthenticationError();
  135.     //     // last username entered by the user
  136.     //     $lastUsername = $authenticationUtils->getLastUsername();
  137.         
  138.         
  139.     //     if ($siteUser) {
  140.     //         // ----change When the homepage is no longer coming soon-----
  141.     //         // return $this->render('security/homepage.html.twig', [
  142.     //         return $this->render('default/coming_soon.html.twig', [
  143.     //             'controller_name' => 'DefaultController',
  144.     //             'siteUser' => $siteUser,
  145.     //             'roles' => $roles,
  146.     //         ]);
  147.     //     }
  148.     //     if (!$siteUser) {
  149.     //          // ----change When the homepage is no longer coming soon-----
  150.     //         // return $this->render('default/index.html.twig', [
  151.     //         return $this->render('default/coming_soon.html.twig', [
  152.     //             'controller_name' => 'DefaultController',
  153.     //             'siteUser' => $siteUser,
  154.     //             'roles' => $roles,
  155.     //             'hostName' => $hostName
  156.     //         ]);
  157.     //     }
  158.     // }
  159.     #[Route('/job/referral/{slug}'name'job_referral'methods: ['GET''POST'])]
  160.     public function referJob(Request $requestJobs $jobAccountRepository $accountRepositoryJobsRepository $jobsRepositoryEmployerService $employerService): Response
  161.     {   
  162.         $referralId $request->query->get('ref');
  163.         $jobId $request->query->get('jobId');
  164.         $account $accountRepository->findOneBy([
  165.             'id' => $referralId
  166.         ]);
  167.         $employerId $account->getEmployerId();
  168.         $companyLogo $employerService->getCompanyLogoById($employerId);
  169.         // -----if job is deactivated------------
  170.         $jobStatusName = array("draft"=>"0","inactive"=>"2");
  171.         $jobStatus $job->getStatus();
  172.         if (in_array($jobStatus$jobStatusName)) {
  173.             $this->addFlash('notAvailable''Unfortunately! This job is unavailable.');
  174.         }
  175.         
  176.         return $this->render('default/job_application_apply.html.twig', [
  177.             'job' => $job,
  178.             'referralId' => $referralId,
  179.             'jobsRepository' => $jobsRepository,
  180.             'companyLogo' => $companyLogo,
  181.             'employerId' => $employerId
  182.         ]);
  183.     }
  184.     #[Route('/apply'name'apply'methods: ['GET''POST'])]
  185.     public function apply(Request $request,  JobApplicationRepository $jobApplicationRepositoryManagerRegistry $doctrine
  186.     ReferralRepository $referralRepository,  RewardSetupRepository $rewardSetupRepositoryJobsRepository $jobsRepositoryAccountRepository $accountRepositoryProfileRepository $profileRepository,
  187.     FileUploaderService $fileUploaderServiceRewardsRepository $rewardsRepository): Response
  188.     {   
  189.         
  190.         $entityManager $doctrine->getManager();
  191.         
  192.         $referralId $request->query->get('ref');
  193.         $referral $referralRepository->findOneBy(['account_id'=> $referralId]);
  194.         $jobId $request->query->get('id');
  195.         $job $jobsRepository->findOneById($jobId);
  196.         $jobApplication = new JobApplication();
  197.         $form $this->createForm(JobApplicationType::class, $jobApplication);
  198.         $form->handleRequest($request);
  199.         $rewardsetup $rewardSetupRepository->findOneBy([
  200.             'job' => $job
  201.             'application_status' => 0
  202.         ]);
  203.         $hostName $_SERVER['HTTP_HOST'];
  204.         $referralLink $hostName $this->generateUrl('job_referral', array(
  205.             'slug' =>  $job->getSlug(), 
  206.             'ref' => $referral->getAccountId()
  207.          ));
  208.         if ($form->isSubmitted() && $form->isValid()) {
  209.             $email $form["email_address"]->getData();
  210.             
  211.             $jobseeker $form["first_name"]->getData() . ' ' $form["last_name"]->getData();
  212.             $existingAccount $accountRepository->findOneBy(['email' => $email]);
  213.             $existingAccountAndApplication $jobApplicationRepository->findOneBy([
  214.                 'email_address' => $email,
  215.                 'job' => $job
  216.             ]);
  217.             $jobApplication->setDateUpdated(New \DateTime());
  218.             if($existingAccountAndApplication) {
  219.                 $this->addFlash('error''Cannot proceed. You already have an active application to this job');
  220.                 return $this->redirectToRoute('apply', ['id' => $job->getId(), 'ref' => $referral->getAccountId()]);
  221.             } elseif ($existingAccount) {
  222.         
  223.                 //create job application record
  224.                 $jobApplication->setChannel(0);
  225.                 $jobApplication->setApplicationSource(0);
  226.                 $jobApplication->setStatus(0);
  227.                 $jobApplication->setReferrer($referral->getAccountId());
  228.                 $jobApplication->setDateCreated(new \DateTime());
  229.                 $jobApplication->setAccountId($existingAccount->getId());
  230.                 $jobApplication->setJob($job);
  231.                 $jobApplication->setReferral($referral);
  232.                 
  233.                 $entityManager->persist($jobApplication);
  234.                 $entityManager->flush();
  235.                 //start: sends an email to referrer when someone applied to their referral link
  236.                 $transport = (new Swift_SmtpTransport('smtp.sendgrid.net'587'tls'))
  237.                 ->setUsername('apikey')
  238.                 ->setPassword('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');
  239.                 
  240.                 $mailer = (new Swift_Mailer($transport));
  241.                 $referrerId $accountRepository->findOneById($jobApplication->getReferrer());
  242.                 $referrerEmail $referrerId->getEmail();
  243.                 $loader = new FilesystemLoader(__DIR__ '/../../templates/');
  244.                 $twig = new Environment($loader);
  245.                 $applicationDate $jobApplication->getDateCreated()->format('M d, Y');
  246.                 $htmlContent $twig->render('email_notifications/new_referral.html.twig', [
  247.                     'referrer_name' => $referrerId->getFirstName(),
  248.                     'referral_id' => $jobApplication->getId(),
  249.                     'referral' => $jobApplication->getFirstname() . ' ' $jobApplication->getLastName(),
  250.                     'job' => $job->getJobTitle(),
  251.                     'application_date' => $applicationDate,
  252.                 ]);
  253.                 
  254.                 $message = (new Swift_Message('If job application has a reward'))
  255.                         ->setFrom(['no-reply@recruitday.com'])
  256.                         // ->setTo(['rdpatriciatolentino@gmail.com']) //change to referrer email address
  257.                         ->setTo($referrerEmail//change to referrer email address
  258.                         ->setBody($htmlContent'text/html');
  259.                                 
  260.                 $result $mailer->send($message);
  261.                 if ($result) {
  262.                     echo "Email sent successfully!";
  263.                 } else {
  264.                     echo "Failed to send email!";
  265.                 }
  266.                 
  267.                 //end: sends an email to referrer when someone applied to their referral link
  268.         
  269.                 
  270.                 
  271.                 $account $accountRepository->findOneBy(['id' => $existingAccount->getId()]);
  272.                 $job $jobsRepository->findOneBy(['id' => $jobId]);
  273.                 
  274.                 //create profile record
  275.                 $profile $profileRepository->findOneBy(['account_id' => $existingAccount->getId()]);
  276.                 if($profile) {
  277.                     // upload file
  278.                     /**
  279.                      * @var UploadedFile $file
  280.                      */
  281.                     // $file = $profile->getResume();
  282.                     $file $form->get('resume')->getData();
  283.                     
  284.                     $fileName md5(uniqid()).'.'.$file->guessExtension();
  285.                     // $fileName = ("test");
  286.                     $file->move('uploads',
  287.                         $fileName
  288.                     );
  289.                     $profile->setResume($fileName);
  290.                     
  291.                     $entityManager->persist($profile);
  292.                     $entityManager->flush();
  293.                     $profileRepository->save($profiletrue);
  294.                 }
  295.                  // start: create reward record
  296.                 if(!is_null($rewardsetup)) {
  297.                     $reward = new Rewards(); 
  298.                     $reward->setApplicationStatus(0);
  299.                     $reward->setJobTitle($job->getJobTitle());
  300.                     $reward->setJobseeker($jobseeker);
  301.                     $reward->setRewardStatus(1);
  302.                     $reward->setReward($rewardsetup->getReward());
  303.                     $reward->setNonCashReward($rewardsetup->getNonCashReward());
  304.                     $reward->setReferralId($referral->getId());
  305.                     $reward->setReferrer($referral->getAccountId());
  306.                     $reward->setRewardSetup($rewardsetup);
  307.                     
  308.                     $entityManager->persist($reward);
  309.                     $entityManager->flush();
  310.                     
  311.                     $rewardsRepository->save($rewardtrue);
  312.                 }
  313.                 // end: create reward record
  314.             }  else {
  315.                 //create account record
  316.                 $account = new Account();
  317.                 $account->setFirstName($form["first_name"]->getData());
  318.                 $account->setLastName($form["last_name"]->getData());
  319.                 $account->setEmail($form["email_address"]->getData());
  320.                 
  321.                 $entityManager->persist($account);
  322.                 $entityManager->flush();
  323.         
  324.                 $accountRepository->save($accounttrue);
  325.                 $accountid $accountRepository->findOneBy(['id' => $account->getId()]);
  326.                 
  327.                 //create profile record
  328.                 $profile = new Profile();
  329.                 $profile->setAccount($accountid);
  330.                                 
  331.                 // upload file
  332.                 /**
  333.                  * @var UploadedFile $file
  334.                  */
  335.                 // $file = $profile->getResume();
  336.                 $file $form->get('resume')->getData();
  337.                 $fileName md5(uniqid()).'.'.$file->guessExtension();
  338.                 // $fileName = ("test");
  339.                 
  340.                 $file->move('uploads',
  341.                     $fileName
  342.                 );
  343.                 $profile->setResume($fileName);
  344.                 $entityManager->persist($profile);
  345.                 $entityManager->flush();
  346.                 $profileRepository->save($profiletrue);
  347.                 
  348.                  //create job application record
  349.                  $jobApplication->setChannel(0);
  350.                  $jobApplication->setApplicationSource(0);
  351.                  $jobApplication->setStatus(0);
  352.                  $jobApplication->setReferrer($referral->getAccountId());
  353.                  $jobApplication->setDateCreated(new \DateTime());
  354.                  $jobApplication->setAccountId($account->getId()); 
  355.                  $jobApplication->setJob($job);
  356.                  $jobApplication->setReferral($referral);
  357.                 
  358.                  $entityManager->persist($jobApplication);
  359.                  $entityManager->flush();
  360.          
  361.                 $job $jobsRepository->findOneBy(['id' => $jobId]);
  362.                  //start: sends an email to referrer when someone applied to their referral link
  363.                  $transport = (new Swift_SmtpTransport('smtp.sendgrid.net'587'tls'))
  364.                  ->setUsername('apikey')
  365.                  ->setPassword('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');
  366.                  
  367.                  $mailer = (new Swift_Mailer($transport));
  368.  
  369.                  $referrerId $accountRepository->findOneById($jobApplication->getReferrer());
  370.                  $referrerEmail $referrerId->getEmail();
  371.                  $loader = new FilesystemLoader(__DIR__ '/../../templates/');
  372.                  $twig = new Environment($loader);
  373.                  
  374.                 $applicationDate $jobApplication->getDateCreated()->format('M d, Y');
  375.                 $htmlContent $twig->render('email_notifications/new_referral.html.twig', [
  376.                     'referrer_name' => $referrerId->getFirstName(),
  377.                     'referral_id' => $jobApplication->getId(),
  378.                     'referral' => $jobApplication->getFirstname() . ' ' $jobApplication->getLastName(),
  379.                     'job' => $job->getJobTitle(),
  380.                     'application_date' => $applicationDate,
  381.                 ]);
  382.                  
  383.                 $message = (new Swift_Message('Someone applied to your referral link'))
  384.                          ->setFrom(['no-reply@recruitday.com'])
  385.                         //  ->setTo(['rdpatriciatolentino@gmail.com']) //change to referrer email address
  386.                          ->setTo($referrerEmail//change to referrer email address
  387.                          ->setBody($htmlContent)
  388.                          ->setContentType('text/html');
  389.                                         
  390.                 $result $mailer->send($message);
  391.                  
  392.                  //end: sends an email to referrer when someone applied to their referral link
  393.                 
  394.                   // start: create reward record
  395.                 if(!is_null($rewardsetup)) {
  396.                     $reward = new Rewards(); 
  397.                     $reward->setApplicationStatus(0);
  398.                     $reward->setJobTitle($job->getJobTitle());
  399.                     $reward->setJobseeker($jobseeker);
  400.                     $reward->setRewardStatus(0);
  401.                     $reward->setReward($rewardsetup->getReward());
  402.                     $reward->setNonCashReward($rewardsetup->getNonCashReward());
  403.                     $reward->setReferralId($referral->getId());
  404.                     $reward->setReferrer($referral->getAccountId());
  405.                     $reward->setRewardSetup($rewardsetup);
  406.                     
  407.                     $entityManager->persist($reward);
  408.                     $entityManager->flush();
  409.                     
  410.                     $rewardsRepository->save($rewardtrue);
  411.                 }
  412.                 // end: create reward record
  413.             }
  414.             $this->addFlash('success''Awesome! Your application was successfully submitted.');
  415.             return $this->redirectToRoute('apply', ['id' => $job->getId(), 'ref' => $referral->getAccountId()]);
  416.             // return $this->redirectToRoute('app_default', [], Response::HTTP_SEE_OTHER);
  417.         }
  418.         // -----if job is deactivated------------
  419.         $jobStatusName = array("draft"=>"0","inactive"=>"2");
  420.         $jobStatus $job->getStatus();
  421.         if (in_array($jobStatus$jobStatusName)) {
  422.             return $this->redirectToRoute('job_referral', ['slug' => $job->getSlug(), 'ref' => $referral->getAccountId()]);
  423.         }
  424.         
  425.         return $this->renderForm('default/apply.html.twig', [
  426.             'controller_name' => 'DefaultController',
  427.             'form' => $form,
  428.             'referralLink' => $referralLink,
  429.         ]);
  430.     }
  431.   
  432. }