2024-03-18 01:35:48 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PHP Mikrotik Billing (https://github.com/hotspotbilling/phpnuxbill/)
|
|
|
|
* by https://t.me/ibnux
|
|
|
|
**/
|
|
|
|
|
|
|
|
_admin();
|
|
|
|
$ui->assign('_title', Lang::T('Send Message'));
|
|
|
|
$ui->assign('_system_menu', 'message');
|
|
|
|
|
|
|
|
$action = $routes['1'];
|
|
|
|
$ui->assign('_admin', $admin);
|
|
|
|
|
|
|
|
if (empty($action)) {
|
|
|
|
$action = 'send';
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($action) {
|
|
|
|
case 'send':
|
|
|
|
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent', 'Sales'])) {
|
|
|
|
_alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard");
|
|
|
|
}
|
|
|
|
|
2025-02-09 16:06:59 +01:00
|
|
|
$appUrl = APP_URL;
|
2025-02-05 16:25:03 +07:00
|
|
|
|
2024-03-18 01:35:48 +01:00
|
|
|
$select2_customer = <<<EOT
|
|
|
|
<script>
|
|
|
|
document.addEventListener("DOMContentLoaded", function(event) {
|
|
|
|
$('#personSelect').select2({
|
|
|
|
theme: "bootstrap",
|
|
|
|
ajax: {
|
|
|
|
url: function(params) {
|
|
|
|
if(params.term != undefined){
|
2025-02-05 16:25:03 +07:00
|
|
|
return '{$appUrl}/?_route=autoload/customer_select2&s='+params.term;
|
2024-03-18 01:35:48 +01:00
|
|
|
}else{
|
2025-02-05 16:25:03 +07:00
|
|
|
return '{$appUrl}/?_route=autoload/customer_select2';
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
EOT;
|
2024-03-18 12:28:54 +01:00
|
|
|
if (isset($routes['2']) && !empty($routes['2'])) {
|
|
|
|
$ui->assign('cust', ORM::for_table('tbl_customers')->find_one($routes['2']));
|
|
|
|
}
|
|
|
|
$id = $routes['2'];
|
|
|
|
$ui->assign('id', $id);
|
2024-03-18 01:35:48 +01:00
|
|
|
$ui->assign('xfooter', $select2_customer);
|
2025-02-04 10:56:02 +07:00
|
|
|
$ui->display('admin/message/single.tpl');
|
2024-03-18 01:35:48 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send-post':
|
|
|
|
// Check user permissions
|
|
|
|
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent', 'Sales'])) {
|
|
|
|
_alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard");
|
|
|
|
}
|
|
|
|
|
2025-04-09 12:36:13 +01:00
|
|
|
$id_customer = $_POST['id_customer'] ?? '';
|
2025-04-09 20:16:11 +01:00
|
|
|
$message = $_POST['message'] ?? '';
|
2025-04-09 12:36:13 +01:00
|
|
|
$via = $_POST['via'] ?? '';
|
|
|
|
$subject = $_POST['subject'] ?? '';
|
|
|
|
|
|
|
|
// Validate subject based on the selected channel
|
|
|
|
if (($via === 'all' || $via === 'email' || $via === 'inbox') && empty($subject)) {
|
|
|
|
r2(getUrl('message/send'), 'e', LANG::T('Subject is required to send message using') . ' ' . $via . '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($id_customer) || empty($message) || empty($via)) {
|
|
|
|
r2(getUrl('message/send'), 'e', Lang::T('Customer, Message, and Channel are required'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$customer = ORM::for_table('tbl_customers')->find_one($id_customer);
|
|
|
|
if (!$customer) {
|
|
|
|
r2(getUrl('message/send'), 'e', Lang::T('Customer not found'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace placeholders in message and subject
|
|
|
|
$currentMessage = str_replace(
|
|
|
|
['[[name]]', '[[user_name]]', '[[phone]]', '[[company_name]]'],
|
|
|
|
[$customer['fullname'], $customer['username'], $customer['phonenumber'], $config['CompanyName']],
|
|
|
|
$message
|
|
|
|
);
|
|
|
|
|
|
|
|
$currentSubject = str_replace(
|
|
|
|
['[[name]]', '[[user_name]]', '[[phone]]', '[[company_name]]'],
|
|
|
|
[$customer['fullname'], $customer['username'], $customer['phonenumber'], $config['CompanyName']],
|
|
|
|
$subject
|
|
|
|
);
|
|
|
|
|
|
|
|
if (strpos($message, '[[payment_link]]') !== false) {
|
|
|
|
$token = User::generateToken($customer['id'], 1);
|
|
|
|
if (!empty($token['token'])) {
|
|
|
|
$tur = ORM::for_table('tbl_user_recharges')
|
|
|
|
->where('customer_id', $customer['id'])
|
|
|
|
->find_one();
|
|
|
|
if ($tur) {
|
|
|
|
$url = '?_route=home&recharge=' . $tur['id'] . '&uid=' . urlencode($token['token']);
|
|
|
|
$currentMessage = str_replace('[[payment_link]]', $url, $currentMessage);
|
2025-02-09 16:06:59 +01:00
|
|
|
}
|
2025-04-09 12:36:13 +01:00
|
|
|
} else {
|
|
|
|
$currentMessage = str_replace('[[payment_link]]', '', $currentMessage);
|
2025-02-09 16:06:59 +01:00
|
|
|
}
|
2025-04-09 12:36:13 +01:00
|
|
|
}
|
2024-03-18 01:35:48 +01:00
|
|
|
|
2025-04-09 12:36:13 +01:00
|
|
|
// Send the message through the selected channels
|
|
|
|
$smsSent = $waSent = $emailSent = $inboxSent = false;
|
2024-03-18 01:35:48 +01:00
|
|
|
|
2025-04-09 12:36:13 +01:00
|
|
|
if ($via === 'sms' || $via === 'both' || $via === 'all') {
|
|
|
|
$smsSent = Message::sendSMS($customer['phonenumber'], $currentSubject);
|
|
|
|
}
|
2024-03-18 01:35:48 +01:00
|
|
|
|
2025-04-09 12:36:13 +01:00
|
|
|
if ($via === 'wa' || $via === 'both' || $via === 'all') {
|
|
|
|
$waSent = Message::sendWhatsapp($customer['phonenumber'], $currentSubject);
|
|
|
|
}
|
2024-03-18 01:35:48 +01:00
|
|
|
|
2025-04-09 12:36:13 +01:00
|
|
|
if ($via === 'email' || $via === 'all') {
|
|
|
|
$emailSent = Message::sendEmail($customer['email'], $currentSubject, $currentMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($via === 'inbox' || $via === 'all') {
|
|
|
|
$inboxSent = Message::addToInbox($customer['id'], $currentSubject, $currentMessage, 'Admin');
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
2025-04-09 12:36:13 +01:00
|
|
|
|
|
|
|
// Check if any message was sent successfully
|
|
|
|
if ($smsSent || $waSent || $emailSent || $inboxSent) {
|
|
|
|
r2(getUrl('message/send'), 's', Lang::T('Message Sent Successfully'));
|
|
|
|
} else {
|
|
|
|
r2(getUrl('message/send'), 'e', Lang::T('Failed to send message'));
|
|
|
|
}
|
|
|
|
|
2024-03-18 01:35:48 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send_bulk':
|
|
|
|
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent', 'Sales'])) {
|
|
|
|
_alert(Lang::T('You do not have permission to access this page'), 'danger', "dashboard");
|
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
$ui->assign('routers', ORM::forTable('tbl_routers')->where('enabled', '1')->find_many());
|
|
|
|
$ui->display('admin/message/bulk.tpl');
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send_bulk_ajax':
|
|
|
|
// Check user permissions
|
|
|
|
if (!in_array($admin['user_type'], ['SuperAdmin', 'Admin', 'Agent', 'Sales'])) {
|
|
|
|
die(json_encode(['status' => 'error', 'message' => 'Permission denied']));
|
|
|
|
}
|
|
|
|
|
2025-02-04 15:13:42 +07:00
|
|
|
set_time_limit(0);
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
// Get request parameters
|
|
|
|
$group = $_REQUEST['group'] ?? '';
|
|
|
|
$message = $_REQUEST['message'] ?? '';
|
|
|
|
$via = $_REQUEST['via'] ?? '';
|
|
|
|
$batch = $_REQUEST['batch'] ?? 100;
|
|
|
|
$page = $_REQUEST['page'] ?? 0;
|
|
|
|
$router = $_REQUEST['router'] ?? null;
|
2025-04-09 20:16:11 +01:00
|
|
|
$test = isset($_REQUEST['test']) && $_REQUEST['test'] === 'on';
|
2025-03-06 18:44:20 +01:00
|
|
|
$service = $_REQUEST['service'] ?? '';
|
2025-04-09 11:39:00 +01:00
|
|
|
$subject = $_REQUEST['subject'] ?? '';
|
2025-02-09 16:06:59 +01:00
|
|
|
|
2025-03-06 18:44:20 +01:00
|
|
|
if (empty($group) || empty($message) || empty($via) || empty($service)) {
|
2025-04-09 12:36:13 +01:00
|
|
|
die(json_encode(['status' => 'error', 'message' => LANG::T('All fields are required')]));
|
2025-04-09 11:39:00 +01:00
|
|
|
}
|
|
|
|
|
2025-04-09 20:16:11 +01:00
|
|
|
if (in_array($via, ['all', 'email', 'inbox']) && empty($subject)) {
|
2025-04-09 11:39:00 +01:00
|
|
|
die(json_encode(['status' => 'error', 'message' => LANG::T('Subject is required to send message using') . ' ' . $via . '.']));
|
2025-02-09 16:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get batch of customers based on group
|
|
|
|
$startpoint = $page * $batch;
|
|
|
|
$customers = [];
|
2025-03-06 18:44:20 +01:00
|
|
|
$totalCustomers = 0;
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
if (isset($router) && !empty($router)) {
|
2025-03-06 18:44:20 +01:00
|
|
|
switch ($router) {
|
|
|
|
case 'radius':
|
|
|
|
$routerName = 'Radius';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$router = ORM::for_table('tbl_routers')->find_one($router);
|
|
|
|
if (!$router) {
|
2025-04-09 12:36:13 +01:00
|
|
|
die(json_encode(['status' => 'error', 'message' => LANG::T('Invalid router')]));
|
2025-03-06 18:44:20 +01:00
|
|
|
}
|
|
|
|
$routerName = $router->name;
|
|
|
|
break;
|
2025-02-09 16:06:59 +01:00
|
|
|
}
|
2025-03-06 18:44:20 +01:00
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
|
2025-03-06 18:44:20 +01:00
|
|
|
if (isset($router) && !empty($router)) {
|
2025-02-09 16:06:59 +01:00
|
|
|
$query = ORM::for_table('tbl_user_recharges')
|
|
|
|
->left_outer_join('tbl_customers', 'tbl_user_recharges.customer_id = tbl_customers.id')
|
2025-03-06 18:44:20 +01:00
|
|
|
->where('tbl_user_recharges.routers', $routerName);
|
|
|
|
|
|
|
|
switch ($service) {
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$validServices = ['PPPoE', 'Hotspot', 'VPN'];
|
|
|
|
if (in_array($service, $validServices)) {
|
|
|
|
$query->where('type', $service);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$totalCustomers = $query->count();
|
|
|
|
|
|
|
|
$query->offset($startpoint)
|
2025-02-09 16:06:59 +01:00
|
|
|
->limit($batch);
|
|
|
|
|
|
|
|
switch ($group) {
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
case 'new':
|
|
|
|
$query->where_raw("DATE(recharged_on) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)");
|
|
|
|
break;
|
|
|
|
case 'expired':
|
|
|
|
$query->where('tbl_user_recharges.status', 'off');
|
|
|
|
break;
|
|
|
|
case 'active':
|
|
|
|
$query->where('tbl_user_recharges.status', 'on');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2025-03-06 18:44:20 +01:00
|
|
|
// Fetch the customers
|
2025-02-09 16:06:59 +01:00
|
|
|
$query->selects([
|
|
|
|
['tbl_customers.phonenumber', 'phonenumber'],
|
|
|
|
['tbl_user_recharges.customer_id', 'customer_id'],
|
|
|
|
['tbl_customers.fullname', 'fullname'],
|
2025-03-24 10:24:08 +01:00
|
|
|
['tbl_customers.username', 'username'],
|
|
|
|
['tbl_customers.email', 'email'],
|
2025-04-09 10:29:22 +01:00
|
|
|
['tbl_customers.service_type', 'service_type'],
|
2025-02-09 16:06:59 +01:00
|
|
|
]);
|
|
|
|
$customers = $query->find_array();
|
|
|
|
} else {
|
|
|
|
switch ($group) {
|
|
|
|
case 'all':
|
2025-03-06 18:44:20 +01:00
|
|
|
$totalCustomersQuery = ORM::for_table('tbl_customers');
|
|
|
|
|
|
|
|
switch ($service) {
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$validServices = ['PPPoE', 'Hotspot', 'VPN'];
|
|
|
|
if (in_array($service, $validServices)) {
|
|
|
|
$totalCustomersQuery->where('service_type', $service);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$totalCustomers = $totalCustomersQuery->count();
|
|
|
|
$customers = $totalCustomersQuery->offset($startpoint)->limit($batch)->find_array();
|
2025-02-09 16:06:59 +01:00
|
|
|
break;
|
2025-03-06 18:44:20 +01:00
|
|
|
|
2025-02-09 16:06:59 +01:00
|
|
|
case 'new':
|
2025-03-06 18:44:20 +01:00
|
|
|
$totalCustomersQuery = ORM::for_table('tbl_customers')
|
|
|
|
->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)");
|
|
|
|
|
|
|
|
switch ($service) {
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$validServices = ['PPPoE', 'Hotspot', 'VPN'];
|
|
|
|
if (in_array($service, $validServices)) {
|
|
|
|
$totalCustomersQuery->where('service_type', $service);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$totalCustomers = $totalCustomersQuery->count();
|
|
|
|
$customers = $totalCustomersQuery->offset($startpoint)->limit($batch)->find_array();
|
2025-02-09 16:06:59 +01:00
|
|
|
break;
|
2025-03-06 18:44:20 +01:00
|
|
|
|
2025-02-09 16:06:59 +01:00
|
|
|
case 'expired':
|
2025-03-06 18:44:20 +01:00
|
|
|
$totalCustomersQuery = ORM::for_table('tbl_user_recharges')
|
|
|
|
->where('status', 'off');
|
|
|
|
|
|
|
|
switch ($service) {
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$validServices = ['PPPoE', 'Hotspot', 'VPN'];
|
|
|
|
if (in_array($service, $validServices)) {
|
|
|
|
$totalCustomersQuery->where('type', $service);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$totalCustomers = $totalCustomersQuery->count();
|
|
|
|
$customers = $totalCustomersQuery->select('customer_id')->offset($startpoint)->limit($batch)->find_array();
|
2025-02-09 16:06:59 +01:00
|
|
|
break;
|
2025-03-06 18:44:20 +01:00
|
|
|
|
2025-02-09 16:06:59 +01:00
|
|
|
case 'active':
|
2025-03-06 18:44:20 +01:00
|
|
|
$totalCustomersQuery = ORM::for_table('tbl_user_recharges')
|
|
|
|
->where('status', 'on');
|
|
|
|
|
|
|
|
switch ($service) {
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$validServices = ['PPPoE', 'Hotspot', 'VPN'];
|
|
|
|
if (in_array($service, $validServices)) {
|
|
|
|
$totalCustomersQuery->where('type', $service);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$totalCustomers = $totalCustomersQuery->count();
|
|
|
|
$customers = $totalCustomersQuery->select('customer_id')->offset($startpoint)->limit($batch)->find_array(); // Get customer data
|
2025-02-09 16:06:59 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure $customers is always an array
|
|
|
|
if (!$customers) {
|
|
|
|
$customers = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send messages
|
2024-03-21 16:52:52 +01:00
|
|
|
$totalSMSSent = 0;
|
|
|
|
$totalSMSFailed = 0;
|
|
|
|
$totalWhatsappSent = 0;
|
|
|
|
$totalWhatsappFailed = 0;
|
2025-03-24 10:24:08 +01:00
|
|
|
$totalEmailSent = 0;
|
|
|
|
$totalEmailFailed = 0;
|
|
|
|
$totalInboxSent = 0;
|
|
|
|
$totalInboxFailed = 0;
|
2025-02-09 16:06:59 +01:00
|
|
|
$batchStatus = [];
|
2025-04-09 11:39:00 +01:00
|
|
|
//$subject = $config['CompanyName'] . ' ' . Lang::T('Notification Message');
|
2025-03-24 10:24:08 +01:00
|
|
|
$form = 'Admin';
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
foreach ($customers as $customer) {
|
|
|
|
$currentMessage = str_replace(
|
|
|
|
['[[name]]', '[[user_name]]', '[[phone]]', '[[company_name]]'],
|
|
|
|
[$customer['fullname'], $customer['username'], $customer['phonenumber'], $config['CompanyName']],
|
|
|
|
$message
|
|
|
|
);
|
|
|
|
|
2025-04-09 11:39:00 +01:00
|
|
|
$currentSubject = str_replace(
|
|
|
|
['[[name]]', '[[user_name]]', '[[phone]]', '[[company_name]]'],
|
|
|
|
[$customer['fullname'], $customer['username'], $customer['phonenumber'], $config['CompanyName']],
|
|
|
|
$subject
|
|
|
|
);
|
|
|
|
|
2025-02-09 16:06:59 +01:00
|
|
|
$phoneNumber = preg_replace('/\D/', '', $customer['phonenumber']);
|
|
|
|
|
|
|
|
if (empty($phoneNumber)) {
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
|
|
|
'phone' => '',
|
|
|
|
'status' => 'No Phone Number'
|
|
|
|
];
|
|
|
|
continue;
|
2025-02-04 15:13:42 +07:00
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
if ($test) {
|
|
|
|
$batchStatus[] = [
|
|
|
|
'name' => $customer['fullname'],
|
2025-03-24 10:24:08 +01:00
|
|
|
'channel' => 'Test Channel',
|
2025-02-09 16:06:59 +01:00
|
|
|
'status' => 'Test Mode',
|
2025-03-06 18:44:20 +01:00
|
|
|
'message' => $currentMessage,
|
|
|
|
'service' => $service,
|
|
|
|
'router' => $routerName,
|
2025-02-09 16:06:59 +01:00
|
|
|
];
|
2024-03-21 16:52:52 +01:00
|
|
|
} else {
|
2025-03-24 10:24:08 +01:00
|
|
|
if ($via === 'sms' || $via === 'both' || $via === 'all') {
|
2025-02-09 16:06:59 +01:00
|
|
|
if (Message::sendSMS($customer['phonenumber'], $currentMessage)) {
|
|
|
|
$totalSMSSent++;
|
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'phone' => $customer['phonenumber'], 'status' => 'SMS Sent', 'message' => $currentMessage];
|
|
|
|
} else {
|
|
|
|
$totalSMSFailed++;
|
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'phone' => $customer['phonenumber'], 'status' => 'SMS Failed', 'message' => $currentMessage];
|
|
|
|
}
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
|
2025-03-24 10:24:08 +01:00
|
|
|
if ($via === 'wa' || $via == 'both' || $via === 'all') {
|
2025-02-09 16:06:59 +01:00
|
|
|
if (Message::sendWhatsapp($customer['phonenumber'], $currentMessage)) {
|
|
|
|
$totalWhatsappSent++;
|
2025-03-24 10:24:08 +01:00
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['phonenumber'], 'status' => 'WhatsApp Sent', 'message' => $currentMessage];
|
2025-02-04 15:29:29 +07:00
|
|
|
} else {
|
2025-02-09 16:06:59 +01:00
|
|
|
$totalWhatsappFailed++;
|
2025-03-24 10:24:08 +01:00
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['phonenumber'], 'status' => 'WhatsApp Failed', 'message' => $currentMessage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($via === 'email' || $via === 'all') {
|
2025-04-09 11:39:00 +01:00
|
|
|
if (Message::sendEmail($customer['email'], $currentSubject, $currentMessage)) {
|
2025-03-24 10:24:08 +01:00
|
|
|
$totalEmailSent++;
|
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['email'], 'status' => 'Email Sent', 'message' => $currentMessage];
|
|
|
|
} else {
|
|
|
|
$totalEmailFailed++;
|
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => $customer['email'], 'status' => 'Email Failed', 'message' => $currentMessage];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($via === 'inbox' || $via === 'all') {
|
2025-04-09 11:39:00 +01:00
|
|
|
if (Message::addToInbox($customer['customer_id'], $currentSubject, $currentMessage, $form)) {
|
2025-03-24 10:24:08 +01:00
|
|
|
$totalInboxSent++;
|
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => 'Inbox', 'status' => 'Inbox Message Sent', 'message' => $currentMessage];
|
|
|
|
} else {
|
|
|
|
$totalInboxFailed++;
|
|
|
|
$batchStatus[] = ['name' => $customer['fullname'], 'channel' => 'Inbox', 'status' => 'Inbox Message Failed', 'message' => $currentMessage];
|
2025-02-09 16:06:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate if there are more customers to process
|
|
|
|
$hasMore = ($startpoint + $batch) < $totalCustomers;
|
|
|
|
|
|
|
|
// Return JSON response
|
|
|
|
echo json_encode([
|
|
|
|
'status' => 'success',
|
|
|
|
'page' => $page + 1,
|
|
|
|
'batchStatus' => $batchStatus,
|
|
|
|
'message' => $currentMessage,
|
2025-03-24 10:24:08 +01:00
|
|
|
'totalSent' => $totalSMSSent + $totalWhatsappSent + $totalEmailSent + $totalInboxSent,
|
|
|
|
'totalFailed' => $totalSMSFailed + $totalWhatsappFailed + $totalEmailFailed + $totalInboxFailed,
|
2025-03-06 18:44:20 +01:00
|
|
|
'hasMore' => $hasMore,
|
|
|
|
'service' => $service,
|
|
|
|
'router' => $routerName,
|
2025-02-09 16:06:59 +01:00
|
|
|
]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'send_bulk_selected':
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
|
|
// Set headers
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
header('Cache-Control: no-cache, no-store, must-revalidate');
|
|
|
|
|
|
|
|
// Get the posted data
|
|
|
|
$customerIds = $_POST['customer_ids'] ?? [];
|
|
|
|
$via = $_POST['message_type'] ?? '';
|
2025-04-09 12:36:13 +01:00
|
|
|
$subject = $_POST['subject'] ?? '';
|
2025-02-09 16:06:59 +01:00
|
|
|
$message = isset($_POST['message']) ? trim($_POST['message']) : '';
|
|
|
|
if (empty($customerIds) || empty($message) || empty($via)) {
|
|
|
|
echo json_encode(['status' => 'error', 'message' => Lang::T('Invalid customer IDs, Message, or Message Type.')]);
|
|
|
|
exit;
|
|
|
|
}
|
2024-03-21 16:52:52 +01:00
|
|
|
|
2025-04-09 12:36:13 +01:00
|
|
|
if ($via === 'all' || $via === 'email' || $via === 'inbox' && empty($subject)) {
|
|
|
|
die(json_encode(['status' => 'error', 'message' => LANG::T('Subject is required to send message using') . ' ' . $via . '.']));
|
|
|
|
}
|
2025-04-09 20:16:11 +01:00
|
|
|
|
2025-02-09 16:06:59 +01:00
|
|
|
// Prepare to send messages
|
|
|
|
$sentCount = 0;
|
|
|
|
$failedCount = 0;
|
|
|
|
$form = 'Admin';
|
|
|
|
|
|
|
|
foreach ($customerIds as $customerId) {
|
|
|
|
$customer = ORM::for_table('tbl_customers')->where('id', $customerId)->find_one();
|
|
|
|
if ($customer) {
|
|
|
|
$messageSent = false;
|
|
|
|
|
|
|
|
// Check the message type and send accordingly
|
|
|
|
try {
|
|
|
|
if ($via === 'sms' || $via === 'all') {
|
|
|
|
$messageSent = Message::sendSMS($customer['phonenumber'], $message);
|
|
|
|
}
|
|
|
|
if (!$messageSent && ($via === 'wa' || $via === 'all')) {
|
|
|
|
$messageSent = Message::sendWhatsapp($customer['phonenumber'], $message);
|
|
|
|
}
|
|
|
|
if (!$messageSent && ($via === 'inbox' || $via === 'all')) {
|
|
|
|
Message::addToInbox($customer['id'], $subject, $message, $form);
|
|
|
|
$messageSent = true;
|
2024-03-21 16:52:52 +01:00
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
if (!$messageSent && ($via === 'email' || $via === 'all')) {
|
|
|
|
$messageSent = Message::sendEmail($customer['email'], $subject, $message);
|
|
|
|
}
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$messageSent = false;
|
|
|
|
$failedCount++;
|
|
|
|
sendTelegram('Failed to send message to ' . $e->getMessage());
|
|
|
|
_log('Failed to send message to ' . $customer['fullname'] . ': ' . $e->getMessage());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($messageSent) {
|
|
|
|
$sentCount++;
|
|
|
|
} else {
|
|
|
|
$failedCount++;
|
2024-03-21 16:52:52 +01:00
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
} else {
|
|
|
|
$failedCount++;
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
}
|
2025-02-09 16:06:59 +01:00
|
|
|
|
|
|
|
// Prepare the response
|
|
|
|
echo json_encode([
|
|
|
|
'status' => 'success',
|
|
|
|
'totalSent' => $sentCount,
|
|
|
|
'totalFailed' => $failedCount
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode(['status' => 'error', 'message' => Lang::T('Invalid request method.')]);
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2025-01-31 16:22:58 +07:00
|
|
|
r2(getUrl('message/send_sms'), 'e', 'action not defined');
|
2024-03-18 01:35:48 +01:00
|
|
|
}
|