migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / interface / cmsportal / patient_form.php
blobee6b8f614ccbfc037b49f2a5c0cf13957e6427a6
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/options.inc.php");
28 require_once("portal.inc.php");
30 $postid = intval($_REQUEST['postid']);
31 $ptid = intval($_REQUEST['ptid' ]);
33 if ($_POST['bn_save']) {
34 $newdata = array();
35 $newdata['patient_data' ] = array();
36 $newdata['employer_data'] = array();
37 $ptid = intval($_POST['ptid']);
38 // Note we are careful to maintain cmsportal_login even if the layout has it
39 // configured as unused.
40 $fres = sqlStatement("SELECT * FROM layout_options WHERE " .
41 "form_id = 'DEM' AND field_id != '' AND (uor > 0 OR field_id = 'cmsportal_login') " .
42 "ORDER BY group_name, seq");
43 while ($frow = sqlFetchArray($fres)) {
44 $data_type = $frow['data_type'];
45 $field_id = $frow['field_id'];
46 $table = 'patient_data';
47 if (isset($_POST["form_$field_id"])) {
48 $newdata[$table][$field_id] = get_layout_form_value($frow);
51 if (empty($ptid)) {
52 $tmp = sqlQuery("SELECT MAX(pid)+1 AS pid FROM patient_data");
53 $ptid = empty($tmp['pid']) ? 1 : intval($tmp['pid']);
54 if (empty($newdata['patient_data']['pubpid'])) {
55 // pubpid for new patient defaults to pid.
56 $newdata['patient_data']['pubpid'] = "$ptid";
58 updatePatientData ($ptid, $newdata['patient_data' ], true);
59 updateEmployerData($ptid, $newdata['employer_data'], true);
60 newHistoryData($ptid);
62 else {
63 $newdata['patient_data']['id'] = $_POST['db_id'];
64 updatePatientData($ptid, $newdata['patient_data']);
66 // Finally, delete the request from the portal.
67 $result = cms_portal_call(array('action' => 'delpost', 'postid' => $postid));
68 if ($result['errmsg']) {
69 die(text($result['errmsg']));
71 echo "<html><body><script language='JavaScript'>\n";
72 echo "if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();\n";
73 echo "document.location.href = 'list_requests.php';\n";
74 echo "</script></body></html>\n";
75 exit();
78 $db_id = 0;
79 if ($ptid) {
80 $ptrow = getPatientData($ptid, "*");
81 $db_id = $ptrow['id'];
84 if ($postid) {
85 $result = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
86 if ($result['errmsg']) {
87 die(text($result['errmsg']));
91 <html>
92 <head>
93 <?php html_header_show(); ?>
94 <link rel=stylesheet href="<?php echo $css_header; ?>" type="text/css">
96 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
97 <style>
99 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
100 tr.detail { font-size:10pt; background-color:#ddddff; }
101 td input { background-color:transparent; }
103 </style>
105 <script type="text/javascript" src="../../library/textformat.js"></script>
106 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
107 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
108 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
109 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-2-2/index.js"></script>
111 <script language="JavaScript">
113 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
115 function myRestoreSession() {
116 if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();
117 return true;
120 // This capitalizes the first letter of each word in the passed input
121 // element. It also strips out extraneous spaces.
122 // Copied from demographics_full.php.
123 function capitalizeMe(elem) {
124 var a = elem.value.split(' ');
125 var s = '';
126 for(var i = 0; i < a.length; ++i) {
127 if (a[i].length > 0) {
128 if (s.length > 0) s += ' ';
129 s += a[i].charAt(0).toUpperCase() + a[i].substring(1);
132 elem.value = s;
135 // Generates and returns a random 6-character password.
137 function randompass() {
138 var newpass = '';
139 var newchar = '';
140 while (newpass.length < 6) {
141 var r = Math.floor(Math.random() * 33); // for 2-9 and a-y
142 if (r > 7) {
143 newchar = String.fromCharCode('a'.charCodeAt(0) + r - 8);
144 if (newchar == 'l') newchar = 'z';
145 } else {
146 newchar = String.fromCharCode('2'.charCodeAt(0) + r);
148 newpass += newchar;
150 var e = document.forms[0].form_cmsportal_login_pass;
151 if (e) e.value = newpass;
154 // If needed, this creates the new patient in the CMS. It executes as an AJAX script
155 // in case it doesn't work and a correction is needed before submitting the form.
157 function validate() {
158 var f = document.forms[0];
159 var errmsg = '';
160 myRestoreSession();
161 if (f.form_cmsportal_login_pass) {
162 var login = encodeURIComponent(f.form_cmsportal_login.value);
163 var pass = encodeURIComponent(f.form_cmsportal_login_pass.value);
164 var email = encodeURIComponent(f.form_email.value);
165 if (login) {
166 if (!pass) {
167 alert('<?php echo xls('Portal password is missing'); ?>');
168 return false;
170 if (!email) {
171 alert('<?php echo xls('Email address is missing'); ?>');
172 return false;
174 // Need a *synchronous* ajax request here. Successful updating of the portal
175 // is required before we can submit the form.
176 $.ajax({
177 type: "GET",
178 dataType: "text",
179 url: 'patient_form_ajax.php?login=' + login + '&pass=' + pass + '&email=' + email,
180 async: false,
181 success: function(data) {
182 if (data) {
183 alert(data);
184 errmsg = data;
190 if (errmsg) return false;
191 return true;
194 </script>
195 </head>
197 <body class="body_top">
198 <center>
200 <form method='post' action='patient_form.php' onsubmit='return validate()'>
202 <input type='hidden' name='db_id' value="<?php echo attr($db_id); ?>" />
203 <input type='hidden' name='ptid' value="<?php echo attr($ptid); ?>" />
204 <input type='hidden' name='postid' value="<?php echo attr($postid); ?>" />
206 <table width='100%' cellpadding='1' cellspacing='2'>
207 <tr class='head'>
208 <th align='left'><?php echo xlt('Field' ); ?></th>
209 <th align='left'><?php echo xlt('Current Value'); ?></th>
210 <th align='left'><?php echo xlt('New Value' ); ?></th>
211 </tr>
213 <?php
214 $lores = sqlStatement("SELECT * FROM layout_options " .
215 "WHERE form_id = ? AND uor > 0 ORDER BY group_name, seq",
216 array('DEM'));
218 // Will be used to indicate if this user does not yet have a portal login.
219 $portal_registration_needed = false;
221 while ($lorow = sqlFetchArray($lores)) {
222 $data_type = $lorow['data_type'];
223 $field_id = $lorow['field_id'];
224 // We deal with this one at the end.
225 if ($field_id == 'cmsportal_login') continue;
226 // Flamingo translates field names to lower case so we have to match with those.
227 $reskey = $field_id;
228 foreach ($result['fields'] as $key => $dummy) {
229 if (strcasecmp($key, $field_id) == 0) $reskey = $key;
231 // Generate form fields for items that are either from the WordPress form
232 // or are mandatory for a new patient.
233 if (isset($result['fields'][$reskey]) || ($lorow['uor'] > 1 && $ptid == 0)) {
234 $list_id = $lorow['list_id'];
235 $field_title = $lorow['title'];
236 if ($field_title === '') $field_title = '(' . $field_id . ')';
238 $currvalue = '';
239 if (isset($ptrow[$field_id])) $currvalue = $ptrow[$field_id];
241 /*****************************************************************
242 $newvalue = '';
243 if (isset($result['fields'][$reskey])) $newvalue = $result['fields'][$reskey];
244 //// Zero-length input means nothing will change.
245 // if ($newvalue === '') $newvalue = $currvalue;
246 // $newvalue = trim($newvalue);
247 $newvalue = cms_field_to_lbf($newvalue, $data_type, $field_id);
248 *****************************************************************/
249 $newvalue = cms_field_to_lbf($data_type, $reskey, $result['fields']);
251 echo " <tr class='detail'>\n";
252 echo " <td class='bold'>" . text($field_title) . "</td>\n";
253 echo " <td>" . generate_display_field($lorow, $currvalue) . "</td>\n";
254 echo " <td>";
255 generate_form_field($lorow, $newvalue);
256 echo "</td>\n";
257 echo " </tr>\n";
261 $field_id = 'cmsportal_login';
262 if (empty($ptrow[$field_id])) {
263 if ($result['post']['user'] !== '') {
264 // Registered in portal but still need to record that in openemr.
265 echo "</table>\n";
266 echo "<input type='hidden' name='form_$field_id' value='" . attr($result['post']['user']) . "' />\n";
268 else {
269 // Portal registration is needed.
270 $newvalue = isset($result['fields']['email']) ? trim($result['fields']['email']) : '';
271 echo " <tr class='detail'>\n";
272 echo " <td class='bold' style='color:red;'>" . xlt('New Portal Login') . "</td>\n";
273 echo " <td>&nbsp;</td>\n";
274 echo " <td>";
275 echo "<input type='text' name='form_$field_id' size='10' maxlength='60' value='" . attr($newvalue) . "' />";
276 echo "&nbsp;&nbsp;" . xlt('Password') . ": ";
277 echo "<input type='text' name='form_" . attr($field_id) . "_pass' size='10' maxlength='60' />";
278 echo "<input type='button' value='" . xla('Generate') . "' onclick='randompass()' />";
279 echo "</td>\n";
280 echo " </tr>\n";
281 echo "</table>\n";
284 else {
285 // Portal login name is already in openemr.
286 echo "</table>\n";
291 <input type='submit' name='bn_save' value='<?php echo xla('Save and Delete Request'); ?>' />
292 &nbsp;
293 <input type='button' value='<?php echo xla('Back'); ?>' onclick="window.history.back()" />
294 <!-- Was: onclick="myRestoreSession();location='list_requests.php'" -->
295 </p>
297 </form>
299 <script language="JavaScript">
301 // hard code validation for old validation, in the new validation possible to add match rules
302 <?php if($GLOBALS['new_validate'] == 0) { ?>
304 // Fix inconsistently formatted phone numbers from the database.
305 var f = document.forms[0];
306 if (f.form_phone_contact) phonekeyup(f.form_phone_contact,mypcc);
307 if (f.form_phone_home ) phonekeyup(f.form_phone_home ,mypcc);
308 if (f.form_phone_biz ) phonekeyup(f.form_phone_biz ,mypcc);
309 if (f.form_phone_cell ) phonekeyup(f.form_phone_cell ,mypcc);
311 <?php }?>
313 randompass();
315 // This is a by-product of generate_form_field().
316 <?php echo $date_init; ?>
318 </script>
320 <!-- include support for the list-add selectbox feature -->
321 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
323 </center>
324 </body>
325 </html>