Preparing package for 3.2.0, can now upgrade from non-package, still need more testing
[openemr.git] / library / options.inc.php
blob4774f573d750267fa98ecfc6c87aee8cdbca7683
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, username 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 $title = $urow['username'] ? xl('Local') : xl('External');
212 echo " title='$title'";
213 if ($urow['id'] == $currvalue) echo " selected";
214 echo ">$uname</option>";
216 echo "</select>";
219 // a billing code (only one of these allowed!)
220 else if ($data_type == 15) {
221 echo "<input type='text'" .
222 " name='form_$field_id'" .
223 " id='form_related_code'" .
224 " size='" . $frow['fld_length'] . "'" .
225 " maxlength='" . $frow['max_length'] . "'" .
226 " title='$description'" .
227 " value='$currescaped'" .
228 " onclick='sel_related()' readonly" .
229 " />";
232 // a set of labeled checkboxes
233 else if ($data_type == 21) {
234 // In this special case, fld_length is the number of columns generated.
235 $cols = max(1, $frow['fld_length']);
236 $avalue = explode('|', $currvalue);
237 $lres = sqlStatement("SELECT * FROM list_options " .
238 "WHERE list_id = '$list_id' ORDER BY seq, title");
239 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
240 $tdpct = (int) (100 / $cols);
241 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
242 $option_id = $lrow['option_id'];
243 // if ($count) echo "<br />";
244 if ($count % $cols == 0) {
245 if ($count) echo "</tr>";
246 echo "<tr>";
248 echo "<td width='$tdpct%'>";
249 echo "<input type='checkbox' name='form_{$field_id}[$option_id]' id='form_{$field_id}[$option_id]' value='1'";
250 if (in_array($option_id, $avalue)) echo " checked";
252 // Added 5-09 by BM - Translate label if applicable
253 echo ">" . xl_list_label($lrow['title']);
255 echo "</td>";
257 if ($count) {
258 echo "</tr>";
259 if ($count > $cols) {
260 // Add some space after multiple rows of checkboxes.
261 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
264 echo "</table>";
267 // a set of labeled text input fields
268 else if ($data_type == 22) {
269 $tmp = explode('|', $currvalue);
270 $avalue = array();
271 foreach ($tmp as $value) {
272 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
273 $avalue[$matches[1]] = $matches[2];
276 $lres = sqlStatement("SELECT * FROM list_options " .
277 "WHERE list_id = '$list_id' ORDER BY seq, title");
278 echo "<table cellpadding='0' cellspacing='0'>";
279 while ($lrow = sqlFetchArray($lres)) {
280 $option_id = $lrow['option_id'];
281 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
282 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
284 // Added 5-09 by BM - Translate label if applicable
285 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
287 echo "<td><input type='text'" .
288 " name='form_{$field_id}[$option_id]'" .
289 " id='form_{$field_id}[$option_id]'" .
290 " size='$fldlength'" .
291 " maxlength='$maxlength'" .
292 " value='" . $avalue[$option_id] . "'";
293 echo " /></td></tr>";
295 echo "</table>";
298 // a set of exam results; 3 radio buttons and a text field:
299 else if ($data_type == 23) {
300 $tmp = explode('|', $currvalue);
301 $avalue = array();
302 foreach ($tmp as $value) {
303 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
304 $avalue[$matches[1]] = $matches[2];
307 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
308 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
309 $lres = sqlStatement("SELECT * FROM list_options " .
310 "WHERE list_id = '$list_id' ORDER BY seq, title");
311 echo "<table cellpadding='0' cellspacing='0'>";
312 echo "<tr><td>&nbsp;</td><td class='bold'>" . xl('N/A') .
313 "&nbsp;</td><td class='bold'>" . xl('Nor') . "&nbsp;</td>" .
314 "<td class='bold'>" . xl('Abn') . "&nbsp;</td><td class='bold'>" .
315 xl('Date/Notes') . "</td></tr>";
316 while ($lrow = sqlFetchArray($lres)) {
317 $option_id = $lrow['option_id'];
318 $restype = substr($avalue[$option_id], 0, 1);
319 $resnote = substr($avalue[$option_id], 2);
321 // Added 5-09 by BM - Translate label if applicable
322 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
324 for ($i = 0; $i < 3; ++$i) {
325 echo "<td><input type='radio'" .
326 " name='radio_{$field_id}[$option_id]'" .
327 " id='radio_{$field_id}[$option_id]'" .
328 " value='$i'";
329 if ($restype === "$i") echo " checked";
330 echo " /></td>";
332 echo "<td><input type='text'" .
333 " name='form_{$field_id}[$option_id]'" .
334 " id='form_{$field_id}[$option_id]'" .
335 " size='$fldlength'" .
336 " maxlength='$maxlength'" .
337 " value='$resnote' /></td>";
338 echo "</tr>";
340 echo "</table>";
343 // the list of active allergies for the current patient
344 // this is read-only!
345 else if ($data_type == 24) {
346 $query = "SELECT title, comments FROM lists WHERE " .
347 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
348 "ORDER BY begdate";
349 // echo "<!-- $query -->\n"; // debugging
350 $lres = sqlStatement($query);
351 $count = 0;
352 while ($lrow = sqlFetchArray($lres)) {
353 if ($count++) echo "<br />";
354 echo $lrow['title'];
355 if ($lrow['comments']) echo ' (' . $lrow['comments'] . ')';
359 // a set of labeled checkboxes, each with a text field:
360 else if ($data_type == 25) {
361 $tmp = explode('|', $currvalue);
362 $avalue = array();
363 foreach ($tmp as $value) {
364 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
365 $avalue[$matches[1]] = $matches[2];
368 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
369 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
370 $lres = sqlStatement("SELECT * FROM list_options " .
371 "WHERE list_id = '$list_id' ORDER BY seq, title");
372 echo "<table cellpadding='0' cellspacing='0'>";
373 while ($lrow = sqlFetchArray($lres)) {
374 $option_id = $lrow['option_id'];
375 $restype = substr($avalue[$option_id], 0, 1);
376 $resnote = substr($avalue[$option_id], 2);
378 // Added 5-09 by BM - Translate label if applicable
379 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
381 echo "<td><input type='checkbox' name='check_{$field_id}[$option_id]' id='check_{$field_id}[$option_id]' value='1'";
382 if ($restype) echo " checked";
383 echo " />&nbsp;</td>";
384 echo "<td><input type='text'" .
385 " name='form_{$field_id}[$option_id]'" .
386 " id='form_{$field_id}[$option_id]'" .
387 " size='$fldlength'" .
388 " maxlength='$maxlength'" .
389 " value='$resnote' /></td>";
390 echo "</tr>";
392 echo "</table>";
395 // single-selection list with ability to add to it
396 else if ($data_type == 26) {
397 echo "<select class='addtolistclass_$list_id' name='form_$field_id' id='form_$field_id' title='$description'>";
398 if ($showEmpty) echo "<option value=''>" . xl($empty_title) . "</option>";
399 $lres = sqlStatement("SELECT * FROM list_options " .
400 "WHERE list_id = '$list_id' ORDER BY seq, title");
401 $got_selected = FALSE;
402 while ($lrow = sqlFetchArray($lres)) {
403 echo "<option value='" . $lrow['option_id'] . "'";
404 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
405 (strlen($currvalue) > 0 && $lrow['option_id'] == $currvalue))
407 echo " selected";
408 $got_selected = TRUE;
410 // Added 5-09 by BM - Translate label if applicable
411 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>";
421 // show the add button if user has access to correct list
422 $outputAddButton = "<input type='button' id='addtolistid_".$list_id."' fieldid='form_".$field_id."' class='addtolist' value='" . xl('Add') . "'>";
423 if (aco_exist('lists', $list_id)) {
424 // a specific aco exist for this list, so ensure access
425 if (acl_check('lists', $list_id)) echo $outputAddButton;
427 else {
428 // no specific aco exist for this list, so check for access to 'default' list
429 if (acl_check('lists', 'default')) echo $outputAddButton;
433 // a set of labeled radio buttons
434 else if ($data_type == 27) {
435 // In this special case, fld_length is the number of columns generated.
436 $cols = max(1, $frow['fld_length']);
437 $lres = sqlStatement("SELECT * FROM list_options " .
438 "WHERE list_id = '$list_id' ORDER BY seq, title");
439 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
440 $tdpct = (int) (100 / $cols);
441 $got_selected = FALSE;
442 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
443 $option_id = $lrow['option_id'];
444 if ($count % $cols == 0) {
445 if ($count) echo "</tr>";
446 echo "<tr>";
448 echo "<td width='$tdpct%'>";
449 echo "<input type='radio' name='form_{$field_id}' id='form_{$field_id}[$option_id]' value='$option_id'";
450 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
451 (strlen($currvalue) > 0 && $option_id == $currvalue))
453 echo " checked";
454 $got_selected = TRUE;
456 echo ">" . xl_list_label($lrow['title']);
457 echo "</td>";
459 if ($count) {
460 echo "</tr>";
461 if ($count > $cols) {
462 // Add some space after multiple rows of radio buttons.
463 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
466 echo "</table>";
467 if (!$got_selected && strlen($currvalue) > 0) {
468 echo "$currvalue <font color='red' title='" . xl('Please choose a valid selection.') . "'>" . xl('Fix this') . "!</font>";
474 function generate_print_field($frow, $currvalue) {
475 global $rootdir, $date_init;
477 $currescaped = htmlspecialchars($currvalue, ENT_QUOTES);
479 $data_type = $frow['data_type'];
480 $field_id = $frow['field_id'];
481 $list_id = $frow['list_id'];
482 $fld_length = $frow['fld_length'];
484 $description = htmlspecialchars(xl_layout_label($frow['description']), ENT_QUOTES);
486 // Can pass $frow['empty_title'] with this variable, otherwise
487 // will default to 'Unassigned'.
488 // If it is 'SKIP' then an empty text title is completely skipped.
489 $showEmpty = true;
490 if (isset($frow['empty_title'])) {
491 if ($frow['empty_title'] == "SKIP") {
492 //do not display an 'empty' choice
493 $showEmpty = false;
494 $empty_title = "Unassigned";
496 else {
497 $empty_title = $frow['empty_title'];
500 else {
501 $empty_title = "Unassigned";
504 // generic single-selection list
505 if ($data_type == 1 || $data_type == 26) {
506 if (empty($fld_length)) {
507 if ($list_id == 'titles') {
508 $fld_length = 3;
509 } else {
510 $fld_length = 10;
513 $tmp = '';
514 if ($currvalue) {
515 $lrow = sqlQuery("SELECT title FROM list_options " .
516 "WHERE list_id = '$list_id' AND option_id = '$currvalue'");
517 $tmp = xl_list_label($lrow['title']);
518 if (empty($tmp)) $tmp = "($currvalue)";
520 /*****************************************************************
521 echo "<input type='text'" .
522 " size='$fld_length'" .
523 " value='$tmp'" .
524 " class='under'" .
525 " />";
526 *****************************************************************/
527 if ($tmp === '') $tmp = '&nbsp;';
528 echo $tmp;
531 // simple text field
532 else if ($data_type == 2 || $data_type == 15) {
533 /*****************************************************************
534 echo "<input type='text'" .
535 " size='$fld_length'" .
536 " value='$currescaped'" .
537 " class='under'" .
538 " />";
539 *****************************************************************/
540 if ($currescaped === '') $currescaped = '&nbsp;';
541 echo $currescaped;
544 // long or multi-line text field
545 else if ($data_type == 3) {
546 echo "<textarea" .
547 " cols='$fld_length'" .
548 " rows='" . $frow['max_length'] . "'>" .
549 $currescaped . "</textarea>";
552 // date
553 else if ($data_type == 4) {
554 /*****************************************************************
555 echo "<input type='text' size='10'" .
556 " value='$currescaped'" .
557 " title='$description'" .
558 " class='under'" .
559 " />";
560 *****************************************************************/
561 if ($currescaped === '') $currescaped = '&nbsp;';
562 echo $currescaped;
565 // provider list
566 else if ($data_type == 10 || $data_type == 11) {
567 $tmp = '';
568 if ($currvalue) {
569 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
570 "WHERE id = '$currvalue'");
571 $tmp = ucwords($urow['fname'] . " " . $urow['lname']);
572 if (empty($tmp)) $tmp = "($currvalue)";
574 /*****************************************************************
575 echo "<input type='text'" .
576 " size='$fld_length'" .
577 " value='$tmp'" .
578 " class='under'" .
579 " />";
580 *****************************************************************/
581 if ($tmp === '') $tmp = '&nbsp;';
582 echo $tmp;
585 // pharmacy list
586 else if ($data_type == 12) {
587 $tmp = '';
588 if ($currvalue) {
589 $pres = get_pharmacies();
590 while ($prow = sqlFetchArray($pres)) {
591 $key = $prow['id'];
592 if ($currvalue == $key) {
593 $tmp = $prow['name'] . ' ' . $prow['area_code'] . '-' .
594 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
595 $prow['line1'] . ' / ' . $prow['city'];
598 if (empty($tmp)) $tmp = "($currvalue)";
600 /*****************************************************************
601 echo "<input type='text'" .
602 " size='$fld_length'" .
603 " value='$tmp'" .
604 " class='under'" .
605 " />";
606 *****************************************************************/
607 if ($tmp === '') $tmp = '&nbsp;';
608 echo $tmp;
611 // squads
612 else if ($data_type == 13) {
613 $tmp = '';
614 if ($currvalue) {
615 $squads = acl_get_squads();
616 if ($squads) {
617 foreach ($squads as $key => $value) {
618 if ($currvalue == $key) {
619 $tmp = $value[3];
623 if (empty($tmp)) $tmp = "($currvalue)";
625 /*****************************************************************
626 echo "<input type='text'" .
627 " size='$fld_length'" .
628 " value='$tmp'" .
629 " class='under'" .
630 " />";
631 *****************************************************************/
632 if ($tmp === '') $tmp = '&nbsp;';
633 echo $tmp;
636 // Address book.
637 else if ($data_type == 14) {
638 $tmp = '';
639 if ($currvalue) {
640 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
641 "WHERE id = '$currvalue'");
642 $uname = $urow['lname'];
643 if ($urow['fname']) $uname .= ", " . $urow['fname'];
644 $tmp = $uname;
645 if (empty($tmp)) $tmp = "($currvalue)";
647 /*****************************************************************
648 echo "<input type='text'" .
649 " size='$fld_length'" .
650 " value='$tmp'" .
651 " class='under'" .
652 " />";
653 *****************************************************************/
654 if ($tmp === '') $tmp = '&nbsp;';
655 echo $tmp;
658 // a set of labeled checkboxes
659 else if ($data_type == 21) {
660 // In this special case, fld_length is the number of columns generated.
661 $cols = max(1, $fld_length);
662 $avalue = explode('|', $currvalue);
663 $lres = sqlStatement("SELECT * FROM list_options " .
664 "WHERE list_id = '$list_id' ORDER BY seq, title");
665 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
666 $tdpct = (int) (100 / $cols);
667 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
668 $option_id = $lrow['option_id'];
669 if ($count % $cols == 0) {
670 if ($count) echo "</tr>";
671 echo "<tr>";
673 echo "<td width='$tdpct%'>";
674 echo "<input type='checkbox'";
675 if (in_array($option_id, $avalue)) echo " checked";
676 echo ">" . xl_list_label($lrow['title']);
677 echo "</td>";
679 if ($count) {
680 echo "</tr>";
681 if ($count > $cols) {
682 // Add some space after multiple rows of checkboxes.
683 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
686 echo "</table>";
689 // a set of labeled text input fields
690 else if ($data_type == 22) {
691 $tmp = explode('|', $currvalue);
692 $avalue = array();
693 foreach ($tmp as $value) {
694 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
695 $avalue[$matches[1]] = $matches[2];
698 $lres = sqlStatement("SELECT * FROM list_options " .
699 "WHERE list_id = '$list_id' ORDER BY seq, title");
700 echo "<table cellpadding='0' cellspacing='0'>";
701 while ($lrow = sqlFetchArray($lres)) {
702 $option_id = $lrow['option_id'];
703 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
704 $fldlength = empty($fld_length) ? 20 : $fld_length;
705 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
706 echo "<td><input type='text'" .
707 " size='$fldlength'" .
708 " value='" . $avalue[$option_id] . "'" .
709 " class='under'" .
710 " /></td></tr>";
712 echo "</table>";
715 // a set of exam results; 3 radio buttons and a text field:
716 else if ($data_type == 23) {
717 $tmp = explode('|', $currvalue);
718 $avalue = array();
719 foreach ($tmp as $value) {
720 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
721 $avalue[$matches[1]] = $matches[2];
724 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
725 $fldlength = empty($fld_length) ? 20 : $fld_length;
726 $lres = sqlStatement("SELECT * FROM list_options " .
727 "WHERE list_id = '$list_id' ORDER BY seq, title");
728 echo "<table cellpadding='0' cellspacing='0'>";
729 echo "<tr><td>&nbsp;</td><td class='bold'>" . xl('N/A') .
730 "&nbsp;</td><td class='bold'>" . xl('Nor') . "&nbsp;</td>" .
731 "<td class='bold'>" . xl('Abn') . "&nbsp;</td><td class='bold'>" .
732 xl('Date/Notes') . "</td></tr>";
733 while ($lrow = sqlFetchArray($lres)) {
734 $option_id = $lrow['option_id'];
735 $restype = substr($avalue[$option_id], 0, 1);
736 $resnote = substr($avalue[$option_id], 2);
737 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
738 for ($i = 0; $i < 3; ++$i) {
739 echo "<td><input type='radio'";
740 if ($restype === "$i") echo " checked";
741 echo " /></td>";
743 echo "<td><input type='text'" .
744 " size='$fldlength'" .
745 " value='$resnote'" .
746 " class='under' /></td>" .
747 "</tr>";
749 echo "</table>";
752 // the list of active allergies for the current patient
753 // this is read-only!
754 else if ($data_type == 24) {
755 $query = "SELECT title, comments FROM lists WHERE " .
756 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
757 "ORDER BY begdate";
758 $lres = sqlStatement($query);
759 $count = 0;
760 while ($lrow = sqlFetchArray($lres)) {
761 if ($count++) echo "<br />";
762 echo $lrow['title'];
763 if ($lrow['comments']) echo ' (' . $lrow['comments'] . ')';
767 // a set of labeled checkboxes, each with a text field:
768 else if ($data_type == 25) {
769 $tmp = explode('|', $currvalue);
770 $avalue = array();
771 foreach ($tmp as $value) {
772 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
773 $avalue[$matches[1]] = $matches[2];
776 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
777 $fldlength = empty($fld_length) ? 20 : $fld_length;
778 $lres = sqlStatement("SELECT * FROM list_options " .
779 "WHERE list_id = '$list_id' ORDER BY seq, title");
780 echo "<table cellpadding='0' cellspacing='0'>";
781 while ($lrow = sqlFetchArray($lres)) {
782 $option_id = $lrow['option_id'];
783 $restype = substr($avalue[$option_id], 0, 1);
784 $resnote = substr($avalue[$option_id], 2);
785 echo "<tr><td>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
786 echo "<td><input type='checkbox'";
787 if ($restype) echo " checked";
788 echo " />&nbsp;</td>";
789 echo "<td><input type='text'" .
790 " size='$fldlength'" .
791 " value='$resnote'" .
792 " class='under'" .
793 " /></td>" .
794 "</tr>";
796 echo "</table>";
799 // a set of labeled radio buttons
800 else if ($data_type == 27) {
801 // In this special case, fld_length is the number of columns generated.
802 $cols = max(1, $frow['fld_length']);
803 $lres = sqlStatement("SELECT * FROM list_options " .
804 "WHERE list_id = '$list_id' ORDER BY seq, title");
805 echo "<table cellpadding='0' cellspacing='0' width='100%'>";
806 $tdpct = (int) (100 / $cols);
807 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
808 $option_id = $lrow['option_id'];
809 if ($count % $cols == 0) {
810 if ($count) echo "</tr>";
811 echo "<tr>";
813 echo "<td width='$tdpct%'>";
814 echo "<input type='radio'";
815 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
816 (strlen($currvalue) > 0 && $option_id == $currvalue))
818 echo " checked";
820 echo ">" . xl_list_label($lrow['title']);
821 echo "</td>";
823 if ($count) {
824 echo "</tr>";
825 if ($count > $cols) {
826 // Add some space after multiple rows of radio buttons.
827 echo "<tr><td colspan='$cols' style='height:0.7em'></td></tr>";
830 echo "</table>";
835 function generate_display_field($frow, $currvalue) {
836 $data_type = $frow['data_type'];
837 $field_id = $frow['field_id'];
838 $list_id = $frow['list_id'];
839 $s = '';
841 // generic selection list or the generic selection list with add on the fly
842 // feature, or radio buttons
843 if ($data_type == 1 || $data_type == 26 || $data_type == 27) {
844 $lrow = sqlQuery("SELECT title FROM list_options " .
845 "WHERE list_id = '$list_id' AND option_id = '$currvalue'");
846 $s = xl_list_label($lrow['title']);
849 // simple text field
850 else if ($data_type == 2) {
851 $s = $currvalue;
854 // long or multi-line text field
855 else if ($data_type == 3) {
856 $s = nl2br($currvalue);
859 // date
860 else if ($data_type == 4) {
861 $s = $currvalue;
864 // provider
865 else if ($data_type == 10 || $data_type == 11) {
866 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
867 "WHERE id = '$currvalue'");
868 $s = ucwords($urow['fname'] . " " . $urow['lname']);
871 // pharmacy list
872 else if ($data_type == 12) {
873 $pres = get_pharmacies();
874 while ($prow = sqlFetchArray($pres)) {
875 $key = $prow['id'];
876 if ($currvalue == $key) {
877 $s .= $prow['name'] . ' ' . $prow['area_code'] . '-' .
878 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
879 $prow['line1'] . ' / ' . $prow['city'];
884 // squads
885 else if ($data_type == 13) {
886 $squads = acl_get_squads();
887 if ($squads) {
888 foreach ($squads as $key => $value) {
889 if ($currvalue == $key) {
890 $s .= $value[3];
896 // address book
897 else if ($data_type == 14) {
898 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
899 "WHERE id = '$currvalue'");
900 $uname = $urow['lname'];
901 if ($urow['fname']) $uname .= ", " . $urow['fname'];
902 $s = $uname;
905 // billing code
906 else if ($data_type == 15) {
907 $s = $currvalue;
910 // a set of labeled checkboxes
911 else if ($data_type == 21) {
912 $avalue = explode('|', $currvalue);
913 $lres = sqlStatement("SELECT * FROM list_options " .
914 "WHERE list_id = '$list_id' ORDER BY seq, title");
915 $count = 0;
916 while ($lrow = sqlFetchArray($lres)) {
917 $option_id = $lrow['option_id'];
918 if (in_array($option_id, $avalue)) {
919 if ($count++) $s .= "<br />";
921 // Added 5-09 by BM - Translate label if applicable
922 $s .= xl_list_label($lrow['title']);
928 // a set of labeled text input fields
929 else if ($data_type == 22) {
930 $tmp = explode('|', $currvalue);
931 $avalue = array();
932 foreach ($tmp as $value) {
933 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
934 $avalue[$matches[1]] = $matches[2];
937 $lres = sqlStatement("SELECT * FROM list_options " .
938 "WHERE list_id = '$list_id' ORDER BY seq, title");
939 $s .= "<table cellpadding='0' cellspacing='0'>";
940 while ($lrow = sqlFetchArray($lres)) {
941 $option_id = $lrow['option_id'];
942 if (empty($avalue[$option_id])) 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 $s .= "<td class='text' valign='top'>" . $avalue[$option_id] . "</td></tr>";
949 $s .= "</table>";
952 // a set of exam results; 3 radio buttons and a text field:
953 else if ($data_type == 23) {
954 $tmp = explode('|', $currvalue);
955 $avalue = array();
956 foreach ($tmp as $value) {
957 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
958 $avalue[$matches[1]] = $matches[2];
961 $lres = sqlStatement("SELECT * FROM list_options " .
962 "WHERE list_id = '$list_id' ORDER BY seq, title");
963 $s .= "<table cellpadding='0' cellspacing='0'>";
964 while ($lrow = sqlFetchArray($lres)) {
965 $option_id = $lrow['option_id'];
966 $restype = substr($avalue[$option_id], 0, 1);
967 $resnote = substr($avalue[$option_id], 2);
968 if (empty($restype) && empty($resnote)) continue;
970 // Added 5-09 by BM - Translate label if applicable
971 $s .= "<tr><td class='bold' valign='top'>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
973 $restype = ($restype == '1') ? xl('Normal') : (($restype == '2') ? xl('Abnormal') : xl('N/A'));
974 // $s .= "<td class='text' valign='top'>$restype</td></tr>";
975 // $s .= "<td class='text' valign='top'>$resnote</td></tr>";
976 $s .= "<td class='text' valign='top'>$restype&nbsp;</td>";
977 $s .= "<td class='text' valign='top'>$resnote</td>";
978 $s .= "</tr>";
980 $s .= "</table>";
983 // the list of active allergies for the current patient
984 else if ($data_type == 24) {
985 $query = "SELECT title, comments FROM lists WHERE " .
986 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
987 "ORDER BY begdate";
988 // echo "<!-- $query -->\n"; // debugging
989 $lres = sqlStatement($query);
990 $count = 0;
991 while ($lrow = sqlFetchArray($lres)) {
992 if ($count++) $s .= "<br />";
993 $s .= $lrow['title'];
994 if ($lrow['comments']) $s .= ' (' . $lrow['comments'] . ')';
998 // a set of labeled checkboxes, each with a text field:
999 else if ($data_type == 25) {
1000 $tmp = explode('|', $currvalue);
1001 $avalue = array();
1002 foreach ($tmp as $value) {
1003 if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
1004 $avalue[$matches[1]] = $matches[2];
1007 $lres = sqlStatement("SELECT * FROM list_options " .
1008 "WHERE list_id = '$list_id' ORDER BY seq, title");
1009 $s .= "<table cellpadding='0' cellspacing='0'>";
1010 while ($lrow = sqlFetchArray($lres)) {
1011 $option_id = $lrow['option_id'];
1012 $restype = substr($avalue[$option_id], 0, 1);
1013 $resnote = substr($avalue[$option_id], 2);
1014 if (empty($restype) && empty($resnote)) continue;
1016 // Added 5-09 by BM - Translate label if applicable
1017 $s .= "<tr><td class='bold' valign='top'>" . xl_list_label($lrow['title']) . "&nbsp;</td>";
1019 $restype = $restype ? xl('Yes') : xl('No');
1020 $s .= "<td class='text' valign='top'>$restype</td></tr>";
1021 $s .= "<td class='text' valign='top'>$resnote</td></tr>";
1022 $s .= "</tr>";
1024 $s .= "</table>";
1027 return $s;
1030 $CPR = 4; // cells per row of generic data
1031 $last_group = '';
1032 $cell_count = 0;
1033 $item_count = 0;
1035 function disp_end_cell() {
1036 global $item_count, $cell_count;
1037 if ($item_count > 0) {
1038 echo "</td>";
1039 $item_count = 0;
1043 function disp_end_row() {
1044 global $cell_count, $CPR;
1045 disp_end_cell();
1046 if ($cell_count > 0) {
1047 for (; $cell_count < $CPR; ++$cell_count) echo "<td></td>";
1048 echo "</tr>\n";
1049 $cell_count = 0;
1053 function disp_end_group() {
1054 global $last_group;
1055 if (strlen($last_group) > 0) {
1056 disp_end_row();
1060 function display_layout_rows($formtype, $result1, $result2='') {
1061 global $item_count, $cell_count, $last_group, $CPR;
1063 $fres = sqlStatement("SELECT * FROM layout_options " .
1064 "WHERE form_id = '$formtype' AND uor > 0 " .
1065 "ORDER BY group_name, seq");
1067 while ($frow = sqlFetchArray($fres)) {
1068 $this_group = $frow['group_name'];
1069 $titlecols = $frow['titlecols'];
1070 $datacols = $frow['datacols'];
1071 $data_type = $frow['data_type'];
1072 $field_id = $frow['field_id'];
1073 $list_id = $frow['list_id'];
1074 $currvalue = '';
1076 if ($formtype == 'DEM') {
1077 if ($GLOBALS['athletic_team']) {
1078 // Skip fitness level and return-to-play date because those appear
1079 // in a special display/update form on this page.
1080 if ($field_id === 'fitness' || $field_id === 'userdate1') continue;
1082 if (strpos($field_id, 'em_') === 0) {
1083 // Skip employer related fields, if it's disabled.
1084 if ($GLOBALS['omit_employers']) continue;
1085 $tmp = substr($field_id, 3);
1086 if (isset($result2[$tmp])) $currvalue = $result2[$tmp];
1088 else {
1089 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
1092 else {
1093 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
1096 // Handle a data category (group) change.
1097 if (strcmp($this_group, $last_group) != 0) {
1098 $group_name = substr($this_group, 1);
1099 // totally skip generating the employer category, if it's disabled.
1100 if ($group_name === 'Employer' && $GLOBALS['omit_employers']) continue;
1101 disp_end_group();
1102 $last_group = $this_group;
1105 // Handle starting of a new row.
1106 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
1107 disp_end_row();
1108 echo "<tr>";
1109 if ($group_name) {
1110 echo "<td class='groupname'>";
1111 //echo "<td class='groupname' style='padding-right:5pt' valign='top'>";
1112 //echo "<font color='#008800'>$group_name</font>";
1114 // Added 5-09 by BM - Translate label if applicable
1115 echo (xl_layout_label($group_name));
1117 $group_name = '';
1118 } else {
1119 //echo "<td class='' style='padding-right:5pt' valign='top'>";
1120 echo "<td valign='top'>&nbsp;";
1122 echo "</td>";
1125 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
1127 // Handle starting of a new label cell.
1128 if ($titlecols > 0) {
1129 disp_end_cell();
1130 //echo "<td class='label' colspan='$titlecols' valign='top'";
1131 echo "<td class='label' colspan='$titlecols' ";
1132 //if ($cell_count == 2) echo " style='padding-left:10pt'";
1133 echo ">";
1134 $cell_count += $titlecols;
1136 ++$item_count;
1138 // Added 5-09 by BM - Translate label if applicable
1139 if ($frow['title']) echo (xl_layout_label($frow['title']).":"); else echo "&nbsp;";
1141 // Handle starting of a new data cell.
1142 if ($datacols > 0) {
1143 disp_end_cell();
1144 //echo "<td class='text data' colspan='$datacols' valign='top'";
1145 echo "<td class='text data' colspan='$datacols'";
1146 //if ($cell_count > 0) echo " style='padding-left:5pt'";
1147 echo ">";
1148 $cell_count += $datacols;
1151 ++$item_count;
1152 echo generate_display_field($frow, $currvalue);
1155 disp_end_group();
1158 // From the currently posted HTML form, this gets the value of the
1159 // field corresponding to the provided layout_options table row.
1161 function get_layout_form_value($frow, $maxlength=255) {
1162 $data_type = $frow['data_type'];
1163 $field_id = $frow['field_id'];
1164 $value = '';
1165 if (isset($_POST["form_$field_id"])) {
1166 if ($data_type == 21) {
1167 // $_POST["form_$field_id"] is an array of checkboxes and its keys
1168 // must be concatenated into a |-separated string.
1169 foreach ($_POST["form_$field_id"] as $key => $val) {
1170 if (strlen($value)) $value .= '|';
1171 $value .= $key;
1174 else if ($data_type == 22) {
1175 // $_POST["form_$field_id"] is an array of text fields to be imploded
1176 // into "key:value|key:value|...".
1177 foreach ($_POST["form_$field_id"] as $key => $val) {
1178 $val = str_replace('|', ' ', $val);
1179 if (strlen($value)) $value .= '|';
1180 $value .= "$key:$val";
1183 else if ($data_type == 23) {
1184 // $_POST["form_$field_id"] is an array of text fields with companion
1185 // radio buttons to be imploded into "key:n:notes|key:n:notes|...".
1186 foreach ($_POST["form_$field_id"] as $key => $val) {
1187 $restype = $_POST["radio_{$field_id}"][$key];
1188 if (empty($restype)) $restype = '0';
1189 $val = str_replace('|', ' ', $val);
1190 if (strlen($value)) $value .= '|';
1191 $value .= "$key:$restype:$val";
1194 else if ($data_type == 25) {
1195 // $_POST["form_$field_id"] is an array of text fields with companion
1196 // checkboxes to be imploded into "key:n:notes|key:n:notes|...".
1197 foreach ($_POST["form_$field_id"] as $key => $val) {
1198 $restype = empty($_POST["check_{$field_id}"][$key]) ? '0' : '1';
1199 $val = str_replace('|', ' ', $val);
1200 if (strlen($value)) $value .= '|';
1201 $value .= "$key:$restype:$val";
1204 else {
1205 $value = $_POST["form_$field_id"];
1209 // Better to die than to silently truncate data!
1210 if ($maxlength && $data_type != 3 && strlen($value) > $maxlength)
1211 die(xl('ERROR: Field') . " '$field_id' " . xl('is too long') .
1212 ":<br />&nbsp;<br />$value");
1214 // Make sure the return value is quote-safe.
1215 return formTrim($value);
1218 // Generate JavaScript validation logic for the required fields.
1220 function generate_layout_validation($form_id) {
1221 $fres = sqlStatement("SELECT * FROM layout_options " .
1222 "WHERE form_id = '$form_id' AND uor > 0 AND field_id != '' " .
1223 "ORDER BY group_name, seq");
1225 while ($frow = sqlFetchArray($fres)) {
1226 if ($frow['uor'] < 2) continue;
1227 $data_type = $frow['data_type'];
1228 $field_id = $frow['field_id'];
1229 $fldtitle = $frow['title'];
1230 if (!$fldtitle) $fldtitle = $frow['description'];
1231 $fldname = "form_$field_id";
1232 switch($data_type) {
1233 case 1:
1234 case 11:
1235 case 12:
1236 case 13:
1237 case 14:
1238 case 26:
1239 echo
1240 " if (f.$fldname.selectedIndex <= 0) {\n" .
1241 " alert('" . xl('Please choose a value for','','',' ') .
1242 xl_layout_label($fldtitle) . "');\n" .
1243 " if (f.$fldname.focus) f.$fldname.focus();\n" .
1244 " return false;\n" .
1245 " }\n";
1246 break;
1247 case 27: // radio buttons
1248 echo
1249 " var i = 0;\n" .
1250 " for (; i < f.$fldname.length; ++i) if (f.$fldname[i].checked) break;\n" .
1251 " if (i >= f.$fldname.length) {\n" .
1252 " alert('" . xl('Please choose a value for','','',' ') .
1253 xl_layout_label($fldtitle) . "');\n" .
1254 " return false;\n" .
1255 " }\n";
1256 break;
1257 case 2:
1258 case 3:
1259 case 4:
1260 case 15:
1261 echo
1262 " if (trimlen(f.$fldname.value) == 0) {\n" .
1263 " alert('" . xl('Please choose a value for','','',' ') .
1264 xl_layout_label($fldtitle) . "');\n" .
1265 " if (f.$fldname.focus) f.$fldname.focus();\n" .
1266 " return false;\n" .
1267 " }\n";
1268 break;