bug fix march continued (#1921)
[openemr.git] / interface / cmsportal / patient_select.php
blob3b1208311e05e51acaa1928019edf588d6f6a7ca
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>
23 require_once("../globals.php");
24 require_once("$srcdir/patient.inc");
25 require_once("$srcdir/options.inc.php");
26 require_once("portal.inc.php");
28 $postid = intval($_REQUEST['postid']);
30 if ($postid) {
31 $result = cms_portal_call(array('action' => 'getpost', 'postid' => $postid));
32 if ($result['errmsg']) {
33 die(text($result['errmsg']));
37 <html>
38 <head>
39 <?php html_header_show(); ?>
40 <link rel=stylesheet href="<?php echo $css_header; ?>" type="text/css">
41 <style>
43 #searchResults {
44 width: 100%;
45 height: 80%;
46 overflow: auto;
48 #searchResults table {
49 width: 96%;
50 border-collapse: collapse;
51 background-color: white;
53 #searchResults th {
54 background-color: lightgrey;
55 font-size: 0.7em;
56 text-align: left;
58 #searchResults td {
59 font-size: 0.7em;
60 border-bottom: 1px solid #eee;
61 cursor: hand;
62 cursor: pointer;
65 .highlight {
66 background-color: #336699;
67 color: white;
70 .oneResult {}
72 </style>
74 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/manual-added-packages/jquery-min-1-2-2/index.js"></script>
75 <script language="JavaScript">
77 $(document).ready(function(){
78 $(".oneresult").mouseover(function() {$(this).addClass("highlight");});
79 $(".oneresult").mouseout(function() {$(this).removeClass("highlight");});
80 });
82 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
84 function myRestoreSession() {
85 if (top.restoreSession) top.restoreSession(); else opener.top.restoreSession();
86 return true;
89 function openPatient(ptid) {
90 // This works whether we are a popup or in the OpenEMR frameset.
91 myRestoreSession();
92 document.location.href = 'patient_form.php?postid=<?php echo xls($postid); ?>&ptid=' + ptid;
95 </script>
96 </head>
98 <body class="body_top">
99 <center>
100 <form method='post' action='patient_select.php' onsubmit='return myRestoreSession()'>
102 <?php
103 // print_r($result); // debugging
104 $login_matches = 0;
105 $login_pid = '';
106 if ($postid) {
107 $clarr = array();
108 $clsql = "0";
109 // Portal Login
110 $cmsportal_login = trim($result['post']['user']);
111 if ($cmsportal_login !== '') {
112 $clsql .= " + ((cmsportal_login IS NOT NULL AND cmsportal_login = ?) * 100)";
113 $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;
123 // Last name.
124 $lname = trim($result['fields']['lname']);
125 if ($lname !== '') {
126 $clsql .= " + ((lname IS NOT NULL AND lname = ?) * 5)";
127 $clarr[] = $lname;
130 // Birth date.
131 $dob = fixDate(trim($result['fields']['dob']), '');
132 if ($dob !== '') {
133 $clsql .= " + ((DOB IS NOT NULL AND DOB = ?) * 5)";
134 $clarr[] = $dob;
137 // SSN match is worth a lot and we allow for matching on last 4 digits.
138 $ssn = preg_replace('/[^0-9]/', '', $result['fields']['ss']);
139 if (strlen($ssn) > 3) {
140 $clsql .= " + ((ss IS NOT NULL AND ss LIKE ?) * 10)";
141 $clarr[] = "%$ssn";
144 // Zip code makes it unnecessary to match on city and state.
145 $zip = preg_replace('/[^0-9]/', '', $result['fields']['postal_code']);
146 $zip = substr($zip, 0, 5);
147 if (strlen($zip) == 5) {
148 $clsql .= " + ((postal_code IS NOT NULL AND postal_code LIKE ?) * 2)";
149 $clarr[] = "$zip%";
152 // This generates a REGEXP query that matches the first 2 words of the street address.
153 if (preg_match('/^\W*(\w+)\W+(\w+)/', $result['fields']['street'], $matches)) {
154 $clsql .= " + ((street IS NOT NULL AND street REGEXP '^[^[:alnum:]]*";
155 $clsql .= $matches[1];
156 $clsql .= "[^[:alnum:]]+";
157 $clsql .= $matches[2];
158 $clsql .= "[[:>:]]') * 2)";
161 $sql = "SELECT $clsql AS closeness, " .
162 "pid, cmsportal_login, fname, lname, mname, DOB, ss, postal_code, " .
163 "street, phone_biz, phone_home, phone_cell, phone_contact " .
164 "FROM patient_data " .
165 "ORDER BY closeness DESC, lname, fname LIMIT 10";
166 $res = sqlStatement($sql, $clarr);
168 // echo "<!-- $sql -->\n"; // debugging
170 $phone = $result['fields']['phone_biz'];
171 if (empty($phone)) {
172 $phone = $result['fields']['phone_home'];
175 if (empty($phone)) {
176 $phone = $result['fields']['phone_cell'];
179 if (empty($phone)) {
180 $phone = $result['fields']['phone_contact'];
184 <div id="searchResults">
185 <table>
186 <tr>
187 <th><?php echo xlt('Portal ID'); ?></th>
188 <th><?php echo xlt('Name'); ?></th>
189 <th><?php echo xlt('Phone'); ?></th>
190 <th><?php echo xlt('SS'); ?></th>
191 <th><?php echo xlt('DOB'); ?></th>
192 <th><?php echo xlt('Address'); ?></th>
193 </tr>
194 <tr>
195 <th style='font-weight:normal'><?php echo text($result['post']['user']); ?></th>
196 <th style='font-weight:normal'><?php echo text("$lname, $fname"); ?></th>
197 <th style='font-weight:normal'><?php echo text($phone); ?></th>
198 <th style='font-weight:normal'><?php echo text($ssn); ?></th>
199 <th style='font-weight:normal'><?php echo text($dob); ?></th>
200 <th style='font-weight:normal'><?php echo text($result['fields']['street'] . ' ' . $zip); ?></th>
201 </tr>
203 <?php
204 while ($row = sqlFetchArray($res)) {
205 if ($row['closeness'] == 0) {
206 continue;
209 if ($row['closeness'] >= 100) {
210 ++$login_matches;
211 $login_pid = $row['pid'];
212 } else {
213 // We have a match on login name but this is not one, so ignore it.
214 if ($login_matches) {
215 continue;
219 $phone = $row['phone_biz'];
220 if (empty($phone)) {
221 $phone = $row['phone_home'];
224 if (empty($phone)) {
225 $phone = $row['phone_cell'];
228 if (empty($phone)) {
229 $phone = $row['phone_contact'];
232 echo " <tr class='oneresult'";
233 echo " onclick=\"openPatient(" .
234 "'" . addslashes($row['pid']) . "'" .
235 ")\">\n";
236 echo " <td";
237 if ($row['cmsportal_login'] !== '' && $result['post']['user'] !== $row['cmsportal_login']) {
238 echo " style='color:red' title='" . xla('Portal ID does not match request from portal!') . "'";
241 echo ">" . text($row['cmsportal_login']) . "</td>\n";
242 echo " <td>" . text($row['lname'] . ", " . $row['fname']) . "</td>\n";
243 echo " <td>" . text($phone) . "</td>\n";
244 echo " <td>" . text($row['ss']) . "</td>\n";
245 echo " <td>" . text($row['DOB']) . "</td>\n";
246 echo " <td>" . text($row['street'] . ' ' . $row['postal_code']) . "</td>\n";
247 echo " </tr>\n";
250 </table>
251 </div>
252 <?php
255 if ($login_matches == 1) {
256 // There is exactly one match by portal login name, this must be it.
257 // There should not be more than one, but if there is then we will
258 // leave them onscreen and let the user choose.
259 echo "<script language='JavaScript'>\n";
260 echo "openPatient('" . addslashes($login_pid) . "');\n";
261 echo "</script>\n";
266 <input type='button' value='<?php echo xla('Add New Patient'); ?>' onclick="openPatient(0)" />
267 &nbsp;
268 <input type='button' value='<?php echo xla('Back'); ?>' onclick="myRestoreSession();location='list_requests.php'" />
269 </p>
271 </form>
272 </center>
273 </body>
274 </html>