merged from head the following: use of formTrim functionto fix quote handling when...
[openemr.git] / library / options.inc.php
blob7dbdc6a1259f5582b04f833153af0ca4e3cb12d6
1 <?php
2 // Copyright (C) 2007-2009 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 // Functions for managing the lists and layouts
11 // Note: there are translation wrappers for the lists and layout labels
12 // at library/translation.inc.php. The functions are titled
13 // xl_list_label() and xl_layout_label() and are controlled by the
14 // $GLOBALS['translate_lists'] and $GLOBALS['translate_layout']
15 // flags in globals.php
17 require_once("formdata.inc.php");
19 $date_init = "";
21 function get_pharmacies() {
22 return sqlStatement("SELECT d.id, d.name, a.line1, a.city, " .
23 "p.area_code, p.prefix, p.number FROM pharmacies AS d " .
24 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = d.id " .
25 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = d.id " .
26 "AND p.type = 2 " .
27 "ORDER BY name, area_code, prefix, number");
30 function generate_form_field($frow, $currvalue) {
31 global $rootdir, $date_init;
33 $currescaped = htmlspecialchars($currvalue, ENT_QUOTES);
35 $data_type = $frow['data_type'];
36 $field_id = $frow['field_id'];
37 $list_id = $frow['list_id'];
39 // Added 5-09 by BM - Translate description if applicable
40 $description = htmlspecialchars(xl_layout_label($frow['description']), ENT_QUOTES);
42 // added 5-2009 by BM to allow modification of the 'empty' text title field.
43 // Can pass $frow['empty_title'] with this variable, otherwise
44 // will default to 'Unassigned'.
45 // modified 6-2009 by BM to allow complete skipping of the 'empty' text title
46 // if make $frow['empty_title'] equal to 'SKIP'
47 $showEmpty = true;
48 if (isset($frow['empty_title'])) {
49 if ($frow['empty_title'] == "SKIP") {
50 //do not display an 'empty' choice
51 $showEmpty = false;
52 $empty_title = "Unassigned";
54 else {
55 $empty_title = $frow['empty_title'];
58 else {
59 $empty_title = "Unassigned";
62 // generic single-selection list
63 if ($data_type == 1) {
64 echo "<select name='form_$field_id' id='form_$field_id' title='$description'>";
65 if ($showEmpty) echo "<option value=''>" . xl($empty_title) . "</option>";
66 $lres = sqlStatement("SELECT * FROM list_options " .
67 "WHERE list_id = '$list_id' ORDER BY seq, title");
68 $got_selected = FALSE;
69 while ($lrow = sqlFetchArray($lres)) {
70 echo "<option value='" . $lrow['option_id'] . "'";
71 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
72 (strlen($currvalue) > 0 && $lrow['option_id'] == $currvalue))
74 echo " selected";
75 $got_selected = TRUE;
78 // Added 5-09 by BM - Translate label if applicable
79 echo ">" . xl_list_label($lrow['title']) . "</option>\n";
82 if (!$got_selected && strlen($currvalue) > 0) {
83 echo "<option value='$currescaped' selected>* $currescaped *</option>";
84 echo "</select>";
85 echo " <font color='red' title='" . xl('Please choose a valid selection from the list.') . "'>" . xl('Fix this') . "!</font>";
87 else {
88 echo "</select>";
92 // simple text field
93 else if ($data_type == 2) {
94 echo "<input type='text'" .
95 " name='form_$field_id'" .
96 " id='form_$field_id'" .
97 " size='" . $frow['fld_length'] . "'" .
98 " maxlength='" . $frow['max_length'] . "'" .
99 " title='$description'" .
100 " value='$currescaped'";
101 if (strpos($frow['edit_options'], 'C') !== FALSE)
102 echo " onchange='capitalizeMe(this)'";
103 echo " />";
106 // long or multi-line text field
107 else if ($data_type == 3) {
108 echo "<textarea" .
109 " name='form_$field_id'" .
110 " id='form_$field_id'" .
111 " title='$description'" .
112 " cols='" . $frow['fld_length'] . "'" .
113 " rows='" . $frow['max_length'] . "'>" .
114 $currescaped . "</textarea>";
117 // date
118 else if ($data_type == 4) {
119 echo "<input type='text' size='10' name='form_$field_id' id='form_$field_id'" .
120 " value='$currescaped'" .
121 " title='$description'" .
122 " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />" .
123 "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
124 " id='img_$field_id' border='0' alt='[?]' style='cursor:pointer'" .
125 " title='" . xl('Click here to choose a date') . "' />";
126 $date_init .= " Calendar.setup({inputField:'form_$field_id', ifFormat:'%Y-%m-%d', button:'img_$field_id'});\n";
129 // provider list, local providers only
130 else if ($data_type == 10) {
131 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
132 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
133 "AND authorized = 1 " .
134 "ORDER BY lname, fname");
135 echo "<select name='form_$field_id' id='form_$field_id' title='$description'>";
136 echo "<option value=''>" . xl('Unassigned') . "</option>";
137 while ($urow = sqlFetchArray($ures)) {
138 $uname = $urow['fname'] . ' ' . $urow['lname'];
139 echo "<option value='" . $urow['id'] . "'";
140 if ($urow['id'] == $currvalue) echo " selected";
141 echo ">$uname</option>";
143 echo "</select>";
146 // provider list, including address book entries with an NPI number
147 else if ($data_type == 11) {
148 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
149 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
150 "AND ( authorized = 1 OR ( username = '' AND npi != '' ) ) " .
151 "ORDER BY lname, fname");
152 echo "<select name='form_$field_id' id='form_$field_id' title='$description'>";
153 echo "<option value=''>" . xl('Unassigned') . "</option>";
154 while ($urow = sqlFetchArray($ures)) {
155 $uname = $urow['fname'] . ' ' . $urow['lname'];
156 echo "<option value='" . $urow['id'] . "'";
157 if ($urow['id'] == $currvalue) echo " selected";
158 echo ">$uname</option>";
160 echo "</select>";
163 // pharmacy list
164 else if ($data_type == 12) {
165 echo "<select name='form_$field_id' id='form_$field_id' title='$description'>";
166 echo "<option value='0'></option>";
167 $pres = get_pharmacies();
168 while ($prow = sqlFetchArray($pres)) {
169 $key = $prow['id'];
170 echo "<option value='$key'";
171 if ($currvalue == $key) echo " selected";
172 echo '>' . $prow['name'] . ' ' . $prow['area_code'] . '-' .
173 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
174 $prow['line1'] . ' / ' . $prow['city'] . "</option>";
176 echo "</select>";
179 // squads
180 else if ($data_type == 13) {
181 echo "<select name='form_$field_id' id='form_$field_id' title='$description'>";
182 echo "<option value=''>&nbsp;</option>";
183 $squads = acl_get_squads();
184 if ($squads) {
185 foreach ($squads as $key => $value) {
186 echo "<option value='$key'";
187 if ($currvalue == $key) echo " selected";
188 echo ">" . $value[3] . "</option>\n";
191 echo "</select>";
194 // Address book, preferring organization name if it exists and is not in
195 // parentheses, and excluding local users who are not providers.
196 // Supports "referred to" practitioners and facilities.
197 else if ($data_type == 14) {
198 $ures = sqlStatement("SELECT id, fname, lname, organization FROM users " .
199 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
200 "AND ( username = '' OR authorized = 1 ) " .
201 "ORDER BY organization, lname, fname");
202 echo "<select name='form_$field_id' id='form_$field_id' title='$description'>";
203 echo "<option value=''>" . xl('Unassigned') . "</option>";
204 while ($urow = sqlFetchArray($ures)) {
205 $uname = $urow['organization'];
206 if (empty($uname) || substr($uname, 0, 1) == '(') {
207 $uname = $urow['lname'];
208 if ($urow['fname']) $uname .= ", " . $urow['fname'];
210 echo "<option value='" . $urow['id'] . "'";
211 if ($urow['id'] == $currvalue) echo " selected";
212 echo ">$uname</option>";
214 echo "</select>";
217 // a billing code (only one of these allowed!)
218 else if ($data_type == 15) {
219 echo "<input type='text'" .
220 " name='form_$field_id'" .
221 " id='form_related_code'" .
222 " size='" . $frow['fld_length'] . "'" .
223 " maxlength='" . $frow['max_length'] . "'" .
224 " title='$description'" .
225 " value='$currescaped'" .
226 " onclick='sel_related()' readonly" .
227 " />";
230 // a set of labeled checkboxes
231 else if ($data_type == 21) {
232 // In this special case, fld_length is the number of columns generated.
233 $cols = max(1, $frow['fld_length']);
234 $avalue = explode('|', $currvalue);
235 $lres = sqlStatement("SELECT * FROM list_options " .
236 "WHERE list_id = '$list_id' ORDER BY seq, title");
237 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
238 $tdpct = (int) (100 / $cols);
239 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
240 $option_id = $lrow['option_id'];
241 // if ($count) echo "<br />";
242 if ($count % $cols == 0) {
243 if ($count) echo "</tr>";
244 echo "<tr>";
246 echo "<td width='$tdpct%'>";
247 echo "<input type='checkbox' name='form_{$field_id}[$option_id]' id='form_{$field_id}[$option_id]' value='1'";
248 if (in_array($option_id, $avalue)) echo " checked";
250 // Added 5-09 by BM - Translate label if applicable
251 echo ">" . xl_list_label($lrow['title']);
253 echo "</td>";
255 if ($count) {
256 echo "</tr>";
257 if ($count > $cols) {
258 // Add some space after multiple rows of checkboxes.
259 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
262 echo "</table>";
265 // a set of labeled text input fields
266 else if ($data_type == 22) {
267 $tmp = explode('|', $currvalue);
268 $avalue = array();
269 foreach ($tmp as $value) {
270 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
271 $avalue[$matches[1]] = $matches[2];
274 $lres = sqlStatement("SELECT * FROM list_options " .
275 "WHERE list_id = '$list_id' ORDER BY seq, title");
276 echo "<table cellpadding='0' cellspacing='0'>";
277 while ($lrow = sqlFetchArray($lres)) {
278 $option_id = $lrow['option_id'];
279 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
280 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
282 // Added 5-09 by BM - Translate label if applicable
283 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
285 echo "<td><input type='text'" .
286 " name='form_{$field_id}[$option_id]'" .
287 " id='form_{$field_id}[$option_id]'" .
288 " size='$fldlength'" .
289 " maxlength='$maxlength'" .
290 " value='" . $avalue[$option_id] . "'";
291 echo " /></td></tr>";
293 echo "</table>";
296 // a set of exam results; 3 radio buttons and a text field:
297 else if ($data_type == 23) {
298 $tmp = explode('|', $currvalue);
299 $avalue = array();
300 foreach ($tmp as $value) {
301 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
302 $avalue[$matches[1]] = $matches[2];
305 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
306 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
307 $lres = sqlStatement("SELECT * FROM list_options " .
308 "WHERE list_id = '$list_id' ORDER BY seq, title");
309 echo "<table cellpadding='0' cellspacing='0'>";
310 echo "<tr><td>&nbsp;</td><td class='bold'>" . xl('N/A') .
311 "&nbsp;</td><td class='bold'>" . xl('Nor') . "&nbsp;</td>" .
312 "<td class='bold'>" . xl('Abn') . "&nbsp;</td><td class='bold'>" .
313 xl('Date/Notes') . "</td></tr>";
314 while ($lrow = sqlFetchArray($lres)) {
315 $option_id = $lrow['option_id'];
316 $restype = substr($avalue[$option_id], 0, 1);
317 $resnote = substr($avalue[$option_id], 2);
319 // Added 5-09 by BM - Translate label if applicable
320 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
322 for ($i = 0; $i < 3; ++$i) {
323 echo "<td><input type='radio'" .
324 " name='radio_{$field_id}[$option_id]'" .
325 " id='radio_{$field_id}[$option_id]'" .
326 " value='$i'";
327 if ($restype === "$i") echo " checked";
328 echo " /></td>";
330 echo "<td><input type='text'" .
331 " name='form_{$field_id}[$option_id]'" .
332 " id='form_{$field_id}[$option_id]'" .
333 " size='$fldlength'" .
334 " maxlength='$maxlength'" .
335 " value='$resnote' /></td>";
336 echo "</tr>";
338 echo "</table>";
341 // the list of active allergies for the current patient
342 // this is read-only!
343 else if ($data_type == 24) {
344 $query = "SELECT title, comments FROM lists WHERE " .
345 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
346 "ORDER BY begdate";
347 // echo "<!-- $query -->\n"; // debugging
348 $lres = sqlStatement($query);
349 $count = 0;
350 while ($lrow = sqlFetchArray($lres)) {
351 if ($count++) echo "<br />";
352 echo $lrow['title'];
353 if ($lrow['comments']) echo ' (' . $lrow['comments'] . ')';
357 // a set of labeled checkboxes, each with a text field:
358 else if ($data_type == 25) {
359 $tmp = explode('|', $currvalue);
360 $avalue = array();
361 foreach ($tmp as $value) {
362 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
363 $avalue[$matches[1]] = $matches[2];
366 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
367 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
368 $lres = sqlStatement("SELECT * FROM list_options " .
369 "WHERE list_id = '$list_id' ORDER BY seq, title");
370 echo "<table cellpadding='0' cellspacing='0'>";
371 while ($lrow = sqlFetchArray($lres)) {
372 $option_id = $lrow['option_id'];
373 $restype = substr($avalue[$option_id], 0, 1);
374 $resnote = substr($avalue[$option_id], 2);
376 // Added 5-09 by BM - Translate label if applicable
377 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
379 echo "<td><input type='checkbox' name='check_{$field_id}[$option_id]' id='check_{$field_id}[$option_id]' value='1'";
380 if ($restype) echo " checked";
381 echo " />&nbsp;</td>";
382 echo "<td><input type='text'" .
383 " name='form_{$field_id}[$option_id]'" .
384 " id='form_{$field_id}[$option_id]'" .
385 " size='$fldlength'" .
386 " maxlength='$maxlength'" .
387 " value='$resnote' /></td>";
388 echo "</tr>";
390 echo "</table>";
393 // single-selection list with ability to add to it
394 else if ($data_type == 26) {
395 echo "<select class='addtolistclass_$list_id' name='form_$field_id' id='form_$field_id' title='$description'>";
396 if ($showEmpty) echo "<option value=''>" . xl($empty_title) . "</option>";
397 $lres = sqlStatement("SELECT * FROM list_options " .
398 "WHERE list_id = '$list_id' ORDER BY seq, title");
399 $got_selected = FALSE;
400 while ($lrow = sqlFetchArray($lres)) {
401 echo "<option value='" . $lrow['option_id'] . "'";
402 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
403 (strlen($currvalue) > 0 && $lrow['option_id'] == $currvalue))
405 echo " selected";
406 $got_selected = TRUE;
409 // Added 5-09 by BM - Translate label if applicable
410 echo ">" . xl_list_label($lrow['title']) . "</option>\n";
413 if (!$got_selected && strlen($currvalue) > 0) {
414 echo "<option value='$currescaped' selected>* $currescaped *</option>";
415 echo "</select>";
416 echo " <font color='red' title='" . xl('Please choose a valid selection from the list.') . "'>" . xl('Fix this') . "!</font>";
418 else {
419 echo "</select>";
422 // show the add button if user has access to correct list
423 $outputAddButton = "<input type='button' id='addtolistid_".$list_id."' fieldid='form_".$field_id."' class='addtolist' value='" . xl('Add') . "'>";
424 if (aco_exist('lists', $list_id)) {
425 // a specific aco exist for this list, so ensure access
426 if (acl_check('lists', $list_id)) echo $outputAddButton;
428 else {
429 // no specific aco exist for this list, so check for access to 'default' list
430 if (acl_check('lists', 'default')) echo $outputAddButton;
436 function generate_print_field($frow, $currvalue) {
437 global $rootdir, $date_init;
439 $currescaped = htmlspecialchars($currvalue, ENT_QUOTES);
441 $data_type = $frow['data_type'];
442 $field_id = $frow['field_id'];
443 $list_id = $frow['list_id'];
444 $fld_length = $frow['fld_length'];
446 $description = htmlspecialchars(xl_layout_label($frow['description']), ENT_QUOTES);
448 // Can pass $frow['empty_title'] with this variable, otherwise
449 // will default to 'Unassigned'.
450 // If it is 'SKIP' then an empty text title is completely skipped.
451 $showEmpty = true;
452 if (isset($frow['empty_title'])) {
453 if ($frow['empty_title'] == "SKIP") {
454 //do not display an 'empty' choice
455 $showEmpty = false;
456 $empty_title = "Unassigned";
458 else {
459 $empty_title = $frow['empty_title'];
462 else {
463 $empty_title = "Unassigned";
466 // generic single-selection list
467 if ($data_type == 1 || $data_type == 26) {
468 if (empty($fld_length)) {
469 if ($list_id == 'titles') {
470 $fld_length = 3;
471 } else {
472 $fld_length = 10;
475 $tmp = '';
476 if ($currvalue) {
477 $lrow = sqlQuery("SELECT title FROM list_options " .
478 "WHERE list_id = '$list_id' AND option_id = '$currvalue'");
479 $tmp = xl_list_label($lrow['title']);
480 if (empty($tmp)) $tmp = "($currvalue)";
482 /*****************************************************************
483 echo "<input type='text'" .
484 " size='$fld_length'" .
485 " value='$tmp'" .
486 " class='under'" .
487 " />";
488 *****************************************************************/
489 if ($tmp === '') $tmp = '&nbsp;';
490 echo $tmp;
493 // simple text field
494 else if ($data_type == 2 || $data_type == 15) {
495 /*****************************************************************
496 echo "<input type='text'" .
497 " size='$fld_length'" .
498 " value='$currescaped'" .
499 " class='under'" .
500 " />";
501 *****************************************************************/
502 if ($currescaped === '') $currescaped = '&nbsp;';
503 echo $currescaped;
506 // long or multi-line text field
507 else if ($data_type == 3) {
508 echo "<textarea" .
509 " cols='$fld_length'" .
510 " rows='" . $frow['max_length'] . "'>" .
511 $currescaped . "</textarea>";
514 // date
515 else if ($data_type == 4) {
516 /*****************************************************************
517 echo "<input type='text' size='10'" .
518 " value='$currescaped'" .
519 " title='$description'" .
520 " class='under'" .
521 " />";
522 *****************************************************************/
523 if ($currescaped === '') $currescaped = '&nbsp;';
524 echo $currescaped;
527 // provider list
528 else if ($data_type == 10 || $data_type == 11) {
529 $tmp = '';
530 if ($currvalue) {
531 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
532 "WHERE id = '$currvalue'");
533 $tmp = ucwords($urow['fname'] . " " . $urow['lname']);
534 if (empty($tmp)) $tmp = "($currvalue)";
536 /*****************************************************************
537 echo "<input type='text'" .
538 " size='$fld_length'" .
539 " value='$tmp'" .
540 " class='under'" .
541 " />";
542 *****************************************************************/
543 if ($tmp === '') $tmp = '&nbsp;';
544 echo $tmp;
547 // pharmacy list
548 else if ($data_type == 12) {
549 $tmp = '';
550 if ($currvalue) {
551 $pres = get_pharmacies();
552 while ($prow = sqlFetchArray($pres)) {
553 $key = $prow['id'];
554 if ($currvalue == $key) {
555 $tmp = $prow['name'] . ' ' . $prow['area_code'] . '-' .
556 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
557 $prow['line1'] . ' / ' . $prow['city'];
560 if (empty($tmp)) $tmp = "($currvalue)";
562 /*****************************************************************
563 echo "<input type='text'" .
564 " size='$fld_length'" .
565 " value='$tmp'" .
566 " class='under'" .
567 " />";
568 *****************************************************************/
569 if ($tmp === '') $tmp = '&nbsp;';
570 echo $tmp;
573 // squads
574 else if ($data_type == 13) {
575 $tmp = '';
576 if ($currvalue) {
577 $squads = acl_get_squads();
578 if ($squads) {
579 foreach ($squads as $key => $value) {
580 if ($currvalue == $key) {
581 $tmp = $value[3];
585 if (empty($tmp)) $tmp = "($currvalue)";
587 /*****************************************************************
588 echo "<input type='text'" .
589 " size='$fld_length'" .
590 " value='$tmp'" .
591 " class='under'" .
592 " />";
593 *****************************************************************/
594 if ($tmp === '') $tmp = '&nbsp;';
595 echo $tmp;
598 // Address book.
599 else if ($data_type == 14) {
600 $tmp = '';
601 if ($currvalue) {
602 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
603 "WHERE id = '$currvalue'");
604 $uname = $urow['lname'];
605 if ($urow['fname']) $uname .= ", " . $urow['fname'];
606 $tmp = $uname;
607 if (empty($tmp)) $tmp = "($currvalue)";
609 /*****************************************************************
610 echo "<input type='text'" .
611 " size='$fld_length'" .
612 " value='$tmp'" .
613 " class='under'" .
614 " />";
615 *****************************************************************/
616 if ($tmp === '') $tmp = '&nbsp;';
617 echo $tmp;
620 // a set of labeled checkboxes
621 else if ($data_type == 21) {
622 // In this special case, fld_length is the number of columns generated.
623 $cols = max(1, $fld_length);
624 $avalue = explode('|', $currvalue);
625 $lres = sqlStatement("SELECT * FROM list_options " .
626 "WHERE list_id = '$list_id' ORDER BY seq, title");
627 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
628 $tdpct = (int) (100 / $cols);
629 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
630 $option_id = $lrow['option_id'];
631 if ($count % $cols == 0) {
632 if ($count) echo "</tr>";
633 echo "<tr>";
635 echo "<td width='$tdpct%'>";
636 echo "<input type='checkbox'";
637 if (in_array($option_id, $avalue)) echo " checked";
638 echo ">" . xl_list_label($lrow['title']);
639 echo "</td>";
641 if ($count) {
642 echo "</tr>";
643 if ($count > $cols) {
644 // Add some space after multiple rows of checkboxes.
645 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
648 echo "</table>";
651 // a set of labeled text input fields
652 else if ($data_type == 22) {
653 $tmp = explode('|', $currvalue);
654 $avalue = array();
655 foreach ($tmp as $value) {
656 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
657 $avalue[$matches[1]] = $matches[2];
660 $lres = sqlStatement("SELECT * FROM list_options " .
661 "WHERE list_id = '$list_id' ORDER BY seq, title");
662 echo "<table cellpadding='0' cellspacing='0'>";
663 while ($lrow = sqlFetchArray($lres)) {
664 $option_id = $lrow['option_id'];
665 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
666 $fldlength = empty($fld_length) ? 20 : $fld_length;
667 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
668 echo "<td><input type='text'" .
669 " size='$fldlength'" .
670 " value='" . $avalue[$option_id] . "'" .
671 " class='under'" .
672 " /></td></tr>";
674 echo "</table>";
677 // a set of exam results; 3 radio buttons and a text field:
678 else if ($data_type == 23) {
679 $tmp = explode('|', $currvalue);
680 $avalue = array();
681 foreach ($tmp as $value) {
682 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
683 $avalue[$matches[1]] = $matches[2];
686 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
687 $fldlength = empty($fld_length) ? 20 : $fld_length;
688 $lres = sqlStatement("SELECT * FROM list_options " .
689 "WHERE list_id = '$list_id' ORDER BY seq, title");
690 echo "<table cellpadding='0' cellspacing='0'>";
691 echo "<tr><td>&nbsp;</td><td class='bold'>" . xl('N/A') .
692 "&nbsp;</td><td class='bold'>" . xl('Nor') . "&nbsp;</td>" .
693 "<td class='bold'>" . xl('Abn') . "&nbsp;</td><td class='bold'>" .
694 xl('Date/Notes') . "</td></tr>";
695 while ($lrow = sqlFetchArray($lres)) {
696 $option_id = $lrow['option_id'];
697 $restype = substr($avalue[$option_id], 0, 1);
698 $resnote = substr($avalue[$option_id], 2);
699 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
700 for ($i = 0; $i < 3; ++$i) {
701 echo "<td><input type='radio'";
702 if ($restype === "$i") echo " checked";
703 echo " /></td>";
705 echo "<td><input type='text'" .
706 " size='$fldlength'" .
707 " value='$resnote' /></td>" .
708 " class='under'" .
709 "</tr>";
711 echo "</table>";
714 // the list of active allergies for the current patient
715 // this is read-only!
716 else if ($data_type == 24) {
717 $query = "SELECT title, comments FROM lists WHERE " .
718 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
719 "ORDER BY begdate";
720 $lres = sqlStatement($query);
721 $count = 0;
722 while ($lrow = sqlFetchArray($lres)) {
723 if ($count++) echo "<br />";
724 echo $lrow['title'];
725 if ($lrow['comments']) echo ' (' . $lrow['comments'] . ')';
729 // a set of labeled checkboxes, each with a text field:
730 else if ($data_type == 25) {
731 $tmp = explode('|', $currvalue);
732 $avalue = array();
733 foreach ($tmp as $value) {
734 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
735 $avalue[$matches[1]] = $matches[2];
738 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
739 $fldlength = empty($fld_length) ? 20 : $fld_length;
740 $lres = sqlStatement("SELECT * FROM list_options " .
741 "WHERE list_id = '$list_id' ORDER BY seq, title");
742 echo "<table cellpadding='0' cellspacing='0'>";
743 while ($lrow = sqlFetchArray($lres)) {
744 $option_id = $lrow['option_id'];
745 $restype = substr($avalue[$option_id], 0, 1);
746 $resnote = substr($avalue[$option_id], 2);
747 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
748 echo "<td><input type='checkbox'";
749 if ($restype) echo " checked";
750 echo " />&nbsp;</td>";
751 echo "<td><input type='text'" .
752 " size='$fldlength'" .
753 " value='$resnote'" .
754 " class='under'" .
755 " /></td>" .
756 "</tr>";
758 echo "</table>";
763 function generate_display_field($frow, $currvalue) {
764 $data_type = $frow['data_type'];
765 $field_id = $frow['field_id'];
766 $list_id = $frow['list_id'];
767 $s = '';
769 // generic selection list or the generic selection list with add on the fly feature
770 if ($data_type == 1 || $data_type == 26) {
771 $lrow = sqlQuery("SELECT title FROM list_options " .
772 "WHERE list_id = '$list_id' AND option_id = '$currvalue'");
774 // Added 5-09 by BM - Translate label if applicable
775 $s = xl_list_label($lrow['title']);
779 // simple text field
780 else if ($data_type == 2) {
781 $s = $currvalue;
784 // long or multi-line text field
785 else if ($data_type == 3) {
786 $s = nl2br($currvalue);
789 // date
790 else if ($data_type == 4) {
791 $s = $currvalue;
794 // provider
795 else if ($data_type == 10 || $data_type == 11) {
796 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
797 "WHERE id = '$currvalue'");
798 $s = ucwords($urow['fname'] . " " . $urow['lname']);
801 // pharmacy list
802 else if ($data_type == 12) {
803 $pres = get_pharmacies();
804 while ($prow = sqlFetchArray($pres)) {
805 $key = $prow['id'];
806 if ($currvalue == $key) {
807 $s .= $prow['name'] . ' ' . $prow['area_code'] . '-' .
808 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
809 $prow['line1'] . ' / ' . $prow['city'];
814 // squads
815 else if ($data_type == 13) {
816 $squads = acl_get_squads();
817 if ($squads) {
818 foreach ($squads as $key => $value) {
819 if ($currvalue == $key) {
820 $s .= $value[3];
826 // address book
827 else if ($data_type == 14) {
828 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
829 "WHERE id = '$currvalue'");
830 $uname = $urow['lname'];
831 if ($urow['fname']) $uname .= ", " . $urow['fname'];
832 $s = $uname;
835 // billing code
836 else if ($data_type == 15) {
837 $s = $currvalue;
840 // a set of labeled checkboxes
841 else if ($data_type == 21) {
842 $avalue = explode('|', $currvalue);
843 $lres = sqlStatement("SELECT * FROM list_options " .
844 "WHERE list_id = '$list_id' ORDER BY seq, title");
845 $count = 0;
846 while ($lrow = sqlFetchArray($lres)) {
847 $option_id = $lrow['option_id'];
848 if (in_array($option_id, $avalue)) {
849 if ($count++) $s .= "<br />";
851 // Added 5-09 by BM - Translate label if applicable
852 $s .= xl_list_label($lrow['title']);
858 // a set of labeled text input fields
859 else if ($data_type == 22) {
860 $tmp = explode('|', $currvalue);
861 $avalue = array();
862 foreach ($tmp as $value) {
863 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
864 $avalue[$matches[1]] = $matches[2];
867 $lres = sqlStatement("SELECT * FROM list_options " .
868 "WHERE list_id = '$list_id' ORDER BY seq, title");
869 $s .= "<table cellpadding='0' cellspacing='0'>";
870 while ($lrow = sqlFetchArray($lres)) {
871 $option_id = $lrow['option_id'];
872 if (empty($avalue[$option_id])) continue;
874 // Added 5-09 by BM - Translate label if applicable
875 $s .= "<tr><td class='bold' valign='top'>" . xl_list_label($lrow['title']) . ":&nbsp;</td>";
877 $s .= "<td class='text' valign='top'>" . $avalue[$option_id] . "</td></tr>";
879 $s .= "</table>";
882 // a set of exam results; 3 radio buttons and a text field:
883 else if ($data_type == 23) {
884 $tmp = explode('|', $currvalue);
885 $avalue = array();
886 foreach ($tmp as $value) {
887 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
888 $avalue[$matches[1]] = $matches[2];
891 $lres = sqlStatement("SELECT * FROM list_options " .
892 "WHERE list_id = '$list_id' ORDER BY seq, title");
893 $s .= "<table cellpadding='0' cellspacing='0'>";
894 while ($lrow = sqlFetchArray($lres)) {
895 $option_id = $lrow['option_id'];
896 $restype = substr($avalue[$option_id], 0, 1);
897 $resnote = substr($avalue[$option_id], 2);
898 if (empty($restype) && empty($resnote)) continue;
900 // Added 5-09 by BM - Translate label if applicable
901 $s .= "<tr><td class='bold' valign='top'>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
903 $restype = ($restype == '1') ? xl('Normal') : (($restype == '2') ? xl('Abnormal') : xl('N/A'));
904 $s .= "<td class='text' valign='top'>$restype</td></tr>";
905 $s .= "<td class='text' valign='top'>$resnote</td></tr>";
906 $s .= "</tr>";
908 $s .= "</table>";
911 // the list of active allergies for the current patient
912 else if ($data_type == 24) {
913 $query = "SELECT title, comments FROM lists WHERE " .
914 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
915 "ORDER BY begdate";
916 // echo "<!-- $query -->\n"; // debugging
917 $lres = sqlStatement($query);
918 $count = 0;
919 while ($lrow = sqlFetchArray($lres)) {
920 if ($count++) $s .= "<br />";
921 $s .= $lrow['title'];
922 if ($lrow['comments']) $s .= ' (' . $lrow['comments'] . ')';
926 // a set of labeled checkboxes, each with a text field:
927 else if ($data_type == 25) {
928 $tmp = explode('|', $currvalue);
929 $avalue = array();
930 foreach ($tmp as $value) {
931 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
932 $avalue[$matches[1]] = $matches[2];
935 $lres = sqlStatement("SELECT * FROM list_options " .
936 "WHERE list_id = '$list_id' ORDER BY seq, title");
937 $s .= "<table cellpadding='0' cellspacing='0'>";
938 while ($lrow = sqlFetchArray($lres)) {
939 $option_id = $lrow['option_id'];
940 $restype = substr($avalue[$option_id], 0, 1);
941 $resnote = substr($avalue[$option_id], 2);
942 if (empty($restype) && empty($resnote)) continue;
944 // Added 5-09 by BM - Translate label if applicable
945 $s .= "<tr><td class='bold' valign='top'>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
947 $restype = $restype ? xl('Yes') : xl('No');
948 $s .= "<td class='text' valign='top'>$restype</td></tr>";
949 $s .= "<td class='text' valign='top'>$resnote</td></tr>";
950 $s .= "</tr>";
952 $s .= "</table>";
955 return $s;
958 $CPR = 4; // cells per row of generic data
959 $last_group = '';
960 $cell_count = 0;
961 $item_count = 0;
963 function disp_end_cell() {
964 global $item_count, $cell_count;
965 if ($item_count > 0) {
966 echo "</td>";
967 $item_count = 0;
971 function disp_end_row() {
972 global $cell_count, $CPR;
973 disp_end_cell();
974 if ($cell_count > 0) {
975 for (; $cell_count < $CPR; ++$cell_count) echo "<td></td>";
976 echo "</tr>\n";
977 $cell_count = 0;
981 function disp_end_group() {
982 global $last_group;
983 if (strlen($last_group) > 0) {
984 disp_end_row();
988 function display_layout_rows($formtype, $result1, $result2='') {
989 global $item_count, $cell_count, $last_group, $CPR;
991 $fres = sqlStatement("SELECT * FROM layout_options " .
992 "WHERE form_id = '$formtype' AND uor > 0 " .
993 "ORDER BY group_name, seq");
995 while ($frow = sqlFetchArray($fres)) {
996 $this_group = $frow['group_name'];
997 $titlecols = $frow['titlecols'];
998 $datacols = $frow['datacols'];
999 $data_type = $frow['data_type'];
1000 $field_id = $frow['field_id'];
1001 $list_id = $frow['list_id'];
1002 $currvalue = '';
1004 if ($formtype == 'DEM') {
1005 if ($GLOBALS['athletic_team']) {
1006 // Skip fitness level and return-to-play date because those appear
1007 // in a special display/update form on this page.
1008 if ($field_id === 'fitness' || $field_id === 'userdate1') continue;
1010 if (strpos($field_id, 'em_') === 0) {
1011 // Skip employer related fields, if it's disabled.
1012 if ($GLOBALS['omit_employers']) continue;
1013 $tmp = substr($field_id, 3);
1014 if (isset($result2[$tmp])) $currvalue = $result2[$tmp];
1016 else {
1017 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
1020 else {
1021 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
1024 // Handle a data category (group) change.
1025 if (strcmp($this_group, $last_group) != 0) {
1026 $group_name = substr($this_group, 1);
1027 // totally skip generating the employer category, if it's disabled.
1028 if ($group_name === 'Employer' && $GLOBALS['omit_employers']) continue;
1029 disp_end_group();
1030 $last_group = $this_group;
1033 // Handle starting of a new row.
1034 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
1035 disp_end_row();
1036 echo "<tr>";
1037 if ($group_name) {
1038 echo "<td class='groupname'>";
1039 //echo "<td class='groupname' style='padding-right:5pt' valign='top'>";
1040 //echo "<font color='#008800'>$group_name</font>";
1042 // Added 5-09 by BM - Translate label if applicable
1043 echo (xl_layout_label($group_name));
1045 $group_name = '';
1046 } else {
1047 //echo "<td class='' style='padding-right:5pt' valign='top'>";
1048 echo '<td>&nbsp;';
1050 echo "</td>";
1053 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
1055 // Handle starting of a new label cell.
1056 if ($titlecols > 0) {
1057 disp_end_cell();
1058 //echo "<td class='label' colspan='$titlecols' valign='top'";
1059 echo "<td class='label' colspan='$titlecols' ";
1060 //if ($cell_count == 2) echo " style='padding-left:10pt'";
1061 echo ">";
1062 $cell_count += $titlecols;
1064 ++$item_count;
1066 // Added 5-09 by BM - Translate label if applicable
1067 if ($frow['title']) echo (xl_layout_label($frow['title']).":"); else echo "&nbsp;";
1069 // Handle starting of a new data cell.
1070 if ($datacols > 0) {
1071 disp_end_cell();
1072 //echo "<td class='text data' colspan='$datacols' valign='top'";
1073 echo "<td class='text data' colspan='$datacols'";
1074 //if ($cell_count > 0) echo " style='padding-left:5pt'";
1075 echo ">";
1076 $cell_count += $datacols;
1079 ++$item_count;
1080 echo generate_display_field($frow, $currvalue);
1083 disp_end_group();
1086 // From the currently posted HTML form, this gets the value of the
1087 // field corresponding to the provided layout_options table row.
1089 function get_layout_form_value($frow) {
1090 $data_type = $frow['data_type'];
1091 $field_id = $frow['field_id'];
1092 $value = '';
1093 if (isset($_POST["form_$field_id"])) {
1094 if ($data_type == 21) {
1095 // $_POST["form_$field_id"] is an array of checkboxes and its keys
1096 // must be concatenated into a |-separated string.
1097 foreach ($_POST["form_$field_id"] as $key => $val) {
1098 if (strlen($value)) $value .= '|';
1099 $value .= $key;
1102 else if ($data_type == 22) {
1103 // $_POST["form_$field_id"] is an array of text fields to be imploded
1104 // into "key:value|key:value|...".
1105 foreach ($_POST["form_$field_id"] as $key => $val) {
1106 $val = str_replace('|', ' ', $val);
1107 if (strlen($value)) $value .= '|';
1108 $value .= "$key:$val";
1111 else if ($data_type == 23) {
1112 // $_POST["form_$field_id"] is an array of text fields with companion
1113 // radio buttons to be imploded into "key:n:notes|key:n:notes|...".
1114 foreach ($_POST["form_$field_id"] as $key => $val) {
1115 $restype = $_POST["radio_{$field_id}"][$key];
1116 if (empty($restype)) $restype = '0';
1117 $val = str_replace('|', ' ', $val);
1118 if (strlen($value)) $value .= '|';
1119 $value .= "$key:$restype:$val";
1122 else if ($data_type == 25) {
1123 // $_POST["form_$field_id"] is an array of text fields with companion
1124 // checkboxes to be imploded into "key:n:notes|key:n:notes|...".
1125 foreach ($_POST["form_$field_id"] as $key => $val) {
1126 $restype = empty($_POST["check_{$field_id}"][$key]) ? '0' : '1';
1127 $val = str_replace('|', ' ', $val);
1128 if (strlen($value)) $value .= '|';
1129 $value .= "$key:$restype:$val";
1132 else {
1133 $value = $_POST["form_$field_id"];
1137 // Make sure the return value is quote-safe.
1138 return formTrim($value);
1141 // Generate JavaScript validation logic for the required fields.
1143 function generate_layout_validation($form_id) {
1144 $fres = sqlStatement("SELECT * FROM layout_options " .
1145 "WHERE form_id = '$form_id' AND uor > 0 AND field_id != '' " .
1146 "ORDER BY group_name, seq");
1148 while ($frow = sqlFetchArray($fres)) {
1149 if ($frow['uor'] < 2) continue;
1150 $data_type = $frow['data_type'];
1151 $field_id = $frow['field_id'];
1152 $fldtitle = $frow['title'];
1153 if (!$fldtitle) $fldtitle = $frow['description'];
1154 $fldname = "form_$field_id";
1155 switch($data_type) {
1156 case 1:
1157 case 11:
1158 case 12:
1159 case 13:
1160 case 14:
1161 echo
1162 " if (f.$fldname.selectedIndex <= 0) {\n" .
1163 " alert('" . xl('Please choose a value for','','',' ') .
1164 xl_layout_label($fldtitle) . "');\n" .
1165 " if (f.$fldname.focus) f.$fldname.focus();\n" .
1166 " return false;\n" .
1167 " }\n";
1168 break;
1169 case 2:
1170 case 3:
1171 case 4:
1172 case 15:
1173 echo
1174 " if (trimlen(f.$fldname.value) == 0) {\n" .
1175 " alert('" . xl('Please choose a value for','','',' ') .
1176 xl_layout_label($fldtitle) . "');\n" .
1177 " if (f.$fldname.focus) f.$fldname.focus();\n" .
1178 " return false;\n" .
1179 " }\n";
1180 break;