<?php
/**
* Created by PhpStorm.
* User: Ronald
* Date: 6/15/2020
* Time: 1:32 PM
*/
namespace App\Mailer;
use App\Entity\CustomerAccount;
use App\Entity\User;
use App\Entity\RequestAccess;
use App\Repository\CustomerAccountRepository;
use App\Repository\UserRepository;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Mailer\MailerInterface;
class AccountMailer extends Mailer
{
/**
* Mailer constructor.
* @param MailerInterface $mailer
* @param ContainerInterface $container
* @param $env
* @throws \Exception
*/
public function __construct(MailerInterface $mailer, ContainerInterface $container,$env)
{
parent::__construct($mailer, $container,$env);
}
//USER Emails
public function newUserEmail(User $user,$plainPassword)
{
$emailBody = $this->twig->render('email/account/newUser.html.twig', array('user' => $user,'plainPassword'=>$plainPassword));
$this->send('Welcome to Overseas Express!', $emailBody, $user->getEmail());
}
public function changePasswordEmail(User $user,$plainPassword)
{
$fromEmail=null;
if($this->container->get('session')->get('customerAccountDomain')){
$fromEmail ='noreply@gtnxpress.com';
}
$emailBody = $this->twig->render('email/account/changePassword.html.twig', array('user' => $user,'plainPassword'=>$plainPassword));
$this->send('Your password has been reset', $emailBody, $user->getEmail(),null,[],false,$fromEmail);
}
public function forgotPasswordEmail(User $user)
{
$fromEmail=null;
if($this->container->get('session')->get('customerAccountDomain')){
$fromEmail ='noreply@gtnxpress.com';
}
$emailBody = $this->twig->render('email/account/forgotPassword.html.twig', array('user' => $user));
$this->send('Reset Password Email', $emailBody, $user->getEmail(),null,[],false,$fromEmail);
}
public function resetPasswordEmail(User $user)
{
$fromEmail=null;
if($this->container->get('session')->get('customerAccountDomain')){
$fromEmail ='noreply@gtnxpress.com';
}
$emailBody = $this->twig->render('email/account/resetPassword.html.twig', array('user' => $user));
$this->send('Reset Password Email', $emailBody, $user->getEmail(),null,[],false,$fromEmail);
}
//CustomerAccount Emails
public function changeCreditLevelEmail(User $user, CustomerAccount $customerAccount, $currentLevel)
{
$emailBody = $this->twig->render('email/account/customerAccountLevelChange.html.twig', array('user'=>$user,'customerAccount' => $customerAccount,'currentLevel'=>$currentLevel));
$this->send('CustomerAccount level change: '.$customerAccount->getName(), $emailBody);
}
//Request Access Emails
public function requestAccessEmail(RequestAccess $requestAccess, UserRepository $userRepository,$isCustomerAccountDomain=false)
{
$customerAccount=null;
$from=null;
$from2=null;
if($isCustomerAccountDomain){
$customerAccount=$requestAccess->getCustomerAccount();
$emailList=['support@gtnxpress.com'];
$from='support@gtnxpress.com';
$from2='noreply@gtnxpress.com';
}
else {
$emailList = ['backoffice@overseasnetwork.com'];
}
//1st Email
$emailBody = $this->twig->render('email/account/requestAccessUser.html.twig', array('requestAccess' => $requestAccess,'customerAccount'=>$customerAccount));
$this->send('New account request', $emailBody, $requestAccess->getEmail(),null,[],false, $from);
// $this->send('New account request', $emailBody, ['marinar@overseasnetwork.com'],null,[],false,'support@gtnxpress.com');//to test
//2nd Email
//Send to users with role "ROLE_MANAGER_ACCOUNT_APPROVAL"
if($managerAccountApprovalList=$userRepository->findAllByRoleManagerAccountApproval($customerAccount)) {
/**
* @var User $managerAccountApproval
*/
foreach ($managerAccountApprovalList as $managerAccountApproval) {
$emailList[] = $managerAccountApproval->getEmail();
}
}
$emailBody = $this->twig->render('email/account/requestAccessAdmin.html.twig', array('requestAccess' => $requestAccess,'customerAccount'=>$customerAccount));
$this->send('New Account Request Pending - ' . $requestAccess->getCustomerAccountName(), $emailBody, $emailList,[],[],false,$from2);
}
public function sendCreationEmail(User $user,$isHost=false)
{
$fromEmail=null;
if($isHost){
$fromEmail='noreply@gtnxpress.com';
$customerAccountDomain=$user->getCustomerAccount();
if($customerAccountDomain->getParentCustomerAccount()){
$customerAccountDomain=$user->getCustomerAccount()->getParentCustomerAccount();
}
$emailBody = $this->twig->render('email/account/sendCreationEmailHost.html.twig', array('user' => $user,'customerAccount'=>$user->getCustomerAccount(),'customerAccountDomain'=>$customerAccountDomain));
}
else {
$emailBody = $this->twig->render('email/account/sendCreationEmail.html.twig', array('user' => $user));
}
$this->send($isHost?'Welcome to '.$user->getCustomerAccount()->getName().'!':'Welcome to OverseasXpress!', $emailBody, $user->getEmail(),null,[],false,$fromEmail);
}
}