golf_backend/app/Controllers/Sendmail_controller.php
suseendhiran17 60ca21e866 susee
2023-06-10 10:19:16 +05:30

535 lines
11 KiB
PHP

<?php namespace App\Controllers;
// use App\Models\Dashboard_model;
use App\Models\User_model;
use App\Models\Dashboard_model;
use App\Models\Common_model;
use App\Models\Member_credits_model;
use App\Models\Packages_and_pricing_model;
use App\Models\Credits_usage_tracking_model;
use App\Models\Email_template_model;
use App\Models\Member_model;
use App\Models\Transaction_model;
class Sendmail_controller extends BaseController
{
public function __construct()
{
$this->session = session();
$this->MemberModel = new Member_model();
$this->UserModel = new User_model();
$this->dModel = new Dashboard_model();
$this->cModel = new Common_model();
$this->creditsModel = new Member_credits_model();
$this->PackagesModel = new Packages_and_pricing_model();
$this->CreditsUsageTrackingModel = new Credits_usage_tracking_model();
$this->MailModel = new Email_template_model();
$this->TransactionModel = new Transaction_model();
helper(['form', 'url']);
}
public function index($to,$code,$return)
{
try{
$email = \Config\Services::email();
$email->setFrom('devbook@mprkv.co.in', 'GolfEvo');
$email->setTo($to);
// login user change passowrd => type 4
if($return == 4)
{
$data['id'] = '4';
$mem_data = $this->MemberModel->where('email', $to)->find();
if ($mem_data)
{
$temp_data = $this->MailModel->where('type' , 4)->where('is_active' , 1)->find();
if ($temp_data[0]['to'])
$email->setCC($temp_data[0]['to']);
if ($temp_data) {
$data['html'] = str_replace("%name%",$mem_data[0]['name'],$temp_data[0]['html']);
}
$message = view('verification_mail_temp',$data);
if ($temp_data[0]['is_active'] == 1)
$email->setSubject($temp_data[0]['subject']);
else
$email->setSubject('Password change');
}
else
{
$user_data = $this->UserModel->where('email', $to)->find();
$temp_data = $this->MailModel->where('type' , 4)->where('is_active' , 1)->find();
if ($temp_data[0]['to'])
$email->setCC($temp_data[0]['to']);
if ($temp_data) {
$data['html'] = str_replace("%name%",$user_data[0]['name'],$temp_data[0]['html']);
}
$message = view('verification_mail_temp',$data);
}
$message = view('verification_mail_temp',$data);
if ($temp_data[0]['is_active'] == 1)
$email->setSubject($temp_data[0]['subject']);
else
$email->setSubject('Password change');
}
// user => type 2
else if($return == 3)
{
$user_data = $this->UserModel->where('email', $to)->find();
$data['id'] = '3';
$data['username'] = $to;
$data['password'] = $code;
$data['url'] = base_url()."admin";
$temp_data = $this->MailModel->where('type' , 2)->where('is_active' , 1)->find();
if ($temp_data[0]['to'])
$email->setCC($temp_data[0]['to']);
if ($temp_data)
$data['html'] = str_replace("%name%",$user_data[0]['name'],$temp_data[0]['html']);
$message = view('verification_mail_temp',$data);
if ($temp_data[0]['is_active'] == 1)
$email->setSubject($temp_data[0]['subject']);
else
$email->setSubject('Username Password');
}
// otp => type 3
else if ($return == 2)
{
$data['id'] = '2';
$data['otp'] = $code;
$mem_data = $this->MemberModel->where('email', $to)->find();
if ($mem_data)
{
$temp_data = $this->MailModel->where('type' , 3)->where('is_active' , 1)->find();
if ($temp_data[0]['to'])
$email->setCC($temp_data[0]['to']);
if ($temp_data) {
$html = str_replace("%name%",$mem_data[0]['name'],$temp_data[0]['html']);
$data['html'] = str_replace("%otp%",$code,$html);
}
$message = view('verification_mail_temp',$data);
if ($temp_data[0]['is_active'] == 1)
$email->setSubject($temp_data[0]['subject']);
else
$email->setSubject('OTP verification');
}
else
{
$user_data = $this->UserModel->where('email', $to)->find();
$temp_data = $this->MailModel->where('type' , 3)->where('is_active' , 1)->find();
if ($temp_data[0]['to'])
$email->setCC($temp_data[0]['to']);
if ($temp_data) {
$html = str_replace("%name%",$user_data[0]['name'],$temp_data[0]['html']);
$data['html'] = str_replace("%otp%",$code,$html);
}
$message = view('verification_mail_temp',$data);
if ($temp_data[0]['is_active'] == 1)
$email->setSubject($temp_data[0]['subject']);
else
$email->setSubject('OTP verification');
}
}
// member => type 1
else if($return == 1)
{
$data['id'] = '1';
$user_data = $this->MemberModel->where('email', $to)->find();
$data['to'] = $to;
$data['code'] = $code;
$data['url'] = base_url()."verify_email/".md5($code);
$temp_data = $this->MailModel->where('type' , 1)->where('is_active' , 1)->find();
if ($temp_data[0]['to'])
$email->setCC($temp_data[0]['to']);
if ($temp_data)
$data['html'] = str_replace("%name%",$user_data[0]['name'],$temp_data[0]['html']);
$message = view('verification_mail_temp',$data);
if ($temp_data[0]['is_active'] == 1)
$email->setSubject($temp_data[0]['subject']);
else
$email->setSubject('Verify your email address');
}
// payment => type 5
else if($return == 5)
{
$TransactionData = $this->TransactionModel->where('member_id', session()->get('logged_user'))->orderBy('created_at', 'DESC')->first();
$Transaction_id = $TransactionData['transaction_id'];
$timestamp = strtotime($TransactionData['created_at']);
$Transaction_date = date('l, F j, Y', $timestamp);
$amount = json_decode($TransactionData['transaction'] , true)['payment']['amount_money']['amount'];
$payment_method = json_decode($TransactionData['transaction'] , true)['payment']['source_type'];
$data['id'] = $return;
$temp_data = $this->MailModel->where('type' , $return)->where('is_active' , 1)->find();
if ($temp_data[0]['to'])
$email->setCC($temp_data[0]['to']);
if ($temp_data) {
$data['html'] = str_replace("%name%",session()->get('logged_user_name'),$temp_data[0]['html']);
$data['html'] = str_replace("%id%",$Transaction_id,$temp_data[0]['html']);
$data['html'] = str_replace("%method%",$payment_method,$temp_data[0]['html']);
$data['html'] = str_replace("%date%", $Transaction_date,$temp_data[0]['html']);
$data['html'] = str_replace("%amount%",$amount,$temp_data[0]['html']);
$message = view('verification_mail_temp',$data);
}
if ($temp_data[0]['is_active'] == 1)
$email->setSubject($temp_data[0]['subject']);
else
$email->setSubject('Username Password');
}
$email->setMessage($message);
if ($email->send())
{
if ($return == 1)
$this->session->setTempdata('mail_success', "Please check your mail to verify",3);
if ($return == 5)
return true;
}
else
{
$error = $email->printDebugger();
//echo 'Email sending failed: ' . $error;
}
} catch (EmailException $e) {
echo 'Email sending failed: ' . $e->getMessage();
}
}
public function verify_email($verification_code)
{
// Get the user data corresponding to the verification code
$user_data = $this->MemberModel->where('md5(verification_code)', $verification_code)->find();
if($user_data)
{
//print_r($user_data);die();
if($user_data[0]['is_email_verified'] == 0)
{
// Update the users status
// $update_data = $this->MemberModel->update_status($user_data['id']);
$data['is_email_verified'] = 1;
$update_data = $this->MemberModel->where('id', $user_data[0]['id'] )->set($data)->update();
if($update_data)
{
return redirect()->to(base_url()."set_password/".md5($user_data[0]['id']));
}
else
{
// Show error message
echo 'Error in verifying email. Please try again.';
}
}
else
{
// Show error message
// echo 'Email already verified.';
return redirect()->to(base_url()."set_password/".md5($user_data[0]['id']));
}
}
else
{
// Show error message
echo 'Invalid verification code.';
}
}
public function forgot_password()
{
$this->session->setTempdata('user',"member",3);
if($this->request->getmethod() == 'get')
{
return view('forgot_password_otp.php');
}
else if($this->request->getmethod() == 'post')
{
$email = $this->request->getVar('email');
$user_data = $this->MemberModel->where('email', $email)->where('is_email_verified', 1 )->find();
if ($user_data)
{
$otp = rand(1000 , 9999);
$this->index($this->request->getVar('email') , $otp , 2 );
$data['status'] = 'success';
$data['otp'] = $otp;
echo json_encode($data);
}
else
{
$data['status'] = 'failed';
echo json_encode($data);
}
}
}
}