-
Notifications
You must be signed in to change notification settings - Fork 6
/
rc_sms_notification.php
308 lines (268 loc) · 10.3 KB
/
rc_sms_notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* RC SMS Cron Notification
*
* Run by cron every hour, look for appointments in pre-notification period and
* send an SMS reminder
*
* @author Unknown
* @author Larry Lart
* @author Jerry Padgett
* @author Robert Down
* @copyright Unknown
* @copyright Copyright (c) 2008 Larry Lart
* @copyright Copyright (c) 2018 Jerry Padgett
* @copyright Copyright (c) 2021 Robert Down <[email protected]>
*/
//hack add for command line version
use OpenEMR\Modules\FaxSMS\Controllers\AppDispatch;
$_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'];
$_SERVER['SERVER_NAME'] = 'localhost';
$backpic = "";
// for cron
if ($argc > 1 && empty($_SESSION['site_id']) && empty($_GET['site'])) {
$c = stripos($argv[1], 'site=');
if ($c === false) {
echo xlt("Missing Site Id using default") . "\n";
$argv[1] = "site=default";
}
$args = explode('=', $argv[1]);
$_GET['site'] = isset($args[1]) ? $args[1] : 'default';
}
if (php_sapi_name() === 'cli') {
$_SERVER[‘HTTP_HOST’] = ‘localhost’;
$ignoreAuth = true;
}
require_once(__DIR__ . "/../../../globals.php");
require_once("$srcdir/appointments.inc.php");
$clientApp = AppDispatch::getApiService();
session_write_close();
set_time_limit(0);
$cred = $clientApp->getCredentials();
$SMS_NOTIFICATION_HOUR = $cred['smsHours'];
$MESSAGE = $cred['smsMessage'];
// check command line for quite option
$bTestRun = isset($_REQUEST['dryrun']) ? 1 : 0;
if ($argc > 1 && $argv[2] == 'test') {
$bTestRun = 1;
}
$TYPE = "SMS";
$CRON_TIME = 150;
$curr_date = date("Y-m-d");
$curr_time = time();
$check_date = date("Y-m-d", mktime(date("h") + $SMS_NOTIFICATION_HOUR, 0, 0, date("m"), date("d"), date("Y")));
//$db_sms_msg = cron_getNotificationData($TYPE);
$db_sms_msg['sms_gateway_type'] = "SMS";
$db_sms_msg['message'] = $MESSAGE;
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo xlt("SMS Notification") ?></title>
</head>
<style>
html {
font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
font-size: 12px;
}
</style>
<body>
<div>
<center>
<p>
<h2><?php echo xlt("Working and may take a few minutes to finish.") ?></h2></p>
</center>
";
<?php
if ($bTestRun) {
echo xlt("We are in Test Mode and no reminders will be sent. This will check what reminders will be sent in Live Mode.");
}
$db_patient = cron_getAlertpatientData($TYPE);
echo "\n<br>" . xlt('Total of') . ": " . count($db_patient) . " " . xlt('Reminders Found') . " " . ($bTestRun ? xlt("and not Sending for reminders") . " " : xlt("and Sending for reminders ")) . $SMS_NOTIFICATION_HOUR . xlt("hrs from now.");
ob_flush();
flush();
// for every event found
$plast = '';
echo "<h3>========================" . $TYPE . " | " . date("Y-m-d H:i:s") . "=========================</h3>";
for ($p = 0; $p < count($db_patient); $p++) {
ob_flush();
flush();
$prow = $db_patient[$p];
$db_sms_msg['sms_gateway_type'] = "RCSMS";
$db_sms_msg['message'] = $MESSAGE;
$app_date = $prow['pc_eventDate'] . " " . $prow['pc_startTime'];
$app_time = strtotime($app_date);
$app_time_hour = round($app_time / 3600);
$curr_total_hour = round(time() / 3600);
$remaining_app_hour = round($app_time_hour - $curr_total_hour);
$remain_hour = round($remaining_app_hour - $SMS_NOTIFICATION_HOUR);
if ($plast != $prow['ulname']) {
echo "<h4>For Provider: " . $prow['utitle'] . ' ' . $prow['ufname'] . ' ' . $prow['ulname'] . "</h4>";
$plast = $prow['ulname'];
}
$strMsg = "<b>* SEND NOTIFICATION BEFORE:" . $SMS_NOTIFICATION_HOUR . " | CRONJOB RUN EVERY:" . $CRON_TIME . " | APPDATETIME: " . $app_date . " | REMAINING APP HOUR:" . ($remaining_app_hour) . " | SEND ALERT AFTER:" . ($remain_hour) . "";
// check in the interval
if ($remain_hour >= -($CRON_TIME) && $remain_hour <= $CRON_TIME) {
//set message
$db_sms_msg['message'] = cron_setmessage($prow, $db_sms_msg);
$isValid = isValidPhone($prow['phone_cell']);
// send sms to patient - if not in test mode
if ($bTestRun == 0 && $isValid) {
cron_InsertNotificationLogEntry($TYPE, $prow, $db_sms_msg);
$clientApp->sendSMS(
$prow['phone_cell'],
$db_sms_msg['email_subject'],
$db_sms_msg['message'],
$db_sms_msg['email_sender']
);
}
if (!$isValid) {
$strMsg .= "<strong style='color:red'>\n* INVALID Mobile Phone# " . $prow['phone_cell'] . " SMS NOT SENT</strong> Patient: " . $prow['fname'] . " " . $prow['lname'] . "</b>";
$db_sms_msg[message] = "ERROR: INVALID Mobile Phone# " . $prow['phone_cell'] . " SMS NOT SENT For: " . $prow['fname'] . " " . $prow['lname'];
if ($bTestRun == 0) {
cron_InsertNotificationLogEntry($TYPE, $prow, $db_sms_msg);
}
} else {
$strMsg .= " | SENT SUCCESSFULLY TO <strong>" . $prow['phone_cell'] . "</strong></b>";
cron_updateentry($TYPE, $prow['pid'], $prow['pc_eid'], $prow['pc_recurrtype']);
}
if ((int)$prow['pc_recurrtype'] > 0) {
$row = fetchRecurrences($prow['pid']);
$strMsg .= "\n<b>A Recurring " . $row[0]['pc_catname'] . " Event Occuring " . $row[0]['pc_recurrspec'] . "</b> Note: Appointment status left unchanged.";
}
$strMsg .= "\n" . $db_sms_msg['message'] . "\n";
echo nl2br($strMsg);
}
}
unset($clientApp);
echo "\n<br><h2>Done!</h2>
</div>
</body>
</html>";
function isValidPhone($phone)
{
$justNums = preg_replace("/[^0-9]/", '', $phone);
if (strlen($justNums) === 11) {
$justNums = preg_replace("/^1/", '', $justNums);
}
//if we have 10 digits left, it's probably valid.
if (strlen($justNums) === 10) {
return $justNums;
} else {
return false;
}
}
/**
* Integrate cron functions into this script
*
* Borrowed from cron_functions.php. Update status yes if alert send to patient
*
* @param unknown $type
* @param int $pid
* @param int $pc_eid
* @param string $recur
* @return void
*/
function cron_updateentry($type, $pid, $pc_eid, $recur = '')
{
global $bTestRun;
if ($bTestRun || (int)trim($recur) > 0) {
return 1;
}
$query = "UPDATE openemr_postcalendar_events SET ";
if ($type == 'SMS') {
$query .= " pc_sendalertsms='YES', pc_apptstatus='SMS' ";
} else {
$query .= " pc_sendalertemail='YES' ";
}
$query .= " where pc_pid=? and pc_eid=? ";
$db_sql = sqlStatement($query, array($pid, $pc_eid));
}
/**
* Cron Get Alert Patient Data
* *
* @param $type
* @return array
*/
function cron_getAlertpatientData()
{
global $SMS_NOTIFICATION_HOUR, $EMAIL_NOTIFICATION_HOUR;
$where = " AND (p.hipaa_allowsms='YES' AND p.phone_cell<>'' AND e.pc_sendalertsms != 'YES' AND e.pc_apptstatus != 'x')";
$check_date = date("Y-m-d", mktime(date("h") + $SMS_NOTIFICATION_HOUR, 0, 0, date("m"), date("d"), date("Y")));
$patient_array = fetchEvents($check_date, $check_date, $where, 'u.lname,pc_startTime,p.lname');
return $patient_array;
}
/**
* Cron Get Notification Data
*
* @param string $type
* @return void
*/
function cron_getNotificationData($type)
{
$db_sms_msg['notification_id'] = '';
$db_sms_msg['sms_gateway_type'] = '';
$query = "select * from automatic_notification where type=?";
//echo "<br>".$query;
$db_sms_msg = sqlFetchArray(sqlStatement($query, [$type]));
return $db_sms_msg;
}
/**
* Cron Insert Notification Log Entry
*
* @param string $type
* @param array $prow
* @param array $db_sms_msg
* @return void
*/
function cron_InsertNotificationLogEntry($type, $prow, $db_sms_msg)
{
global $SMS_GATEWAY_USENAME, $SMS_GATEWAY_PASSWORD, $SMS_GATEWAY_APIKEY;
if ($type == 'SMS') {
$smsgateway_info = ""; //$db_sms_msg['sms_gateway_type'] . "|||" . $SMS_GATEWAY_USENAME . "|||" . $SMS_GATEWAY_PASSWORD . "|||" . $SMS_GATEWAY_APIKEY;
} else {
$smsgateway_info = $db_sms_msg['email_sender'] . "|||" . $db_sms_msg['email_subject'];
}
$patient_info = $prow['title'] . " " . $prow['fname'] . " " . $prow['mname'] . " " . $prow['lname'] . "|||" . $prow['phone_cell'] . "|||" . $prow['email'];
$data_info = $prow['pc_eventDate'] . "|||" . $prow['pc_endDate'] . "|||" . $prow['pc_startTime'] . "|||" . $prow['pc_endTime'];
$sdate = date("Y-m-d H:i:s");
$sql_loginsert = "INSERT INTO `notification_log` ( `iLogId` , `pid` , `pc_eid` , `sms_gateway_type` , `message` , `type` , `patient_info` , `smsgateway_info` , `pc_eventDate` , `pc_endDate` , `pc_startTime` , `pc_endTime` , `dSentDateTime` ) VALUES (NULL,?,?,?,?,?,?,?,?,?,?,?,?)";
$safe = array($prow['pid'], $prow['pc_eid'], $db_sms_msg['sms_gateway_type'], $db_sms_msg['message'], $db_sms_msg['type'] || '', $patient_info, $smsgateway_info, $prow['pc_eventDate'], $prow['pc_endDate'], $prow['pc_startTime'], $prow['pc_endTime'], $sdate);
$db_loginsert = sqlStatement($sql_loginsert, $safe);
}
/**
* Cron Set Message
*
* @param array $prow
* @param array $db_sms_msg
* @return string
*/
function cron_setmessage($prow, $db_sms_msg)
{
// larry :: debug
//echo "\nDEBUG :cron_setmessage: set message ".$prow['title']." ".$prow['fname']." ".$prow['mname']." ".$prow['lname']."\n";
$NAME = $prow['title'] . " " . $prow['fname'] . " " . $prow['mname'] . " " . $prow['lname'];
$apptProvider = $prow['utitle'] . ' ' . $prow['ufname'] . ' ' . $prow['ulname'];
$PROVIDER = $apptProvider;
$ORG = $prow['name'];
$dtWrk = strtotime($prow['pc_eventDate'] . ' ' . $prow['pc_startTime']);
$DATE = date('l F j, Y', $dtWrk);
$STARTTIME = date('g:i A', $dtWrk);
$ENDTIME = $prow['pc_endTime'];
$find_array = array("***NAME***", "***PROVIDER***", "***DATE***", "***STARTTIME***", "***ENDTIME***", "***ORG***");
$replace_array = array($NAME, $PROVIDER, $DATE, $STARTTIME, $ENDTIME, $ORG);
$message = str_replace($find_array, $replace_array, $db_sms_msg['message']);
$message = htmlspecialchars($message);
return $message;
}
/**
* Get Notification Settings
*
* @return void
*/
function cron_GetNotificationSettings()
{
$strQuery = "SELECT * FROM notification_settings WHERE type='SMS/Email Settings'";
$vectNotificationSettings = sqlFetchArray(sqlStatement($strQuery));
return ($vectNotificationSettings);
}