Highway to PSR2
[openemr.git] / interface / main / dated_reminders / dated_reminders_add.php
blob7cadf28d975f12099bf527aff7685a06080d3c24
1 <?php
2 /**
3 * Used for adding dated reminders.
5 * @package OpenEMR
6 * @author Craig Bezuidenhout <http://www.tajemo.co.za/>
7 * @author Brady Miller <brady.g.miller@gmail.com>
8 * @copyright Copyright (C) 2012 tajemo.co.za <http://www.tajemo.co.za/>
9 * @copyright Copyright (C) 2017 Brady Miller <brady.g.miller@gmail.com>
10 * @link http://www.open-emr.org
13 require_once("../../globals.php");
14 require_once("$srcdir/dated_reminder_functions.php");
16 $dateRanges = array();
17 // $dateranges = array ( number_period => text to display ) == period is always in the singular
18 // eg. $dateRanges['4_week'] = '4 Weeks From Now';
19 $dateRanges['1_day'] = xl('1 Day From Now');
20 $dateRanges['2_day'] = xl('2 Days From Now');
21 $dateRanges['3_day'] = xl('3 Days From Now');
22 $dateRanges['4_day'] = xl('4 Days From Now');
23 $dateRanges['5_day'] = xl('5 Days From Now');
24 $dateRanges['6_day'] = xl('6 Days From Now');
25 $dateRanges['1_week'] = xl('1 Week From Now');
26 $dateRanges['2_week'] = xl('2 Weeks From Now');
27 $dateRanges['3_week'] = xl('3 Weeks From Now');
28 $dateRanges['4_week'] = xl('4 Weeks From Now');
29 $dateRanges['5_week'] = xl('5 Weeks From Now');
30 $dateRanges['6_week'] = xl('6 Weeks From Now');
31 $dateRanges['1_month'] = xl('1 Month From Now');
32 $dateRanges['2_month'] = xl('2 Months From Now');
33 $dateRanges['3_month'] = xl('3 Months From Now');
34 $dateRanges['4_month'] = xl('4 Months From Now');
35 $dateRanges['5_month'] = xl('5 Months From Now');
36 $dateRanges['6_month'] = xl('6 Months From Now');
37 $dateRanges['7_month'] = xl('7 Months From Now');
38 $dateRanges['8_month'] = xl('8 Months From Now');
39 $dateRanges['9_month'] = xl('9 Months From Now');
40 $dateRanges['1_year'] = xl('1 Year From Now');
41 $dateRanges['2_year'] = xl('2 Years From Now');
43 // --- need to add a check to ensure the post is being sent from the correct location ???
45 // default values for $this_message
46 $this_message = array('message'=>'','message_priority'=>3,'dueDate'=>'');
47 $forwarding = false;
49 // default values for Max words to input in a reminder
50 $max_reminder_words=160;
52 // ---------------- FOR FORWARDING MESSAGES ------------->
53 if (isset($_GET['mID']) and is_numeric($_GET['mID'])) {
54 $forwarding = true;
55 $this_message = getReminderById($_GET['mID']);
58 // ---------------END FORWARDING MESSAGES ----------------
62 // --- add reminders
63 if ($_POST) {
64 // --- initialize $output as blank
65 $output = '';
66 // ------ fills an array with all recipients
67 $sendTo = $_POST['sendTo'];
69 // for incase of data error, this allows the previously entered data to re-populate the boxes
70 $this_message['message'] = (isset($_POST['message']) ? $_POST['message'] : '');
71 $this_message['priority'] = (isset($_POST['priority']) ? $_POST['priority'] : '');
72 $this_message['dueDate'] = (isset($_POST['dueDate']) ? $_POST['dueDate'] : '');
75 // --------------------------------------------------------------------------------------------------------------------------
76 // --- check for the post, if it is valid, commit to the database, close this window and run opener.Handeler
77 if (// ------- check sendTo is not empty
78 !empty($sendTo) and
79 // ------- check dueDate, only allow valid dates, todo -> enhance date checker
80 isset($_POST['dueDate']) and preg_match('/\d{4}[-]\d{2}[-]\d{2}/', $_POST['dueDate']) and
81 // ------- check priority, only allow 1-3
82 isset($_POST['priority']) and intval($_POST['priority']) <= 3 and
83 // ------- check message, only up to 160 characters limited by Db
84 isset($_POST['message']) and strlen($_POST['message']) <= $max_reminder_words and strlen($_POST['message']) > 0 and
85 // ------- check if PatientID is set and in numeric
86 isset($_POST['PatientID']) and is_numeric($_POST['PatientID'])
87 ) {
88 $dueDate = $_POST['dueDate'];
89 $priority = intval($_POST['priority']);
90 $message = $_POST['message'];
91 $fromID = $_SESSION['authId'];
92 $patID = $_POST['PatientID'];
93 if (isset($_POST['sendSeperately']) and $_POST['sendSeperately']) {
94 foreach ($sendTo as $st) {
95 $ReminderSent = sendReminder(array($st), $fromID, $message, $dueDate, $patID, $priority);
97 } else {
98 // -------- Send the reminder
99 $ReminderSent = sendReminder($sendTo, $fromID, $message, $dueDate, $patID, $priority);
102 // --------------------------------------------------------------------------------------------------------------------------
103 if (!$ReminderSent) {
104 $output .= '<div style="text-size:2em; text-align:center; color:red">* '.xlt('Please select a valid recipient').'</div> ';
105 } else {
106 // --------- echo javascript
107 echo '<html><body>'
108 ."<script type=\"text/javascript\" src=\"". $webroot ."/interface/main/tabs/js/include_opener.js\"></script>"
109 .'<script language="JavaScript">';
110 // ------------ 1) refresh parent window this updates if sent to self
111 echo ' if (opener && !opener.closed && opener.updateme) opener.updateme("new");';
112 // ------------ 2) communicate with user
113 echo ' alert("'.addslashes(xl('Reminder Sent')).'");';
114 // ------------ 3) close this window
115 echo ' window.close();';
116 echo '</script></body></html>';
117 // --------- stop script from executing further
118 exit;
121 // --------------------------------------------------------------------------------------------------------------------------
122 } // --------------------------------------------------------------------------------------------------------------------------
124 else {
125 // ------- if POST error
126 $output .= '<div style="text-size:2em; text-align:center; color:red">* '.xlt('Data Error').'</div> ';
129 // ------- if any errors, communicate with the user
130 echo $output;
133 // end add reminders
135 // get current patient, first check if this is a forwarded message, if it is use the original pid
136 if (isset($this_message['pid'])) {
137 $patientID = (isset($this_message['pid']) ? $this_message['pid'] : 0);
138 } else {
139 $patientID = (isset($pid) ? $pid : 0);
142 <html>
143 <head>
144 <title><?php echo xlt('Send a Reminder') ?></title>
146 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
147 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
149 <script type="text/javascript" src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js?v=<?php echo $v_js_includes; ?>"></script>
150 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/topdialog.js?v=<?php echo $v_js_includes; ?>"></script>
151 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js?v=<?php echo $v_js_includes; ?>"></script>
152 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js?v=<?php echo $v_js_includes; ?>"></script>
153 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script>
154 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
155 <script language="JavaScript">
156 $(document).ready(function (){
158 $('#timeSpan').change(function(){
159 var value = $(this).val();
160 var arr = value.split('_');
161 var span = arr[1];
162 var period = parseInt(arr[0]);
163 var d=new Date();
164 if(span == 'day'){
165 d.setDate(d.getDate()+period);
167 else if(span == 'week'){
168 var weekInDays = period * 7;
169 d.setDate(d.getDate()+weekInDays);
171 else if(span == 'month'){
172 d.setMonth(d.getMonth()+period);
174 else if(span == 'year'){
175 var yearsInMonths = period * 12;
176 d.setMonth(d.getMonth()+yearsInMonths);
178 var curr_date = d.getDate().toString();
179 if(curr_date.length == 1){
180 curr_date = '0'+curr_date;
182 var curr_month = d.getMonth() + 1; //months are zero based
183 curr_month = curr_month.toString();
184 if(curr_month.length == 1){
185 curr_month = '0'+curr_month;
187 var curr_year = d.getFullYear();
188 $('#dueDate').val(curr_year + "-" + curr_month + "-" + curr_date);
192 $("#sendButton").click(function(){
193 $('#errorMessage').html('');
194 errorMessage = '';
195 var PatientID = $('#PatientID').val();
196 var dueDate = $('#dueDate').val();
197 var priority = $('#priority:checked').val();
198 var message = $("#message").val();
199 // todo : check if PatientID is numeric , no rush as this is checked in the php after the post
201 // check to see if a recipient has been set
202 // todo : check if they are all numeric , no rush as this is checked in the php after the post
204 if (!$("#sendTo option:selected").length){
205 errorMessage = errorMessage + '* <?php echo xla('Please Select A Recipient') ?><br />';
209 // Check if Date is set
210 // todo : add check to see if dueDate is a valid date , no rush as this is checked in the php after the post
211 if(dueDate == ''){
212 errorMessage = errorMessage + '* <?php echo xla('Please enter a due date') ?><br />';
215 // check if message is set
216 if(message == ''){
217 errorMessage = errorMessage + '* <?php echo xla('Please enter a message') ?><br />';
220 if(errorMessage != ''){
221 // handle invalid queries
222 $('#errorMessage').html(errorMessage);
224 else{
225 // handle valid queries
226 // post the form to self
227 top.restoreSession();
228 $("#addDR").submit();
230 return false;
233 $("#removePatient").click(function(){
234 $("#PatientID").val("0");
235 $("#patientName").val("<?php echo xla('Click to select patient'); ?>");
236 $(this).hide();
237 return false;
239 // update word counter
240 var messegeTextarea=$("#message")[0];
241 limitText(messegeTextarea.form.message,messegeTextarea.form.countdown,<?php echo $max_reminder_words ?>);
243 $('.datepicker').datetimepicker({
244 <?php $datetimepicker_timepicker = false; ?>
245 <?php $datetimepicker_showseconds = false; ?>
246 <?php $datetimepicker_formatInput = false; ?>
247 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
248 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
252 function sel_patient(){
253 window.open('../../main/calendar/find_patient_popup.php', '_newDRPat', '' + ",width=" + 500 + ",height=" + 400 + ",left=" + 25 + ",top=" + 25 + ",screenX=" + 25 + ",screenY=" + 25);
256 function setpatient(pid, lname, fname, dob){
257 $("#patientName").val(fname +' '+ lname)
258 $("#PatientID").val(pid);
259 $("#removePatient").show();
260 return false;
263 function limitText(limitField, limitCount, limitNum) {
264 if (limitField.value.length > limitNum) {
265 limitField.value = limitField.value.substring(0, limitNum);
266 } else {
267 limitCount.value = limitNum - limitField.value.length;
271 function selectAll(){
272 $("#sendTo").each(function(){$("#sendTo option").attr("selected","selected"); });
274 </script>
275 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
276 </head>
277 <body class="body_top">
278 <!-- Required for the popup date selectors -->
279 <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
282 <h1><?php echo xlt('Send a Reminder') ?></h1>
283 <form id="addDR" style="margin:0 0 10px 0;" id="newMessage" method="post" onsubmit="return top.restoreSession()">
284 <div style="text-align:center; color:red" id="errorMessage"></div>
286 <fieldset>
287 <?php echo xlt('Link To Patient') ?> :
288 <input type='text' size='10' id='patientName' name='patientName' style='width:200px;cursor:pointer;cursor:hand'
289 value='<?php echo ($patientID > 0 ? attr(getPatName($patientID)) : xla('Click to select patient')); ?>' onclick='sel_patient()'
290 title='<?php xla('Click to select patient'); ?>' readonly />
291 <input type="hidden" name="PatientID" id="PatientID" value="<?php echo (isset($patientID) ? attr($patientID) : 0) ?>" />
292 <button <?php echo ($patientID > 0 ? '' : 'style="display:none"') ?> id="removePatient"><?php echo xlt('unlink patient') ?></button>
293 </fieldset>
296 <br />
299 <fieldset>
300 <table style="width:100%;" cellpadding="5px">
301 <tr>
302 <td style="width:20%; text-align:right" valign="top">
303 <?php echo xlt('Send to') ?> : <br /><?php echo xlt('([ctrl] + click to select multiple recipients)'); ?>
304 </td>
305 <td style="width:60%;">
306 <select style="width:100%" id="sendTo" name="sendTo[]" multiple="multiple">
307 <option value="<?php echo attr(intval($_SESSION['authId'])); ?>"><?php echo xlt('Myself') ?></option>
308 <?php //
309 $uSQL = sqlStatement('SELECT id, fname, mname, lname FROM `users` WHERE `active` = 1 AND `facility_id` > 0 AND id != ?', array(intval($_SESSION['authId'])));
310 for ($i=2; $uRow=sqlFetchArray($uSQL); $i++) {
311 echo '<option value="',attr($uRow['id']),'">',text($uRow['fname'].' '.$uRow['mname'].' '.$uRow['lname']),'</option>';
314 </select> <br />
315 <input title="<?php echo xlt('Selecting this will create a message that needs to be processed by each recipient individually (this is not a group task).') ?>" type="checkbox" name="sendSeperately" id="sendSeperately" /> <label title="<?php echo xlt('Selecting this will create a message that needs to be processed by each recipient individually (this is not a group task).') ?>" for="sendSeperately">(<?php echo xlt('Each recipient must set their own messages as completed.') ?>)</label>
316 </td>
317 <td style="text-align:right">
318 <a class="css_button_small" style="cursor:pointer" onclick="selectAll();" ><span><?php echo xlt('Send to all') ?></span></a>
319 </td>
320 </table>
321 </fieldset>
323 <br />
325 <fieldset>
326 <?php echo xlt('Due Date') ?> : <input type='text' class='datepicker' name='dueDate' id="dueDate" size='20' value="<?php echo ($this_message['dueDate'] == '' ? date('Y-m-d') : attr($this_message['dueDate'])); ?>" title='<?php echo htmlspecialchars(xl('yyyy-mm-dd'), ENT_QUOTES); ?>' />
327 <?php echo xlt('OR') ?>
328 <?php echo xlt('Select a Time Span') ?> : <select id="timeSpan">
329 <option value="__BLANK__"> -- <?php echo xlt('Select a Time Span') ?> -- </option>
330 <?php
331 $optionTxt = '';
332 foreach ($dateRanges as $val => $txt) {
333 $optionTxt .= '<option value="'.attr($val).'">'.text($txt).'</option>';
336 echo $optionTxt;
338 </select>
339 </td>
340 </fieldset>
343 <br />
347 <fieldset>
348 <?php echo xlt('Priority') ?> :
349 <input <?php echo ($this_message['message_priority'] == 3 ? 'checked="checked"' : '') ?> type="radio" name="priority" id="priority_3" value='3'> <label for="priority_3"><?php echo xlt('Low') ?></label>
350 <input <?php echo ($this_message['message_priority'] == 2 ? 'checked="checked"' : '') ?> type="radio" name="priority" id="priority_2" value='2'> <label for="priority_2"><?php echo xlt('Medium') ?></label>
351 <input <?php echo ($this_message['message_priority'] == 1 ? 'checked="checked"' : '') ?> type="radio" name="priority" id="priority_1" value='1'> <label for="priority_1"><?php echo xlt('High') ?></label>
352 </fieldset>
355 <br />
358 <fieldset>
359 <table style="width:100%;">
360 <tr>
361 <td valign="top" style="width:25%">
362 <?php echo xlt('Type Your message here') ?> :<br /><br />
363 <font size="1">(<?php echo xlt('Maximum characters') ?>: <?php echo $max_reminder_words ?>)<br />
364 </td>
365 <td valign="top" style="width:75%">
366 <textarea onKeyDown="limitText(this.form.message,this.form.countdown,<?php echo $max_reminder_words ?>);"
367 onKeyUp="limitText(this.form.message,this.form.countdown,<?php echo $max_reminder_words ?>);"
368 style="width:100%; height:50px" name="message" id="message"><?php echo text($this_message['dr_message_text']); ?></textarea>
369 <br />
370 <?php echo xlt('Characters Remaining') ?> : <input style="border:0; background:none;" readonly type="text" name="countdown" size="3" value="<?php echo $max_reminder_words ?>"> </font>
371 </td>
372 </tr>
373 </table>
374 </fieldset>
377 <p align="center">
378 <input type="submit" id="sendButton" value="<?php echo xla('Send This Message') ?>" />
379 </p>
380 </form>
381 <?php
382 $_GET['sentBy'] = array($_SESSION['authId']);
383 $_GET['sd'] = date('Y/m/d');
384 $TempRemindersArray = logRemindersArray();
385 $remindersArray = array();
386 foreach ($TempRemindersArray as $RA) {
387 $remindersArray[$RA['messageID']]['messageID'] = $RA['messageID'];
388 $remindersArray[$RA['messageID']]['ToName'] = ($remindersArray[$RA['messageID']]['ToName'] ? $remindersArray[$RA['messageID']]['ToName'].', '.$RA['ToName'] : $RA['ToName']);
389 $remindersArray[$RA['messageID']]['PatientName'] = $RA['PatientName'];
390 $remindersArray[$RA['messageID']]['message'] = $RA['message'];
391 $remindersArray[$RA['messageID']]['dDate'] = $RA['dDate'];
394 echo '<h2>',xlt('Messages You have sent Today'),'</h2>';
395 echo '<table border="1" width="100%" cellpadding="5px" id="logTable">
396 <thead>
397 <tr>
398 <th>'.xlt('ID').'</th>
399 <th>'.xlt('To').'</th>
400 <th>'.xlt('Patient').'</th>
401 <th>'.xlt('Message').'</th>
402 <th>'.xlt('Due Date').'</th>
403 </tr>
404 </thead>
405 <tbody>';
407 foreach ($remindersArray as $RA) {
408 echo '<tr class="heading">
409 <td>',text($RA['messageID']),'</td>
410 <td>',text($RA['ToName']),'</td>
411 <td>',text($RA['PatientName']),'</td>
412 <td>',text($RA['message']),'</td>
413 <td>',text($RA['dDate']),'</td>
414 </tr>';
417 echo '</tbody></table>';
419 </body>
420 </html>