dump db version
[openemr.git] / interface / new / new_search_popup.php
blob4a3726c4a5aed2dd723dd6f694fb74eb6b9e9845
1 <?php
2 /**
3 * new_search_popup.php
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Rod Roark <rod@sunsetsystems.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2010-2017 Rod Roark <rod@sunsetsystems.com>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once("../globals.php");
16 require_once("$srcdir/patient.inc");
18 if (!empty($_POST)) {
19 if (!verifyCsrfToken($_POST["csrf_token_form"])) {
20 csrfNotVerified();
24 $fstart = $_REQUEST['fstart'] + 0;
26 $searchcolor = empty($GLOBALS['layout_search_color']) ?
27 '#ffff55' : $GLOBALS['layout_search_color'];
29 <html>
30 <head>
31 <?php html_header_show();?>
32 <script type="text/javascript" src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script>
34 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
35 <style>
36 form {
37 padding: 0px;
38 margin: 0px;
40 #searchCriteria {
41 text-align: center;
42 width: 100%;
43 font-size: 0.8em;
44 background-color: #ddddff;
45 font-weight: bold;
46 padding: 3px;
48 #searchResultsHeader {
49 width: 100%;
50 background-color: lightgrey;
52 #searchResultsHeader table {
53 width: 96%; /* not 100% because the 'searchResults' table has a scrollbar */
54 border-collapse: collapse;
56 #searchResultsHeader th {
57 font-size: 0.7em;
59 #searchResults {
60 width: 100%;
61 height: 80%;
62 overflow: auto;
65 .srName { width: 12%; }
66 .srPhone { width: 11%; }
67 .srSS { width: 11%; }
68 .srDOB { width: 8%; }
69 .srID { width: 7%; }
70 .srMisc { width: 10%; }
72 #searchResults table {
73 width: 100%;
74 border-collapse: collapse;
75 background-color: white;
77 #searchResults tr {
78 cursor: hand;
79 cursor: pointer;
81 #searchResults td {
82 font-size: 0.7em;
83 border-bottom: 1px solid #eee;
85 .oneResult {
87 .topResult {
88 background-color: <?php echo attr($searchcolor); ?>;
90 .billing {
91 color: red;
92 font-weight: bold;
94 .highlight {
95 background-color: #336699;
96 color: white;
98 </style>
100 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery/dist/jquery.min.js"></script>
102 <script language="JavaScript">
104 // This is called when forward or backward paging is done.
106 function submitList(offset) {
107 var f = document.forms[0];
108 var i = parseInt(f.fstart.value) + offset;
109 if (i < 0) i = 0;
110 f.fstart.value = i;
111 f.submit();
114 </script>
116 </head>
117 <body class="body_top">
119 <form method='post' action='new_search_popup.php' name='theform'>
120 <input type="hidden" name="csrf_token_form" value="<?php echo attr(collectCsrfToken()); ?>" />
122 <input type='hidden' name='fstart' value='<?php echo attr($fstart); ?>' />
124 <?php
125 $MAXSHOW = 100; // maximum number of results to display at once
127 // Construct query and save search parameters as form fields.
128 // An interesting requirement is to sort on the number of matching fields.
130 $message = "";
131 $numfields = 0;
132 $relevance = "0";
133 // array to hold the sql parameters for binding
134 // Note in this special situation, there are two:
135 // 1. For the main sql statement - $sqlBindArray
136 // 2. For the _set_patient_inc_count function - $sqlBindArraySpecial
137 // (this only holds $where and not $relevance binded values)
138 $sqlBindArray = array();
139 $sqlBindArraySpecial = array();
140 $where = "1 = 0";
142 foreach ($_REQUEST as $key => $value) {
143 if (substr($key, 0, 3) != 'mf_') {
144 continue; // "match field"
147 $fldname = substr($key, 3);
148 // pubpid requires special treatment. Match on that is fatal.
149 if ($fldname == 'pubpid') {
150 $relevance .= " + 1000 * ( ".add_escape_custom($fldname)." LIKE ? )";
151 array_push($sqlBindArray, $value);
152 } else {
153 $relevance .= " + ( ".add_escape_custom($fldname)." LIKE ? )";
154 array_push($sqlBindArray, $value);
157 $where .= " OR ".add_escape_custom($fldname)." LIKE ?";
158 array_push($sqlBindArraySpecial, $value);
159 echo "<input type='hidden' name='".attr($key)."' value='".attr($value)."' />\n";
160 ++$numfields;
163 $sql = "SELECT *, ( $relevance ) AS relevance, " .
164 "DATE_FORMAT(DOB,'%m/%d/%Y') as DOB_TS " .
165 "FROM patient_data WHERE $where " .
166 "ORDER BY relevance DESC, lname, fname, mname " .
167 "LIMIT ".escape_limit($fstart).", ".escape_limit($MAXSHOW)."";
169 $sqlBindArray = array_merge($sqlBindArray, $sqlBindArraySpecial);
170 $rez = sqlStatement($sql, $sqlBindArray);
171 $result = array();
172 while ($row = sqlFetchArray($rez)) {
173 $result[] = $row;
176 _set_patient_inc_count($MAXSHOW, count($result), $where, $sqlBindArraySpecial);
179 </form>
181 <table border='0' cellpadding='5' cellspacing='0' width='100%'>
182 <tr>
183 <td class='text'>
184 &nbsp;
185 </td>
186 <td class='text' align='center'>
187 <?php if ($message) {
188 echo "<font color='red'><b>".text($message)."</b></font>\n";
189 } ?>
190 </td>
191 <td class='text' align='right'>
192 <?php
193 // Show start and end row number, and number of rows, with paging links.
194 $count = $GLOBALS['PATIENT_INC_COUNT'];
195 $fend = $fstart + $MAXSHOW;
196 if ($fend > $count) {
197 $fend = $count;
200 <?php if ($fstart) { ?>
201 <a href="javascript:submitList(-<?php echo attr($MAXSHOW); ?>)">
202 &lt;&lt;
203 </a>
204 &nbsp;&nbsp;
205 <?php } ?>
206 <?php echo ($fstart + 1) . text(" - $fend of $count") ?>
207 <?php if ($count > $fend) { ?>
208 &nbsp;&nbsp;
209 <a href="javascript:submitList(<?php echo attr($MAXSHOW); ?>)">
210 &gt;&gt;
211 </a>
212 <?php } ?>
213 </td>
214 </tr>
215 </table>
217 <div id="searchResultsHeader" class="head">
218 <table>
219 <tr>
220 <th class="srID" ><?php echo xlt('Hits');?></th>
221 <th class="srName" ><?php echo xlt('Name');?></th>
222 <?php
223 // This gets address plus other fields that are mandatory, up to a limit of 5.
224 $extracols = array();
225 $tres = sqlStatement("SELECT field_id, title FROM layout_options " .
226 "WHERE form_id = 'DEM' AND field_id != '' AND " .
227 "( uor > 1 OR uor > 0 AND edit_options LIKE '%D%' ) AND " .
228 "field_id NOT LIKE 'title' AND " .
229 "field_id NOT LIKE '_name' " .
230 "ORDER BY group_id, seq, title LIMIT 9");
232 while ($trow = sqlFetchArray($tres)) {
233 $extracols[$trow['field_id']] = $trow['title'];
234 echo "<th class='srMisc'>" . text(xl_layout_label($trow['title'])) . "</th>\n";
238 </tr>
239 </table>
240 </div>
242 <div id="searchResults">
244 <table>
245 <tr>
246 <?php
247 $pubpid_matched = false;
248 if ($result) {
249 foreach ($result as $iter) {
250 $relevance = $iter['relevance'];
251 if ($relevance > 999) {
252 $relevance -= 999;
253 $pubpid_matched = true;
256 echo "<tr id='" . attr($iter['pid']) . "' class='oneresult";
257 // Highlight entries where all fields matched.
258 echo $numfields <= $iter['relevance'] ? " topresult" : "";
259 echo "'>";
260 echo "<td class='srID'>" . text($relevance) . "</td>\n";
261 echo "<td class='srName'>" . text($iter['lname'] . ", " . $iter['fname']) . "</td>\n";
262 foreach ($extracols as $field_id => $title) {
263 echo "<td class='srMisc'>" . text($iter[$field_id]) . "</td>\n";
268 </table>
269 </div> <!-- end searchResults DIV -->
271 <center>
272 <?php if ($pubpid_matched) { ?>
273 <input type='button' value='<?php echo xla('Cancel'); ?>'
274 onclick='dlgclose();' />
275 <?php } else { ?>
276 <input type='button' value='<?php echo xla('Confirm Create New Patient'); ?>'
277 onclick='dlgclose("srcConfirmSave", false);' />
278 <?php } ?>
279 </center>
281 <script language="javascript">
283 // jQuery stuff to make the page a little easier to use
285 $(document).ready(function() {
286 $(".oneresult").mouseover(function() { $(this).addClass("highlight"); });
287 $(".oneresult").mouseout(function() { $(this).removeClass("highlight"); });
288 $(".oneresult").click(function() { SelectPatient(this); });
291 var SelectPatient = function (eObj) {
292 <?php
293 // The layout loads just the demographics frame here, which in turn
294 // will set the pid and load all the other frames.
295 $newPage = "../patient_file/summary/demographics.php?set_pid=";
296 $target = "document";
299 objID = eObj.id;
300 var parts = objID.split("~");
301 opener.<?php echo $target; ?>.location.href = '<?php echo $newPage; ?>' + parts[0];
302 dlgclose();
303 return true;
306 var f = opener.document.forms[0];
307 <?php if ($pubpid_matched) { ?>
308 alert(<?php echo xlj('A patient with this ID already exists.'); ?>);
309 <?php } else { ?>
310 opener.force_submit = true;
311 f.create.value = <?php echo xlj('Confirm Create New Patient'); ?>;
312 <?php } ?>
314 <?php if (!count($result)) { ?>
315 $("<td><?php echo xlt('No matches were found.'); ?></td>").appendTo("#searchResults tr");
316 <?php } ?>
318 </script>
320 </body>
321 </html>