is('post')) {
$records = $request->getVar();
$records['template_name'] = "";
helper('notification');
$notification = new NotificationHelper();
$data = $notification->sendEmail($records);
if (isset($data['error'])) {
session()->setFlashdata('error', 'Message: ' . $data['error']);
}
if (isset($data['success'])) {
session()->setFlashdata('success', 'Message: ' . $data['success']);
}
}
$data['page_name'] = 'Custom Notifications';
$this->render_page('notifications_form', $data);
}
public function whatsapp_custom_notifications()
{
$request = \Config\Services::request();
if ($request->is('post')) {
$records = $request->getVar();
$params = (object) Null;
$this->logger->info("Whatsapp : sending message instance id = " . $_ENV['WAAI_INSTANCE'] . " - " . $_ENV['WAAI_TOKEN']);
try {
if ($records['categories'] == 'SEND_WAAI_GROUP_URL') {
if ($records['group_id'] != "") {
$params->group_id = $records['group_id'];
} else {
throw new \Exception("Group Id Missing Please Try again..");
}
} else if ($records['categories'] == 'SEND_WAAI_URL') {
if ($records['mobile'] != "") {
$params->number = (int)'91' . $records['mobile'];
} else {
throw new \Exception("Mobile Number Missing Please Try again..");
}
} else {
throw new \Exception("Please Choose your Category");
}
if ($records['type'] == 'media') {
if ($records['media_url'] != "") {
$params->media_url = $records['media_url'];
} else {
throw new \Exception("Media Url Missing Please Try again..");
}
}
$url = constant($records['categories']);
$params->type = $records['type'] == "" ? "text" : $records['type'];
$params->instance_id = $_ENV['WAAI_INSTANCE'];
$params->access_token = $_ENV['WAAI_TOKEN'];
$params->message = $records['description'];
$this->logger->info("Whatsapp : sending message request = " . json_encode($params));
$this->logger->info("Whatsapp : sending message request type b4 = " . gettype($params));
helper('notification');
$notification = new NotificationHelper();
$success = $notification->sendWhatsAppMessage($url, "POST", $params);
session()->setFlashdata('success', 'Message : ' . $success);
$this->logger->info("Whatsapp : Reponse = " . $success);
} catch (\Exception $e) {
$this->logger->error("Whatsapp : Exception Message = " . $e->getMessage() . "
File: " . $e->getFile() . "
Line: " . $e->getLine() . "
");
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
}
}
$data['page_name'] = 'Custom Notifications';
$this->render_page('notifications_form', $data);
}
public function due_date_notifications()
{
$date = $this->request->getGet('date');
$model = new NotificationModel();
$details = $model->getSubscriptionDueDetail($date);
// echo "
";
// print_r($details);
// echo "";
// die;
$records = [];
$recipient_name = "";
$recipient_email = "";
$recipient_mobile = "";
$subscription_name = "";
$business_name = "";
$to_subscription = "";
if (isset($details)) {
$this->logger->info("Notification : Duedate notification Request data type = " . gettype($details));
}
helper('notification');
$notification = new NotificationHelper();
foreach ($details as $rec) {
$recipient_name = $rec['customer_name'];
$recipient_email = $rec['customer_email'];
$recipient_mobile = $rec['customer_mobile'];
// $recipient_email = "sanjeev.p@venbainfotech.com";
// $recipient_mobile = 6369084112;
$business_name = $rec['business_name'];
$subscription_name = $rec['title'];
$to_subscription = $rec['to_subscription'];
}
$records['template_name'] = '';
$records['recipient_name'] = $recipient_name;
$records['recipient_email'] = $recipient_email ? $recipient_email : "sanjeev.p@venbainfotech.com";
$records['subject'] = $business_name . " - Due Date Reminder";
$records['description'] = "
Hello " . $recipient_name . ",
This is a reminder that the following subscription is due soon:
Subscription Name:" . $subscription_name . "
Due Date: " . $to_subscription . "
Please make sure to complete this subscription on time.
Thank you.
"; $template = "Hello " . $recipient_name . ",\r\r\n\nThis is a reminder that the following subscription is due soon:\r\n- Subscription Name: " . $subscription_name . "\r\n Due Date: " . $to_subscription . "\r\n\nPlease make sure to complete this subscription on time.\n Thank you."; $params = (object) Null; $params->number = (int)'91' . $recipient_mobile; $params->type = "text"; $params->message = $template; $params->instance_id = $_ENV['WAAI_INSTANCE']; $params->access_token = $_ENV['WAAI_TOKEN']; $this->logger->info("Notification : Duedate notification Email Request data = " . json_encode($records)); $email_result = $notification->sendEmail($records); $this->logger->info("Notification : Duedate notification Email Response = " . json_encode($email_result)); $this->logger->info("Notification : Duedate notification Whatsapp Request data = " . json_encode($params)); $whatsapp_result = $notification->sendWhatsAppMessage(SEND_WAAI_URL, "POST", $params); $this->logger->info("Notification : Duedate notification Whatsapp Response = " . json_encode($whatsapp_result)); } public function template_creation() { $data['page_name'] = 'Template Details'; $model = new NotificationModel(); $data['template'] = $model->getTemplateDetails(); $this->render_page('template_creation', $data); // echo "templates";die; } public function template_creation_form($id) { $data['group_details'] = $this->get_group_details(); $model = new NotificationModel(); if ($id === '0') { $this->logger->info("Template Creation: In Add Page"); $data['page_name'] = 'Add Template'; $data['template_details'] = []; } elseif ($id !== '0') { $this->logger->info("Template Creation: In Edit Page ID =" . $id); $data['page_name'] = 'Edit Template'; $model->setTable('templates'); $where = ['template_id' => (int)$id]; $result = $model->where($where)->findAll(); $data['template_details'] = !empty($result[0]) ? $result[0] : []; } // print_r($data['template_details']);die(); $this->render_page('template_creation_form', $data); } public function template_creation_delete($id) { try { helper('session'); $session_uid = get_logged_user_id(); $model = new NotificationModel(); $model->setTable('templates'); $where = ['template_id' => (int)$id]; $result = $model->where($where)->findAll(); if ($result) { $this->logger->Info("Template: Going to Inactive ID = " . $id); $data = ['isactive' => 0, 'updated_by' => $session_uid, 'template_id' => (int)$id]; $statement = $model->saveDetails($data, 'template_id', 'templates'); // just updating Inactive status only. if ($statement['success']) { session()->setFlashdata('success', $statement['success'] ? 'Deleted successfully.' : ""); $this->logger->info($statement['log']); } else if ($statement['error']) { session()->setFlashdata('error', $statement['error']); $this->logger->error($statement['log']); } else { throw new \Exception("Template: Err Occur on data the Template Inactive"); } } } catch (\Exception $e) { $this->logger->error("Template: Err Occur = " . $e->getMessage() . " File = " . $e->getFile() . " Line = " . $e->getLine()); session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } return redirect()->route('template_creation'); } public function get_group_details() { $model = new NotificationModel(); $model->setTable('customer_groups'); // $details = $model->where('isactive',1)->orderBy('group_id', 'ASC')->findAll(); $where = ['isactive' => 1, 'group_name != ' => 'EXPIRYDATE']; $details = $model->where($where)->orderBy('group_id', 'ASC')->findAll(); return $details; } public function get_template_details() { $model = new NotificationModel(); $model->setTable('templates'); $details = $model->where('isactive', 1)->orderBy('template_id', 'ASC')->findAll(); return $details; } public function template_creation_insert() { $this->logger->info("Template: Inserting/Updating Details"); try { ## Declarions helper('session'); $model = new NotificationModel(); $session_uid = get_logged_user_id(); $id = $this->request->getPost('template_id'); $subject = $this->request->getPost('subject') ? $this->request->getPost('subject') : NULL; $isactive = $id != "" ? $this->request->getPost('isactive') : 'on'; $data = [ 'template_id' => $id, 'template_name' => $this->request->getPost('templatename'), 'subject' => $subject, 'message' => $this->request->getPost('templatemessage'), 'mode' => $this->request->getPost('mode'), 'isactive' => (($isactive == 'on') ? 1 : 0), 'created_by' => $session_uid, 'updated_by' => $session_uid ]; $statement = $model->saveDetails($data, 'template_id', 'templates'); if ($statement['success']) { session()->setFlashdata('success', $statement['success']); $this->logger->info($statement['log']); } else if ($statement['error']) { session()->setFlashdata('error', $statement['error']); $this->logger->error($statement['log']); } else { throw new \Exception("Template: Err Occur"); } } catch (\Exception $e) { $this->logger->error("Template: Err Occur =" . $e->getMessage()); session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } return redirect()->route('template_creation'); } public function campaign_creation() { $data['page_name'] = 'Campaign Details'; $model = new NotificationModel(); $data['campaign'] = $model->getCampaignDetails(); $this->render_page('campaign_creation', $data); // echo "templates";die; } public function campaign_creation_form($id) { $data['group_details'] = $this->get_group_details(); $data['template_details'] = $this->get_template_details(); $model = new NotificationModel(); if ($id === '0') { $this->logger->info("Campaign Creation: In Add Page"); $data['page_name'] = 'Add Campaign'; $data['campaign_details'] = []; } elseif ($id !== '0') { $this->logger->info("Campaign Creation: In Edit Page ID =" . $id); $data['page_name'] = 'Edit Campaign'; $model->setTable('notification_campaign'); $where = ['campaign_id' => (int)$id]; $result = $model->where($where)->findAll(); if (!empty($result)) { foreach ($result as $i => $item) { $result[$i]['group_id'] = unserialize($item['group_id']); } } $data['campaign_details'] = !empty($result[0]) ? $result[0] : []; } $this->render_page('campaign_creation_form', $data); } public function campaign_creation_delete($id) { try { helper('session'); $session_uid = get_logged_user_id(); $model = new NotificationModel(); $model->setTable('notification_campaign'); $where = ['campaign_id' => (int)$id]; $result = $model->where($where)->findAll(); if ($result) { $this->logger->Info("Campaign: Going to Inactive ID = " . $id); $data = ['isactive' => 0, 'updated_by' => $session_uid, 'campaign_id' => (int)$id]; $statement = $model->saveDetails($data, 'campaign_id', 'notification_campaign'); // just updating Inactive status only. if ($statement['success']) { session()->setFlashdata('success', $statement['success'] ? 'Deleted successfully.' : ""); $this->logger->info($statement['log']); } else if ($statement['error']) { session()->setFlashdata('error', $statement['error']); $this->logger->error($statement['log']); } else { throw new \Exception("Campaign: Err Occur on data the Template Inactive"); } } } catch (\Exception $e) { $this->logger->error("Campaign: Err Occur = " . $e->getMessage() . " File = " . $e->getFile() . " Line = " . $e->getLine()); session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } return redirect()->route('campaign_creation'); } public function campaign_creation_insert() { $this->logger->info("Campaign: Inserting/Updating Details"); try { ## Declarions helper('session'); $model = new NotificationModel(); $session_uid = get_logged_user_id(); $id = $this->request->getPost('campaign_id'); $request_data = $this->request->getPost(); $isactive = $id == "" ? 1 : (isset($request_data['isactive']) && $request_data['isactive'] == 'on' ? 1 : 0); $data = [ 'campaign_id' => $id, 'campaign_name' => $this->request->getPost('campaign_name'), 'template_id' => $this->request->getPost('template_id'), 'scheduled_date' => $this->request->getPost('scheduled_date'), 'scheduled_time' => $this->request->getPost('scheduled_time'), 'mode' => $this->request->getPost('mode'), 'group_id' => serialize($this->request->getPost('group_id')), 'isactive' => $isactive, 'created_by' => $session_uid, 'updated_by' => $session_uid ]; if ($id != "") { $notify_mod = new NotificationModel(); $notify_mod->setTable('notification_campaign'); $where = ['campaign_id' => (int)$id]; $isactive_check = $notify_mod->where($where)->select('isactive')->first(); if ((int)$isactive_check['isactive'] === 0 && $isactive === 0) { $statement['success'] = 'Notification Campaign already In-Active. can`t able to update.'; $statement['error'] = ""; $statement['log'] = "Notification Campaign: already inactive status.Can`t able to update, Notification Campaign Group ID = " . $id; } else { $statement = $model->saveDetails($data, 'campaign_id', 'notification_campaign'); } } else { $statement = $model->saveDetails($data, 'campaign_id', 'notification_campaign'); } if ($statement['success']) { session()->setFlashdata('success', $statement['success']); $this->logger->info($statement['log']); } else if ($statement['error']) { session()->setFlashdata('error', $statement['error']); $this->logger->error($statement['log']); } else { throw new \Exception("Campaign: Err Occur"); } } catch (\Exception $e) { $this->logger->error("Campaign: Err Occur =" . $e->getMessage()); session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } return redirect()->route('campaign_creation'); } public function getExpCustomerDetail($window_time = null) { // Step 1: add the days to retrive the data// $providedDate = date('Y-m-d'); $newDate = date('Y-m-d', strtotime($providedDate . ' + ' . $window_time . ' days')); // Step 2 :Get the customer detail// $NotificationModel = new NotificationModel(); $ExpCustomerDetail = $NotificationModel->getExpDate(); // Retrieve the stored query if (empty($ExpCustomerDetail)) { $this->logger->info('No customer group found by the name of EXPIRYDATE'); return; } //Step 3: Replace the placeholder WINDOW_TIME with the user-provided value $modifiedQuery = str_replace('WINDOW_TIME', $window_time, $ExpCustomerDetail); if (empty($modifiedQuery)) { $this->logger->info('There is no sub query'); return; } $db = \Config\Database::connect(); $queryResult = $db->query($modifiedQuery)->getResultArray(); $notification = new NotificationHelper(); if (empty($queryResult)) { $this->logger->info("There is no customer expiring on date:{$newDate}"); return; } foreach ($queryResult as $row) { $userFirstName = $row['first_name']; $userLastName = $row['last_name']; $fullName = $userFirstName . ' ' . $userLastName; $userMobileNumber = $row['mobile_no']; $schemename = $row['title']; $expirydate = $row['to_subscription']; if (empty($fullName)) { $this->logger->info('There is no subcription Name'); continue; } $emailTemplateName = $NotificationModel->getTempName(EXPIRAY_NOTIFY_TEMPLATE_EMAIL); if (!empty($emailTemplateName)) { //Step 4: Prepare email content based on the template $emailContent = $emailTemplateName[0]['message']; $emailContent = str_replace("{USER_NAME}", $fullName, $emailContent); $emailContent = str_replace("{SCHEME_NAME}", $schemename, $emailContent); $emailContent = str_replace("{EXPIRY_DATE}", $expirydate, $emailContent); $emailData = [ 'template_name' => '', 'recipient_email' => $row['email'], 'subject' => 'Your Subscription Expiry Notification', 'description' => $emailContent, ]; // Step 5:Send email using NotificationHelper $emailResult = $notification->sendEmail($emailData); if (isset($emailResult['success'])) { echo 'Email sent successfully'; } else { echo 'Failed to send email'; } } else { $this->logger->info('There is no email template found by the name of EXPIRAY_NOTIFY_TEMPLATE_EMAIL'); continue; } // $emailTemplateName $whatsappTemplateName = $NotificationModel->getTempName(EXPIRAY_NOTIFY_TEMPLATE_WHATSAPP); if (!empty($whatsappTemplateName)) { if (empty($userMobileNumber)) { $this->logger->info('There is no subscriber mobile number'); continue; } //Step 6: Prepare whatsapp content based on the template $whatsappContent = $whatsappTemplateName[0]['message']; $whatsappContent = str_replace("{USER_NAME}", $fullName, $emailContent); $whatsappContent = str_replace("{SCHEME_NAME}", $schemename, $emailContent); $whatsappContent = str_replace("{EXPIRY_DATE}", $expirydate, $emailContent); // Step 7: Send email using NotificationHelper $params = (object) Null; $url = SEND_WAAI_URL; $params->number = (int)'91' . $userMobileNumber; $params->type = "text"; $params->message = $whatsappContent; $params->instance_id = $_ENV['WAAI_INSTANCE']; $params->access_token = $_ENV['WAAI_TOKEN']; $this->logger->info("EXPIRAY NOTIFY TEMPLATE WHATSAPP Request = " . json_encode($params)); $this->logger->info("EXPIRAY NOTIFY TEMPLATE WHATSAPP Request type b4 = " . gettype($params)); $whatsapp_result = $notification->sendWhatsAppMessage($url, "POST", $params); $this->logger->info("EXPIRAY NOTIFY TEMPLATE WHATSAPP Reponse = " . $whatsapp_result); } else { $this->logger->info('There is no Whatsapp template found by the name of EXPIRAY NOTIFY TEMPLATE WHATSAPP '); continue; } // $Whatsapp Template Name } // for loop } ################################################################ # Auto Scheduled Notification ################################################################ ## notification campaign details public function auto_scheduled_notification(){ $this->logger->info("Auto Scheduled Notification Started"); $model = new NotificationModel(); $model->setTable('notification_campaign'); $where = ['status'=>0]; $notification_campaign_result = $model->where($where)->findAll(); $today = date('Y-m-d'); $get_notification_campaign_details = []; if (!empty($notification_campaign_result)) { foreach ($notification_campaign_result as $n => $ncr) { if($today == $ncr['scheduled_date']){ $this->logger->info("Notification Campaign : Scheduled Date = " .$ncr['scheduled_date']."/".$ncr['scheduled_time'].", Today = ".$today ); $get_notification_campaign_details[] = $ncr; }else{ $this->logger->info("Notification Campaign : There is no Scheduled, bcoz Date Are Different = " .$ncr['scheduled_date'].", Today = ".$today ); } } }else{ $this->logger->info("Notification Campaign : Details not found "); echo "Notification Campaign : Details not found" ; return []; } if(!empty($get_notification_campaign_details)){ $this->logger->info("Notification Campaign : Details founded proceed next Step"); $this->get_auto_scheduled_template_details($get_notification_campaign_details); }else{ $this->logger->info("Notification Campaign : Details not found "); echo "Notification Campaign : Details not found" ; return []; } } ## template_details public function get_auto_scheduled_template_details($nc_dls){ $template_result = []; $model = new NotificationModel(); if (!empty($nc_dls)) { foreach ($nc_dls as $i => $item) { $group_id = $item['group_id']; $template_id = $item['template_id']; $template_result[$i]['campaign_id'] = $item['campaign_id']; $template_result[$i]['campaign_name'] = $item['campaign_name']; $template_result[$i]['campaign_mode'] = $item['mode']; $template_result[$i]['campaign_status'] = $item['status']; $template_result[$i]['campaign_isactive'] = $item['isactive']; $template_result[$i]['campaign_template_id'] = $template_id; $template_result[$i]['scheduled_date'] = $item['scheduled_date']; $template_result[$i]['scheduled_time'] = $item['scheduled_time']; $template_result[$i]['group_id'] = unserialize($group_id); $model->setTable('templates'); $where = ['template_id'=>$template_id]; $result = $model->where($where)->findAll(); if(!empty($result)){ foreach ($result as $s => $sub_array) { $template_result[$i]['template_id'] = $sub_array['template_id']; $template_result[$i]['template_name'] = $sub_array['template_name']; $template_result[$i]['template_mode'] = $sub_array['mode']; $template_result[$i]['template_isactive'] = $sub_array['isactive']; $template_result[$i]['subject'] = $sub_array['subject']; $template_result[$i]['message'] = $sub_array['message']; } } } } else { $this->logger->info("Template : Details not found "); echo "Template : Details not found" ; return []; } if(!empty($template_result)){ // echo "";
$this->logger->info("Template : Details founded proceed next step ");
$this->get_auto_scheduled_customer_group_details($template_result);
// echo "";
}else{
$this->logger->info("Template : Details not found ");
echo "Template : Details not found" ;
return [];
}
}
#Customer Group Details
public function get_auto_scheduled_customer_group_details($t_dls){
// echo "";
// print_r($t_dls);
// echo "";
// return;
$customer_group_result = [];
$model = new NotificationModel();
if (!empty($t_dls)) {
foreach ($t_dls as $i => $item) {
$customer_group_result[$i] = $item;
$group_id = $item['group_id'];
$model->setTable('customer_groups');
$group_arr_result = $model->whereIn('group_id', $group_id)->findAll();
// echo "".$i;
// print_r($item);
// echo "";
if(!empty( $group_arr_result)){
foreach ($group_arr_result as $s => $sub_array) {
$customer_group_result[$i]['group_name'] = $sub_array['group_name'];
$customer_group_result[$i]['group_isactive'] = $sub_array['isactive'];
$db = \Config\Database::connect();
$sql = (string)$sub_array['group_query'];
$customer_group_result[$i]['group_query'] = $sql;
if($sql){
$query = $db->query($sql);
$results = $query->getResultArray();
if(!empty($results)){
$customer_group_result[$i]['group_query_results'] = $results;
$customer_group_result[$i]['group_query_customer_count'] = count($results);
}else{
$customer_group_result[$i]['group_query_results'] = [];
$customer_group_result[$i]['group_query_customer_count'] = 0;
}
}else{
$customer_group_result[$i]['group_query_results'] = [];
$customer_group_result[$i]['group_query_customer_count'] = 0;
}
}
}
}
} else {
$this->logger->info("Customer Groups : Details not found ");
echo "Customer Groups : Details not found" ;
return [];
}
if(!empty($customer_group_result)){
echo "";
$final = $this->get_auto_scheduled_content_replacement($customer_group_result);
print_r($final);
echo "";
}else{
$this->logger->info("Customer Groups : Details not found ");
echo "Customer Groups : Details not found" ;
return [];
}
}
# D
public function get_auto_scheduled_content_replacement($data){
// echo "";
// echo print_r($data);
// echo "";
$return_statement = [];
if (!empty($data)) {
foreach($data as $inx => $item) {
$return_statement[$inx] = $item;
switch($item['campaign_mode']) {
case "Whatsapp":
$query_results = $item['group_query_results'];
$whatsappContent = $item['message'];
foreach($query_results as $q => $qr) {
$whatsappContent = str_replace("{USER_NAME}", $qr["customer_name"], $whatsappContent);
$whatsappContent = str_replace("{SCHEME_NAME}", $qr["scheme_name"], $whatsappContent);
$whatsappContent = str_replace("{EXPIRY_DATE}", $qr["formatted_due_date"], $whatsappContent);
$params = (object) Null;
$url = SEND_WAAI_URL;
$params->number = (int)'91' . $qr["mobile_no"];
$params->type = "text";
$params->message = $whatsappContent;
$params->instance_id = $_ENV['WAAI_INSTANCE'];
$params->access_token = $_ENV['WAAI_TOKEN'];
$return_statement[$inx]['group_query_results'][$q]['content'] = $params;
$return_statement[$inx]['group_query_results'][$q]['mode'] = $item['campaign_mode'];
}
break;
case "Email":
$emailContent = $item['message'];
$query_results = $item['group_query_results'];
foreach($query_results as $q => $qr) {
$emailContent = str_replace("{USER_NAME}", $qr["customer_name"], $emailContent);
$emailContent = str_replace("{SCHEME_NAME}", $qr["scheme_name"], $emailContent);
$emailContent = str_replace("{EXPIRY_DATE}", $qr["formatted_due_date"], $emailContent);
$emailData = [
'template_name' => '',
'recipient_email' => $qr["email"],
'subject' => $item['subject'] != "" ? $item['subject'] : ucfirst(str_replace('_', ' ', (string)$qr["scheme_name"]))." Notification" ,
'description' => $emailContent,
];
$return_statement[$inx]['group_query_results'][$q]['content'] = $emailData;
$return_statement[$inx]['group_query_results'][$q]['mode'] = $item['campaign_mode'];
}
break;
default:break;
}
}
}
return $return_statement;
}
############## Auto Scheduled Notification Ends ################
}