fixes to prior statements improvements
[openemr.git] / interface / cmsportal / patient_form.php
blob76ac8997e584855796c12b20c82e4f20e85b0329
1 <?php
2 /**
3 * Patient matching and selection for the WordPress Patient Portal.
5 * Copyright (C) 2014 Rod Roark <rod@sunsetsystems.com>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
18 * @package OpenEMR
19 * @author Rod Roark <rod@sunsetsystems.com>
22 $sanitize_all_escapes = true;
23 $fake_register_globals = false;
25 require_once("../globals.php");
26 require_once("$srcdir/patient.inc");
27 require_once("$srcdir/formdata.inc.php");
28 require_once("$srcdir/options.inc.php");
29 require_once("portal.inc.php");
31 $postid = intval($_REQUEST['postid']);
32 $ptid = intval($_REQUEST['ptid' ]);
34 if ($_POST['bn_save']) {
35 $newdata = array();
36 $newdata['patient_data' ] = array();
37 $newdata['employer_data'] = array();
38 $ptid = intval($_POST['ptid']);
39 // Note we are careful to maintain cmsportal_login even if the layout has it
40 // configured as unused.
41 $fres = sqlStatement("SELECT * FROM layout_options WHERE " .
42 "form_id = 'DEM' AND field_id != '' AND (uor > 0 OR field_id = 'cmsportal_login') " .
43 "ORDER BY group_name, seq");
44 while ($frow = sqlFetchArray($fres)) {
45 $data_type = $frow['data_type'];
46 $field_id = $frow['field_id'];
47 $table = 'patient_data';
48 if (isset($_POST["form_$field_id"])) {
49 $newdata[$table][$field_id] = get_layout_form_value($frow);
52 if (empty($ptid)) {
53 $tmp = sqlQuery("SELECT MAX(pid)+1 AS pid FROM patient_data");
54 $ptid = empty($tmp['pid']) ? 1 : intval($tmp['pid']);
55 if (empty($newdata['patient_data']['pubpid'])) {
56 // pubpid for new patient defaults to pid.
57 $newdata['patient_data']['pubpid'] = "$ptid";
59 updatePatientData ($ptid, $newdata['patient_data' ], true);
60 updateEmployerData($ptid, $newdata['employer_data'], true);
61 newHistoryData($ptid);
63 else {
64 $newdata['patient_data']['id'] = $_POST['db_id'];
65 updatePatientData($ptid, $newdata['patient_data']);
67 // Finally, delete the request from the portal.
68 $result = cms_portal_call(array('action' => 'delpost', 'postid' => $postid));
69 if ($result['errmsg']) {
70 die(text($result['errmsg']));
72 echo "<html><body><script language='JavaScript'>\n";
73 echo "if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();\n";
74 echo "document.location.href = 'list_requests.php';\n";
75 echo "</script></body></html>\n";
76 exit();
79 $db_id = 0;
80 if ($ptid) {
81 $ptrow = getPatientData($ptid, "*");
82 $db_id = $ptrow['id'];
85 if ($postid) {
86 $result = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
87 if ($result['errmsg']) {
88 die(text($result['errmsg']));
92 <html>
93 <head>
94 <?php html_header_show(); ?>
95 <link rel=stylesheet href="<?php echo $css_header; ?>" type="text/css">
97 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
98 <style>
100 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
101 tr.detail { font-size:10pt; background-color:#ddddff; }
102 td input { background-color:transparent; }
104 </style>
106 <script type="text/javascript" src="../../library/textformat.js"></script>
107 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
108 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
109 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
110 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-2-2/index.js"></script>
112 <script language="JavaScript">
114 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
116 function myRestoreSession() {
117 if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();
118 return true;
121 // This capitalizes the first letter of each word in the passed input
122 // element. It also strips out extraneous spaces.
123 // Copied from demographics_full.php.
124 function capitalizeMe(elem) {
125 var a = elem.value.split(' ');
126 var s = '';
127 for(var i = 0; i < a.length; ++i) {
128 if (a[i].length > 0) {
129 if (s.length > 0) s += ' ';
130 s += a[i].charAt(0).toUpperCase() + a[i].substring(1);
133 elem.value = s;
136 // Generates and returns a random 6-character password.
138 function randompass() {
139 var newpass = '';
140 var newchar = '';
141 while (newpass.length < 6) {
142 var r = Math.floor(Math.random() * 33); // for 2-9 and a-y
143 if (r > 7) {
144 newchar = String.fromCharCode('a'.charCodeAt(0) + r - 8);
145 if (newchar == 'l') newchar = 'z';
146 } else {
147 newchar = String.fromCharCode('2'.charCodeAt(0) + r);
149 newpass += newchar;
151 var e = document.forms[0].form_cmsportal_login_pass;
152 if (e) e.value = newpass;
155 // If needed, this creates the new patient in the CMS. It executes as an AJAX script
156 // in case it doesn't work and a correction is needed before submitting the form.
158 function validate() {
159 var f = document.forms[0];
160 var errmsg = '';
161 myRestoreSession();
162 if (f.form_cmsportal_login_pass) {
163 var login = encodeURIComponent(f.form_cmsportal_login.value);
164 var pass = encodeURIComponent(f.form_cmsportal_login_pass.value);
165 var email = encodeURIComponent(f.form_email.value);
166 if (login) {
167 if (!pass) {
168 alert('<?php echo xls('Portal password is missing'); ?>');
169 return false;
171 if (!email) {
172 alert('<?php echo xls('Email address is missing'); ?>');
173 return false;
175 // Need a *synchronous* ajax request here. Successful updating of the portal
176 // is required before we can submit the form.
177 $.ajax({
178 type: "GET",
179 dataType: "text",
180 url: 'patient_form_ajax.php?login=' + login + '&pass=' + pass + '&email=' + email,
181 async: false,
182 success: function(data) {
183 if (data) {
184 alert(data);
185 errmsg = data;
191 if (errmsg) return false;
192 return true;
195 </script>
196 </head>
198 <body class="body_top">
199 <center>
201 <form method='post' action='patient_form.php' onsubmit='return validate()'>
203 <input type='hidden' name='db_id' value="<?php echo attr($db_id); ?>" />
204 <input type='hidden' name='ptid' value="<?php echo attr($ptid); ?>" />
205 <input type='hidden' name='postid' value="<?php echo attr($postid); ?>" />
207 <table width='100%' cellpadding='1' cellspacing='2'>
208 <tr class='head'>
209 <th align='left'><?php echo xlt('Field' ); ?></th>
210 <th align='left'><?php echo xlt('Current Value'); ?></th>
211 <th align='left'><?php echo xlt('New Value' ); ?></th>
212 </tr>
214 <?php
215 $lores = sqlStatement("SELECT * FROM layout_options " .
216 "WHERE form_id = ? AND uor > 0 ORDER BY group_name, seq",
217 array('DEM'));
219 // Will be used to indicate if this user does not yet have a portal login.
220 $portal_registration_needed = false;
222 while ($lorow = sqlFetchArray($lores)) {
223 $data_type = $lorow['data_type'];
224 $field_id = $lorow['field_id'];
225 // We deal with this one at the end.
226 if ($field_id == 'cmsportal_login') continue;
227 // Flamingo translates field names to lower case so we have to match with those.
228 $reskey = $field_id;
229 foreach ($result['fields'] as $key => $dummy) {
230 if (strcasecmp($key, $field_id) == 0) $reskey = $key;
232 // Generate form fields for items that are either from the WordPress form
233 // or are mandatory for a new patient.
234 if (isset($result['fields'][$reskey]) || ($lorow['uor'] > 1 && $ptid == 0)) {
235 $list_id = $lorow['list_id'];
236 $field_title = $lorow['title'];
237 if ($field_title === '') $field_title = '(' . $field_id . ')';
239 $currvalue = '';
240 if (isset($ptrow[$field_id])) $currvalue = $ptrow[$field_id];
242 /*****************************************************************
243 $newvalue = '';
244 if (isset($result['fields'][$reskey])) $newvalue = $result['fields'][$reskey];
245 //// Zero-length input means nothing will change.
246 // if ($newvalue === '') $newvalue = $currvalue;
247 // $newvalue = trim($newvalue);
248 $newvalue = cms_field_to_lbf($newvalue, $data_type, $field_id);
249 *****************************************************************/
250 $newvalue = cms_field_to_lbf($data_type, $reskey, $result['fields']);
252 echo " <tr class='detail'>\n";
253 echo " <td class='bold'>" . text($field_title) . "</td>\n";
254 echo " <td>" . generate_display_field($lorow, $currvalue) . "</td>\n";
255 echo " <td>";
256 generate_form_field($lorow, $newvalue);
257 echo "</td>\n";
258 echo " </tr>\n";
262 $field_id = 'cmsportal_login';
263 if (empty($ptrow[$field_id])) {
264 if ($result['post']['user'] !== '') {
265 // Registered in portal but still need to record that in openemr.
266 echo "</table>\n";
267 echo "<input type='hidden' name='form_$field_id' value='" . attr($result['post']['user']) . "' />\n";
269 else {
270 // Portal registration is needed.
271 $newvalue = isset($result['fields']['email']) ? trim($result['fields']['email']) : '';
272 echo " <tr class='detail'>\n";
273 echo " <td class='bold' style='color:red;'>" . xlt('New Portal Login') . "</td>\n";
274 echo " <td>&nbsp;</td>\n";
275 echo " <td>";
276 echo "<input type='text' name='form_$field_id' size='10' maxlength='60' value='" . attr($newvalue) . "' />";
277 echo "&nbsp;&nbsp;" . xlt('Password') . ": ";
278 echo "<input type='text' name='form_" . attr($field_id) . "_pass' size='10' maxlength='60' />";
279 echo "<input type='button' value='" . xla('Generate') . "' onclick='randompass()' />";
280 echo "</td>\n";
281 echo " </tr>\n";
282 echo "</table>\n";
285 else {
286 // Portal login name is already in openemr.
287 echo "</table>\n";
292 <input type='submit' name='bn_save' value='<?php echo xla('Save and Delete Request'); ?>' />
293 &nbsp;
294 <input type='button' value='<?php echo xla('Back'); ?>' onclick="window.history.back()" />
295 <!-- Was: onclick="myRestoreSession();location='list_requests.php'" -->
296 </p>
298 </form>
300 <script language="JavaScript">
302 // hard code validation for old validation, in the new validation possible to add match rules
303 <?php if($GLOBALS['new_validate'] == 0) { ?>
305 // Fix inconsistently formatted phone numbers from the database.
306 var f = document.forms[0];
307 if (f.form_phone_contact) phonekeyup(f.form_phone_contact,mypcc);
308 if (f.form_phone_home ) phonekeyup(f.form_phone_home ,mypcc);
309 if (f.form_phone_biz ) phonekeyup(f.form_phone_biz ,mypcc);
310 if (f.form_phone_cell ) phonekeyup(f.form_phone_cell ,mypcc);
312 <?php }?>
314 randompass();
316 // This is a by-product of generate_form_field().
317 <?php echo $date_init; ?>
319 </script>
321 <!-- include support for the list-add selectbox feature -->
322 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
324 </center>
325 </body>
326 </html>