migrated ubiquitous libraries to composer autoloader (#421)
[openemr.git] / interface / cmsportal / patient_select.php
blobef801de1b37230a66b19713e32678c35a957f923
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/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']; ?>/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;
115 // First name.
116 $fname = trim($result['fields']['fname']);
117 if ($fname !== '') {
118 $clsql .= " + ((fname IS NOT NULL AND fname = ?) * 5)";
119 $clarr[] = $fname;
121 // Last name.
122 $lname = trim($result['fields']['lname']);
123 if ($lname !== '') {
124 $clsql .= " + ((lname IS NOT NULL AND lname = ?) * 5)";
125 $clarr[] = $lname;
127 // Birth date.
128 $dob = fixDate(trim($result['fields']['dob']), '');
129 if ($dob !== '') {
130 $clsql .= " + ((DOB IS NOT NULL AND DOB = ?) * 5)";
131 $clarr[] = $dob;
133 // SSN match is worth a lot and we allow for matching on last 4 digits.
134 $ssn = preg_replace('/[^0-9]/', '', $result['fields']['ss']);
135 if (strlen($ssn) > 3) {
136 $clsql .= " + ((ss IS NOT NULL AND ss LIKE ?) * 10)";
137 $clarr[] = "%$ssn";
139 // Zip code makes it unnecessary to match on city and state.
140 $zip = preg_replace('/[^0-9]/', '', $result['fields']['postal_code']);
141 $zip = substr($zip, 0, 5);
142 if (strlen($zip) == 5) {
143 $clsql .= " + ((postal_code IS NOT NULL AND postal_code LIKE ?) * 2)";
144 $clarr[] = "$zip%";
146 // This generates a REGEXP query that matches the first 2 words of the street address.
147 if (preg_match('/^\W*(\w+)\W+(\w+)/', $result['fields']['street'], $matches)) {
148 $clsql .= " + ((street IS NOT NULL AND street REGEXP '^[^[:alnum:]]*";
149 $clsql .= $matches[1];
150 $clsql .= "[^[:alnum:]]+";
151 $clsql .= $matches[2];
152 $clsql .= "[[:>:]]') * 2)";
155 $sql = "SELECT $clsql AS closeness, " .
156 "pid, cmsportal_login, fname, lname, mname, DOB, ss, postal_code, " .
157 "street, phone_biz, phone_home, phone_cell, phone_contact " .
158 "FROM patient_data " .
159 "ORDER BY closeness DESC, lname, fname LIMIT 10";
160 $res = sqlStatement($sql, $clarr);
162 // echo "<!-- $sql -->\n"; // debugging
164 $phone = $result['fields']['phone_biz'];
165 if (empty($phone)) $phone = $result['fields']['phone_home'];
166 if (empty($phone)) $phone = $result['fields']['phone_cell'];
167 if (empty($phone)) $phone = $result['fields']['phone_contact'];
170 <div id="searchResults">
171 <table>
172 <tr>
173 <th><?php echo xlt('Portal ID'); ?></th>
174 <th><?php echo xlt('Name' ); ?></th>
175 <th><?php echo xlt('Phone' ); ?></th>
176 <th><?php echo xlt('SS' ); ?></th>
177 <th><?php echo xlt('DOB' ); ?></th>
178 <th><?php echo xlt('Address' ); ?></th>
179 </tr>
180 <tr>
181 <th style='font-weight:normal'><?php echo text($result['post']['user']); ?></th>
182 <th style='font-weight:normal'><?php echo text("$lname, $fname"); ?></th>
183 <th style='font-weight:normal'><?php echo text($phone); ?></th>
184 <th style='font-weight:normal'><?php echo text($ssn ); ?></th>
185 <th style='font-weight:normal'><?php echo text($dob ); ?></th>
186 <th style='font-weight:normal'><?php echo text($result['fields']['street'] . ' ' . $zip); ?></th>
187 </tr>
189 <?php
190 while ($row = sqlFetchArray($res)) {
191 if ($row['closeness'] == 0) continue;
192 if ($row['closeness'] >= 100) {
193 ++$login_matches;
194 $login_pid = $row['pid'];
196 else {
197 // We have a match on login name but this is not one, so ignore it.
198 if ($login_matches) continue;
200 $phone = $row['phone_biz'];
201 if (empty($phone)) $phone = $row['phone_home'];
202 if (empty($phone)) $phone = $row['phone_cell'];
203 if (empty($phone)) $phone = $row['phone_contact'];
205 echo " <tr class='oneresult'";
206 echo " onclick=\"openPatient(" .
207 "'" . addslashes($row['pid']) . "'" .
208 ")\">\n";
209 echo " <td";
210 if ($row['cmsportal_login'] !== '' && $result['post']['user'] !== $row['cmsportal_login']) {
211 echo " style='color:red' title='" . xla('Portal ID does not match request from portal!') . "'";
213 echo ">" . text($row['cmsportal_login']) . "</td>\n";
214 echo " <td>" . text($row['lname'] . ", " . $row['fname'] ) . "</td>\n";
215 echo " <td>" . text($phone ) . "</td>\n";
216 echo " <td>" . text($row['ss'] ) . "</td>\n";
217 echo " <td>" . text($row['DOB'] ) . "</td>\n";
218 echo " <td>" . text($row['street'] . ' ' . $row['postal_code']) . "</td>\n";
219 echo " </tr>\n";
222 </table>
223 </div>
224 <?php
226 if ($login_matches == 1) {
227 // There is exactly one match by portal login name, this must be it.
228 // There should not be more than one, but if there is then we will
229 // leave them onscreen and let the user choose.
230 echo "<script language='JavaScript'>\n";
231 echo "openPatient('" . addslashes($login_pid) . "');\n";
232 echo "</script>\n";
237 <input type='button' value='<?php echo xla('Add New Patient'); ?>' onclick="openPatient(0)" />
238 &nbsp;
239 <input type='button' value='<?php echo xla('Back'); ?>' onclick="myRestoreSession();location='list_requests.php'" />
240 </p>
242 </form>
243 </center>
244 </body>
245 </html>