Focus the search term on load
[openemr.git] / interface / main / finder / patient_select.php
blobeb01e9c9b5221c71e8d8951fe7c33d4c55661789
1 <?php
2 // This program is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU General Public License
4 // as published by the Free Software Foundation; either version 2
5 // of the License, or (at your option) any later version.
7 //SANITIZE ALL ESCAPES
8 $sanitize_all_escapes=true;
9 //
11 //STOP FAKE REGISTER GLOBALS
12 $fake_register_globals=false;
15 require_once("../../globals.php");
16 require_once("$srcdir/patient.inc");
17 require_once("$srcdir/formdata.inc.php");
19 $fstart = isset($_REQUEST['fstart']) ? $_REQUEST['fstart'] : 0;
20 $popup = empty($_REQUEST['popup']) ? 0 : 1;
21 $message = isset($_GET['message']) ? $_GET['message'] : "";
24 <html>
25 <head>
26 <?php html_header_show();?>
28 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
29 <style>
30 form {
31 padding: 0px;
32 margin: 0px;
34 #searchCriteria {
35 text-align: center;
36 width: 100%;
37 font-size: 0.8em;
38 background-color: #ddddff;
39 font-weight: bold;
40 padding: 3px;
42 #searchResultsHeader {
43 width: 100%;
44 background-color: lightgrey;
46 #searchResultsHeader table {
47 width: 96%; /* not 100% because the 'searchResults' table has a scrollbar */
48 border-collapse: collapse;
50 #searchResultsHeader th {
51 font-size: 0.7em;
53 #searchResults {
54 width: 100%;
55 height: 80%;
56 overflow: auto;
59 .srName { width: 12%; }
60 .srPhone { width: 11%; }
61 .srSS { width: 11%; }
62 .srDOB { width: 8%; }
63 .srID { width: 7%; }
64 .srPID { width: 7%; }
65 .srNumEnc { width: 11%; }
66 .srNumDays { width: 11%; }
67 .srDateLast { width: 11%; }
68 .srDateNext { width: 11%; }
69 .srMisc { width: 10%; }
71 #searchResults table {
72 width: 100%;
73 border-collapse: collapse;
74 background-color: white;
76 #searchResults tr {
77 cursor: hand;
78 cursor: pointer;
80 #searchResults td {
81 font-size: 0.7em;
82 border-bottom: 1px solid #eee;
84 .oneResult { }
85 .billing { color: red; font-weight: bold; }
86 .highlight {
87 background-color: #336699;
88 color: white;
90 </style>
92 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.2.2.min.js"></script>
94 <script language="JavaScript">
96 // This is called when forward or backward paging is done.
98 function submitList(offset) {
99 var f = document.forms[0];
100 var i = parseInt(f.fstart.value) + offset;
101 if (i < 0) i = 0;
102 f.fstart.value = i;
103 f.submit();
106 </script>
108 </head>
109 <body class="body_top">
111 <form method='post' action='patient_select.php' name='theform'>
112 <input type='hidden' name='fstart' value='<?php echo htmlspecialchars( $fstart, ENT_QUOTES); ?>' />
114 <?php
115 $MAXSHOW = 100; // maximum number of results to display at once
117 //the maximum number of patient records to display:
118 $sqllimit = $MAXSHOW;
119 $given = "*, DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS";
120 $orderby = "lname ASC, fname ASC";
122 $search_service_code = trim($_POST['search_service_code']);
123 echo "<input type='hidden' name='search_service_code' value='" .
124 htmlspecialchars($search_service_code, ENT_QUOTES) . "' />\n";
126 if ($popup) {
127 echo "<input type='hidden' name='popup' value='1' />\n";
129 // Construct WHERE clause and save search parameters as form fields.
130 $sqlBindArray = array();
131 $where = "1 = 1";
132 $fres = sqlStatement("SELECT * FROM layout_options " .
133 "WHERE form_id = 'DEM' AND uor > 0 AND field_id != '' " .
134 "ORDER BY group_name, seq");
135 while ($frow = sqlFetchArray($fres)) {
136 $field_id = $frow['field_id'];
137 if (strpos($field_id, 'em_') === 0) continue;
138 $data_type = $frow['data_type'];
139 if (!empty($_REQUEST[$field_id])) {
140 $value = trim($_REQUEST[$field_id]);
141 if ($field_id == 'pid') {
142 $where .= " AND $field_id = ?";
143 array_push($sqlBindArray,$value);
145 else if ($field_id == 'pubpid') {
146 $where .= " AND $field_id LIKE ?";
147 array_push($sqlBindArray,$value);
149 else {
150 $where .= " AND $field_id LIKE ?";
151 array_push($sqlBindArray,$value."%");
153 echo "<input type='hidden' name='" . htmlspecialchars( $field_id, ENT_QUOTES) .
154 "' value='" . htmlspecialchars( $value, ENT_QUOTES) . "' />\n";
158 // If a non-empty service code was given, then restrict to patients who
159 // have been provided that service. Since the code is used in a LIKE
160 // clause, % and _ wildcards are supported.
161 if ($search_service_code) {
162 $where .=
163 " AND ( SELECT COUNT(*) FROM billing AS b WHERE " .
164 "b.pid = patient_data.pid AND " .
165 "b.activity = 1 AND " .
166 "b.code_type != 'COPAY' AND " .
167 "b.code LIKE ? " .
168 ") > 0";
169 array_push($sqlBindArray, $search_service_code);
172 $sql = "SELECT $given FROM patient_data " .
173 "WHERE $where ORDER BY $orderby LIMIT $fstart, $sqllimit";
174 $rez = sqlStatement($sql,$sqlBindArray);
175 $result = array();
176 while ($row = sqlFetchArray($rez)) $result[] = $row;
177 _set_patient_inc_count($sqllimit, count($result), $where, $sqlBindArray);
179 else {
180 $patient = $_REQUEST['patient'];
181 $findBy = $_REQUEST['findBy'];
182 $searchFields = $_REQUEST['searchFields'];
184 echo "<input type='hidden' name='patient' value='" . htmlspecialchars( $patient, ENT_QUOTES) . "' />\n";
185 echo "<input type='hidden' name='findBy' value='" . htmlspecialchars( $findBy, ENT_QUOTES) . "' />\n";
187 if ($findBy == "Last")
188 $result = getPatientLnames("$patient", $given, $orderby, $sqllimit, $fstart);
189 else if ($findBy == "ID")
190 $result = getPatientId("$patient", $given, "id ASC, ".$orderby, $sqllimit, $fstart);
191 else if ($findBy == "DOB")
192 $result = getPatientDOB("$patient", $given, "DOB ASC, ".$orderby, $sqllimit, $fstart);
193 else if ($findBy == "SSN")
194 $result = getPatientSSN("$patient", $given, "ss ASC, ".$orderby, $sqllimit, $fstart);
195 elseif ($findBy == "Phone") //(CHEMED) Search by phone number
196 $result = getPatientPhone("$patient", $given, $orderby, $sqllimit, $fstart);
197 else if ($findBy == "Any")
198 $result = getByPatientDemographics("$patient", $given, $orderby, $sqllimit, $fstart);
199 else if ($findBy == "Filter") {
200 $result = getByPatientDemographicsFilter($searchFields, "$patient",
201 $given, $orderby, $sqllimit, $fstart, $search_service_code);
206 </form>
208 <table border='0' cellpadding='5' cellspacing='0' width='100%'>
209 <tr>
210 <td class='text'>
211 <a href="./patient_select_help.php" target=_new>[<?php echo htmlspecialchars( xl('Help'), ENT_NOQUOTES); ?>]&nbsp</a>
212 </td>
213 <td class='text' align='center'>
214 <?php if ($message) echo "<font color='red'><b>".htmlspecialchars( $message, ENT_NOQUOTES)."</b></font>\n"; ?>
215 </td>
216 <td class='text' align='right'>
217 <?php
218 // Show start and end row number, and number of rows, with paging links.
220 // $count = $fstart + $GLOBALS['PATIENT_INC_COUNT']; // Why did I do that???
221 $count = $GLOBALS['PATIENT_INC_COUNT'];
222 $fend = $fstart + $MAXSHOW;
223 if ($fend > $count) $fend = $count;
225 <?php if ($fstart) { ?>
226 <a href="javascript:submitList(-<?php echo $MAXSHOW ?>)">
227 &lt;&lt;
228 </a>
229 &nbsp;&nbsp;
230 <?php } ?>
231 <?php echo ($fstart + 1) . htmlspecialchars( " - $fend of $count", ENT_NOQUOTES); ?>
232 <?php if ($count > $fend) { ?>
233 &nbsp;&nbsp;
234 <a href="javascript:submitList(<?php echo $MAXSHOW ?>)">
235 &gt;&gt;
236 </a>
237 <?php } ?>
238 </td>
239 </tr>
240 </table>
242 <div id="searchResultsHeader">
243 <table>
244 <tr>
245 <th class="srName"><?php echo htmlspecialchars( xl('Name'), ENT_NOQUOTES);?></th>
246 <th class="srPhone"><?php echo htmlspecialchars( xl('Phone'), ENT_NOQUOTES);?></th>
247 <th class="srSS"><?php echo htmlspecialchars( xl('SS'), ENT_NOQUOTES);?></th>
248 <th class="srDOB"><?php echo htmlspecialchars( xl('DOB'), ENT_NOQUOTES);?></th>
249 <th class="srID"><?php echo htmlspecialchars( xl('ID'), ENT_NOQUOTES);?></th>
251 <?php if (empty($GLOBALS['patient_search_results_style'])) { ?>
252 <th class="srPID"><?php echo htmlspecialchars( xl('PID'), ENT_NOQUOTES);?></th>
253 <th class="srNumEnc"><?php echo htmlspecialchars( xl('[Number Of Encounters]'), ENT_NOQUOTES);?></th>
254 <th class="srNumDays"><?php echo htmlspecialchars( xl('[Days Since Last Encounter]'), ENT_NOQUOTES);?></th>
255 <th class="srDateLast"><?php echo htmlspecialchars( xl('[Date of Last Encounter]'), ENT_NOQUOTES);?></th>
256 <th class="srDateNext">
257 <?php
258 $add_days = 90;
259 if (!$popup && preg_match('/^(\d+)\s*(.*)/',$patient,$matches) > 0) {
260 $add_days = $matches[1];
261 $patient = $matches[2];
264 [<?php echo htmlspecialchars( $add_days, ENT_NOQUOTES);?> <?php echo htmlspecialchars( xl('Days From Last Encounter'), ENT_NOQUOTES); ?>]
265 </th>
267 <?php
269 else {
270 // Alternate patient search results style; this gets address plus other
271 // fields that are mandatory, up to a limit of 5.
272 $extracols = array();
273 $tres = sqlStatement("SELECT field_id, title FROM layout_options " .
274 "WHERE form_id = 'DEM' AND ( uor > 1 AND field_id != '' " .
275 "OR uor > 0 AND field_id = 'street' ) AND " .
276 "field_id NOT LIKE '_name' AND " .
277 "field_id NOT LIKE 'phone%' AND " .
278 "field_id NOT LIKE 'title' AND " .
279 "field_id NOT LIKE 'ss' AND " .
280 "field_id NOT LIKE 'DOB' AND " .
281 "field_id NOT LIKE 'pubpid' " .
282 "ORDER BY group_name, seq LIMIT 5");
283 while ($trow = sqlFetchArray($tres)) {
284 $extracols[$trow['field_id']] = $trow['title'];
285 echo "<th class='srMisc'>" . htmlspecialchars(xl($trow['title']), ENT_NOQUOTES) . "</th>\n";
290 </tr>
291 </table>
292 </div>
294 <div id="searchResults">
296 <table>
297 <tr>
298 <?php
299 if ($result) {
300 foreach ($result as $iter) {
301 echo "<tr class='oneresult' id='".htmlspecialchars( $iter['pid'], ENT_QUOTES)."'>";
302 echo "<td class='srName'>" . htmlspecialchars($iter['lname'] . ", " . $iter['fname']) . "</td>\n";
303 //other phone number display setup for tooltip
304 $phone_biz = '';
305 if ($iter{"phone_biz"} != "") {
306 $phone_biz = " [business phone ".$iter{"phone_biz"}."] ";
308 $phone_contact = '';
309 if ($iter{"phone_contact"} != "") {
310 $phone_contact = " [contact phone ".$iter{"phone_contact"}."] ";
312 $phone_cell = '';
313 if ($iter{"phone_cell"} != "") {
314 $phone_cell = " [cell phone ".$iter{"phone_cell"}."] ";
316 $all_other_phones = $phone_biz.$phone_contact.$phone_cell;
317 if ($all_other_phones == '') {$all_other_phones = xl('No other phone numbers listed');}
318 //end of phone number display setup, now display the phone number(s)
319 echo "<td class='srPhone' title='".htmlspecialchars( $all_other_phones, ENT_QUOTES)."'>" .
320 htmlspecialchars( $iter['phone_home'], ENT_NOQUOTES) . "</td>\n";
322 echo "<td class='srSS'>" . htmlspecialchars( $iter['ss'], ENT_NOQUOTES) . "</td>";
323 if ($iter{"DOB"} != "0000-00-00 00:00:00") {
324 echo "<td class='srDOB'>" . htmlspecialchars( $iter['DOB_TS'], ENT_NOQUOTES) . "</td>";
325 } else {
326 echo "<td class='srDOB'>&nbsp;</td>";
329 echo "<td class='srID'>" . htmlspecialchars( $iter['pubpid'], ENT_NOQUOTES) . "</td>";
331 if (empty($GLOBALS['patient_search_results_style'])) {
333 echo "<td class='srPID'>" . htmlspecialchars( $iter['pid'], ENT_NOQUOTES) . "</td>";
335 //setup for display of encounter date info
336 $encounter_count = 0;
337 $day_diff = '';
338 $last_date_seen = '';
339 $next_appt_date= '';
340 $pid = '';
342 // calculate date differences based on date of last encounter with billing entries
343 $query = "select DATE_FORMAT(max(form_encounter.date),'%m/%d/%y') as mydate," .
344 " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
345 " DATE_FORMAT(max(form_encounter.date) + interval " .
346 add_escape_custom($add_days) .
347 " day,'%m/%d/%y') as next_appt, dayname(max(form_encounter.date) + interval " .
348 add_escape_custom($add_days) .
349 " day) as next_appt_day from form_encounter " .
350 "join billing on billing.encounter = form_encounter.encounter and " .
351 "billing.pid = form_encounter.pid and billing.activity = 1 and " .
352 "billing.code_type not like 'COPAY' where ".
353 "form_encounter.pid = ?";
354 $statement= sqlStatement($query, array($iter{"pid"}) );
355 if ($results = sqlFetchArray($statement)) {
356 $last_date_seen = $results['mydate'];
357 $day_diff = $results['day_diff'];
358 $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
360 // calculate date differences based on date of last encounter regardless of billing
361 $query = "select DATE_FORMAT(max(form_encounter.date),'%m/%d/%y') as mydate," .
362 " (to_days(current_date())-to_days(max(form_encounter.date))) as day_diff," .
363 " DATE_FORMAT(max(form_encounter.date) + interval " .
364 add_escape_custom($add_days) .
365 " day,'%m/%d/%y') as next_appt, dayname(max(form_encounter.date) + interval " .
366 add_escape_custom($add_days) .
367 " day) as next_appt_day from form_encounter " .
368 " where form_encounter.pid = ?";
369 $statement= sqlStatement($query, array($iter{"pid"}) );
370 if ($results = sqlFetchArray($statement)) {
371 $last_date_seen = $results['mydate'];
372 $day_diff = $results['day_diff'];
373 $next_appt_date= $results['next_appt_day'].', '.$results['next_appt'];
376 //calculate count of encounters by distinct billing dates with cpt4
377 //entries
378 $query = "select count(distinct date) as encounter_count " .
379 " from billing ".
380 " where code_type not like 'COPAY' and activity = 1 " .
381 " and pid = ?";
382 $statement= sqlStatement($query, array($iter{"pid"}) );
383 if ($results = sqlFetchArray($statement)) {
384 $encounter_count_billed = $results['encounter_count'];
386 // calculate count of encounters, regardless of billing
387 $query = "select count(date) as encounter_count ".
388 " from form_encounter where ".
389 " pid = ?";
390 $statement= sqlStatement($query, array($iter{"pid"}) );
391 if ($results = sqlFetchArray($statement)) {
392 $encounter_count = $results['encounter_count'];
394 echo "<td class='srNumEnc'>" . htmlspecialchars( $encounter_count, ENT_NOQUOTES) . "</td>\n";
395 echo "<td class='srNumDay'>" . htmlspecialchars( $day_diff, ENT_NOQUOTES) . "</td>\n";
396 echo "<td class='srDateLast'>" . htmlspecialchars( $last_date_seen, ENT_NOQUOTES) . "</td>\n";
397 echo "<td class='srDateNext'>" . htmlspecialchars( $next_appt_date, ENT_NOQUOTES) . "</td>\n";
400 else { // alternate search results style
401 foreach ($extracols as $field_id => $title) {
402 echo "<td class='srMisc'>" . htmlspecialchars( $iter[$field_id], ENT_NOQUOTES) . "</td>\n";
408 </table>
409 </div> <!-- end searchResults DIV -->
411 <script language="javascript">
413 // jQuery stuff to make the page a little easier to use
415 $(document).ready(function(){
416 // $("#searchparm").focus();
417 $(".oneresult").mouseover(function() { $(this).addClass("highlight"); });
418 $(".oneresult").mouseout(function() { $(this).removeClass("highlight"); });
419 $(".oneresult").click(function() { SelectPatient(this); });
420 // $(".event").dblclick(function() { EditEvent(this); });
423 var SelectPatient = function (eObj) {
424 <?php
425 // For the old layout we load a frameset that also sets up the new pid.
426 // The new layout loads just the demographics frame here, which in turn
427 // will set the pid and load all the other frames.
428 if ($GLOBALS['concurrent_layout']) {
429 $newPage = "../../patient_file/summary/demographics.php?set_pid=";
430 $target = "document";
432 else {
433 $newPage = "../../patient_file/patient_file.php?set_pid=";
434 $target = "top";
437 objID = eObj.id;
438 var parts = objID.split("~");
439 <?php if ($popup) echo "opener."; echo $target; ?>.location.href = '<?php echo $newPage; ?>' + parts[0];
440 <?php if ($popup) echo "window.close();\n"; ?>
441 return true;
444 </script>
446 </body>
447 </html>