MU2 for items e2 and f1.
[openemr.git] / interface / cmsportal / patient_select.php
blob834c57e8b78052ffcfe8faa4ab5f7c8b2552c0d5
1 <?php
2 /**
3 * Patient matching and selection for the WordPress Patient Portal.
5 * LICENSE: This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
16 * @package OpenEMR
17 * @author Rod Roark <rod@sunsetsystems.com>
20 $sanitize_all_escapes = true;
21 $fake_register_globals = false;
23 require_once("../globals.php");
24 require_once("$srcdir/patient.inc");
25 require_once("$srcdir/formdata.inc.php");
26 require_once("$srcdir/options.inc.php");
27 require_once("portal.inc.php");
29 $postid = intval($_REQUEST['postid']);
31 if ($postid) {
32 $result = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
33 if ($result['errmsg']) {
34 die(text($result['errmsg']));
38 <html>
39 <head>
40 <?php html_header_show(); ?>
41 <link rel=stylesheet href="<?php echo $css_header; ?>" type="text/css">
42 <style>
44 #searchResults {
45 width: 100%;
46 height: 80%;
47 overflow: auto;
49 #searchResults table {
50 width: 96%;
51 border-collapse: collapse;
52 background-color: white;
54 #searchResults th {
55 background-color: lightgrey;
56 font-size: 0.7em;
57 text-align: left;
59 #searchResults td {
60 font-size: 0.7em;
61 border-bottom: 1px solid #eee;
62 cursor: hand;
63 cursor: pointer;
66 .highlight {
67 background-color: #336699;
68 color: white;
71 .oneResult {}
73 </style>
75 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script>
76 <script language="JavaScript">
78 $(document).ready(function(){
79 $(".oneresult").mouseover(function() {$(this).addClass("highlight");});
80 $(".oneresult").mouseout(function() {$(this).removeClass("highlight");});
81 });
83 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
85 function myRestoreSession() {
86 if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();
87 return true;
90 function openPatient(ptid) {
91 // This works whether we are a popup or in the OpenEMR frameset.
92 myRestoreSession();
93 document.location.href = 'patient_form.php?postid=<?php echo xls($postid); ?>&ptid=' + ptid;
96 </script>
97 </head>
99 <body class="body_top">
100 <center>
101 <form method='post' action='patient_select.php' onsubmit='return myRestoreSession()'>
103 <?php
104 // print_r($result); // debugging
105 $login_matches = 0;
106 $login_pid = '';
107 if ($postid) {
108 $clarr = array();
109 $clsql = "0";
110 // Portal Login
111 $cmsportal_login = trim($result['post']['user']);
112 if ($cmsportal_login !== '') {
113 $clsql .= " + ((cmsportal_login IS NOT NULL AND cmsportal_login = ?) * 100)";
114 $clarr[] = $cmsportal_login;
116 // First name.
117 $fname = trim($result['fields']['fname']);
118 if ($fname !== '') {
119 $clsql .= " + ((fname IS NOT NULL AND fname = ?) * 5)";
120 $clarr[] = $fname;
122 // Last name.
123 $lname = trim($result['fields']['lname']);
124 if ($lname !== '') {
125 $clsql .= " + ((lname IS NOT NULL AND lname = ?) * 5)";
126 $clarr[] = $lname;
128 // Birth date.
129 $dob = fixDate(trim($result['fields']['dob']), '');
130 if ($dob !== '') {
131 $clsql .= " + ((DOB IS NOT NULL AND DOB = ?) * 5)";
132 $clarr[] = $dob;
134 // SSN match is worth a lot and we allow for matching on last 4 digits.
135 $ssn = preg_replace('/[^0-9]/', '', $result['fields']['ss']);
136 if (strlen($ssn) > 3) {
137 $clsql .= " + ((ss IS NOT NULL AND ss LIKE ?) * 10)";
138 $clarr[] = "%$ssn";
140 // Zip code makes it unnecessary to match on city and state.
141 $zip = preg_replace('/[^0-9]/', '', $result['fields']['postal_code']);
142 $zip = substr($zip, 0, 5);
143 if (strlen($zip) == 5) {
144 $clsql .= " + ((postal_code IS NOT NULL AND postal_code LIKE ?) * 2)";
145 $clarr[] = "$zip%";
147 // This generates a REGEXP query that matches the first 2 words of the street address.
148 if (preg_match('/^\W*(\w+)\W+(\w+)/', $result['fields']['street'], $matches)) {
149 $clsql .= " + ((street IS NOT NULL AND street REGEXP '^[^[:alnum:]]*";
150 $clsql .= $matches[1];
151 $clsql .= "[^[:alnum:]]+";
152 $clsql .= $matches[2];
153 $clsql .= "[[:>:]]') * 2)";
156 $sql = "SELECT $clsql AS closeness, " .
157 "pid, cmsportal_login, fname, lname, mname, DOB, ss, postal_code, " .
158 "street, phone_biz, phone_home, phone_cell, phone_contact " .
159 "FROM patient_data " .
160 "ORDER BY closeness DESC, lname, fname LIMIT 10";
161 $res = sqlStatement($sql, $clarr);
163 // echo "<!-- $sql -->\n"; // debugging
165 $phone = $result['fields']['phone_biz'];
166 if (empty($phone)) $phone = $result['fields']['phone_home'];
167 if (empty($phone)) $phone = $result['fields']['phone_cell'];
168 if (empty($phone)) $phone = $result['fields']['phone_contact'];
171 <div id="searchResults">
172 <table>
173 <tr>
174 <th><?php echo xlt('Portal ID'); ?></th>
175 <th><?php echo xlt('Name' ); ?></th>
176 <th><?php echo xlt('Phone' ); ?></th>
177 <th><?php echo xlt('SS' ); ?></th>
178 <th><?php echo xlt('DOB' ); ?></th>
179 <th><?php echo xlt('Address' ); ?></th>
180 </tr>
181 <tr>
182 <th style='font-weight:normal'><?php echo text($result['post']['user']); ?></th>
183 <th style='font-weight:normal'><?php echo text("$lname, $fname"); ?></th>
184 <th style='font-weight:normal'><?php echo text($phone); ?></th>
185 <th style='font-weight:normal'><?php echo text($ssn ); ?></th>
186 <th style='font-weight:normal'><?php echo text($dob ); ?></th>
187 <th style='font-weight:normal'><?php echo text($result['fields']['street'] . ' ' . $zip); ?></th>
188 </tr>
190 <?php
191 while ($row = sqlFetchArray($res)) {
192 if ($row['closeness'] == 0) continue;
193 if ($row['closeness'] >= 100) {
194 ++$login_matches;
195 $login_pid = $row['pid'];
197 else {
198 // We have a match on login name but this is not one, so ignore it.
199 if ($login_matches) continue;
201 $phone = $row['phone_biz'];
202 if (empty($phone)) $phone = $row['phone_home'];
203 if (empty($phone)) $phone = $row['phone_cell'];
204 if (empty($phone)) $phone = $row['phone_contact'];
206 echo " <tr class='oneresult'";
207 echo " onclick=\"openPatient(" .
208 "'" . addslashes($row['pid']) . "'" .
209 ")\">\n";
210 echo " <td";
211 if ($row['cmsportal_login'] !== '' && $result['post']['user'] !== $row['cmsportal_login']) {
212 echo " style='color:red' title='" . xla('Portal ID does not match request from portal!') . "'";
214 echo ">" . text($row['cmsportal_login']) . "</td>\n";
215 echo " <td>" . text($row['lname'] . ", " . $row['fname'] ) . "</td>\n";
216 echo " <td>" . text($phone ) . "</td>\n";
217 echo " <td>" . text($row['ss'] ) . "</td>\n";
218 echo " <td>" . text($row['DOB'] ) . "</td>\n";
219 echo " <td>" . text($row['street'] . ' ' . $row['postal_code']) . "</td>\n";
220 echo " </tr>\n";
223 </table>
224 </div>
225 <?php
227 if ($login_matches == 1) {
228 // There is exactly one match by portal login name, this must be it.
229 // There should not be more than one, but if there is then we will
230 // leave them onscreen and let the user choose.
231 echo "<script language='JavaScript'>\n";
232 echo "openPatient('" . addslashes($login_pid) . "');\n";
233 echo "</script>\n";
238 <input type='button' value='<?php echo xla('Add New Patient'); ?>' onclick="openPatient(0)" />
239 &nbsp;
240 <input type='button' value='<?php echo xla('Back'); ?>' onclick="myRestoreSession();location='list_requests.php'" />
241 </p>
243 </form>
244 </center>
245 </body>
246 </html>