change custom notification Comma seperator mobile number : ps

This commit is contained in:
VE10-Sanjeev 2024-04-11 07:45:01 +00:00
parent a81ddaf6b7
commit c73b186e71
3 changed files with 184 additions and 100 deletions

View File

@ -13,6 +13,7 @@ class Notifications extends BaseController
################################################################ ################################################################
public function custom_notifications(){ public function custom_notifications(){
if ($this->request->is('post')) { if ($this->request->is('post')) {
session()->setFlashdata('reset', true);
$records = $this->request->getVar(); $records = $this->request->getVar();
if($records['mode'] == 'Email'){ if($records['mode'] == 'Email'){
helper('notification'); helper('notification');
@ -45,28 +46,46 @@ class Notifications extends BaseController
}else{ }else{
$records = $this->request->getVar(); $records = $this->request->getVar();
$doc_details = $this->request->getFile('media_doc');
$params = (object) Null; $params = (object) Null;
$parents_status = 0; $parents_status = 0;
$receiving_status = 0; $receiving_status = 0;
$doc_details = $this->request->getFile('media_doc');
$doc_path = APPPATH . '../public/uploads/'; $doc_path = APPPATH . '../public/uploads/';
$doc_name = ''; $doc_name = '';
$this->logger->info("Whatsapp : sending message instance id = " . $_ENV['WAAI_INSTANCE'] . " - " . $_ENV['WAAI_TOKEN']); $this->logger->info("Whatsapp : sending message instance id = " . $_ENV['WAAI_INSTANCE'] . " - " . $_ENV['WAAI_TOKEN']);
if($records['mobile'] != ""){
$string = $records['mobile'];
$mobile_no_arr = explode(',', $string);
$total_number_count = count($mobile_no_arr);
$sended_number_count = 0;
$not_sended_number_count = 0;
if ($records['type'] == 'document') {
$doc_name = $doc_details->getName();
if($doc_name !== '' && $doc_details->isValid()) {
$doc_details->move($doc_path, $doc_name);
$this->logger->info("Name After Upload".$doc_name);
}
}
$NotificationModel = new NotificationModel();
$history_parents_data = ['campaign_id' => 0,'status' => 0];
$history_parent_id = $NotificationModel->save_notification_history_parents($history_parents_data);
// print_r($mobile_no_arr);
if(!empty($mobile_no_arr)){
try { try {
foreach ($mobile_no_arr as $mobile) {
// echo "----$mobile";
if (trim(strlen($mobile)) == 10) {
if (ctype_digit($mobile)) {
// ------------ Step 1
if ($records['categories'] == 'SEND_WAAI_GROUP_URL') { if ($records['categories'] == 'SEND_WAAI_GROUP_URL') {
if ($records['group_id'] != "") { if ($records['group_id'] != "") {
$params->group_id = $records['group_id']; $params->group_id = $records['group_id'];
} else { } else {
throw new \Exception("Group Id Missing Please Try again.."); throw new \Exception("Group Id Missing Please Try again..");
} }
if ($records['mobile'] != "") {
$params->number = (int)'91' . $records['mobile'];
} else {
throw new \Exception("Mobile Number Missing Please Try again..");
}
} else if ($records['categories'] == 'SEND_WAAI_URL') { } else if ($records['categories'] == 'SEND_WAAI_URL') {
if ($records['mobile'] != "") { if ($records['mobile'] != "") {
$params->number = (int)'91' . $records['mobile']; $params->number = (int)'91' . $mobile;
} else { } else {
throw new \Exception("Mobile Number Missing Please Try again.."); throw new \Exception("Mobile Number Missing Please Try again..");
@ -74,6 +93,8 @@ class Notifications extends BaseController
} else { } else {
throw new \Exception("Please Choose your Category"); throw new \Exception("Please Choose your Category");
} }
// echo "***".$params->number;
//---------Step 2
if ($records['type'] == 'media') { if ($records['type'] == 'media') {
if ($records['media_url'] != "") { if ($records['media_url'] != "") {
$params->media_url = $records['media_url']; $params->media_url = $records['media_url'];
@ -81,60 +102,80 @@ class Notifications extends BaseController
} else { } else {
throw new \Exception("Media Url Missing Please Try again.."); throw new \Exception("Media Url Missing Please Try again..");
} }
} }else if ($records['type'] == 'document') {
else if ($records['type'] == 'document') {
$doc_name = $doc_details->getName(); $doc_name = $doc_details->getName();
if($doc_name == ''){ if($doc_name == ''){
$this->logger->error("Err on upload name not Founded ".$records['media_doc']); $this->logger->error("Err on upload name not Founded ".$records['media_doc']);
throw new \Exception("Media Document Missing Please Try again.."); throw new \Exception("Media Document Missing Please Try again..");
}else{ }else{
if ($doc_details->isValid()) {
$doc_details->move($doc_path, $doc_name);
$this->logger->info("Name After Upload".$doc_name);
$params->media_url = base_url().'public/uploads/'.$doc_name; $params->media_url = base_url().'public/uploads/'.$doc_name;
$params->type = "media"; $params->type = "media";
}else{
$params->media_url = base_url().'public/uploads/'.$doc_name;
$params->type = "media";
}
} }
}else{ }else{
$params->type = "text" ; $params->type = "text" ;
} }
$url = constant($records['categories']); $url = constant($records['categories']);
$params->instance_id = $_ENV['WAAI_INSTANCE']; $params->instance_id = $_ENV['WAAI_INSTANCE'];
$params->access_token = $_ENV['WAAI_TOKEN']; $params->access_token = $_ENV['WAAI_TOKEN'];
$params->message = strip_tags(ltrim($records['description'])); $params->message = strip_tags(ltrim($records['description']));
$this->logger->info("Whatsapp : sending message request = " . json_encode($params)); $this->logger->info("Whatsapp : sending message request = " . json_encode($params));
$this->logger->info("Whatsapp : sending message request type b4 = " . gettype($params)); $this->logger->info("Whatsapp : sending message request type b4 = " . gettype($params));
helper('notification'); helper('notification');
$notification = new NotificationHelper(); $notification = new NotificationHelper();
$success = $notification->sendWhatsAppMessage($url, "POST", $params); $success = $notification->sendWhatsAppMessage($url, "POST", $params);
// echo "<pre>";print_r($success);echo "</pre>";
$receiving_status = $success['receiving_status']; $receiving_status = $success['receiving_status'];
$parents_status = $success['receiving_status']; $parents_status = $success['receiving_status'];
$this->logger->info("Whatsapp : Reponse ==> Mobile Number = ".$mobile." Message = ". json_encode($success['reponse']));
$history_msg = strtolower(gettype($success)) == "string" ? $success : json_encode($success['reponse']); $history_msg = strtolower(gettype($success)) == "string" ? $success : json_encode($success['reponse']);
$tag = (int)$receiving_status === 1 ? 'success' : 'error' ; // $tag = (int)$receiving_status === 1 ? 'success' : 'error' ;
session()->setFlashdata($tag, 'Message : ' . $success['message']); // session()->setFlashdata($tag, 'Message : ' . $success['message']);
$this->logger->info("Whatsapp : Reponse = " . $success['message']); $this->logger->info("Whatsapp : Reponse = " . $success['message']);
} catch (\Exception $e) { (int)$receiving_status === 1 ? $sended_number_count++ : $not_sended_number_count++;
$this->logger->error("Whatsapp : Exception Message = " . $e->getMessage() . "<br> File: " . $e->getFile() . "<br> Line: " . $e->getLine() . "<br>"); }elseif (ctype_alpha($mobile)) {
session()->setFlashdata('error', 'Message: ' . $e->getMessage()); $history_msg = "Mobile Number = ".$mobile." is a character.";
$history_msg = $e->getMessage(); $this->logger->info("Whatsapp : Reponse ==> ".$history_msg);
$not_sended_number_count++;
throw new \Exception($history_msg);
}else {
$history_msg = "Mobile Number = ".$mobile." is a combination of characters and numbers.";
$this->logger->info("Whatsapp : Reponse ==> ".$history_msg);
$not_sended_number_count++;
throw new \Exception($history_msg);
}
}else {
$history_msg = "Mobile Number = ".$mobile." does not have 10 digits.";
$this->logger->info("Whatsapp : Reponse ==> ".$history_msg);
$not_sended_number_count++;
throw new \Exception($history_msg);
} }
$NotificationModel = new NotificationModel(); $NotificationModel = new NotificationModel();
$history_parents_data = ['campaign_id' => 0,'status' => $parents_status]; $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,0);
$history_parent_id = $NotificationModel->save_notification_history_parents($history_parents_data); $this->logger->info($history_parent_statement);
$history_child_data = [ 'fkid' => $history_parent_id, $history_child_data = [ 'fkid' => $history_parent_id,
'customer_id' => 0, 'customer_id' => 0,
'receiving_entity' => $records['mobile'], 'receiving_entity' => $mobile." (original : ".$records['mobile'].")",
'receiving_status' => $receiving_status, 'receiving_status' => $receiving_status,
'message' => $records['description'], 'message' => $records['description'],
'result'=>$history_msg]; 'result'=>$history_msg];
$history_child_id = $NotificationModel->save_notification_history_child($history_child_data); $history_child_id = $NotificationModel->save_notification_history_child($history_child_data);
$this->logger->info('Custom Notifications Whatsapp : historychildid = '.$history_child_id); $this->logger->info('Custom Notifications Whatsapp : historychildid = '.$history_child_id);
} }
session()->setFlashdata("success","Message : Sent Success (". $sended_number_count." / ".$total_number_count." )");
$this->logger->info("Whatsapp : Final ==> ".$sended_number_count." Sended out of ".$total_number_count." ( Not sended numbers count ".$not_sended_number_count." ) ");
} catch (\Exception $e) {
$this->logger->error("Whatsapp : Exception Message = " . $e->getMessage() . "<br> File: " . $e->getFile() . "<br> Line: " . $e->getLine() . "<br>");
session()->setFlashdata('error', 'Message: ' . $e->getMessage());
}
}else{
session()->setFlashdata('error', 'Message : Mobile Not Founded');
}
}else{
session()->setFlashdata('error', 'Message : Mobile Field Empty');
}
}
} }
$records = []; $records = [];
$data['page_name'] = 'Custom Notifications'; $data['page_name'] = 'Custom Notifications';

View File

@ -296,6 +296,17 @@ public function customerGroupQueryExecution($queries){
return $query->get()->getResultArray(); return $query->get()->getResultArray();
} }
public function getAllCustomerMobileNumber(){
$arr = $this->select('mobile_no')
->where('mobile_no IS NOT NULL')
->where('mobile_no !=', '')
->groupBy('mobile_no')
->having('COUNT(*) >', 1)
->findAll();
return count($arr)>0 ? $arr : [];
}
} }

View File

@ -22,7 +22,7 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<?php endif; ?> <?php session()->setFlashdata('reset', true); endif; ?>
<?php if (session()->getFlashdata('error')) : ?> <?php if (session()->getFlashdata('error')) : ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert"> <div class="alert alert-danger alert-dismissible fade show" role="alert">
<?= session('error') ?> <?= session('error') ?>
@ -30,11 +30,10 @@
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<?php endif; ?> <?php session()->setFlashdata('reset', true); endif; ?>
<?php endif; ?> <?php endif; ?>
<br> <br>
<form role="form" class="parsley-examples" method="POST" enctype="multipart/form-data" action="<?= base_url() . "custom_notifications"; ?>" id="notifyCustomForm"> <form role="form" class="parsley-examples" method="POST" enctype="multipart/form-data" action="<?= base_url() . "custom_notifications"; ?>" id="notifyCustomForm">
<div class="form-group row"> <div class="form-group row">
<label class="col-3 col-form-label" for="mode">Mode</label> <label class="col-3 col-form-label" for="mode">Mode</label>
<div class="col-9"> <div class="col-9">
@ -109,15 +108,21 @@
</div> </div>
<div id="fields-for-mobile"> <div id="fields-for-mobile">
<div class="form-group row"> <div class="form-group row">
<label for="mobile" class="col-3 col-form-label">Mobile Number<span class="text-danger"> *</span></label> <label for="mobile" class="col-3 col-form-label">Mobile Number<span class="text-danger symbol"> *</span></label>
<div class="col-9"> <div class="col-7">
<!-- <input type="number" placeholder="Mobile Number" class="form-control" id="mobile" name="mobile"> --> <!-- <input type="number" placeholder="Mobile Number" class="form-control" id="mobile" name="mobile"> -->
<!-- <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" data-toggle="input-mask" data-mask-format="0000000000" <input type="text" class="form-control" id="cmobile" name="cmobile" value="<?= isset($customer['mobile_no']) ? $customer['mobile_no'] : '' ?>" placeholder="Mobile Number (Enter only numbers)" required maxlength="10" onkeypress="return restriction(event)" /> --> <!-- <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" data-toggle="input-mask" data-mask-format="0000000000" <input type="text" class="form-control" id="cmobile" name="cmobile" value="<?= isset($customer['mobile_no']) ? $customer['mobile_no'] : '' ?>" placeholder="Mobile Number (Enter only numbers)" required maxlength="10" onkeypress="return restriction(event)" /> -->
<!-- <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" data-toggle="input-mask" data-mask-format="0000000000"> --> <!-- <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" data-toggle="input-mask" data-mask-format="0000000000"> -->
<!-- Modified input with validation --> <!-- Modified input with validation -->
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" maxlength="10" onkeypress="return restrictNonNumeric(event)"> <input type="text" class="form-control" id="mobile" name="mobile" placeholder="Mobile Number" onkeypress="return restrictNonNumeric(event)">
<span id="mobileValidationMessage"></span> <span id="mobileValidationMessage"></span>
</div> </div>
<div class="col-2" style="display: none;">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="all_customers" name="all_customers">
<label class="form-check-label" for="all_customers">All Customers</label>
</div>
</div>
</div> </div>
</div> </div>
@ -136,7 +141,7 @@
</div> </div>
<div class="form-check form-check-inline"> <div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="type" value="document" onchange="change_fields(this.value);"> <input class="form-check-input" type="radio" name="type" value="document" onchange="change_fields(this.value);">
<span class="form-check-label">Document File</label> <span class="form-check-label">Upload Image</label>
</div> </div>
</div> </div>
</div> </div>
@ -229,6 +234,31 @@
// makeFormEmpty(); // makeFormEmpty();
} }
} }
const checkbox = document.getElementById("all_customers");
// When the checkbox is changed
checkbox.addEventListener("change", function () {
// Get the mobile input element
const mobileInput = document.getElementById("mobile");
document.getElementById('mobileValidationMessage').textContent = "";
// If the checkbox is checked
if (this.checked) {
// Set mobile input field to readonly and clear its value
mobileInput.readOnly = true;
mobileInput.value = "";
// Make the mobile input field not required
mobileInput.removeAttribute("required");
} else {
// If the checkbox is not checked, remove readonly attribute
mobileInput.readOnly = false;
// Make the mobile input field required
mobileInput.setAttribute("required", "required");
}
});
// Get references to the dropdown and field divs // Get references to the dropdown and field divs
const fieldsForMediaURL = document.getElementById("fields-for-mediaurl"); const fieldsForMediaURL = document.getElementById("fields-for-mediaurl");
const fieldsForDoc = document.getElementById("fields-for-doc"); const fieldsForDoc = document.getElementById("fields-for-doc");
@ -356,7 +386,9 @@
<script> <script>
function restrictNonNumeric(event) { function restrictNonNumeric(event) {
var keyCode = event.which ? event.which : event.keyCode; var keyCode = event.which ? event.which : event.keyCode;
var isValid = (keyCode >= 48 && keyCode <= 57) || keyCode === 8; // Allow numeric input and backspace var isValid = (keyCode >= 48 && keyCode <= 57) || // Numeric keys
keyCode === 8 || // Backspace
keyCode === 44; // Comma
if (!isValid) { if (!isValid) {
event.preventDefault(); event.preventDefault();
return false; return false;
@ -364,19 +396,19 @@
return true; return true;
} }
document.getElementById('mobile').addEventListener('input', function() { // document.getElementById('mobile').addEventListener('input', function() {
var mobileNumber = this.value; // var mobileNumber = this.value;
var isValid = /^[0-9]{10}$/.test(mobileNumber); // var isValid = /^[0-9]{10}$/.test(mobileNumber);
var validationMessage = document.getElementById('mobileValidationMessage'); // var validationMessage = document.getElementById('mobileValidationMessage');
if (isValid) { // if (isValid) {
validationMessage.textContent = 'Valid mobile number'; // validationMessage.textContent = 'Valid mobile number';
validationMessage.style.color = "green"; // validationMessage.style.color = "green";
} else { // } else {
validationMessage.textContent = 'Invalid mobile number'; // validationMessage.textContent = 'Invalid mobile number';
validationMessage.style.color = "red"; // validationMessage.style.color = "red";
} // }
}); // });
</script> </script>
<script> <script>
function makeFormEmpty() { function makeFormEmpty() {