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 " .
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))
39 echo ">" . $lrow['title'] . "</option>\n";
41 if (!$got_selected && strlen($currvalue) > 0) {
42 echo "<option value='$currescaped' selected>* $currescaped *</option>";
44 echo " <font color='red' title='Please choose a valid selection " .
45 "from the list'>Fix this!</font>";
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)'";
65 // long or multi-line text field
66 else if ($data_type == 3) {
68 " name='form_$field_id'" .
69 " title='$description'" .
70 " cols='" . $frow['fld_length'] . "'" .
71 " rows='" . $frow['max_length'] . "'>" .
72 $currescaped . "</textarea>";
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>";
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>";
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)) {
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>";
138 else if ($data_type == 13) {
139 echo "<select name='form_$field_id' title='$description'>";
140 echo "<option value=''> </option>";
141 $squads = acl_get_squads();
143 foreach ($squads as $key => $value) {
144 echo "<option value='$key'";
145 if ($currvalue == $key) echo " selected";
146 echo ">" . $value[3] . "</option>\n";
152 // Address book, preferring organization name if it exists and is not in
153 // parentheses, and excluding local users who are not providers.
154 // Supports "referred to" practitioners and facilities.
155 else if ($data_type == 14) {
156 $ures = sqlStatement("SELECT id, fname, lname, organization FROM users " .
157 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
158 "AND ( username = '' OR authorized = 1 ) " .
159 "ORDER BY organization, lname, fname");
160 echo "<select name='form_$field_id' title='$description'>";
161 echo "<option value=''>" . xl('Unassigned') . "</option>";
162 while ($urow = sqlFetchArray($ures)) {
163 $uname = $urow['organization'];
164 if (empty($uname) ||
substr($uname, 0, 1) == '(') {
165 $uname = $urow['lname'];
166 if ($urow['fname']) $uname .= ", " . $urow['fname'];
168 echo "<option value='" . $urow['id'] . "'";
169 if ($urow['id'] == $currvalue) echo " selected";
170 echo ">$uname</option>";
175 // a billing code (only one of these allowed!)
176 else if ($data_type == 15) {
177 echo "<input type='text'" .
178 " name='form_$field_id'" .
179 " id='form_related_code'" .
180 " size='" . $frow['fld_length'] . "'" .
181 " maxlength='" . $frow['max_length'] . "'" .
182 " title='$description'" .
183 " value='$currescaped'" .
184 " onclick='sel_related()' readonly" .
188 // a set of labeled checkboxes
189 else if ($data_type == 21) {
190 // In this special case, fld_length is the number of columns generated.
191 $cols = max(1, $frow['fld_length']);
192 $avalue = explode('|', $currvalue);
193 $lres = sqlStatement("SELECT * FROM list_options " .
194 "WHERE list_id = '$list_id' ORDER BY seq");
195 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
196 $tdpct = (int) (100 / $cols);
197 for ($count = 0; $lrow = sqlFetchArray($lres); ++
$count) {
198 $option_id = $lrow['option_id'];
199 // if ($count) echo "<br />";
200 if ($count %
$cols == 0) {
201 if ($count) echo "</tr>";
204 echo "<td width='$tdpct%'>";
205 echo "<input type='checkbox' name='form_{$field_id}[$option_id]' value='1'";
206 if (in_array($option_id, $avalue)) echo " checked";
207 echo ">" . $lrow['title'];
212 if ($count > $cols) {
213 // Add some space after multiple rows of checkboxes.
214 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
220 // a set of labeled text input fields
221 else if ($data_type == 22) {
222 $tmp = explode('|', $currvalue);
224 foreach ($tmp as $value) {
225 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
226 $avalue[$matches[1]] = $matches[2];
229 $lres = sqlStatement("SELECT * FROM list_options " .
230 "WHERE list_id = '$list_id' ORDER BY seq");
231 echo "<table cellpadding='0' cellspacing='0'>";
232 while ($lrow = sqlFetchArray($lres)) {
233 $option_id = $lrow['option_id'];
234 $maxlength = empty($frow['max_length']) ?
255 : $frow['max_length'];
235 $fldlength = empty($frow['fld_length']) ?
20 : $frow['fld_length'];
236 echo "<tr><td>" . $lrow['title'] . " </td>";
237 echo "<td><input type='text'" .
238 " name='form_{$field_id}[$option_id]'" .
239 " size='$fldlength'" .
240 " maxlength='$maxlength'" .
241 " value='" . $avalue[$option_id] . "'";
242 echo " /></td></tr>";
247 // a set of exam results; 3 radio buttons and a text field:
248 else if ($data_type == 23) {
249 $tmp = explode('|', $currvalue);
251 foreach ($tmp as $value) {
252 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
253 $avalue[$matches[1]] = $matches[2];
256 $maxlength = empty($frow['max_length']) ?
255 : $frow['max_length'];
257 $fldlength = empty($frow['fld_length']) ?
20 : $frow['fld_length'];
258 $lres = sqlStatement("SELECT * FROM list_options " .
259 "WHERE list_id = '$list_id' ORDER BY seq");
260 echo "<table cellpadding='0' cellspacing='0'>";
261 echo "<tr><td> </td><td class='bold'>N/A </td><td class='bold'>Nor </td>" .
262 "<td class='bold'>Abn </td><td class='bold'>Date/Notes</td></tr>";
263 while ($lrow = sqlFetchArray($lres)) {
264 $option_id = $lrow['option_id'];
265 $restype = substr($avalue[$option_id], 0, 1);
266 $resnote = substr($avalue[$option_id], 2);
267 echo "<tr><td>" . $lrow['title'] . " </td>";
268 for ($i = 0; $i < 3; ++
$i) {
269 echo "<td><input type='radio'" .
270 " name='radio_{$field_id}[$option_id]'" .
272 if ($restype === "$i") echo " checked";
275 echo "<td><input type='text'" .
276 " name='form_{$field_id}[$option_id]'" .
277 " size='$fldlength'" .
278 " maxlength='$maxlength'" .
279 " value='$resnote' /></td>";
285 // the list of active allergies for the current patient
286 // this is read-only!
287 else if ($data_type == 24) {
288 $query = "SELECT title, comments FROM lists WHERE " .
289 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
291 // echo "<!-- $query -->\n"; // debugging
292 $lres = sqlStatement($query);
294 while ($lrow = sqlFetchArray($lres)) {
295 if ($count++
) echo "<br />";
297 if ($lrow['comments']) echo ' (' . $lrow['comments'] . ')';
301 // a set of labeled checkboxes, each with a text field:
302 else if ($data_type == 25) {
303 $tmp = explode('|', $currvalue);
305 foreach ($tmp as $value) {
306 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
307 $avalue[$matches[1]] = $matches[2];
310 $maxlength = empty($frow['max_length']) ?
255 : $frow['max_length'];
311 $fldlength = empty($frow['fld_length']) ?
20 : $frow['fld_length'];
312 $lres = sqlStatement("SELECT * FROM list_options " .
313 "WHERE list_id = '$list_id' ORDER BY seq");
314 echo "<table cellpadding='0' cellspacing='0'>";
315 while ($lrow = sqlFetchArray($lres)) {
316 $option_id = $lrow['option_id'];
317 $restype = substr($avalue[$option_id], 0, 1);
318 $resnote = substr($avalue[$option_id], 2);
319 echo "<tr><td>" . $lrow['title'] . " </td>";
320 echo "<td><input type='checkbox' name='check_{$field_id}[$option_id]' value='1'";
321 if ($restype) echo " checked";
322 echo " /> </td>";
323 echo "<td><input type='text'" .
324 " name='form_{$field_id}[$option_id]'" .
325 " size='$fldlength'" .
326 " maxlength='$maxlength'" .
327 " value='$resnote' /></td>";
335 function generate_display_field($frow, $currvalue) {
336 $data_type = $frow['data_type'];
337 $field_id = $frow['field_id'];
338 $list_id = $frow['list_id'];
341 // generic selection list
342 if ($data_type == 1) {
343 $lrow = sqlQuery("SELECT title FROM list_options " .
344 "WHERE list_id = '$list_id' AND option_id = '$currvalue'");
349 else if ($data_type == 2) {
353 // long or multi-line text field
354 else if ($data_type == 3) {
355 $s = nl2br($currvalue);
359 else if ($data_type == 4) {
364 else if ($data_type == 10 ||
$data_type == 11) {
365 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
366 "WHERE id = '$currvalue'");
367 $s = ucwords($urow['fname'] . " " . $urow['lname']);
371 else if ($data_type == 12) {
372 $pres = get_pharmacies();
373 while ($prow = sqlFetchArray($pres)) {
375 if ($currvalue == $key) {
376 $s .= $prow['name'] . ' ' . $prow['area_code'] . '-' .
377 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
378 $prow['line1'] . ' / ' . $prow['city'];
384 else if ($data_type == 13) {
385 $squads = acl_get_squads();
387 foreach ($squads as $key => $value) {
388 if ($currvalue == $key) {
396 else if ($data_type == 14) {
397 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
398 "WHERE id = '$currvalue'");
399 $uname = $urow['lname'];
400 if ($urow['fname']) $uname .= ", " . $urow['fname'];
405 else if ($data_type == 15) {
409 // a set of labeled checkboxes
410 else if ($data_type == 21) {
411 $avalue = explode('|', $currvalue);
412 $lres = sqlStatement("SELECT * FROM list_options " .
413 "WHERE list_id = '$list_id' ORDER BY seq");
415 while ($lrow = sqlFetchArray($lres)) {
416 $option_id = $lrow['option_id'];
417 if (in_array($option_id, $avalue)) {
418 if ($count++
) $s .= "<br />";
419 $s .= $lrow['title'];
424 // a set of labeled text input fields
425 else if ($data_type == 22) {
426 $tmp = explode('|', $currvalue);
428 foreach ($tmp as $value) {
429 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
430 $avalue[$matches[1]] = $matches[2];
433 $lres = sqlStatement("SELECT * FROM list_options " .
434 "WHERE list_id = '$list_id' ORDER BY seq");
435 $s .= "<table cellpadding='0' cellspacing='0'>";
436 while ($lrow = sqlFetchArray($lres)) {
437 $option_id = $lrow['option_id'];
438 if (empty($avalue[$option_id])) continue;
439 $s .= "<tr><td class='bold' valign='top'>" . $lrow['title'] . ": </td>";
440 $s .= "<td class='text' valign='top'>" . $avalue[$option_id] . "</td></tr>";
445 // a set of exam results; 3 radio buttons and a text field:
446 else if ($data_type == 23) {
447 $tmp = explode('|', $currvalue);
449 foreach ($tmp as $value) {
450 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
451 $avalue[$matches[1]] = $matches[2];
454 $lres = sqlStatement("SELECT * FROM list_options " .
455 "WHERE list_id = '$list_id' ORDER BY seq");
456 $s .= "<table cellpadding='0' cellspacing='0'>";
457 while ($lrow = sqlFetchArray($lres)) {
458 $option_id = $lrow['option_id'];
459 $restype = substr($avalue[$option_id], 0, 1);
460 $resnote = substr($avalue[$option_id], 2);
461 if (empty($restype) && empty($resnote)) continue;
462 $s .= "<tr><td class='bold' valign='top'>" . $lrow['title'] . " </td>";
463 $restype = ($restype == '1') ?
'Normal' : (($restype == '2') ?
'Abnormal' : 'N/A');
464 $s .= "<td class='text' valign='top'>$restype</td></tr>";
465 $s .= "<td class='text' valign='top'>$resnote</td></tr>";
471 // the list of active allergies for the current patient
472 else if ($data_type == 24) {
473 $query = "SELECT title, comments FROM lists WHERE " .
474 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
476 // echo "<!-- $query -->\n"; // debugging
477 $lres = sqlStatement($query);
479 while ($lrow = sqlFetchArray($lres)) {
480 if ($count++
) $s .= "<br />";
481 $s .= $lrow['title'];
482 if ($lrow['comments']) $s .= ' (' . $lrow['comments'] . ')';
486 // a set of labeled checkboxes, each with a text field:
487 else if ($data_type == 25) {
488 $tmp = explode('|', $currvalue);
490 foreach ($tmp as $value) {
491 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
492 $avalue[$matches[1]] = $matches[2];
495 $lres = sqlStatement("SELECT * FROM list_options " .
496 "WHERE list_id = '$list_id' ORDER BY seq");
497 $s .= "<table cellpadding='0' cellspacing='0'>";
498 while ($lrow = sqlFetchArray($lres)) {
499 $option_id = $lrow['option_id'];
500 $restype = substr($avalue[$option_id], 0, 1);
501 $resnote = substr($avalue[$option_id], 2);
502 if (empty($restype) && empty($resnote)) continue;
503 $s .= "<tr><td class='bold' valign='top'>" . $lrow['title'] . " </td>";
504 $restype = $restype ?
'Yes' : 'No';
505 $s .= "<td class='text' valign='top'>$restype</td></tr>";
506 $s .= "<td class='text' valign='top'>$resnote</td></tr>";
515 $CPR = 4; // cells per row of generic data
520 function disp_end_cell() {
521 global $item_count, $cell_count;
522 if ($item_count > 0) {
528 function disp_end_row() {
529 global $cell_count, $CPR;
531 if ($cell_count > 0) {
532 for (; $cell_count < $CPR; ++
$cell_count) echo "<td></td>";
538 function disp_end_group() {
540 if (strlen($last_group) > 0) {
545 function display_layout_rows($formtype, $result1, $result2='') {
546 global $item_count, $cell_count, $last_group, $CPR;
548 $fres = sqlStatement("SELECT * FROM layout_options " .
549 "WHERE form_id = '$formtype' AND uor > 0 " .
550 "ORDER BY group_name, seq");
552 while ($frow = sqlFetchArray($fres)) {
553 $this_group = $frow['group_name'];
554 $titlecols = $frow['titlecols'];
555 $datacols = $frow['datacols'];
556 $data_type = $frow['data_type'];
557 $field_id = $frow['field_id'];
558 $list_id = $frow['list_id'];
561 if ($formtype == 'DEM') {
562 if ($GLOBALS['athletic_team']) {
563 // Skip fitness level and return-to-play date because those appear
564 // in a special display/update form on this page.
565 if ($field_id === 'fitness' ||
$field_id === 'userdate1') continue;
567 if (strpos($field_id, 'em_') === 0) {
568 $tmp = substr($field_id, 3);
569 if (isset($result2[$tmp])) $currvalue = $result2[$tmp];
572 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
576 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
579 // Handle a data category (group) change.
580 if (strcmp($this_group, $last_group) != 0) {
582 $group_name = substr($this_group, 1);
583 $last_group = $this_group;
586 // Handle starting of a new row.
587 if (($titlecols > 0 && $cell_count >= $CPR) ||
$cell_count == 0) {
591 echo "<td class='groupname'>";
592 //echo "<td class='groupname' style='padding-right:5pt' valign='top'>";
593 //echo "<font color='#008800'>$group_name</font>";
597 //echo "<td class='' style='padding-right:5pt' valign='top'>";
603 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
605 // Handle starting of a new label cell.
606 if ($titlecols > 0) {
608 //echo "<td class='label' colspan='$titlecols' valign='top'";
609 echo "<td class='label' colspan='$titlecols' ";
610 //if ($cell_count == 2) echo " style='padding-left:10pt'";
612 $cell_count +
= $titlecols;
616 if ($frow['title']) echo $frow['title'] . ":"; else echo " ";
618 // Handle starting of a new data cell.
621 //echo "<td class='text data' colspan='$datacols' valign='top'";
622 echo "<td class='text data' colspan='$datacols'";
623 //if ($cell_count > 0) echo " style='padding-left:5pt'";
625 $cell_count +
= $datacols;
629 echo generate_display_field($frow, $currvalue);
635 // From the currently posted HTML form, this gets the value of the
636 // field corresponding to the provided layout_options table row.
638 function get_layout_form_value($frow) {
639 $data_type = $frow['data_type'];
640 $field_id = $frow['field_id'];
642 if (isset($_POST["form_$field_id"])) {
643 if ($data_type == 21) {
644 // $_POST["form_$field_id"] is an array of checkboxes and its keys
645 // must be concatenated into a |-separated string.
646 foreach ($_POST["form_$field_id"] as $key => $val) {
647 if (strlen($value)) $value .= '|';
651 else if ($data_type == 22) {
652 // $_POST["form_$field_id"] is an array of text fields to be imploded
653 // into "key:value|key:value|...".
654 foreach ($_POST["form_$field_id"] as $key => $val) {
655 $val = str_replace('|', ' ', $val);
656 if (strlen($value)) $value .= '|';
657 $value .= "$key:$val";
660 else if ($data_type == 23) {
661 // $_POST["form_$field_id"] is an array of text fields with companion
662 // radio buttons to be imploded into "key:n:notes|key:n:notes|...".
663 foreach ($_POST["form_$field_id"] as $key => $val) {
664 $restype = $_POST["radio_{$field_id}"][$key];
665 if (empty($restype)) $restype = '0';
666 $val = str_replace('|', ' ', $val);
667 if (strlen($value)) $value .= '|';
668 $value .= "$key:$restype:$val";
671 else if ($data_type == 25) {
672 // $_POST["form_$field_id"] is an array of text fields with companion
673 // checkboxes to be imploded into "key:n:notes|key:n:notes|...".
674 foreach ($_POST["form_$field_id"] as $key => $val) {
675 $restype = empty($_POST["check_{$field_id}"][$key]) ?
'0' : '1';
676 $val = str_replace('|', ' ', $val);
677 if (strlen($value)) $value .= '|';
678 $value .= "$key:$restype:$val";
682 $value = $_POST["form_$field_id"];
688 // Generate JavaScript validation logic for the required fields.
690 function generate_layout_validation($form_id) {
691 $fres = sqlStatement("SELECT * FROM layout_options " .
692 "WHERE form_id = '$form_id' AND uor > 0 AND field_id != '' " .
693 "ORDER BY group_name, seq");
695 while ($frow = sqlFetchArray($fres)) {
696 if ($frow['uor'] < 2) continue;
697 $data_type = $frow['data_type'];
698 $field_id = $frow['field_id'];
699 $fldtitle = $frow['title'];
700 if (!$fldtitle) $fldtitle = $frow['description'];
701 $fldname = "form_$field_id";
709 " if (f.$fldname.selectedIndex <= 0) {\n" .
710 " alert('Please choose a value for $fldtitle');\n" .
711 " if (f.$fldname.focus) f.$fldname.focus();\n" .
720 " if (trimlen(f.$fldname.value) == 0) {\n" .
721 " alert('Please enter a value for $fldtitle');\n" .
722 " if (f.$fldname.focus) f.$fldname.focus();\n" .