<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\UserDetails;
use App\Entity\Survey;
use App\Entity\Contact;
use App\Form\SignupType;
use App\Utils\Utils;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\PersistentCollection;
use Doctrine\DBAL\Connection;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Http\Authentication\RememberMe;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Swift_Mailer;
class DefaultController extends AbstractController
{
//private $fromEmail;
protected $mailer;
protected $translator;
protected $passwordHasher;
public function __construct(TranslatorInterface $translator, Swift_Mailer $mailer, UserPasswordHasherInterface $passwordHasher)
{
$this->mailer = $mailer;
$this->translator = $translator;
$this->passwordHasher = $passwordHasher;
}
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request)
{
return $this->render('default/index.html.twig', ['language' => $request->getLocale(),
'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR]);
}
/**
* @Route("/faq", name="faq")
*/
public function faqAction(Request $request, Connection $conn, UserInterface $user = null, \Swift_Mailer $mailer)
{
$fromEmail = $this->getParameter('mailer_from_user');
$contactType = $request->query->get('type');
$msg = "";
if (sizeof($request->request) > 0 && $request->request->get('userid') == '' && $request->request->get('password') == '' && $request->request->get('checkme') == "online") {
$contact = new Contact();
$contact->init($user, $conn);
$result = $contact->addContactUsItem(array('reason' => $contactType,
'first_name' => $request->request->get("firstName"),
'last_name' => $request->request->get("lastName"),
'company' => $request->request->get("organization"),
'job_title' => $request->request->get("jobTitle"),
'email' => $request->request->get("email"),
'phone' => $request->request->get("phone"),
'ext' => $request->request->get("ext"),
'subject' => $request->request->get("subject"),
'message' => $request->request->get("message")));
$message = (new \Swift_Message($this->translator->trans('gmaw') . " - " . $this->translator->trans('nav-contact')))
->setFrom($fromEmail)
->setTo($fromEmail)
->setBody($this->renderView('default/contact-us-email.html.twig', ['form_topic' => $request->request->get('reason'),
'form_first_name' => $request->request->get('firstName'),
'form_last_name' => $request->request->get('lastName'),
'form_organization' => $request->request->get('organization'),
'form_job_title' => $request->request->get('jobTitle'),
'form_email' => $request->request->get('email'),
'form_phone_number' => $request->request->get('phone'),
'form_ext' => $request->request->get('ext'),
'form_subject' => $request->request->get('subject'),
'form_message' => $request->request->get('message')]), 'text/html');
$this->mailer->send($message);
$msg = "thanks";
}
return $this->render('default/faq.html.twig', ['type' => ($contactType ? $contactType : 'choose'),
'base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'msg' => $msg]);
}
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request, AuthenticationUtils $authUtils, Connection $conn, \Swift_Mailer $mailer, UserInterface $user = null)
{
if ($conn != null && $user != null) {
return new RedirectResponse("/dashboard/home");
}
$fromEmail = $this->getParameter('mailer_from_user');
$msg = "";
if (sizeof($request->request) > 0) {
if ($request->request->get("forgot_password") != '') {
$user = new User();
$userDetails = new UserDetails();
$userDetails->init($user, $conn);
$activation_hash = $userDetails->getPasswordHashByEmailAddress($request->request->get("forgot_password"));
if ($activation_hash !== false) {
$message = (new \Swift_Message($this->translator->trans('reset-pw-email-sub')))
->setFrom($fromEmail)
->setTo($request->request->get("forgot_password"))
->setBody($this->renderView('default/reset-password-email.html.twig', array('activation_hash' => $activation_hash)), 'text/html');
$this->mailer->send($message);
$msg = "passwordReset";
} else {
$msg = "emailNotFound";
}
} else {
$msg = "loginError";
}
}
return $this->render('default/login.html.twig', [
'msg' => $msg,
'error' => $authUtils->getLastAuthenticationError()
]);
}
/**
* @Route("/forgot-password", name="forgot_password")
*/
public function forgotPassword(Request $request, AuthenticationUtils $authUtils, Connection $conn, \Swift_Mailer $mailer, UserInterface $user = null)
{
$locale = $request->getLocale();
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');
}
$msg = "";
if (sizeof($request->request) > 0) {
if ($request->request->get("forgot_password") != '') {
$user = new User();
$userDetails = new UserDetails();
$userDetails->init($user, $conn);
$activation_hash = $userDetails->getPasswordHashByEmailAddress($request->request->get("forgot_password"));
if ($activation_hash !== false) {
$message = (new \Swift_Message($this->translator->trans('reset-pw-email-sub')))
->setFrom($fromEmail)
->setTo($request->request->get("forgot_password"))
->setBody($this->renderView('default/reset-password-email.html.twig', array('activation_hash' => $activation_hash)), 'text/html');
$this->mailer->send($message);
$msg = "passwordReset";
} else {
$msg = "emailNotFound";
}
} else {
$msg = "loginError";
}
}
return $this->render('default/login.html.twig', ['msg' => $msg, 'error' => '']);
}
/**
* @Route("/login/reset/{activation_hash}", name="reset_password")
*/
public function resetPassword(Request $request, Connection $conn, $activation_hash = false)
{
$user = new User();
$userDetails = new UserDetails();
$userDetails->init($user, $conn);
if ($activation_hash) {
$user_id = $userDetails->getUserIDByResetKey($activation_hash);
if ($user_id !== false) {
if (sizeof($request->request) > 0) {
$new_password = $this->passwordHasher->hashPassword($user, $request->request->get('password'));
$userDetails->updatePassword($user_id, $new_password);
return $this->redirectToRoute('login');
}
return $this->render('default/reset-password-form.html.twig', ['activation_hash' => $activation_hash]);
} else {
return $this->redirectToRoute('login');
}
} else {
return $this->redirectToRoute('homepage');
}
}
public function changeLanguageAction(Request $request)
{
return new RedirectResponse($request->headers->get('referer'));
}
/**
* @Route("/resources", name="resources")
*/
public function resourcesAction(Request $request)
{
if (sizeof($request->request) > 0) {
$selected_files = $request->get('resources');
$zip = new \ZipArchive();
$zipName = "resources" . time() . ".zip";
$zip->open($zipName, \ZipArchive::CREATE);
foreach ($selected_files as $file) {
$zip->addFromString($file, file_get_contents("../web/assets/pdfs/" . (($request->getLocale() == 'fr') ? 'fre/' : '') . $file));
}
$zip->close();
$response = new Response(file_get_contents($zipName));
$response->headers->set('Content-Type', 'application/zip');
$response->headers->set('Content-Disposition', 'attachment;filename="' . $zipName . '"');
$response->headers->set('Content-length', filesize($zipName));
unlink('../web/' . $zipName);
return $response;
}
return $this->render('default/resources.html.twig');
}
/**
* @Route("/terms", name="terms")
*/
public function termsAction(Request $request)
{
return $this->render('default/terms.html.twig');
}
/**
* @Route("/contact-us", name="contact_us")
*/
public function contactUsAction(Request $request, Connection $conn, \Swift_Mailer $mailer)
{
$fromEmail = $this->getParameter('mailer_from_user');
$toEmail = $this->getParameter('mailer_contact_us_to');
$subjectOfEmail = "MHCC-Paramedic - Contact Us - " . $request->getLocale();
$msg = "";
$reason = "Contact us comment";
//Userid, password, checkme fields are here to handle spam.
if (sizeof($request->request) > 0 && $request->request->get('userid') == '' && $request->request->get('password') == '' && $request->request->get('checkme') == "online") {
$contact = new Contact();
$contact->init(false, $conn);
$result = $contact->addContactUsItem(array('reason' => $reason,
'first_name' => $request->request->get("firstName"),
'last_name' => $request->request->get("lastName"),
'job_title' => $request->request->get("jobTitle"),
'email' => $request->request->get("emailContactUs"),
'phone' => $request->request->get("phoneContactUs"),
'subject' => $request->request->get("subjectContactUs"),
'message' => $request->request->get("messageContactUs")));
$message = (new \Swift_Message($subjectOfEmail))
->setFrom($fromEmail)
->setTo($toEmail) // $fromEmail
->setBody($this->renderView('default/contact-us-email.html.twig', ['form_topic' => $request->request->get('reason'),
'form_first_name' => $request->request->get('firstName'),
'form_last_name' => $request->request->get('lastName'),
'form_job_title' => $request->request->get('jobTitle'),
'form_email' => $request->request->get('emailContactUs'),
'form_phone_number' => $request->request->get('phoneContactUs'),
'form_subject' => $request->request->get('subjectContactUs'),
'form_message' => $request->request->get('messageContactUs')]), 'text/html');
try {
$this->mailer->send($message);
} catch (Exception $ex) {
$msg = $ex->getMessage();
}
$msg = "sent";
}
return $this->render('default/contact-us.html.twig', ['base_dir' => realpath($this->getParameter('kernel.project_dir')) . DIRECTORY_SEPARATOR,
'msg' => $msg]);
}
}