From c73b186e7139de1c251ddaaebe843ffad4354a21 Mon Sep 17 00:00:00 2001 From: VE10-Sanjeev Date: Thu, 11 Apr 2024 07:45:01 +0000 Subject: [PATCH] change custom notification Comma seperator mobile number : ps --- app/Controllers/Notifications.php | 201 ++++++++++++++++++------------ app/Models/NotificationModel.php | 11 ++ app/Views/notifications_form.php | 72 ++++++++--- 3 files changed, 184 insertions(+), 100 deletions(-) diff --git a/app/Controllers/Notifications.php b/app/Controllers/Notifications.php index 6e027bf6..1b5f2196 100755 --- a/app/Controllers/Notifications.php +++ b/app/Controllers/Notifications.php @@ -13,6 +13,7 @@ class Notifications extends BaseController ################################################################ public function custom_notifications(){ if ($this->request->is('post')) { + session()->setFlashdata('reset', true); $records = $this->request->getVar(); if($records['mode'] == 'Email'){ helper('notification'); @@ -45,95 +46,135 @@ class Notifications extends BaseController }else{ $records = $this->request->getVar(); + $doc_details = $this->request->getFile('media_doc'); $params = (object) Null; $parents_status = 0; - $receiving_status = 0; - $doc_details = $this->request->getFile('media_doc'); + $receiving_status = 0; $doc_path = APPPATH . '../public/uploads/'; $doc_name = ''; $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.."); - } - 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') { - 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']; - $params->type = "media"; - } else { - throw new \Exception("Media Url Missing Please Try again.."); - } - } - else if ($records['type'] == 'document') { - $doc_name = $doc_details->getName(); - - if($doc_name == ''){ - $this->logger->error("Err on upload name not Founded ".$records['media_doc']); - throw new \Exception("Media Document Missing Please Try again.."); - }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->type = "media"; - }else{ - $params->media_url = base_url().'public/uploads/'.$doc_name; - $params->type = "media"; + 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 { + 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['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' . $mobile; + + } else { + throw new \Exception("Mobile Number Missing Please Try again.."); + } + } else { + throw new \Exception("Please Choose your Category"); + } + // echo "***".$params->number; + //---------Step 2 + if ($records['type'] == 'media') { + if ($records['media_url'] != "") { + $params->media_url = $records['media_url']; + $params->type = "media"; + } else { + throw new \Exception("Media Url Missing Please Try again.."); + } + }else if ($records['type'] == 'document') { + $doc_name = $doc_details->getName(); + if($doc_name == ''){ + $this->logger->error("Err on upload name not Founded ".$records['media_doc']); + throw new \Exception("Media Document Missing Please Try again.."); + }else{ + $params->media_url = base_url().'public/uploads/'.$doc_name; + $params->type = "media"; + } + }else{ + $params->type = "text" ; + } + $url = constant($records['categories']); + $params->instance_id = $_ENV['WAAI_INSTANCE']; + $params->access_token = $_ENV['WAAI_TOKEN']; + $params->message = strip_tags(ltrim($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); + // echo "
";print_r($success);echo "
"; + $receiving_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']); + // $tag = (int)$receiving_status === 1 ? 'success' : 'error' ; + // session()->setFlashdata($tag, 'Message : ' . $success['message']); + $this->logger->info("Whatsapp : Reponse = " . $success['message']); + (int)$receiving_status === 1 ? $sended_number_count++ : $not_sended_number_count++; + }elseif (ctype_alpha($mobile)) { + $history_msg = "Mobile Number = ".$mobile." is a character."; + $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(); + $history_parent_statement = $NotificationModel->update_notification_history_parent_status(['status' => 1],$history_parent_id,0); + $this->logger->info($history_parent_statement); + $history_child_data = [ 'fkid' => $history_parent_id, + 'customer_id' => 0, + 'receiving_entity' => $mobile." (original : ".$records['mobile'].")", + 'receiving_status' => $receiving_status, + 'message' => $records['description'], + 'result'=>$history_msg]; + $history_child_id = $NotificationModel->save_notification_history_child($history_child_data); + $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() . "
File: " . $e->getFile() . "
Line: " . $e->getLine() . "
"); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); + } + }else{ + session()->setFlashdata('error', 'Message : Mobile Not Founded'); + } }else{ - $params->type = "text" ; + session()->setFlashdata('error', 'Message : Mobile Field Empty'); } - - $url = constant($records['categories']); - $params->instance_id = $_ENV['WAAI_INSTANCE']; - $params->access_token = $_ENV['WAAI_TOKEN']; - $params->message = strip_tags(ltrim($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); - $receiving_status = $success['receiving_status']; - $parents_status = $success['receiving_status']; - $history_msg = strtolower(gettype($success)) == "string" ? $success : json_encode($success['reponse']); - $tag = (int)$receiving_status === 1 ? 'success' : 'error' ; - session()->setFlashdata($tag, 'Message : ' . $success['message']); - $this->logger->info("Whatsapp : Reponse = " . $success['message']); - } catch (\Exception $e) { - $this->logger->error("Whatsapp : Exception Message = " . $e->getMessage() . "
File: " . $e->getFile() . "
Line: " . $e->getLine() . "
"); - session()->setFlashdata('error', 'Message: ' . $e->getMessage()); - $history_msg = $e->getMessage(); - } - $NotificationModel = new NotificationModel(); - $history_parents_data = ['campaign_id' => 0,'status' => $parents_status]; - $history_parent_id = $NotificationModel->save_notification_history_parents($history_parents_data); - $history_child_data = [ 'fkid' => $history_parent_id, - 'customer_id' => 0, - 'receiving_entity' => $records['mobile'], - 'receiving_status' => $receiving_status, - 'message' => $records['description'], - 'result'=>$history_msg]; - $history_child_id = $NotificationModel->save_notification_history_child($history_child_data); - $this->logger->info('Custom Notifications Whatsapp : historychildid = '.$history_child_id); } } $records = []; diff --git a/app/Models/NotificationModel.php b/app/Models/NotificationModel.php index 12a59c59..9d0e0640 100644 --- a/app/Models/NotificationModel.php +++ b/app/Models/NotificationModel.php @@ -296,6 +296,17 @@ public function customerGroupQueryExecution($queries){ 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 : []; + + } } diff --git a/app/Views/notifications_form.php b/app/Views/notifications_form.php index 66f6c503..5b3a9d1d 100644 --- a/app/Views/notifications_form.php +++ b/app/Views/notifications_form.php @@ -22,7 +22,7 @@ - + setFlashdata('reset', true); endif; ?> getFlashdata('error')) : ?> - + setFlashdata('reset', true); endif; ?>
" id="notifyCustomForm"> -
@@ -109,15 +108,21 @@
- -
+ +
- +
+
@@ -136,7 +141,7 @@
- Document File + Upload Image
@@ -229,6 +234,31 @@ // 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 const fieldsForMediaURL = document.getElementById("fields-for-mediaurl"); const fieldsForDoc = document.getElementById("fields-for-doc"); @@ -356,7 +386,9 @@