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() . "