Skip to content

Commit

Permalink
Merge pull request #130 from Focuslinkstech/Development
Browse files Browse the repository at this point in the history
send single and Bulk SMS, WhatsApp or Both WA/SMS message to customers
  • Loading branch information
ibnux authored Mar 18, 2024
2 parents d31edde + 518917a commit d36f39a
Show file tree
Hide file tree
Showing 5 changed files with 549 additions and 172 deletions.
212 changes: 212 additions & 0 deletions system/controllers/message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?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");
}

$select2_customer = <<<EOT
<script>
document.addEventListener("DOMContentLoaded", function(event) {
$('#personSelect').select2({
theme: "bootstrap",
ajax: {
url: function(params) {
if(params.term != undefined){
return './index.php?_route=autoload/customer_select2&s='+params.term;
}else{
return './index.php?_route=autoload/customer_select2';
}
}
}
});
});
</script>
EOT;
$c = ORM::for_table('tbl_customers')->find_many();
$ui->assign('c', $c);
$ui->assign('xfooter', $select2_customer);
$ui->display('message.tpl');
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");
}

// Get form data
$id_customer = $_POST['id_customer'];
$message = $_POST['message'];
$via = $_POST['via'];

// Check if fields are empty
if ($id_customer == '' or $message == '' or $via == '') {
r2(U . 'message/send', 'e', Lang::T('All field is required'));
} else {
// Get customer details from the database
$c = ORM::for_table('tbl_customers')->find_one($id_customer);

// Replace placeholders in the message with actual values
$message = str_replace('[[name]]', $c['fullname'], $message);
$message = str_replace('[[user_name]]', $c['username'], $message);
$message = str_replace('[[phone]]', $c['phonenumber'], $message);
$message = str_replace('[[company_name]]', $config['CompanyName'], $message);


//Send the message
if ($via == 'sms' || $via == 'both') {
$smsSent = Message::sendSMS($c['phonenumber'], $message);
}

if ($via == 'wa' || $via == 'both') {
$waSent = Message::sendWhatsapp($c['phonenumber'], $message);
}

if (isset($smsSent) || isset($waSent)) {
r2(U . 'message/send', 's', Lang::T('Message Sent Successfully'));
} else {
r2(U . 'message/send', 'e', Lang::T('Failed to send message'));
}
}
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");
}
$ui->display('message-bulk.tpl');
break;
case 'send_bulk-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");
}

// Get form data
$group = $_POST['group'];
$message = $_POST['message'];
$via = $_POST['via'];

// Initialize counters
$successCount = 0;
$failCount = 0;
$successMessages = [];
$failMessages = [];

// Check if fields are empty
if ($group == '' or $message == '' or $via == '') {
r2(U . 'message/send_bulk', 'e', Lang::T('All fields are required'));
} else {
// Get customer details from the database based on the selected group
if ($group == 'all') {
$customers = ORM::for_table('tbl_customers')->find_many();
} elseif ($group == 'new') {
// Get customers created just a month ago
$customers = ORM::for_table('tbl_customers')->where_raw("DATE(created_at) >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)")->find_many();
} elseif ($group == 'expired') {
// Get expired user recharges where status is 'off'
$expired = ORM::for_table('tbl_user_recharges')->where('status', 'off')->find_many();
$customer_ids = [];
foreach ($expired as $recharge) {
$customer_ids[] = $recharge->customer_id;
}
$customers = ORM::for_table('tbl_customers')->where_in('id', $customer_ids)->find_many();
} elseif ($group == 'active') {
// Get active user recharges where status is 'on'
$active = ORM::for_table('tbl_user_recharges')->where('status', 'on')->find_many();
$customer_ids = [];
foreach ($active as $recharge) {
$customer_ids[] = $recharge->customer_id;
}
$customers = ORM::for_table('tbl_customers')->where_in('id', $customer_ids)->find_many();
}

// Loop through customers and send messages
foreach ($customers as $customer) {
// Replace placeholders in the message with actual values for each customer
$message = str_replace('[[name]]', $customer['fullname'], $message);
$message = str_replace('[[user_name]]', $customer['username'], $message);
$message = str_replace('[[phone]]', $customer['phonenumber'], $message);
$message = str_replace('[[company_name]]', $config['CompanyName'], $message);

// Send the message based on the selected method
if ($via == 'sms' || $via == 'both') {
$smsSent = Message::sendSMS($customer['phonenumber'], $message);
if ($smsSent) {
$successCount++;
$successMessages[] = "SMS sent to {$customer['fullname']}: {$customer['phonenumber']}";
} else {
$failCount++;
$failMessages[] = "Failed to send SMS to {$customer['fullname']}: {$customer['phonenumber']}";
}
// Introduce a delay of 5 seconds between each SMS
sleep(5);
}

if ($via == 'wa' || $via == 'both') {
$waSent = Message::sendWhatsapp($customer['phonenumber'], $message);
if ($waSent) {
$successCount++;
$successMessages[] = "WhatsApp message sent to {$customer['fullname']}: {$customer['phonenumber']}";
} else {
$failCount++;
$failMessages[] = "Failed to send WhatsApp message to {$customer['fullname']}: {$customer['phonenumber']}";
}
// Introduce a delay of 5 seconds between each WhatsApp message
sleep(5);
}
}

$responseMessage = '';

if ($successCount > 0) {
$responseMessage .= "Messages Sent Successfully: {$successCount}<br>";
$responseMessage .= "<ul>";
foreach ($successMessages as $successMessage) {
$responseMessage .= "<li>{$successMessage}</li>";
}
$responseMessage .= "</ul>";
}

if ($failCount > 0) {
$responseMessage .= "Failed to send messages: {$failCount}<br>";
$responseMessage .= "<ul>";
foreach ($failMessages as $failMessage) {
$responseMessage .= "<li>{$failMessage}</li>";
}
$responseMessage .= "</ul>";
}

if ($responseMessage != '') {
r2(U . 'message/send_bulk', 's', $responseMessage);
} else {
r2(U . 'message/send_bulk', 'e', Lang::T('No messages sent'));
}
}

break;




default:
r2(U . 'message/send_sms', 'e', 'action not defined');
}
23 changes: 22 additions & 1 deletion ui/ui/customers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="col-md-8">
<form id="site-search" method="post" action="{$_url}customers/list/">
<div class="input-group">
<input type="text" name="search" value="{$search}" class="form-control"
<input type="text" id="search-input" name="search" value="{$search}" class="form-control"
placeholder="{Lang::T('Search')}...">
<div class="input-group-btn">
<button class="btn btn-success" type="submit"><span
Expand Down Expand Up @@ -79,4 +79,25 @@
</div>
</div>

<script>
// Functionality to filter table rows based on admin input
document.addEventListener('DOMContentLoaded', function () {
var searchInput = document.getElementById('search-input');
var tableRows = document.querySelectorAll('tbody tr');
searchInput.addEventListener('input', function () {
var searchText = this.value.toLowerCase();
tableRows.forEach(function (row) {
var rowData = row.textContent.toLowerCase();
if (rowData.includes(searchText)) {
row.style.display = '';
} else {
row.style.display = 'none';
}
});
});
});
</script>
{include file="sections/footer.tpl"}
62 changes: 62 additions & 0 deletions ui/ui/message-bulk.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{include file="sections/header.tpl"}

<div class="row">
<div class="col-sm-12 col-md-12">
<div class="panel panel-primary panel-hovered panel-stacked mb30">
<div class="panel-heading">{Lang::T('Send Bulk Message')}</div>
<div class="panel-body">
<form class="form-horizontal" method="post" role="form" action="{$_url}message/send_bulk-post">
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Group')}</label>
<div class="col-md-6">
<select class="form-control" name="group" id="group">
<option value="all" selected>{Lang::T('All Customers')}</option>
<option value="new">{Lang::T('New Customers')}</option>
<option value="expired">{Lang::T('Expired Customers')}</option>
<option value="active">{Lang::T('Active Customers')}</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Send Via')}</label>
<div class="col-md-6">
<select class="form-control" name="via" id="via">
<option value="sms" selected>{Lang::T('SMS')}</option>
<option value="wa">{Lang::T('WhatsApp')}</option>
<option value="both">{Lang::T('SMS and WhatsApp')}</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Message')}</label>
<div class="col-md-6">
<textarea class="form-control" id="message" name="message" placeholder="{Lang::T('Compose your message...')}" rows="5"></textarea>
</div>
<p class="help-block col-md-4">
{Lang::T('Use placeholders:')}
<br>
<b>{Lang::T('[[name]]')}</b> - {Lang::T('Customer Name')}
<br>
<b>{Lang::T('[[user_name]]')}</b> - {Lang::T('Customer Username')}
<br>
<b>{Lang::T('[[phone]]')}</b> - {Lang::T('Customer Phone')}
<br>
<b>{Lang::T('[[company_name]]')}</b> - {Lang::T('Your Company Name')}
</p>
</div>

<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success" type="submit">{Lang::T('Send Message')}</button>
<a href="{$_url}dashboard" class="btn btn-default">{Lang::T('Cancel')}</a>
</div>
</div>
</form>

</div>
</div>
</div>
</div>


{include file="sections/footer.tpl"}
65 changes: 65 additions & 0 deletions ui/ui/message.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{include file="sections/header.tpl"}

<div class="row">
<div class="col-sm-12 col-md-12">
<div class="panel panel-primary panel-hovered panel-stacked mb30">
<div class="panel-heading">{Lang::T('Send Personal Message')}</div>
<div class="panel-body">
<form class="form-horizontal" method="post" role="form" action="{$_url}message/send-post" >
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Customer')}</label>
<div class="col-md-6">
<select id="personSelect" class="form-control select2" name="id_customer" style="width: 100%" data-placeholder="Select a customer...">
<option></option>
{foreach $c as $cs}
{if $id eq $cs['id']}
<option value="{$cs['id']}" selected>{$cs['username']}</option>
{else}
<option value="{$cs['id']}">{$cs['username']}</option>
{/if}
{/foreach}
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Send Via')}</label>
<div class="col-md-6">
<select class="form-control" name="via" id="via">
<option value="sms" selected> {Lang::T('SMS')}</option>
<option value="wa"> {Lang::T('WhatsApp')}</option>
<option value="both"> {Lang::T('SMS and WhatsApp')}</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">{Lang::T('Message')}</label>
<div class="col-md-6">
<textarea class="form-control" id="message" name="message" placeholder="{Lang::T('Compose your message...')}" rows="5"></textarea>
</div>
<p class="help-block col-md-4">
{Lang::T('Use placeholders:')}
<br>
<b>{Lang::T('[[name]]')}</b> - {Lang::T('Customer Name')}
<br>
<b>{Lang::T('[[user_name]]')}</b> - {Lang::T('Customer Username')}
<br>
<b>{Lang::T('[[phone]]')}</b> - {Lang::T('Customer Phone')}
<br>
<b>{Lang::T('[[company_name]]')}</b> - {Lang::T('Your Company Name')}
</p>
</div>

<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button class="btn btn-success" type="submit">{Lang::T('Send Message')}</button>
<a href="{$_url}dashboard" class="btn btn-default">{Lang::T('Cancel')}</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>


{include file="sections/footer.tpl"}
Loading

0 comments on commit d36f39a

Please sign in to comment.