Highway to PSR2
[openemr.git] / interface / cmsportal / patient_form.php
blobe9db96c49b8e0609e01f55b9e8add1ee7a681695
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>
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);
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";
60 updatePatientData($ptid, $newdata['patient_data' ], true);
61 updateEmployerData($ptid, $newdata['employer_data'], true);
62 newHistoryData($ptid);
63 } else {
64 $newdata['patient_data']['id'] = $_POST['db_id'];
65 updatePatientData($ptid, $newdata['patient_data']);
68 // Finally, delete the request from the portal.
69 $result = cms_portal_call(array('action' => 'delpost', 'postid' => $postid));
70 if ($result['errmsg']) {
71 die(text($result['errmsg']));
74 echo "<html><body><script language='JavaScript'>\n";
75 echo "if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();\n";
76 echo "document.location.href = 'list_requests.php';\n";
77 echo "</script></body></html>\n";
78 exit();
81 $db_id = 0;
82 if ($ptid) {
83 $ptrow = getPatientData($ptid, "*");
84 $db_id = $ptrow['id'];
87 if ($postid) {
88 $result = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
89 if ($result['errmsg']) {
90 die(text($result['errmsg']));
94 <html>
95 <head>
96 <?php html_header_show(); ?>
97 <link rel=stylesheet href="<?php echo $css_header; ?>" type="text/css">
99 <style type="text/css">@import url(../../library/dynarch_calendar.css);</style>
100 <style>
102 tr.head { font-size:10pt; background-color:#cccccc; text-align:center; }
103 tr.detail { font-size:10pt; background-color:#ddddff; }
104 td input { background-color:transparent; }
106 </style>
108 <script type="text/javascript" src="../../library/textformat.js"></script>
109 <script type="text/javascript" src="../../library/dynarch_calendar.js"></script>
110 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
111 <script type="text/javascript" src="../../library/dynarch_calendar_setup.js"></script>
112 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-2-2/index.js"></script>
114 <script language="JavaScript">
116 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
118 function myRestoreSession() {
119 if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();
120 return true;
123 // This capitalizes the first letter of each word in the passed input
124 // element. It also strips out extraneous spaces.
125 // Copied from demographics_full.php.
126 function capitalizeMe(elem) {
127 var a = elem.value.split(' ');
128 var s = '';
129 for(var i = 0; i < a.length; ++i) {
130 if (a[i].length > 0) {
131 if (s.length > 0) s += ' ';
132 s += a[i].charAt(0).toUpperCase() + a[i].substring(1);
135 elem.value = s;
138 // Generates and returns a random 6-character password.
140 function randompass() {
141 var newpass = '';
142 var newchar = '';
143 while (newpass.length < 6) {
144 var r = Math.floor(Math.random() * 33); // for 2-9 and a-y
145 if (r > 7) {
146 newchar = String.fromCharCode('a'.charCodeAt(0) + r - 8);
147 if (newchar == 'l') newchar = 'z';
148 } else {
149 newchar = String.fromCharCode('2'.charCodeAt(0) + r);
151 newpass += newchar;
153 var e = document.forms[0].form_cmsportal_login_pass;
154 if (e) e.value = newpass;
157 // If needed, this creates the new patient in the CMS. It executes as an AJAX script
158 // in case it doesn't work and a correction is needed before submitting the form.
160 function validate() {
161 var f = document.forms[0];
162 var errmsg = '';
163 myRestoreSession();
164 if (f.form_cmsportal_login_pass) {
165 var login = encodeURIComponent(f.form_cmsportal_login.value);
166 var pass = encodeURIComponent(f.form_cmsportal_login_pass.value);
167 var email = encodeURIComponent(f.form_email.value);
168 if (login) {
169 if (!pass) {
170 alert('<?php echo xls('Portal password is missing'); ?>');
171 return false;
173 if (!email) {
174 alert('<?php echo xls('Email address is missing'); ?>');
175 return false;
177 // Need a *synchronous* ajax request here. Successful updating of the portal
178 // is required before we can submit the form.
179 $.ajax({
180 type: "GET",
181 dataType: "text",
182 url: 'patient_form_ajax.php?login=' + login + '&pass=' + pass + '&email=' + email,
183 async: false,
184 success: function(data) {
185 if (data) {
186 alert(data);
187 errmsg = data;
193 if (errmsg) return false;
194 return true;
197 </script>
198 </head>
200 <body class="body_top">
201 <center>
203 <form method='post' action='patient_form.php' onsubmit='return validate()'>
205 <input type='hidden' name='db_id' value="<?php echo attr($db_id); ?>" />
206 <input type='hidden' name='ptid' value="<?php echo attr($ptid); ?>" />
207 <input type='hidden' name='postid' value="<?php echo attr($postid); ?>" />
209 <table width='100%' cellpadding='1' cellspacing='2'>
210 <tr class='head'>
211 <th align='left'><?php echo xlt('Field'); ?></th>
212 <th align='left'><?php echo xlt('Current Value'); ?></th>
213 <th align='left'><?php echo xlt('New Value'); ?></th>
214 </tr>
216 <?php
217 $lores = sqlStatement(
218 "SELECT * FROM layout_options " .
219 "WHERE form_id = ? AND uor > 0 ORDER BY group_name, seq",
220 array('DEM')
223 // Will be used to indicate if this user does not yet have a portal login.
224 $portal_registration_needed = false;
226 while ($lorow = sqlFetchArray($lores)) {
227 $data_type = $lorow['data_type'];
228 $field_id = $lorow['field_id'];
229 // We deal with this one at the end.
230 if ($field_id == 'cmsportal_login') {
231 continue;
234 // Flamingo translates field names to lower case so we have to match with those.
235 $reskey = $field_id;
236 foreach ($result['fields'] as $key => $dummy) {
237 if (strcasecmp($key, $field_id) == 0) {
238 $reskey = $key;
242 // Generate form fields for items that are either from the WordPress form
243 // or are mandatory for a new patient.
244 if (isset($result['fields'][$reskey]) || ($lorow['uor'] > 1 && $ptid == 0)) {
245 $list_id = $lorow['list_id'];
246 $field_title = $lorow['title'];
247 if ($field_title === '') {
248 $field_title = '(' . $field_id . ')';
251 $currvalue = '';
252 if (isset($ptrow[$field_id])) {
253 $currvalue = $ptrow[$field_id];
256 /*****************************************************************
257 $newvalue = '';
258 if (isset($result['fields'][$reskey])) $newvalue = $result['fields'][$reskey];
259 //// Zero-length input means nothing will change.
260 // if ($newvalue === '') $newvalue = $currvalue;
261 // $newvalue = trim($newvalue);
262 $newvalue = cms_field_to_lbf($newvalue, $data_type, $field_id);
263 *****************************************************************/
264 $newvalue = cms_field_to_lbf($data_type, $reskey, $result['fields']);
266 echo " <tr class='detail'>\n";
267 echo " <td class='bold'>" . text($field_title) . "</td>\n";
268 echo " <td>" . generate_display_field($lorow, $currvalue) . "</td>\n";
269 echo " <td>";
270 generate_form_field($lorow, $newvalue);
271 echo "</td>\n";
272 echo " </tr>\n";
276 $field_id = 'cmsportal_login';
277 if (empty($ptrow[$field_id])) {
278 if ($result['post']['user'] !== '') {
279 // Registered in portal but still need to record that in openemr.
280 echo "</table>\n";
281 echo "<input type='hidden' name='form_$field_id' value='" . attr($result['post']['user']) . "' />\n";
282 } else {
283 // Portal registration is needed.
284 $newvalue = isset($result['fields']['email']) ? trim($result['fields']['email']) : '';
285 echo " <tr class='detail'>\n";
286 echo " <td class='bold' style='color:red;'>" . xlt('New Portal Login') . "</td>\n";
287 echo " <td>&nbsp;</td>\n";
288 echo " <td>";
289 echo "<input type='text' name='form_$field_id' size='10' maxlength='60' value='" . attr($newvalue) . "' />";
290 echo "&nbsp;&nbsp;" . xlt('Password') . ": ";
291 echo "<input type='text' name='form_" . attr($field_id) . "_pass' size='10' maxlength='60' />";
292 echo "<input type='button' value='" . xla('Generate') . "' onclick='randompass()' />";
293 echo "</td>\n";
294 echo " </tr>\n";
295 echo "</table>\n";
297 } else {
298 // Portal login name is already in openemr.
299 echo "</table>\n";
304 <input type='submit' name='bn_save' value='<?php echo xla('Save and Delete Request'); ?>' />
305 &nbsp;
306 <input type='button' value='<?php echo xla('Back'); ?>' onclick="window.history.back()" />
307 <!-- Was: onclick="myRestoreSession();location='list_requests.php'" -->
308 </p>
310 </form>
312 <script language="JavaScript">
314 // hard code validation for old validation, in the new validation possible to add match rules
315 <?php if ($GLOBALS['new_validate'] == 0) { ?>
317 // Fix inconsistently formatted phone numbers from the database.
318 var f = document.forms[0];
319 if (f.form_phone_contact) phonekeyup(f.form_phone_contact,mypcc);
320 if (f.form_phone_home ) phonekeyup(f.form_phone_home ,mypcc);
321 if (f.form_phone_biz ) phonekeyup(f.form_phone_biz ,mypcc);
322 if (f.form_phone_cell ) phonekeyup(f.form_phone_cell ,mypcc);
324 <?php }?>
326 randompass();
328 // This is a by-product of generate_form_field().
329 <?php echo $date_init; ?>
331 </script>
333 <!-- include support for the list-add selectbox feature -->
334 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
336 </center>
337 </body>
338 </html>