nhance-enrollment/app/Controllers/NotificationController.php
2025-02-17 12:43:20 +05:30

283 lines
11 KiB
PHP
Executable File

<?php
// declare(strict_types=1);
namespace App\Controllers;
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;
public function __construct()
{
$this->myLogger = \Config\Services::mylogger();
$this->notificationModel = new NotificationModel();
$this->clientModel = new ClientModel();
$this->MailAttachmentModel = new MailAttachmentModel();
}
// 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');
$notificationModel = new NotificationModel();
$find_notification = $notificationModel->where('client_id',$form_data['client_id'])->where('template_name',$form_data['template_name'])->first();
if ($find_notification) {
$id = $find_notification['id'];
$form_data['updated_by'] = get_session_userid();
$notificationModel->update($id,$form_data);
$complete = "Update";
}else{
$form_data['created_by'] = get_session_userid();
$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_reminder_mail_btn',
// 'member_ecard_mail_btn',
'member_review_and_summary_mail_btn',
// 'account_maneger_summary_mail_btn',
// 'client_hr_summary_mail_btn'
];
$data = [];
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);
}
}
return json_encode("true");
}
// 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($value){
$value['data'] = $this->MailAttachmentModel->where('notification_id', $value['id'])->where('is_active', 1)->findAll();
}
// return $this->respond(['status' => true, 'code' => 200, '' => 'Attachment deleted successfully'], 200);
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
public function sentTestMail($template_id, $test_mail)
{
$notification_data = $this->notificationModel->where('id', $template_id)->where('enabled', 1)->first();
$client_data = $this->clientModel->where('id', $notification_data['client_id'])->where('is_active', 1)->first();
if(!empty($notification_data)){
$params = [
'client_data' => $client_data,
'notification_data' => $notification_data,
'test_mail' => $test_mail
];
$testMailData = sendMailNotification::sendMailNotificationForTesting($notification_data['template_name'], $params);
// print_r($testMailData); die;
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']);
}
}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 = file_Upload($uploadedFile, $uploadFilePath); // Assume file_Upload handles file saving
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('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();
$file_path = WRITEPATH . $attachment_data_for_unlink_file['file_path'];
if ($this->MailAttachmentModel->where('id', $id)->delete()) {
unlink($file_path);
$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);
}
}
}