added support for the scanned_notes encounter form
[openemr.git] / library / options.inc.php
blobb24c063ae2af5919e59f77d798d6285ae3df3960
1 <?php
3 $date_init = "";
5 function get_pharmacies() {
6 return sqlStatement("SELECT d.id, d.name, a.line1, a.city, " .
7 "p.area_code, p.prefix, p.number FROM pharmacies AS d " .
8 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = d.id " .
9 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = d.id " .
10 "AND p.type = 2 " .
11 "ORDER BY name, area_code, prefix, number");
14 function generate_form_field($frow, $currvalue) {
15 global $rootdir, $date_init;
17 $currescaped = htmlspecialchars($currvalue, ENT_QUOTES);
19 $data_type = $frow['data_type'];
20 $field_id = $frow['field_id'];
21 $list_id = $frow['list_id'];
22 $description = htmlspecialchars($frow['description'], ENT_QUOTES);
24 // generic single-selection list
25 if ($data_type == 1) {
26 echo "<select name='form_$field_id' title='$description'>";
27 echo "<option value=''>" . xl('Unassigned') . "</option>";
28 $lres = sqlStatement("SELECT * FROM list_options " .
29 "WHERE list_id = '$list_id' ORDER BY seq");
30 $got_selected = FALSE;
31 while ($lrow = sqlFetchArray($lres)) {
32 echo "<option value='" . $lrow['option_id'] . "'";
33 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
34 (strlen($currvalue) > 0 && $lrow['option_id'] == $currvalue))
36 echo " selected";
37 $got_selected = TRUE;
39 echo ">" . $lrow['title'] . "</option>\n";
41 if (!$got_selected && strlen($currvalue) > 0) {
42 echo "<option value='$currescaped' selected>* $currescaped *</option>";
43 echo "</select>";
44 echo " <font color='red' title='Please choose a valid selection " .
45 "from the list'>Fix this!</font>";
47 else {
48 echo "</select>";
52 // simple text field
53 else if ($data_type == 2) {
54 echo "<input type='text'" .
55 " name='form_$field_id'" .
56 " size='" . $frow['fld_length'] . "'" .
57 " maxlength='" . $frow['max_length'] . "'" .
58 " title='$description'" .
59 " value='$currescaped'";
60 if (strpos($frow['edit_options'], 'C') !== FALSE)
61 echo " onchange='capitalizeMe(this)'";
62 echo " />";
65 // long or multi-line text field
66 else if ($data_type == 3) {
67 echo "<textarea" .
68 " name='form_$field_id'" .
69 " title='$description'" .
70 " cols='" . $frow['fld_length'] . "'" .
71 " rows='" . $frow['max_length'] . "'>" .
72 $currescaped . "</textarea>";
75 // date
76 else if ($data_type == 4) {
77 echo "<input type='text' size='10' name='form_$field_id' id='form_$field_id'" .
78 " value='$currescaped'" .
79 " title='$description'" .
80 " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />" .
81 "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
82 " id='img_$field_id' border='0' alt='[?]' style='cursor:pointer'" .
83 " title='" . xl('Click here to choose a date') . "' />";
84 $date_init .= " Calendar.setup({inputField:'form_$field_id', ifFormat:'%Y-%m-%d', button:'img_$field_id'});\n";
87 // provider list, local providers only
88 else if ($data_type == 10) {
89 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
90 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
91 "AND authorized = 1 " .
92 "ORDER BY lname, fname");
93 echo "<select name='form_$field_id' title='$description'>";
94 echo "<option value=''>" . xl('Unassigned') . "</option>";
95 while ($urow = sqlFetchArray($ures)) {
96 $uname = $urow['fname'] . ' ' . $urow['lname'];
97 echo "<option value='" . $urow['id'] . "'";
98 if ($urow['id'] == $currvalue) echo " selected";
99 echo ">$uname</option>";
101 echo "</select>";
104 // provider list, including address book entries with an NPI number
105 else if ($data_type == 11) {
106 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
107 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
108 "AND ( authorized = 1 OR ( username = '' AND npi != '' ) ) " .
109 "ORDER BY lname, fname");
110 echo "<select name='form_$field_id' title='$description'>";
111 echo "<option value=''>" . xl('Unassigned') . "</option>";
112 while ($urow = sqlFetchArray($ures)) {
113 $uname = $urow['fname'] . ' ' . $urow['lname'];
114 echo "<option value='" . $urow['id'] . "'";
115 if ($urow['id'] == $currvalue) echo " selected";
116 echo ">$uname</option>";
118 echo "</select>";
121 // pharmacy list
122 else if ($data_type == 12) {
123 echo "<select name='form_$field_id' title='$description'>";
124 echo "<option value='0'></option>";
125 $pres = get_pharmacies();
126 while ($prow = sqlFetchArray($pres)) {
127 $key = $prow['id'];
128 echo "<option value='$key'";
129 if ($currvalue == $key) echo " selected";
130 echo '>' . $prow['name'] . ' ' . $prow['area_code'] . '-' .
131 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
132 $prow['line1'] . ' / ' . $prow['city'] . "</option>";
134 echo "</select>";
137 // squads
138 else if ($data_type == 13) {
139 echo "<select name='form_$field_id' title='$description'>";
140 echo "<option value=''>&nbsp;</option>";
141 $squads = acl_get_squads();
142 if ($squads) {
143 foreach ($squads as $key => $value) {
144 echo "<option value='$key'";
145 if ($currvalue == $key) echo " selected";
146 echo ">" . $value[3] . "</option>\n";
149 echo "</select>";
152 // address book, preferring organization name if it exists and is not in parentheses
153 else if ($data_type == 14) {
154 $ures = sqlStatement("SELECT id, fname, lname, organization FROM users " .
155 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
156 "ORDER BY organization, lname, fname");
157 echo "<select name='form_$field_id' title='$description'>";
158 echo "<option value=''>" . xl('Unassigned') . "</option>";
159 while ($urow = sqlFetchArray($ures)) {
160 $uname = $urow['organization'];
161 if (empty($uname) || substr($uname, 0, 1) == '(') {
162 $uname = $urow['lname'];
163 if ($urow['fname']) $uname .= ", " . $urow['fname'];
165 echo "<option value='" . $urow['id'] . "'";
166 if ($urow['id'] == $currvalue) echo " selected";
167 echo ">$uname</option>";
169 echo "</select>";
172 // a billing code (only one of these allowed!)
173 else if ($data_type == 15) {
174 echo "<input type='text'" .
175 " name='form_$field_id'" .
176 " id='form_related_code'" .
177 " size='" . $frow['fld_length'] . "'" .
178 " maxlength='" . $frow['max_length'] . "'" .
179 " title='$description'" .
180 " value='$currescaped'" .
181 " onclick='sel_related()' readonly" .
182 " />";
185 // a set of labeled checkboxes
186 else if ($data_type == 21) {
187 // In this special case, fld_length is the number of columns generated.
188 $cols = max(1, $frow['fld_length']);
189 $avalue = explode('|', $currvalue);
190 $lres = sqlStatement("SELECT * FROM list_options " .
191 "WHERE list_id = '$list_id' ORDER BY seq");
192 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
193 $tdpct = (int) (100 / $cols);
194 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
195 $option_id = $lrow['option_id'];
196 // if ($count) echo "<br />";
197 if ($count % $cols == 0) {
198 if ($count) echo "</tr>";
199 echo "<tr>";
201 echo "<td width='$tdpct%'>";
202 echo "<input type='checkbox' name='form_{$field_id}[$option_id]' value='1'";
203 if (in_array($option_id, $avalue)) echo " checked";
204 echo ">" . $lrow['title'];
205 echo "</td>";
207 if ($count) {
208 echo "</tr>";
209 if ($count > $cols) {
210 // Add some space after multiple rows of checkboxes.
211 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
214 echo "</table>";
217 // a set of labeled text input fields
218 else if ($data_type == 22) {
219 $tmp = explode('|', $currvalue);
220 $avalue = array();
221 foreach ($tmp as $value) {
222 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
223 $avalue[$matches[1]] = $matches[2];
226 $lres = sqlStatement("SELECT * FROM list_options " .
227 "WHERE list_id = '$list_id' ORDER BY seq");
228 echo "<table cellpadding='0' cellspacing='0'>";
229 while ($lrow = sqlFetchArray($lres)) {
230 $option_id = $lrow['option_id'];
231 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
232 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
233 echo "<tr><td>" . $lrow['title'] . "&nbsp;</td>";
234 echo "<td><input type='text'" .
235 " name='form_{$field_id}[$option_id]'" .
236 " size='" . $frow['fld_length'] . "'" .
237 " maxlength='$maxlength'" .
238 " value='" . $avalue[$option_id] . "'";
239 echo " /></td></tr>";
241 echo "</table>";
244 // a set of exam results; 3 radio buttons and a text field:
245 else if ($data_type == 23) {
246 $tmp = explode('|', $currvalue);
247 $avalue = array();
248 foreach ($tmp as $value) {
249 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
250 $avalue[$matches[1]] = $matches[2];
253 $lres = sqlStatement("SELECT * FROM list_options " .
254 "WHERE list_id = '$list_id' ORDER BY seq");
255 echo "<table cellpadding='0' cellspacing='0'>";
256 echo "<tr><td>&nbsp;</td><td class='bold'>N/A&nbsp;</td><td class='bold'>Nor&nbsp;</td>" .
257 "<td class='bold'>Abn&nbsp;</td><td class='bold'>Date/Notes</td></tr>";
258 while ($lrow = sqlFetchArray($lres)) {
259 $option_id = $lrow['option_id'];
260 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
261 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
262 $restype = substr($avalue[$option_id], 0, 1);
263 $resnote = substr($avalue[$option_id], 2);
264 echo "<tr><td>" . $lrow['title'] . "&nbsp;</td>";
265 for ($i = 0; $i < 3; ++$i) {
266 echo "<td><input type='radio'" .
267 " name='radio_{$field_id}[$option_id]'" .
268 " value='$i'";
269 if ($restype === "$i") echo " checked";
270 echo " /></td>";
272 echo "<td><input type='text'" .
273 " name='form_{$field_id}[$option_id]'" .
274 " size='" . $frow['fld_length'] . "'" .
275 " maxlength='$maxlength'" .
276 " value='$resnote' /></td>";
277 echo "</tr>";
279 echo "</table>";
282 // the list of active allergies for the current patient
283 // this is read-only!
284 else if ($data_type == 24) {
285 $query = "SELECT title, comments FROM lists WHERE " .
286 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
287 "ORDER BY begdate";
288 // echo "<!-- $query -->\n"; // debugging
289 $lres = sqlStatement($query);
290 $count = 0;
291 while ($lrow = sqlFetchArray($lres)) {
292 if ($count++) echo "<br />";
293 echo $lrow['title'];
294 if ($lrow['comments']) echo ' (' . $lrow['comments'] . ')';
300 function generate_display_field($frow, $currvalue) {
301 $data_type = $frow['data_type'];
302 $field_id = $frow['field_id'];
303 $list_id = $frow['list_id'];
304 $s = '';
306 // generic selection list
307 if ($data_type == 1) {
308 $lrow = sqlQuery("SELECT title FROM list_options " .
309 "WHERE list_id = '$list_id' AND option_id = '$currvalue'");
310 $s = $lrow['title'];
313 // simple text field
314 else if ($data_type == 2) {
315 $s = $currvalue;
318 // long or multi-line text field
319 else if ($data_type == 3) {
320 $s = nl2br($currvalue);
323 // date
324 else if ($data_type == 4) {
325 $s = $currvalue;
328 // provider
329 else if ($data_type == 10 || $data_type == 11) {
330 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
331 "WHERE id = '$currvalue'");
332 $s = ucwords($urow['fname'] . " " . $urow['lname']);
335 // pharmacy list
336 else if ($data_type == 12) {
337 $pres = get_pharmacies();
338 while ($prow = sqlFetchArray($pres)) {
339 $key = $prow['id'];
340 if ($currvalue == $key) {
341 $s .= $prow['name'] . ' ' . $prow['area_code'] . '-' .
342 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
343 $prow['line1'] . ' / ' . $prow['city'];
348 // squads
349 else if ($data_type == 13) {
350 $squads = acl_get_squads();
351 if ($squads) {
352 foreach ($squads as $key => $value) {
353 if ($currvalue == $key) {
354 $s .= $value[3];
360 // address book
361 else if ($data_type == 14) {
362 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
363 "WHERE id = '$currvalue'");
364 $uname = $urow['lname'];
365 if ($urow['fname']) $uname .= ", " . $urow['fname'];
366 $s = $uname;
369 // billing code
370 else if ($data_type == 15) {
371 $s = $currvalue;
374 // a set of labeled checkboxes
375 else if ($data_type == 21) {
376 $avalue = explode('|', $currvalue);
377 $lres = sqlStatement("SELECT * FROM list_options " .
378 "WHERE list_id = '$list_id' ORDER BY seq");
379 $count = 0;
380 while ($lrow = sqlFetchArray($lres)) {
381 $option_id = $lrow['option_id'];
382 if (in_array($option_id, $avalue)) {
383 if ($count++) $s .= "<br />";
384 $s .= $lrow['title'];
389 // a set of labeled text input fields
390 else if ($data_type == 22) {
391 $tmp = explode('|', $currvalue);
392 $avalue = array();
393 foreach ($tmp as $value) {
394 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
395 $avalue[$matches[1]] = $matches[2];
398 $lres = sqlStatement("SELECT * FROM list_options " .
399 "WHERE list_id = '$list_id' ORDER BY seq");
400 $s .= "<table cellpadding='0' cellspacing='0'>";
401 while ($lrow = sqlFetchArray($lres)) {
402 $option_id = $lrow['option_id'];
403 if (empty($avalue[$option_id])) continue;
404 $s .= "<tr><td class='bold' valign='top'>" . $lrow['title'] . ":&nbsp;</td>";
405 $s .= "<td class='text' valign='top'>" . $avalue[$option_id] . "</td></tr>";
407 $s .= "</table>";
410 // a set of exam results; 3 radio buttons and a text field:
411 else if ($data_type == 23) {
412 $tmp = explode('|', $currvalue);
413 $avalue = array();
414 foreach ($tmp as $value) {
415 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
416 $avalue[$matches[1]] = $matches[2];
419 $lres = sqlStatement("SELECT * FROM list_options " .
420 "WHERE list_id = '$list_id' ORDER BY seq");
421 $s .= "<table cellpadding='0' cellspacing='0'>";
422 while ($lrow = sqlFetchArray($lres)) {
423 $option_id = $lrow['option_id'];
424 $restype = substr($avalue[$option_id], 0, 1);
425 $resnote = substr($avalue[$option_id], 2);
426 if (empty($restype) && empty($resnote)) continue;
427 $s .= "<tr><td class='bold' valign='top'>" . $lrow['title'] . "&nbsp;</td>";
428 $restype = ($restype == '1') ? 'Normal' : (($restype == '2') ? 'Abnormal' : 'N/A');
429 $s .= "<td class='text' valign='top'>$restype</td></tr>";
430 $s .= "<td class='text' valign='top'>$resnote</td></tr>";
431 $s .= "</tr>";
433 $s .= "</table>";
436 // the list of active allergies for the current patient
437 else if ($data_type == 24) {
438 $query = "SELECT title, comments FROM lists WHERE " .
439 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
440 "ORDER BY begdate";
441 // echo "<!-- $query -->\n"; // debugging
442 $lres = sqlStatement($query);
443 $count = 0;
444 while ($lrow = sqlFetchArray($lres)) {
445 if ($count++) $s .= "<br />";
446 $s .= $lrow['title'];
447 if ($lrow['comments']) $s .= ' (' . $lrow['comments'] . ')';
451 return $s;
454 $CPR = 4; // cells per row of generic data
455 $last_group = '';
456 $cell_count = 0;
457 $item_count = 0;
459 function disp_end_cell() {
460 global $item_count, $cell_count;
461 if ($item_count > 0) {
462 echo "</td>";
463 $item_count = 0;
467 function disp_end_row() {
468 global $cell_count, $CPR;
469 disp_end_cell();
470 if ($cell_count > 0) {
471 for (; $cell_count < $CPR; ++$cell_count) echo "<td></td>";
472 echo "</tr>\n";
473 $cell_count = 0;
477 function disp_end_group() {
478 global $last_group;
479 if (strlen($last_group) > 0) {
480 disp_end_row();
484 function display_layout_rows($formtype, $result1, $result2='') {
485 global $item_count, $cell_count, $last_group, $CPR;
487 $fres = sqlStatement("SELECT * FROM layout_options " .
488 "WHERE form_id = '$formtype' AND uor > 0 " .
489 "ORDER BY group_name, seq");
491 while ($frow = sqlFetchArray($fres)) {
492 $this_group = $frow['group_name'];
493 $titlecols = $frow['titlecols'];
494 $datacols = $frow['datacols'];
495 $data_type = $frow['data_type'];
496 $field_id = $frow['field_id'];
497 $list_id = $frow['list_id'];
498 $currvalue = '';
500 if ($formtype == 'DEM') {
501 if ($GLOBALS['athletic_team']) {
502 // Skip fitness level and return-to-play date because those appear
503 // in a special display/update form on this page.
504 if ($field_id === 'fitness' || $field_id === 'userdate1') continue;
506 if (strpos($field_id, 'em_') === 0) {
507 $tmp = substr($field_id, 3);
508 if (isset($result2[$tmp])) $currvalue = $result2[$tmp];
510 else {
511 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
514 else {
515 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
518 // Handle a data category (group) change.
519 if (strcmp($this_group, $last_group) != 0) {
520 disp_end_group();
521 $group_name = substr($this_group, 1);
522 $last_group = $this_group;
525 // Handle starting of a new row.
526 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
527 disp_end_row();
528 echo " <tr><td class='bold' style='padding-right:5pt' valign='top'>";
529 if ($group_name) {
530 echo "<font color='#008800'>$group_name</font>";
531 $group_name = '';
532 } else {
533 echo '&nbsp;';
535 echo "</td>";
538 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
540 // Handle starting of a new label cell.
541 if ($titlecols > 0) {
542 disp_end_cell();
543 echo "<td class='bold' colspan='$titlecols' valign='top'";
544 if ($cell_count == 2) echo " style='padding-left:10pt'";
545 echo ">";
546 $cell_count += $titlecols;
548 ++$item_count;
550 if ($frow['title']) echo $frow['title'] . ":"; else echo "&nbsp;";
552 // Handle starting of a new data cell.
553 if ($datacols > 0) {
554 disp_end_cell();
555 echo "<td colspan='$datacols' class='text' valign='top'";
556 if ($cell_count > 0) echo " style='padding-left:5pt'";
557 echo ">";
558 $cell_count += $datacols;
561 ++$item_count;
562 echo generate_display_field($frow, $currvalue);
565 disp_end_group();
568 // From the currently posted HTML form, this gets the value of the
569 // field corresponding to the provided layout_options table row.
571 function get_layout_form_value($frow) {
572 $data_type = $frow['data_type'];
573 $field_id = $frow['field_id'];
574 $value = '';
575 if (isset($_POST["form_$field_id"])) {
576 if ($data_type == 21) {
577 // $_POST["form_$field_id"] is an array of checkboxes and its keys
578 // must be concatenated into a |-separated string.
579 foreach ($_POST["form_$field_id"] as $key => $val) {
580 if (strlen($value)) $value .= '|';
581 $value .= $key;
584 else if ($data_type == 22) {
585 // $_POST["form_$field_id"] is an array of text fields to be imploded
586 // into "key:value|key:value|...".
587 foreach ($_POST["form_$field_id"] as $key => $val) {
588 $val = str_replace('|', ' ', $val);
589 if (strlen($value)) $value .= '|';
590 $value .= "$key:$val";
593 else if ($data_type == 23) {
594 // $_POST["form_$field_id"] is an array of text fields with companion
595 // radio buttons to be imploded into "key:n:notes|key:n:notes|...".
596 foreach ($_POST["form_$field_id"] as $key => $val) {
597 $restype = $_POST["radio_{$field_id}"][$key];
598 if (empty($restype)) $restype = '0';
599 $val = str_replace('|', ' ', $val);
600 if (strlen($value)) $value .= '|';
601 $value .= "$key:$restype:$val";
604 else {
605 $value = $_POST["form_$field_id"];
608 return $value;