diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 97bdc9e..9cd32d7 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -21,6 +21,7 @@ $routes->get('metadata', 'SamlController::metadata'); $routes->post('auth/login', 'AuthController::login'); $routes->get('auth/oauthClient', 'AuthController::oauthClient'); $routes->post('auth/oauthlogin', 'AuthController::oauthlogin'); +$routes->get('auth/msRedirectionHandler', 'AuthController::msRedirectionHandler'); //forgot password diff --git a/app/Controllers/AuthController.php b/app/Controllers/AuthController.php index e83c537..d77ba70 100644 --- a/app/Controllers/AuthController.php +++ b/app/Controllers/AuthController.php @@ -40,7 +40,9 @@ class AuthController extends ResourceController $this->provider = new Azure([ 'clientId' => "9af66888-eb85-48ec-b3a0-ba857734f52b", 'clientSecret' => "N2_8Q~FBEXtoZft2UgJ6FQZCh3ZIbJydYgxREbUb", - 'redirectUri' => "https://apitest.tripapprovaltool.com/tstat/index.html#/authredirection", + // 'redirectUri' => "https://apitest.tripapprovaltool.com/tstat/authredirection", + // 'redirectUri' => "https://apitest.tripapprovaltool.com/tstat_be/auth/msRedirectionHandler", + 'redirectUri' => env('MS_REDIRECTION_URL'), 'tenant' => "284bf2b6-6dd8-4b46-881c-de45fde35736", ]); $this->provider->scope = [ @@ -314,5 +316,13 @@ class AuthController extends ResourceController return googleOAuthLogin(false); } + public function msRedirectionHandler() + { + $FeURL = env('FE_URL') . '#/authredirection?code=' . $this->request->getGet('code'); + + return redirect()->to($FeURL); + } + + } diff --git a/app/Controllers/PlanController.php b/app/Controllers/PlanController.php index 7773af7..92c796e 100644 --- a/app/Controllers/PlanController.php +++ b/app/Controllers/PlanController.php @@ -1434,8 +1434,8 @@ class PlanController extends ResourceController { echo '
';
-        sendPlanCreationMail(5);
-        print_r(getPlanApproverAction(5)); die;
+        // sendPlanCreationMail(23);
+        print_r(getPlanApproverAction(32)); die;
 
         // echo '
';
         // print_r(getAllowedClassForUser(2,4)['hotel']); die;
diff --git a/app/Helpers/mail_helper.php b/app/Helpers/mail_helper.php
index 1f9673c..c431723 100644
--- a/app/Helpers/mail_helper.php
+++ b/app/Helpers/mail_helper.php
@@ -299,99 +299,214 @@ if (!function_exists('sendUserCreationMail')) {
 }
 
 if (!function_exists('send_email')) {
-    function send_email($org_id , $toEmailId,  $template , $userData , $planData = null, $url = null)
-    {
-        try {
+    // function send_email($org_id , $toEmailId,  $template , $userData , $planData = null, $url = null)
+    // {
+    //     try {
 
-            // Fetch Organization data
-            $organizationModel = new OrganizationModel();
-            $orgData = $organizationModel->where('org_id', $org_id )->first();
-            // Fetch Template data
-            $mailTemplateModel = new MailTemplateModel();
-            $templateData = $mailTemplateModel->where('org_id', $org_id )->where('template_name', $template )->first();
-            log_message('info', "Template : ". $template ." , ToMail : ". $toEmailId);
+    //         // Fetch Organization data
+    //         $organizationModel = new OrganizationModel();
+    //         $orgData = $organizationModel->where('org_id', $org_id )->first();
+    //         // Fetch Template data
+    //         $mailTemplateModel = new MailTemplateModel();
+    //         $templateData = $mailTemplateModel->where('org_id', $org_id )->where('template_name', $template )->first();
+    //         log_message('info', "Template : ". $template ." , ToMail : ". $toEmailId);
 
-            //replace the placeholder
-            foreach ($userData as $key => $value) {
-                $placeHolder = '%'.$key.'%';
-                $templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
-            }
+    //         //replace the placeholder
+    //         foreach ($userData as $key => $value) {
+    //             $placeHolder = '%'.$key.'%';
+    //             $templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
+    //         }
 
-            if(!empty($planData)){
-                foreach ($planData as $key => $value) {
-                    $placeHolder = '%'.$key.'%';
-                    $templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
-                }
+    //         if(!empty($planData)){
+    //             foreach ($planData as $key => $value) {
+    //                 $placeHolder = '%'.$key.'%';
+    //                 $templateData['body_html'] = str_replace($placeHolder, $value ?? '', $templateData['body_html']);
+    //             }
 
-                if($planData['exceptional_plan_reason'] != null && $planData['exceptional_plan_reason'] != '')
-                {
-                    $reasonHtmlString = '           
-                                            
-                                                
-                                            
-                                            
-                                                   
-                                            
-                                        
- Reason for travel policy exception -
- '.$planData['exceptional_plan_reason'].' -
'; + // if($planData['exceptional_plan_reason'] != null && $planData['exceptional_plan_reason'] != '') + // { + // $reasonHtmlString = ' + // + // + // + // + // + // + //
+ // Reason for travel policy exception + //
+ // '.$planData['exceptional_plan_reason'].' + //
'; - $templateData['body_html'] = $templateData['body_html'].''.$reasonHtmlString; - } + // $templateData['body_html'] = $templateData['body_html'].''.$reasonHtmlString; + // } - } + // } - if($url != null){ + // if($url != null){ - $placeHolder = '%trip_review_link%'; - $templateData['body_html'] = str_replace($placeHolder, $url , $templateData['body_html']); + // $placeHolder = '%trip_review_link%'; + // $templateData['body_html'] = str_replace($placeHolder, $url , $templateData['body_html']); - } + // } - $subject = $templateData['subject']; - $html = $templateData['body_html']; + // $subject = $templateData['subject']; + // $html = $templateData['body_html']; - $email = \Config\Services::email(true); - $email->initialize([ - 'protocol' => 'smtp', - 'SMTPHost' => $orgData['mail_host'], - 'SMTPUser' => $orgData['mail_user_name'], - 'SMTPPass' => $orgData['mail_password'], - 'SMTPPort' => (int)$orgData['mail_port'], - 'mailType' => 'html' - ]); - $email->setFrom($orgData['sender_email'], $orgData['name']); - $email->setTo($toEmailId); - $email->setSubject($subject); - $email->setMessage($html); + // $email = \Config\Services::email(false); + // $email->initialize([ + // 'protocol' => 'smtp', + // 'SMTPHost' => $orgData['mail_host'], + // 'SMTPUser' => $orgData['mail_user_name'], + // 'SMTPPass' => $orgData['mail_password'], + // 'SMTPPort' => (int)$orgData['mail_port'], + // 'SMTPCrypto' => 'tls', + // 'mailType' => 'html', + // 'charset' => 'utf-8', + // 'newline' => "\r\n", + // 'wordWrap' => true, + // ]); + + + // $email->setFrom($orgData['sender_email'], $orgData['name']); + // $email->setTo($toEmailId); + // $email->setSubject($subject); + // $email->setMessage($html); - if(!empty($planData)){ - $email->attach($planData['pdf_file_path']); - } + // if(!empty($planData)){ + // $email->attach($planData['pdf_file_path']); + // } - // Try sending email - if ($email->send()) { - log_message('info', "[EMAIL SENT] To approver/traveller : {$toEmailId}, Template: {$template}"); - return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId]; - } else { - // Email send failed, log clean debug info - $debugInfo = $email->printDebugger(['headers', 'subject', 'body']); - log_message('error', "[EMAIL FAILED] To approver/traveller : {$toEmailId}, Template: {$template}\nError:\n" . strip_tags($debugInfo)); + // // Try sending email + // if ($email->send()) { + // log_message('info', "[EMAIL SENT] To approver/traveller : {$toEmailId}, Template: {$template}"); + // return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId]; + // } else { + // // Email send failed, log clean debug info + // $debugInfo = $email->printDebugger(['headers', 'subject', 'body']); + // log_message('error', "[EMAIL FAILED] To approver/traveller : {$toEmailId}, Template: {$template}\nError:\n" . strip_tags($debugInfo)); - return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'data' => $toEmailId]; + // return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'data' => $toEmailId]; + // } + + // } catch (\Exception $e) { + // log_message('critical', "[EMAIL EXCEPTION] To approver/traveller : {$toEmailId}, Error: " . $e->getMessage().$e->getLine()); + // return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage(), 'data' => $toEmailId]; + // } + // } + + + if (!function_exists('send_email')) { + function send_email($org_id, $toEmailId, $template, $userData, $planData = null, $url = null) + { + try { + // Load models + $organizationModel = new \App\Models\OrganizationModel(); + $mailTemplateModel = new \App\Models\MailTemplateModel(); + + // Fetch organization & template + $orgData = $organizationModel->where('org_id', $org_id)->first(); + $templateData = $mailTemplateModel->where('org_id', $org_id)->where('template_name', $template)->first(); + + if (!$orgData || !$templateData) { + log_message('error', "[EMAIL ERROR] Missing org/template data for org_id={$org_id}, template={$template}"); + return ['status' => 'failed', 'code' => 404, 'message' => 'Missing organization or template data']; + } + + // Replace placeholders from user data + foreach ($userData as $key => $value) { + $templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']); + } + + // Replace placeholders from plan data + if (!empty($planData)) { + foreach ($planData as $key => $value) { + $templateData['body_html'] = str_replace('%' . $key . '%', $value ?? '', $templateData['body_html']); + } + + // Exceptional reason block + if (!empty($planData['exceptional_plan_reason'])) { + $reasonHtml = ' + + + + + + + +
+ Reason for travel policy exception +
+ ' . esc($planData['exceptional_plan_reason']) . ' +
'; + $templateData['body_html'] .= $reasonHtml; + } + } + + // Add review URL if present + if ($url !== null) { + $templateData['body_html'] = str_replace('%trip_review_link%', $url, $templateData['body_html']); + } + + // Initialize email + $email = \Config\Services::email(false); + $email->initialize([ + 'protocol' => 'smtp', + 'SMTPHost' => $orgData['mail_host'], + 'SMTPUser' => $orgData['mail_user_name'], + 'SMTPPass' => $orgData['mail_password'], // Use app password if Gmail + 'SMTPPort' => (int) $orgData['mail_port'], + 'SMTPCrypto' => 'tls', // ensure STARTTLS + 'mailType' => 'html', + 'charset' => 'utf-8', + 'newline' => "\r\n", + 'wordWrap' => true, + ]); + + $email->setFrom($orgData['sender_email'], $orgData['name'] ?? 'TripApprovalTool'); + $email->setTo($toEmailId); + $email->setSubject($templateData['subject']); + $email->setMessage($templateData['body_html']); + + // Attach file if exists + // if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path'])) { + // $email->attach($planData['pdf_file_path']); + // } + + if (!empty($planData['pdf_file_path']) && file_exists($planData['pdf_file_path']) && is_readable($planData['pdf_file_path'])) { + $email->attach( + $planData['pdf_file_path'], + 'attachment', + basename($planData['pdf_file_path']), + 'application/pdf' + ); + } else { + log_message('error', "[EMAIL] PDF file not attached. Path: {$planData['pdf_file_path']}"); + } + + + // Send email + if ($email->send()) { + log_message('info', "[EMAIL SENT] To: {$toEmailId}, Template: {$template}"); + return ['status' => 'success', 'code' => 200, 'message' => 'Email sent successfully', 'data' => $toEmailId]; + } else { + // Log full debugger info + $debug = $email->printDebugger(['headers', 'subject', 'body']); + log_message('error', "[EMAIL FAILED] To: {$toEmailId}, Template: {$template}\n" . strip_tags($debug)); + return ['status' => 'failed', 'code' => 500, 'message' => 'Email sending failed', 'debug' => strip_tags($debug)]; + } + + } catch (\Throwable $e) { + log_message('critical', "[EMAIL EXCEPTION] To: {$toEmailId}, Error: " . $e->getMessage() . ' on line ' . $e->getLine()); + return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage()]; } - - } catch (\Exception $e) { - log_message('critical', "[EMAIL EXCEPTION] To approver/traveller : {$toEmailId}, Error: " . $e->getMessage().$e->getLine()); - return ['status' => 'failed', 'code' => 500, 'message' => 'Exception: ' . $e->getMessage(), 'data' => $toEmailId]; } } + } if (!function_exists('send_email_agent')) {