418 lines
17 KiB
PHP
Executable File
418 lines
17 KiB
PHP
Executable File
<?php
|
|
|
|
// declare(strict_types=1);
|
|
|
|
namespace App\Controllers;
|
|
use App\Models\CommonEMailModel;
|
|
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use CodeIgniter\API\ResponseTrait;
|
|
|
|
use App\Controllers\Jobs ;
|
|
|
|
use App\Models\NotificationModel;
|
|
use App\Models\ClientModel;
|
|
use App\Models\EmployeeModel;
|
|
use App\Models\UserModel;
|
|
use App\Models\MailAttachmentModel;
|
|
|
|
use App\Helpers\sendMailNotification;
|
|
use App\Helpers\MailHelper;
|
|
|
|
class NotificationController extends AdminController
|
|
{
|
|
use ResponseTrait;
|
|
protected $myLogger;
|
|
protected $notificationModel;
|
|
protected $clientModel;
|
|
protected $MailAttachmentModel;
|
|
|
|
protected $CommonEMailModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->myLogger = \Config\Services::mylogger();
|
|
$this->notificationModel = new NotificationModel();
|
|
$this->clientModel = new ClientModel();
|
|
$this->MailAttachmentModel = new MailAttachmentModel();
|
|
$this->CommonEMailModel = new CommonEMailModel();
|
|
}
|
|
|
|
// This is for Template Name Camel Case to Snake Case for store in DB
|
|
public function camelCaseToSnakeCase($input)
|
|
{
|
|
$input = preg_replace('/(?<!^)([A-Z])/', '$1', $input);
|
|
$input = str_replace(' ', '_', $input);
|
|
return strtolower($input);
|
|
}
|
|
|
|
// Create Or Update the Notification
|
|
public function createNotification()
|
|
{
|
|
|
|
$form_data['template_name'] = $this->camelCaseToSnakeCase($this->request->getPost('template_name'));
|
|
$form_data['subject'] = $this->request->getPost('subject');
|
|
$form_data['mail_content'] = $this->request->getPost('mailContent');
|
|
$form_data['client_id'] = $this->request->getPost('client_id');
|
|
$form_data['mail_content_json'] = $this->request->getPost('mailJson');
|
|
$form_data['common_mail'] = $this->request->getPost('common_mail')??'';
|
|
$notificationModel = new NotificationModel();
|
|
|
|
$find_notification = $notificationModel->where('client_id',$form_data['client_id'])->where('template_name',$form_data['template_name'])->first();
|
|
|
|
if ($find_notification) {
|
|
$this->logger->debug("inside notification update");
|
|
|
|
$id = $find_notification['id'] ?? '';
|
|
$form_data['updated_by'] = get_session_userid();
|
|
$notificationModel->update($id,$form_data);
|
|
$complete = "Update";
|
|
|
|
}else{
|
|
$this->logger->debug("inside notification insert");
|
|
|
|
$form_data['created_by'] = get_session_userid();
|
|
$id = $notificationModel->insert($form_data);
|
|
$complete = "Inserted";
|
|
}
|
|
|
|
return json_encode($complete);
|
|
|
|
}
|
|
|
|
|
|
// This is for the Enabls in Notification area and Update the Recipient and HR Mail Id in( Enables -> Notification table and Mail id's -> Client table)
|
|
public function update_enable()
|
|
{
|
|
$checkboxNames = [
|
|
'member_welcome_mail_btn',
|
|
'member_common_mail_btn',
|
|
'member_reminder_mail_btn',
|
|
'member_ecard_mail_btn',
|
|
'member_review_and_summary_mail_btn',
|
|
'account_maneger_summary_mail_btn',
|
|
'client_hr_summary_mail_btn',
|
|
'hr_cd_insufficient_balance_mail_btn',
|
|
];
|
|
$data = [];
|
|
$emptyTemplate = [];
|
|
$updatedTemplate = [];
|
|
|
|
foreach ($checkboxNames as $checkboxName)
|
|
{
|
|
$data[str_replace('_btn', '', $checkboxName)] = ($this->request->getPost($checkboxName) === "true") ? 1 : 0;
|
|
}
|
|
|
|
$notificationModel = new NotificationModel();
|
|
$clientModel = new ClientModel();
|
|
|
|
$id =$this->request->getPost('client_id');
|
|
$clientData['common_mails'] = $this->request->getPost('nhance_team_mail_ids');
|
|
$clientData['hr_mails'] = $this->request->getPost('hr_mail_id');
|
|
$clientData['reply_to'] = $this->request->getPost('reply_to');
|
|
$clientData['mail_domain'] = $this->request->getPost('mail_domain');
|
|
$clientModel->update($id,$clientData);
|
|
|
|
foreach ($data as $key => $value)
|
|
{
|
|
$find = $notificationModel->where('client_id',$this->request->getPost('client_id'))->where('template_name', $key)->first();
|
|
if ($find)
|
|
{
|
|
$id = $find['id'];
|
|
$changeData['enabled']= $value;
|
|
$update = $notificationModel->update($id, $changeData);
|
|
$updatedTemplate [] = $key;
|
|
}
|
|
|
|
if(!$find && $value == 1){
|
|
$emptyTemplate [] = $key;
|
|
}
|
|
}
|
|
|
|
if(count($emptyTemplate) > 0){
|
|
return $this->response->setJSON([
|
|
'status' => false,
|
|
'code' => 400,
|
|
'message' => 'Notification settings updation Failed.',
|
|
'updated_templates' => $updatedTemplate,
|
|
'empty_templates' => $emptyTemplate
|
|
]);
|
|
}
|
|
|
|
return $this->response->setJSON([
|
|
'status' => true,
|
|
'code' => 200,
|
|
'message' => 'Notification settings updated successfully.',
|
|
'updated_templates' => $updatedTemplate,
|
|
'empty_templates' => $emptyTemplate
|
|
]);
|
|
}
|
|
|
|
// This for Load the Template Data for in View Page
|
|
public function getMailTemplateData($template_name,$client_id)
|
|
{
|
|
$notificationModel = new NotificationModel();
|
|
$value = $notificationModel->where('client_id',$client_id)->where('template_name',$template_name)->first()??[];
|
|
if(!empty($value)){
|
|
$value['data'] = $this->MailAttachmentModel->where('notification_id', $value['id'])->where('is_active', 1)->findAll()??[];
|
|
}
|
|
|
|
if( isset($value['template_name']) && !empty($value['template_name']) && $value['template_name'] == "member_common_mail"){
|
|
$common_mails = $value['common_mail']??[];
|
|
$value['comman_mails'] = !empty($common_mails) ? $common_mails : "";
|
|
}
|
|
|
|
|
|
|
|
|
|
return json_encode($value);
|
|
}
|
|
|
|
// This will send the Reminder Mail
|
|
public function reminder_mail()
|
|
{
|
|
$clientModel = new ClientModel();
|
|
$employeeModel = new EmployeeModel();
|
|
$notificationModel = new NotificationModel();
|
|
|
|
$client_list = $clientModel->where('is_active',1)->findAll();
|
|
|
|
$wholeData = [];
|
|
foreach($client_list as $key=>$value){
|
|
$notification_data = $notificationModel->where('client_id',$value['id'])->where('template_name', 'member_reminder_mail')->first();
|
|
|
|
$client_data = $clientModel->where('id', $value['id'])->first();
|
|
|
|
if(isset($notification_data) && $notification_data['enabled'] == 1){
|
|
$emp_data = $employeeModel->where('client_id', $value['id'])->where('is_active',1)->where('emp_status','draft')->findAll();
|
|
if( count($emp_data) != 0)
|
|
{
|
|
$params['emp_data'] = $emp_data;
|
|
$params['client_data'] = $client_data;
|
|
$params['notification_data'] = $notification_data;
|
|
$wholeData[] =sendMailNotification::sendMailNotification('member_reminder_mail', $params);
|
|
}
|
|
}
|
|
}
|
|
|
|
$count = 0;
|
|
foreach ($wholeData as $index => $values) {
|
|
$whole_index = $index;
|
|
foreach ($values as $index => $value) {
|
|
$count++;
|
|
$temp_whole_data[] = $value;
|
|
if($count == 20 || $whole_index == count($wholeData)-1 && $index == count($values)-1){
|
|
if (count($temp_whole_data) > 0) {
|
|
$job_details = new Jobs();
|
|
$r = Jobs::addJob(['job_name' => 'bulk_mail','payload' => $temp_whole_data]);
|
|
|
|
$temp_whole_data = [];
|
|
$count = 0;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// This function for sent a mail for testing
|
|
// keep watching this
|
|
public function sentTestMail($template_id, $test_mail, $string_flag)
|
|
{
|
|
$notification_data = $this->notificationModel->where('id', $template_id)->first();
|
|
|
|
|
|
if(empty($notification_data)) {
|
|
return $this->respond(['status' => false,'code' => 200, 'message' => 'Notification Template not enabled']);
|
|
}
|
|
|
|
$client_data = $this->clientModel->where('id', $notification_data['client_id'])->where('is_active', 1)->first();
|
|
|
|
$params = [
|
|
'client_data' => $client_data,
|
|
'notification_data' => $notification_data,
|
|
'test_mail' => $test_mail,
|
|
'test_mail_list' => ""
|
|
];
|
|
|
|
$testMailData = sendMailNotification::sendMailNotificationForTesting($notification_data['template_name'], $params);
|
|
|
|
if(empty($testMailData)) {
|
|
return $this->respond(['status' => false,'code' => 200, 'message' => 'Test Mail Data Does Not Exist']);
|
|
}
|
|
|
|
if($string_flag == "send"){
|
|
$mail_send_return1 = MailHelper::send_email($testMailData);
|
|
$this->myLogger->logme("info", $mail_send_return1);
|
|
return $this->respond(['status' => true,'code' => 200, 'respond' => json_decode($mail_send_return1)]);
|
|
}
|
|
|
|
if($string_flag == "preview"){
|
|
return $this->respond([
|
|
'status' => true,
|
|
'code' => 200,
|
|
'content' => $testMailData
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
public function sendCommonTestMail()
|
|
{
|
|
|
|
$template_id = $this->request->getPost('template_id');
|
|
|
|
// $test_mail = $this->request->getPost('test_mail');
|
|
|
|
$test_mail_list = $this->request->getPost('test_mail_list');
|
|
|
|
|
|
$notification_data = $this->notificationModel->where('id', $template_id)->first();
|
|
$client_data = $this->clientModel->where('id', $notification_data['client_id'])->where('is_active', 1)->first();
|
|
|
|
if(!empty($notification_data)){
|
|
|
|
// First Index as test mail all are testmaillist
|
|
// $params = [
|
|
// 'client_data' => $client_data,
|
|
// 'notification_data' => $notification_data,
|
|
// 'test_mail' => $test_mail,
|
|
// 'test_mail_list' => $test_mail_list
|
|
// ];
|
|
// $testMailData = sendMailNotification::sendMailNotificationForTesting($notification_data['template_name'], $params);
|
|
// if (!empty($testMailData)) {
|
|
// $mail_send_return1 = MailHelper::send_email($testMailData);
|
|
// $this->myLogger->logme("info", $mail_send_return1);
|
|
// $this->myLogger->logme("info", $mail_send_return1);
|
|
// return $this->respond(['status' => true,'code' => 200, 'respond' => json_decode($mail_send_return1)]);
|
|
// }else{
|
|
// return $this->respond(['status' => false,'code' => 200, 'message' => 'Test Mail Data Does Not Exist']);
|
|
// }
|
|
|
|
|
|
$mailArray = explode(',', $test_mail_list);
|
|
$successMails = [];
|
|
$failedMails = [];
|
|
|
|
foreach ($mailArray as $singleMail) {
|
|
$singleMail = trim($singleMail);
|
|
|
|
$params = [
|
|
'client_data' => $client_data,
|
|
'notification_data' => $notification_data,
|
|
'test_mail' => $singleMail,
|
|
'test_mail_list' => "" // optional
|
|
];
|
|
|
|
$testMailData = sendMailNotification::sendMailNotificationForTesting($notification_data['template_name'], $params);
|
|
|
|
if (!empty($testMailData)) {
|
|
$mail_send_return1 = MailHelper::send_email($testMailData);
|
|
|
|
if ($mail_send_return1) {
|
|
$successMails[] = $singleMail;
|
|
} else {
|
|
$failedMails[] = $singleMail;
|
|
}
|
|
|
|
$this->myLogger->logme("info", "Mail attempt to {$singleMail}: " . $mail_send_return1);
|
|
} else {
|
|
$failedMails[] = $singleMail;
|
|
$this->myLogger->logme("info", "Test Mail Data Does Not Exist for {$singleMail}");
|
|
}
|
|
}
|
|
|
|
// Prepare final response
|
|
if (!empty($successMails)) {
|
|
$responseMessage = "Count " . count($successMails) . " mail(s) sent successfully: \n" . implode("\n", $successMails);
|
|
if (!empty($failedMails)) {
|
|
$responseMessage .= "\nCount " . count($failedMails) . " mail(s) failed: \n" . implode("\n", $failedMails);
|
|
}
|
|
return $this->respond(['status' => true,'code' => 200,'message' => $responseMessage]);
|
|
} else {
|
|
return $this->respond(['status' => false,'code' => 200,'message' => 'All mails failed to send: ' . implode(", ", $failedMails)]);
|
|
}
|
|
|
|
}else{
|
|
|
|
return $this->respond(['status' => false,'code' => 200, 'message' => 'Notification Template not enabled']);
|
|
|
|
}
|
|
}
|
|
|
|
public function insertAttachmentsForMailTemplates()
|
|
{
|
|
$form_data = $this->request->getPost();
|
|
|
|
// Check if client_id and template_name are provided
|
|
if (empty($form_data['client_id']) || empty($form_data['template_name'])) {
|
|
return $this->respond(['status' => false, 'code' => 400, 'message' => 'Client ID and template name are required.'], 400);
|
|
}
|
|
|
|
// Attempt to find the notification
|
|
$find_notification = $this->notificationModel->where('client_id', $form_data['client_id'])
|
|
->where('template_name', $form_data['template_name'])
|
|
->first();
|
|
|
|
if (!$find_notification) {
|
|
return $this->respond(['status' => false, 'code' => 404, 'message' => 'Notification data not found.'], 404);
|
|
}
|
|
|
|
// Define upload path and attempt file upload
|
|
$uploadFilePath = WRITEPATH . 'uploads/attachments';
|
|
$uploadedFile = $this->request->getFile('file');
|
|
$fileName = storage_file_Upload($uploadedFile, $uploadFilePath, UPLOAD_EXT_MAIL_ATTACHMENTS);
|
|
|
|
if ($fileName) {
|
|
// Prepare data for insertion
|
|
$data = [
|
|
'notification_id' => $find_notification['id'],
|
|
'file_name' => $fileName,
|
|
'file_path' => 'uploads/attachments/' . $fileName,
|
|
];
|
|
|
|
|
|
// Insert file attachment record
|
|
if ($this->MailAttachmentModel->insert($data)) {
|
|
// Retrieve active attachments to return in response
|
|
$attachment_data = $this->MailAttachmentModel->where('notification_id', $find_notification['id'])->where('is_active', 1)->findAll()??[];
|
|
return $this->respond([
|
|
'status' => true,
|
|
'code' => 200,
|
|
'data' => $attachment_data,
|
|
'message' => 'File uploaded successfully'
|
|
], 200);
|
|
} else {
|
|
return $this->respond(['status' => false, 'code' => 500, 'message' => 'Failed to save file attachment data.'], 500);
|
|
}
|
|
} else {
|
|
return $this->respond(['status' => false, 'code' => 400, 'message' => 'File upload failed.'], 400);
|
|
}
|
|
}
|
|
|
|
public function removeAttachmentsForMailTemplates($id, $client_id, $template_name)
|
|
{
|
|
$attachment_data_for_unlink_file = $this->MailAttachmentModel->where('id', $id)->first();
|
|
$storedName = basename((string) ($attachment_data_for_unlink_file['file_name'] ?? ''));
|
|
|
|
if ($this->MailAttachmentModel->where('id', $id)->delete()) {
|
|
|
|
if ($storedName !== '') {
|
|
$storage = \Config\Services::getFileStorageService();
|
|
$storage->delete(WRITEPATH . 'uploads/attachments', $storedName);
|
|
}
|
|
|
|
$find_notification = $this->notificationModel->where('client_id', $client_id)->where('template_name', $template_name)->first();
|
|
$attachmentDatas = $this->MailAttachmentModel->where('notification_id', $find_notification['id'])->where('is_active', 1)->findAll();
|
|
|
|
return $this->respond(['status' => true, 'code' => 200, 'message' => 'Attachment deleted successfully', 'data' => $attachmentDatas], 200);
|
|
|
|
} else {
|
|
return $this->respond(['status' => false, 'code' => 404, 'message' => 'The Client has active policy'], 200);
|
|
}
|
|
}
|
|
|
|
} |