Multiple improvements from IPPF related to layouts. (#1081)
[openemr.git] / interface / cmsportal / patient_form.php
blob7636686ad1332f566f0818d1ecaa34bcc9f9caac
1 <?php
2 /**
3 * Patient matching and selection for the WordPress Patient Portal.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Rod Roark <rod@sunsetsystems.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2014 Rod Roark <rod@sunsetsystems.com>
10 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../globals.php");
16 require_once("$srcdir/patient.inc");
17 require_once("$srcdir/options.inc.php");
18 require_once("portal.inc.php");
20 $postid = intval($_REQUEST['postid']);
21 $ptid = intval($_REQUEST['ptid' ]);
23 if ($_POST['bn_save']) {
24 $newdata = array();
25 $newdata['patient_data' ] = array();
26 $newdata['employer_data'] = array();
27 $ptid = intval($_POST['ptid']);
28 // Note we are careful to maintain cmsportal_login even if the layout has it
29 // configured as unused.
30 $fres = sqlStatement("SELECT * FROM layout_options WHERE " .
31 "form_id = 'DEM' AND field_id != '' AND (uor > 0 OR field_id = 'cmsportal_login') " .
32 "ORDER BY group_id, seq");
33 while ($frow = sqlFetchArray($fres)) {
34 $data_type = $frow['data_type'];
35 $field_id = $frow['field_id'];
36 $table = 'patient_data';
37 if (isset($_POST["form_$field_id"])) {
38 $newdata[$table][$field_id] = get_layout_form_value($frow);
42 if (empty($ptid)) {
43 $tmp = sqlQuery("SELECT MAX(pid)+1 AS pid FROM patient_data");
44 $ptid = empty($tmp['pid']) ? 1 : intval($tmp['pid']);
45 if (empty($newdata['patient_data']['pubpid'])) {
46 // pubpid for new patient defaults to pid.
47 $newdata['patient_data']['pubpid'] = "$ptid";
50 updatePatientData($ptid, $newdata['patient_data' ], true);
51 updateEmployerData($ptid, $newdata['employer_data'], true);
52 newHistoryData($ptid);
53 } else {
54 $newdata['patient_data']['id'] = $_POST['db_id'];
55 updatePatientData($ptid, $newdata['patient_data']);
58 // Finally, delete the request from the portal.
59 $result = cms_portal_call(array('action' => 'delpost', 'postid' => $postid));
60 if ($result['errmsg']) {
61 die(text($result['errmsg']));
64 echo "<html><body><script language='JavaScript'>\n";
65 echo "if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();\n";
66 echo "document.location.href = 'list_requests.php';\n";
67 echo "</script></body></html>\n";
68 exit();
71 $db_id = 0;
72 if ($ptid) {
73 $ptrow = getPatientData($ptid, "*");
74 $db_id = $ptrow['id'];
77 if ($postid) {
78 $result = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
79 if ($result['errmsg']) {
80 die(text($result['errmsg']));
84 <html>
85 <head>
86 <?php html_header_show(); ?>
87 <link rel=stylesheet href="<?php echo $css_header; ?>" type="text/css">
88 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
90 <style>
92 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
93 tr.detail { font-size:10pt; background-color:#ddddff; }
94 td input { background-color:transparent; }
96 </style>
98 <script type="text/javascript" src="../../library/textformat.js?v=<?php echo $v_js_includes; ?>"></script>
99 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-7-2/index.js"></script>
100 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script>
102 <script language="JavaScript">
104 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
106 function myRestoreSession() {
107 if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();
108 return true;
111 // This capitalizes the first letter of each word in the passed input
112 // element. It also strips out extraneous spaces.
113 // Copied from demographics_full.php.
114 function capitalizeMe(elem) {
115 var a = elem.value.split(' ');
116 var s = '';
117 for(var i = 0; i < a.length; ++i) {
118 if (a[i].length > 0) {
119 if (s.length > 0) s += ' ';
120 s += a[i].charAt(0).toUpperCase() + a[i].substring(1);
123 elem.value = s;
126 // Generates and returns a random 6-character password.
128 function randompass() {
129 var newpass = '';
130 var newchar = '';
131 while (newpass.length < 6) {
132 var r = Math.floor(Math.random() * 33); // for 2-9 and a-y
133 if (r > 7) {
134 newchar = String.fromCharCode('a'.charCodeAt(0) + r - 8);
135 if (newchar == 'l') newchar = 'z';
136 } else {
137 newchar = String.fromCharCode('2'.charCodeAt(0) + r);
139 newpass += newchar;
141 var e = document.forms[0].form_cmsportal_login_pass;
142 if (e) e.value = newpass;
145 // If needed, this creates the new patient in the CMS. It executes as an AJAX script
146 // in case it doesn't work and a correction is needed before submitting the form.
148 function validate() {
149 var f = document.forms[0];
150 var errmsg = '';
151 myRestoreSession();
152 if (f.form_cmsportal_login_pass) {
153 var login = encodeURIComponent(f.form_cmsportal_login.value);
154 var pass = encodeURIComponent(f.form_cmsportal_login_pass.value);
155 var email = encodeURIComponent(f.form_email.value);
156 if (login) {
157 if (!pass) {
158 alert('<?php echo xls('Portal password is missing'); ?>');
159 return false;
161 if (!email) {
162 alert('<?php echo xls('Email address is missing'); ?>');
163 return false;
165 // Need a *synchronous* ajax request here. Successful updating of the portal
166 // is required before we can submit the form.
167 $.ajax({
168 type: "GET",
169 dataType: "text",
170 url: 'patient_form_ajax.php?login=' + login + '&pass=' + pass + '&email=' + email,
171 async: false,
172 success: function(data) {
173 if (data) {
174 alert(data);
175 errmsg = data;
181 if (errmsg) return false;
182 return true;
185 $(document).ready(function() {
186 $('.datepicker').datetimepicker({
187 <?php $datetimepicker_timepicker = false; ?>
188 <?php $datetimepicker_showseconds = false; ?>
189 <?php $datetimepicker_formatInput = false; ?>
190 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
191 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
193 $('.datetimepicker').datetimepicker({
194 <?php $datetimepicker_timepicker = true; ?>
195 <?php $datetimepicker_showseconds = false; ?>
196 <?php $datetimepicker_formatInput = false; ?>
197 <?php require($GLOBALS['srcdir'] . '/js/xl/jquery-datetimepicker-2-5-4.js.php'); ?>
198 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
202 </script>
203 </head>
205 <body class="body_top">
206 <center>
208 <form method='post' action='patient_form.php' onsubmit='return validate()'>
210 <input type='hidden' name='db_id' value="<?php echo attr($db_id); ?>" />
211 <input type='hidden' name='ptid' value="<?php echo attr($ptid); ?>" />
212 <input type='hidden' name='postid' value="<?php echo attr($postid); ?>" />
214 <table width='100%' cellpadding='1' cellspacing='2'>
215 <tr class='head'>
216 <th align='left'><?php echo xlt('Field'); ?></th>
217 <th align='left'><?php echo xlt('Current Value'); ?></th>
218 <th align='left'><?php echo xlt('New Value'); ?></th>
219 </tr>
221 <?php
222 $lores = sqlStatement(
223 "SELECT * FROM layout_options " .
224 "WHERE form_id = ? AND uor > 0 ORDER BY group_id, seq",
225 array('DEM')
228 // Will be used to indicate if this user does not yet have a portal login.
229 $portal_registration_needed = false;
231 while ($lorow = sqlFetchArray($lores)) {
232 $data_type = $lorow['data_type'];
233 $field_id = $lorow['field_id'];
234 // We deal with this one at the end.
235 if ($field_id == 'cmsportal_login') {
236 continue;
239 // Flamingo translates field names to lower case so we have to match with those.
240 $reskey = $field_id;
241 foreach ($result['fields'] as $key => $dummy) {
242 if (strcasecmp($key, $field_id) == 0) {
243 $reskey = $key;
247 // Generate form fields for items that are either from the WordPress form
248 // or are mandatory for a new patient.
249 if (isset($result['fields'][$reskey]) || ($lorow['uor'] > 1 && $ptid == 0)) {
250 $list_id = $lorow['list_id'];
251 $field_title = $lorow['title'];
252 if ($field_title === '') {
253 $field_title = '(' . $field_id . ')';
256 $currvalue = '';
257 if (isset($ptrow[$field_id])) {
258 $currvalue = $ptrow[$field_id];
261 /*****************************************************************
262 $newvalue = '';
263 if (isset($result['fields'][$reskey])) $newvalue = $result['fields'][$reskey];
264 //// Zero-length input means nothing will change.
265 // if ($newvalue === '') $newvalue = $currvalue;
266 // $newvalue = trim($newvalue);
267 $newvalue = cms_field_to_lbf($newvalue, $data_type, $field_id);
268 *****************************************************************/
269 $newvalue = cms_field_to_lbf($data_type, $reskey, $result['fields']);
271 echo " <tr class='detail'>\n";
272 echo " <td class='bold'>" . text($field_title) . "</td>\n";
273 echo " <td>" . generate_display_field($lorow, $currvalue) . "</td>\n";
274 echo " <td>";
275 generate_form_field($lorow, $newvalue);
276 echo "</td>\n";
277 echo " </tr>\n";
281 $field_id = 'cmsportal_login';
282 if (empty($ptrow[$field_id])) {
283 if ($result['post']['user'] !== '') {
284 // Registered in portal but still need to record that in openemr.
285 echo "</table>\n";
286 echo "<input type='hidden' name='form_$field_id' value='" . attr($result['post']['user']) . "' />\n";
287 } else {
288 // Portal registration is needed.
289 $newvalue = isset($result['fields']['email']) ? trim($result['fields']['email']) : '';
290 echo " <tr class='detail'>\n";
291 echo " <td class='bold' style='color:red;'>" . xlt('New Portal Login') . "</td>\n";
292 echo " <td>&nbsp;</td>\n";
293 echo " <td>";
294 echo "<input type='text' name='form_$field_id' size='10' maxlength='60' value='" . attr($newvalue) . "' />";
295 echo "&nbsp;&nbsp;" . xlt('Password') . ": ";
296 echo "<input type='text' name='form_" . attr($field_id) . "_pass' size='10' maxlength='60' />";
297 echo "<input type='button' value='" . xla('Generate') . "' onclick='randompass()' />";
298 echo "</td>\n";
299 echo " </tr>\n";
300 echo "</table>\n";
302 } else {
303 // Portal login name is already in openemr.
304 echo "</table>\n";
309 <input type='submit' name='bn_save' value='<?php echo xla('Save and Delete Request'); ?>' />
310 &nbsp;
311 <input type='button' value='<?php echo xla('Back'); ?>' onclick="window.history.back()" />
312 <!-- Was: onclick="myRestoreSession();location='list_requests.php'" -->
313 </p>
315 </form>
317 <script language="JavaScript">
319 // hard code validation for old validation, in the new validation possible to add match rules
320 <?php if ($GLOBALS['new_validate'] == 0) { ?>
322 // Fix inconsistently formatted phone numbers from the database.
323 var f = document.forms[0];
324 if (f.form_phone_contact) phonekeyup(f.form_phone_contact,mypcc);
325 if (f.form_phone_home ) phonekeyup(f.form_phone_home ,mypcc);
326 if (f.form_phone_biz ) phonekeyup(f.form_phone_biz ,mypcc);
327 if (f.form_phone_cell ) phonekeyup(f.form_phone_cell ,mypcc);
329 <?php }?>
331 randompass();
333 // This is a by-product of generate_form_field().
334 <?php echo $date_init; ?>
336 </script>
338 <!-- include support for the list-add selectbox feature -->
339 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
341 </center>
342 </body>
343 </html>