<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Security;
use App\Entity\Account;
use App\Repository\AccountRepository;
use App\Entity\JobApplication;
use App\Form\JobApplicationType;
use App\Repository\JobApplicationRepository;
use Symfony\Component\HttpFoundation\Request;
use App\Entity\Jobs;
use App\Form\JobsType;
use App\Repository\JobsRepository;
use App\Form\AccountType;
use App\Entity\Rewards;
use App\Form\RewardsType;
use App\Repository\RewardsRepository;
use App\Entity\Referral;
use App\Repository\ReferralRepository;
use App\Repository\RewardSetupRepository;
use App\Repository\ProfileRepository;
use App\Entity\Profile;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Service\FileUploaderService;
use App\Entity\Employer;
use App\Service\EmployerService;
use App\Entity\InquiryLead;
use App\Form\InquiryLead1Type;
use App\Repository\InquiryLeadRepository;
use Swift_SmtpTransport;
use Swift_Mailer;
use Swift_Message;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class DefaultController extends AbstractController
{
private $security;
#[Route('/default', name: 'app_default')]
public function index(Request $request, InquiryLeadRepository $inquiryLeadRepository,
AccountRepository $accountRepository, Security $security, AuthenticationUtils $authenticationUtils): Response
{
$siteUser = $this->getUser();
$this ->security = $security;
$roles = null;
if ($this->security->isGranted('ROLE_EMPLOYER')) {
$roles = 'role_employer';
}
if ($this->security->isGranted('ROLE_EMPLOYEE')) {
$roles = 'role_employee';
}
// get the login error if there is one
$hostName = $_SERVER['HTTP_HOST'];
$baseHostName = $hostName;
$baseHostOne = str_replace('www.','',$baseHostName);
$hostName = str_replace('dev.','',$baseHostOne);
$hostName = explode('.', $hostName)[0];
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
// $siteUser = $this->getUser();
$inquiryLead = new InquiryLead();
$form = $this->createForm(InquiryLead1Type::class, $inquiryLead);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$inquiry = $form["message"]->getData();
$transport = (new Swift_SmtpTransport('smtp.sendgrid.net', 587, 'tls'))
->setUsername('apikey')
->setPassword('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');
// working!
// $transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
// ->setUsername('rdpatriciatolentino@gmail.com')
// ->setPassword('tbfw qinu owdd kzgx');
$mailer = (new Swift_Mailer($transport));
$message = (new Swift_Message('ERS Inquiry'))
->setFrom(['no-reply@recruitday.com'])
->setTo(['rdpatriciatolentino@gmail.com'])
->setBody($inquiry)
->setContentType('text/html');
$result = $mailer->send($message);
$inquiryLeadRepository->save($inquiryLead, true);
$this->addFlash('success', 'Thank you for your message. Our representatitve will contact you as soon as possible.');
}
if ($siteUser) {
return $this->renderForm('inquiry_lead/new.html.twig', [
'inquiry_lead' => $inquiryLead,
'form' => $form,
'siteUser' => $siteUser,
'roles' => $roles
]);
}
if (!$siteUser) {
return $this->renderForm('inquiry_lead/new.html.twig', [
'inquiry_lead' => $inquiryLead,
'form' => $form,
'siteUser' => $siteUser,
'roles' => $roles,
'hostName' => $hostName
]);
}
}
// #[Route('/default', name: 'app_default')]
// public function index( AccountRepository $accountRepository, Security $security, AuthenticationUtils $authenticationUtils): Response
// {
// $siteUser = $this->getUser();
// $this ->security = $security;
// $roles = null;
// if ($this->security->isGranted('ROLE_EMPLOYER')) {
// $roles = 'role_employer';
// }
// if ($this->security->isGranted('ROLE_EMPLOYEE')) {
// $roles = 'role_employee';
// }
// // get the login error if there is one
// $hostName = $_SERVER['HTTP_HOST'];
// $baseHostName = $hostName;
// $baseHostOne = str_replace('www.','',$baseHostName);
// $hostName = str_replace('dev.','',$baseHostOne);
// $hostName = explode('.', $hostName)[0];
// $error = $authenticationUtils->getLastAuthenticationError();
// // last username entered by the user
// $lastUsername = $authenticationUtils->getLastUsername();
// if ($siteUser) {
// // ----change When the homepage is no longer coming soon-----
// // return $this->render('security/homepage.html.twig', [
// return $this->render('default/coming_soon.html.twig', [
// 'controller_name' => 'DefaultController',
// 'siteUser' => $siteUser,
// 'roles' => $roles,
// ]);
// }
// if (!$siteUser) {
// // ----change When the homepage is no longer coming soon-----
// // return $this->render('default/index.html.twig', [
// return $this->render('default/coming_soon.html.twig', [
// 'controller_name' => 'DefaultController',
// 'siteUser' => $siteUser,
// 'roles' => $roles,
// 'hostName' => $hostName
// ]);
// }
// }
#[Route('/job/referral/{slug}', name: 'job_referral', methods: ['GET', 'POST'])]
public function referJob(Request $request, Jobs $job, AccountRepository $accountRepository, JobsRepository $jobsRepository, EmployerService $employerService): Response
{
$referralId = $request->query->get('ref');
$jobId = $request->query->get('jobId');
$account = $accountRepository->findOneBy([
'id' => $referralId
]);
$employerId = $account->getEmployerId();
$companyLogo = $employerService->getCompanyLogoById($employerId);
// -----if job is deactivated------------
$jobStatusName = array("draft"=>"0","inactive"=>"2");
$jobStatus = $job->getStatus();
if (in_array($jobStatus, $jobStatusName)) {
$this->addFlash('notAvailable', 'Unfortunately! This job is unavailable.');
}
return $this->render('default/job_application_apply.html.twig', [
'job' => $job,
'referralId' => $referralId,
'jobsRepository' => $jobsRepository,
'companyLogo' => $companyLogo,
'employerId' => $employerId
]);
}
#[Route('/apply', name: 'apply', methods: ['GET', 'POST'])]
public function apply(Request $request, JobApplicationRepository $jobApplicationRepository, ManagerRegistry $doctrine,
ReferralRepository $referralRepository, RewardSetupRepository $rewardSetupRepository, JobsRepository $jobsRepository, AccountRepository $accountRepository, ProfileRepository $profileRepository,
FileUploaderService $fileUploaderService, RewardsRepository $rewardsRepository): Response
{
$entityManager = $doctrine->getManager();
$referralId = $request->query->get('ref');
$referral = $referralRepository->findOneBy(['account_id'=> $referralId]);
$jobId = $request->query->get('id');
$job = $jobsRepository->findOneById($jobId);
$jobApplication = new JobApplication();
$form = $this->createForm(JobApplicationType::class, $jobApplication);
$form->handleRequest($request);
$rewardsetup = $rewardSetupRepository->findOneBy([
'job' => $job,
'application_status' => 0
]);
$hostName = $_SERVER['HTTP_HOST'];
$referralLink = $hostName . $this->generateUrl('job_referral', array(
'slug' => $job->getSlug(),
'ref' => $referral->getAccountId()
));
if ($form->isSubmitted() && $form->isValid()) {
$email = $form["email_address"]->getData();
$jobseeker = $form["first_name"]->getData() . ' ' . $form["last_name"]->getData();
$existingAccount = $accountRepository->findOneBy(['email' => $email]);
$existingAccountAndApplication = $jobApplicationRepository->findOneBy([
'email_address' => $email,
'job' => $job
]);
$jobApplication->setDateUpdated(New \DateTime());
if($existingAccountAndApplication) {
$this->addFlash('error', 'Cannot proceed. You already have an active application to this job');
return $this->redirectToRoute('apply', ['id' => $job->getId(), 'ref' => $referral->getAccountId()]);
} elseif ($existingAccount) {
//create job application record
$jobApplication->setChannel(0);
$jobApplication->setApplicationSource(0);
$jobApplication->setStatus(0);
$jobApplication->setReferrer($referral->getAccountId());
$jobApplication->setDateCreated(new \DateTime());
$jobApplication->setAccountId($existingAccount->getId());
$jobApplication->setJob($job);
$jobApplication->setReferral($referral);
$entityManager->persist($jobApplication);
$entityManager->flush();
//start: sends an email to referrer when someone applied to their referral link
$transport = (new Swift_SmtpTransport('smtp.sendgrid.net', 587, 'tls'))
->setUsername('apikey')
->setPassword('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');
$mailer = (new Swift_Mailer($transport));
$referrerId = $accountRepository->findOneById($jobApplication->getReferrer());
$referrerEmail = $referrerId->getEmail();
$loader = new FilesystemLoader(__DIR__ . '/../../templates/');
$twig = new Environment($loader);
$applicationDate = $jobApplication->getDateCreated()->format('M d, Y');
$htmlContent = $twig->render('email_notifications/new_referral.html.twig', [
'referrer_name' => $referrerId->getFirstName(),
'referral_id' => $jobApplication->getId(),
'referral' => $jobApplication->getFirstname() . ' ' . $jobApplication->getLastName(),
'job' => $job->getJobTitle(),
'application_date' => $applicationDate,
]);
$message = (new Swift_Message('If job application has a reward'))
->setFrom(['no-reply@recruitday.com'])
// ->setTo(['rdpatriciatolentino@gmail.com']) //change to referrer email address
->setTo($referrerEmail) //change to referrer email address
->setBody($htmlContent, 'text/html');
$result = $mailer->send($message);
if ($result) {
echo "Email sent successfully!";
} else {
echo "Failed to send email!";
}
//end: sends an email to referrer when someone applied to their referral link
$account = $accountRepository->findOneBy(['id' => $existingAccount->getId()]);
$job = $jobsRepository->findOneBy(['id' => $jobId]);
//create profile record
$profile = $profileRepository->findOneBy(['account_id' => $existingAccount->getId()]);
if($profile) {
// upload file
/**
* @var UploadedFile $file
*/
// $file = $profile->getResume();
$file = $form->get('resume')->getData();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
// $fileName = ("test");
$file->move('uploads',
$fileName
);
$profile->setResume($fileName);
$entityManager->persist($profile);
$entityManager->flush();
$profileRepository->save($profile, true);
}
// start: create reward record
if(!is_null($rewardsetup)) {
$reward = new Rewards();
$reward->setApplicationStatus(0);
$reward->setJobTitle($job->getJobTitle());
$reward->setJobseeker($jobseeker);
$reward->setRewardStatus(1);
$reward->setReward($rewardsetup->getReward());
$reward->setNonCashReward($rewardsetup->getNonCashReward());
$reward->setReferralId($referral->getId());
$reward->setReferrer($referral->getAccountId());
$reward->setRewardSetup($rewardsetup);
$entityManager->persist($reward);
$entityManager->flush();
$rewardsRepository->save($reward, true);
}
// end: create reward record
} else {
//create account record
$account = new Account();
$account->setFirstName($form["first_name"]->getData());
$account->setLastName($form["last_name"]->getData());
$account->setEmail($form["email_address"]->getData());
$entityManager->persist($account);
$entityManager->flush();
$accountRepository->save($account, true);
$accountid = $accountRepository->findOneBy(['id' => $account->getId()]);
//create profile record
$profile = new Profile();
$profile->setAccount($accountid);
// upload file
/**
* @var UploadedFile $file
*/
// $file = $profile->getResume();
$file = $form->get('resume')->getData();
$fileName = md5(uniqid()).'.'.$file->guessExtension();
// $fileName = ("test");
$file->move('uploads',
$fileName
);
$profile->setResume($fileName);
$entityManager->persist($profile);
$entityManager->flush();
$profileRepository->save($profile, true);
//create job application record
$jobApplication->setChannel(0);
$jobApplication->setApplicationSource(0);
$jobApplication->setStatus(0);
$jobApplication->setReferrer($referral->getAccountId());
$jobApplication->setDateCreated(new \DateTime());
$jobApplication->setAccountId($account->getId());
$jobApplication->setJob($job);
$jobApplication->setReferral($referral);
$entityManager->persist($jobApplication);
$entityManager->flush();
$job = $jobsRepository->findOneBy(['id' => $jobId]);
//start: sends an email to referrer when someone applied to their referral link
$transport = (new Swift_SmtpTransport('smtp.sendgrid.net', 587, 'tls'))
->setUsername('apikey')
->setPassword('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');
$mailer = (new Swift_Mailer($transport));
$referrerId = $accountRepository->findOneById($jobApplication->getReferrer());
$referrerEmail = $referrerId->getEmail();
$loader = new FilesystemLoader(__DIR__ . '/../../templates/');
$twig = new Environment($loader);
$applicationDate = $jobApplication->getDateCreated()->format('M d, Y');
$htmlContent = $twig->render('email_notifications/new_referral.html.twig', [
'referrer_name' => $referrerId->getFirstName(),
'referral_id' => $jobApplication->getId(),
'referral' => $jobApplication->getFirstname() . ' ' . $jobApplication->getLastName(),
'job' => $job->getJobTitle(),
'application_date' => $applicationDate,
]);
$message = (new Swift_Message('Someone applied to your referral link'))
->setFrom(['no-reply@recruitday.com'])
// ->setTo(['rdpatriciatolentino@gmail.com']) //change to referrer email address
->setTo($referrerEmail) //change to referrer email address
->setBody($htmlContent)
->setContentType('text/html');
$result = $mailer->send($message);
//end: sends an email to referrer when someone applied to their referral link
// start: create reward record
if(!is_null($rewardsetup)) {
$reward = new Rewards();
$reward->setApplicationStatus(0);
$reward->setJobTitle($job->getJobTitle());
$reward->setJobseeker($jobseeker);
$reward->setRewardStatus(0);
$reward->setReward($rewardsetup->getReward());
$reward->setNonCashReward($rewardsetup->getNonCashReward());
$reward->setReferralId($referral->getId());
$reward->setReferrer($referral->getAccountId());
$reward->setRewardSetup($rewardsetup);
$entityManager->persist($reward);
$entityManager->flush();
$rewardsRepository->save($reward, true);
}
// end: create reward record
}
$this->addFlash('success', 'Awesome! Your application was successfully submitted.');
return $this->redirectToRoute('apply', ['id' => $job->getId(), 'ref' => $referral->getAccountId()]);
// return $this->redirectToRoute('app_default', [], Response::HTTP_SEE_OTHER);
}
// -----if job is deactivated------------
$jobStatusName = array("draft"=>"0","inactive"=>"2");
$jobStatus = $job->getStatus();
if (in_array($jobStatus, $jobStatusName)) {
return $this->redirectToRoute('job_referral', ['slug' => $job->getSlug(), 'ref' => $referral->getAccountId()]);
}
return $this->renderForm('default/apply.html.twig', [
'controller_name' => 'DefaultController',
'form' => $form,
'referralLink' => $referralLink,
]);
}
}