<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\UserDetails;
use App\Utils\Utils;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\DBAL\Connection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\PersistentCollection;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Utils\ResultGenerator;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Translation\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;
use Swift_Mailer;
/**
* About controller.
*
* @Route("about")
*/
class AboutController extends AbstractController
{
//private $fromEmail = "inquiries@guardingmindsatwork.ca";
//private $mailHost = "localhost";
//private $mailUser = "test@guardingmindsatwork.ca";
//private $mailPassword = "qg3bvia8";
protected $translator;
protected $mailer;
protected $passwordHasher;
public function __construct(TranslatorInterface $translator, Swift_Mailer $mailer, UserPasswordHasherInterface $passwordHasher)
{
$this->translator = $translator;
$this->mailer = $mailer;
$this->passwordHasher = $passwordHasher;
}
/**
* @Route("/about", name="about")
*/
public function aboutAction(Request $request)
{
return $this->render('about/about.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-safety", name="about_safety")
*/
public function whatsafetyAction(Request $request)
{
return $this->render('about/safety-what.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-psychosocial-factors", name="about_factors")
*/
public function aboutFactorsAction(Request $request)
{
return $this->render('about/1-psychosocial-factors.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR ]);
}
/**
* @Route("/about-needs", name="about_needs")
*/
public function aboutNeedsAction(Request $request)
{
return $this->render('about/2-identify-needs.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-resources", name="about_resources")
*/
public function aboutResourcesAction(Request $request)
{
return $this->render('about/3-determine-resources.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-participants", name="about_participants")
*/
public function aboutParticipantsAction(Request $request)
{
return $this->render('about/4-identify-participants.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-plan", name="about_plan")
*/
public function aboutPlanAction(Request $request)
{
return $this->render('about/5-create-plan.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-signup", name="about_signup")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function signupAction(Request $request, Connection $conn, SessionInterface $session)
{
$user = new User();
$userDetails = new UserDetails();
$userDetails->init($user, $conn);
$utils = new Utils();
$utils->init($user, $conn);
$msg = "";
if (sizeof($request->request) > 0) {
if ($request->request->get('emailAddress') != '' && $userDetails->getActivcationIDByEmailAddress($request->request->get('emailAddress')) === false) {
// encode the user password
$password = $this->passwordHasher->hashPassword($user, $request->request->get('password'));
$user->setPassword($password);
$user->setUserName($request->request->get('emailAddress'));
$user->setRole("ROLE_USER");
$activation_id = uniqid(rand());
$user->setActivationId($activation_id);
$user->setLanguageId($userDetails->getLanguageId($request->getLocale()));
// save the user
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$userInfoArr = array( 'user_id' => $user->getUserId(),
'first_name' => $request->request->get('firstName'),
'last_name' => $request->request->get('lastName'),
'personal_title' => $request->request->get('personalTitle'),
'job_title' => $request->request->get('jobTitle'),
'department' => $request->request->get('department'),
'phone_number' => $request->request->get('phoneNumber'),
'street_address' => $request->request->get('address'),
'city_name' => $request->request->get('city'),
'province' => $request->request->get('province'),
'postal_code' => $request->request->get('postalCode'),
'iso_country' => $request->request->get('country'),
'allow_share_data' => $request->request->get('termsAccepted') ? 1 : 0);
$companyInfoArr = array( 'user_id' => $user->getUserId(),
'company_name' => $request->request->get('organization'),
'employees_in_org' => $request->request->get('employeesInOrg'),
'org_type' => $request->request->get('orgType'),
'sector' => $request->request->get('sector'),
'union_status' => $request->request->get('unionStatus'),
'company_category_id' => $request->request->get('companyCategoryId') );
$userDetails->addUserDetails($userInfoArr, $companyInfoArr);
$session = new Session();
$session->set('signup_email_address', $request->request->get('emailAddress'));
return $this->redirectToRoute('about_signup_thankyou');
} else {
$msg = "emailAlreadyExists";
}
}
$countries = $userDetails->getCountryData($userDetails->getLanguageId($request->getLocale()));
$provinces = $userDetails->getProvinceData($userDetails->getLanguageId($request->getLocale()));
$companyCategory = $userDetails->getCompanyCategory($userDetails->getLanguageId($request->getLocale()));
return $this->render('about/6-signup.html.twig', [ 'countries' => $countries,
'provinces' => $provinces,
'company_cat' => $companyCategory,
'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'msg' => $msg ]);
}
/**
* @Route("/about-signup-thankyou", name="about_signup_thankyou")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function aboutSignUpThankYou(Request $request, SessionInterface $session, Connection $conn, \Swift_Mailer $mailer)
{
$session = new Session();
$signup_email_address = $session->get('signup_email_address');
$locale = $request->getLocale();
if ($signup_email_address != '') {
$session->set('signup_email_address', '');
$user = new User();
$userDetails = new UserDetails();
$userDetails->init($user, $conn);
$activation_code = $userDetails->getActivcationIDByEmailAddress($signup_email_address);
// MAYBE USED FOR GETTING CONTAINER INFO?
//$transport = $this->container()->get('swiftmailer.transport.real');
if ($locale == 'en') {
$fromEmail = $this->getParameter('mailer_from_user');
} elseif ($locale == 'fr') {
$fromEmail = $this->getParameter('mailer_from_user_fr');
} else {
$fromEmail = $this->getParameter('mailer_from_user');
}
$message = (new \Swift_Message($this->translator->trans('confirm-email')))
->setFrom($fromEmail)
->setTo($signup_email_address)
->setBody(
$this->renderView(
'about/10-activation-email.html.twig',
array( 'activation_code' => $activation_code)
),
'text/html'
);
$this->mailer->send($message);
return $this->render('about/9-about-signup-thankyou.html.twig', [ 'signup_email_address' => $signup_email_address ]);
} else {
return $this->redirectToRoute('about_signup');
}
}
/**
* @Route("/about-signup-activation/{activation_code}", name="about_signup_activation")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function aboutSignUpActivation(Request $request, $activation_code, Connection $conn, \Swift_Mailer $mailer)
{
$user = new User();
$userDetails = new UserDetails();
$userDetails->init($user, $conn);
$locale = $request->getLocale();
if ($activation_code != '') {
$activated_email = $userDetails->activateUser($activation_code);
if ($activated_email != "") {
if ($locale == 'en') {
$fromEmail = $this->getParameter('mailer_from_user');
} elseif ($locale == 'fr') {
$fromEmail = $this->getParameter('mailer_from_user_fr');
} else {
$fromEmail = $this->getParameter('mailer_from_user');
}
$message = (new \Swift_Message($this->translator->trans('email-welcome-chw')) )
->setFrom($fromEmail)
->setTo($activated_email)
->setBody(
$this->renderView(
'about/11-activation-confirmed-email.html.twig',
array( 'email' => $activated_email)
),
'text/html'
);
$this->mailer->send($message);
} else {
return $this->redirectToRoute('login');
}
}
return $this->render('default/login.html.twig', [ 'msg' => 'accountActivated' ]);
}
/**
* @Route("/about-results", name="about_results")
*/
public function aboutResultsAction(Request $request)
{
return $this->render('about/7-track-results.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-evaluate", name="about_evaluate")
*/
public function aboutEvaluateAction(Request $request)
{
return $this->render('about/8-evaluate.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-review", name="about_review")
*/
public function aboutReview(Request $request)
{
return $this->render('about/review.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
/**
* @Route("/about-steps", name="about_steps")
*/
public function aboutSteps(Request $request)
{
return $this->render('about/about-steps.html.twig', [ 'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'language' => $request->getLocale() ]);
}
}