nhance/app/Controllers/NotificationController.php
2024-09-13 09:00:09 +05:30

162 lines
5.8 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\Helpers\sendMailNotification;
class NotificationController extends AdminController
{
use ResponseTrait;
protected $myLogger;
protected $notificationModel;
public function __construct()
{
$this->myLogger = \Config\Services::mylogger();
$this->notificationModel = new NotificationModel();
}
// 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');
$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');
$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();
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;
}
}
}
}
}
}