From a81bb5c4f508b77d1b45b7b2cd30d80f96de2955 Mon Sep 17 00:00:00 2001 From: iBNu Maksum Date: Tue, 4 Feb 2025 15:29:29 +0700 Subject: [PATCH] Fix bulk Message delay and no phone number --- system/controllers/message.php | 135 ++++++++++++++++----------------- ui/ui/admin/message/bulk.tpl | 6 +- 2 files changed, 68 insertions(+), 73 deletions(-) diff --git a/system/controllers/message.php b/system/controllers/message.php index 78fa3790..bd968b29 100644 --- a/system/controllers/message.php +++ b/system/controllers/message.php @@ -163,80 +163,75 @@ EOT; $totalCustomers = count($customers); $totalBatches = ceil($totalCustomers / $batchSize); - // Loop through batches - for ($batchIndex = 0; $batchIndex < $totalBatches; $batchIndex++) { - // Get the starting and ending index for the current batch - $start = $batchIndex * $batchSize; - $end = min(($batchIndex + 1) * $batchSize, $totalCustomers); - $batchCustomers = array_slice($customers, $start, $end - $start); + // Loop through customers in the current batch and send messages + foreach ($customers as $customer) { + // Create a copy of the original message for each customer and save it as currentMessage + $currentMessage = $message; + $currentMessage = str_replace('[[name]]', $customer['fullname'], $currentMessage); + $currentMessage = str_replace('[[user_name]]', $customer['username'], $currentMessage); + $currentMessage = str_replace('[[phone]]', $customer['phonenumber'], $currentMessage); + $currentMessage = str_replace('[[company_name]]', $config['CompanyName'], $currentMessage); - // Loop through customers in the current batch and send messages - foreach ($batchCustomers as $customer) { - // Create a copy of the original message for each customer and save it as currentMessage - $currentMessage = $message; - $currentMessage = str_replace('[[name]]', $customer['fullname'], $currentMessage); - $currentMessage = str_replace('[[user_name]]', $customer['username'], $currentMessage); - $currentMessage = str_replace('[[phone]]', $customer['phonenumber'], $currentMessage); - $currentMessage = str_replace('[[company_name]]', $config['CompanyName'], $currentMessage); - - // Send the message based on the selected method - if ($test === 'yes') { - // Only for testing, do not send messages to customers - $batchStatus[] = [ - 'name' => $customer['fullname'], - 'phone' => $customer['phonenumber'], - 'message' => $currentMessage, - 'status' => 'Test Mode - Message not sent' - ]; - } else { - // Send the actual messages - if ($via == 'sms' || $via == 'both') { - $smsSent = Message::sendSMS($customer['phonenumber'], $currentMessage); - if ($smsSent) { - $totalSMSSent++; - $batchStatus[] = [ - 'name' => $customer['fullname'], - 'phone' => $customer['phonenumber'], - 'message' => $currentMessage, - 'status' => 'SMS Message Sent' - ]; - } else { - $totalSMSFailed++; - $batchStatus[] = [ - 'name' => $customer['fullname'], - 'phone' => $customer['phonenumber'], - 'message' => $currentMessage, - 'status' => 'SMS Message Failed' - ]; - } - } - - if ($via == 'wa' || $via == 'both') { - $waSent = Message::sendWhatsapp($customer['phonenumber'], $currentMessage); - if ($waSent) { - $totalWhatsappSent++; - $batchStatus[] = [ - 'name' => $customer['fullname'], - 'phone' => $customer['phonenumber'], - 'message' => $currentMessage, - 'status' => 'WhatsApp Message Sent' - ]; - } else { - $totalWhatsappFailed++; - $batchStatus[] = [ - 'name' => $customer['fullname'], - 'phone' => $customer['phonenumber'], - 'message' => $currentMessage, - 'status' => 'WhatsApp Message Failed' - ]; - } + if(empty($customer['phonenumber'])){ + $batchStatus[] = [ + 'name' => $customer['fullname'], + 'phone' => $customer['phonenumber'], + 'message' => $currentMessage, + 'status' => 'No Phone Number' + ]; + }else + // Send the message based on the selected method + if ($test === 'yes') { + // Only for testing, do not send messages to customers + $batchStatus[] = [ + 'name' => $customer['fullname'], + 'phone' => $customer['phonenumber'], + 'message' => $currentMessage, + 'status' => 'Test Mode - Message not sent' + ]; + } else { + // Send the actual messages + if ($via == 'sms' || $via == 'both') { + $smsSent = Message::sendSMS($customer['phonenumber'], $currentMessage); + if ($smsSent) { + $totalSMSSent++; + $batchStatus[] = [ + 'name' => $customer['fullname'], + 'phone' => $customer['phonenumber'], + 'message' => $currentMessage, + 'status' => 'SMS Message Sent' + ]; + } else { + $totalSMSFailed++; + $batchStatus[] = [ + 'name' => $customer['fullname'], + 'phone' => $customer['phonenumber'], + 'message' => $currentMessage, + 'status' => 'SMS Message Failed' + ]; } } - } - // Introduce a delay between each batch - if ($batchIndex < $totalBatches - 1) { - sleep($delay); + if ($via == 'wa' || $via == 'both') { + $waSent = Message::sendWhatsapp($customer['phonenumber'], $currentMessage); + if ($waSent) { + $totalWhatsappSent++; + $batchStatus[] = [ + 'name' => $customer['fullname'], + 'phone' => $customer['phonenumber'], + 'message' => $currentMessage, + 'status' => 'WhatsApp Message Sent' + ]; + } else { + $totalWhatsappFailed++; + $batchStatus[] = [ + 'name' => $customer['fullname'], + 'phone' => $customer['phonenumber'], + 'message' => $currentMessage, + 'status' => 'WhatsApp Message Failed' + ]; + } + } } } } diff --git a/ui/ui/admin/message/bulk.tpl b/ui/ui/admin/message/bulk.tpl index 8a1cb44e..cabc019e 100644 --- a/ui/ui/admin/message/bulk.tpl +++ b/ui/ui/admin/message/bulk.tpl @@ -5,7 +5,7 @@
{if $page>0 && $totalCustomers>0} - + {/if}
{Lang::T('Send Bulk Message')}
@@ -57,7 +57,7 @@