adding support for layouts of GCAC and contraception issues
[openemr.git] / library / options.inc.php
blob3b686f3b9cdfe56ce04f10b685d6c81a06af3b1f
1 <?php
3 $date_init = "";
5 function get_pharmacies() {
6 return sqlStatement("SELECT d.id, d.name, a.line1, a.city, " .
7 "p.area_code, p.prefix, p.number FROM pharmacies AS d " .
8 "LEFT OUTER JOIN addresses AS a ON a.foreign_id = d.id " .
9 "LEFT OUTER JOIN phone_numbers AS p ON p.foreign_id = d.id " .
10 "AND p.type = 2 " .
11 "ORDER BY name, area_code, prefix, number");
14 function generate_form_field($frow, $currvalue) {
15 global $rootdir, $date_init;
17 $currescaped = htmlspecialchars($currvalue, ENT_QUOTES);
19 $data_type = $frow['data_type'];
20 $field_id = $frow['field_id'];
21 $list_id = $frow['list_id'];
22 $description = htmlspecialchars($frow['description'], ENT_QUOTES);
24 // generic single-selection list
25 if ($data_type == 1) {
26 echo "<select name='form_$field_id' title='$description'>";
27 echo "<option value=''>" . xl('Unassigned') . "</option>";
28 $lres = sqlStatement("SELECT * FROM list_options " .
29 "WHERE list_id = '$list_id' ORDER BY seq");
30 $got_selected = FALSE;
31 while ($lrow = sqlFetchArray($lres)) {
32 echo "<option value='" . $lrow['option_id'] . "'";
33 if ((strlen($currvalue) == 0 && $lrow['is_default']) ||
34 (strlen($currvalue) > 0 && $lrow['option_id'] == $currvalue))
36 echo " selected";
37 $got_selected = TRUE;
39 echo ">" . $lrow['title'] . "</option>\n";
41 if (!$got_selected && strlen($currvalue) > 0) {
42 echo "<option value='$currescaped' selected>* $currescaped *</option>";
43 echo "</select>";
44 echo " <font color='red' title='Please choose a valid selection " .
45 "from the list'>Fix this!</font>";
47 else {
48 echo "</select>";
52 // simple text field
53 else if ($data_type == 2) {
54 echo "<input type='text'" .
55 " name='form_$field_id'" .
56 " size='" . $frow['fld_length'] . "'" .
57 " maxlength='" . $frow['max_length'] . "'" .
58 " title='$description'" .
59 " value='$currescaped'";
60 if (strpos($frow['edit_options'], 'C') !== FALSE)
61 echo " onchange='capitalizeMe(this)'";
62 echo " />";
65 // long or multi-line text field
66 else if ($data_type == 3) {
67 echo "<textarea" .
68 " name='form_$field_id'" .
69 " title='$description'" .
70 " cols='" . $frow['fld_length'] . "'" .
71 " rows='" . $frow['max_length'] . "'>" .
72 $currescaped . "</textarea>";
75 // date
76 else if ($data_type == 4) {
77 echo "<input type='text' size='10' name='form_$field_id' id='form_$field_id'" .
78 " value='$currescaped'" .
79 " title='$description'" .
80 " onkeyup='datekeyup(this,mypcc)' onblur='dateblur(this,mypcc)' />" .
81 "<img src='$rootdir/pic/show_calendar.gif' align='absbottom' width='24' height='22'" .
82 " id='img_$field_id' border='0' alt='[?]' style='cursor:pointer'" .
83 " title='" . xl('Click here to choose a date') . "' />";
84 $date_init .= " Calendar.setup({inputField:'form_$field_id', ifFormat:'%Y-%m-%d', button:'img_$field_id'});\n";
87 // provider list, local providers only
88 else if ($data_type == 10) {
89 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
90 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
91 "AND authorized = 1 " .
92 "ORDER BY lname, fname");
93 echo "<select name='form_$field_id' title='$description'>";
94 echo "<option value=''>" . xl('Unassigned') . "</option>";
95 while ($urow = sqlFetchArray($ures)) {
96 $uname = $urow['fname'] . ' ' . $urow['lname'];
97 echo "<option value='" . $urow['id'] . "'";
98 if ($urow['id'] == $currvalue) echo " selected";
99 echo ">$uname</option>";
101 echo "</select>";
104 // provider list, including address book entries with an NPI number
105 else if ($data_type == 11) {
106 $ures = sqlStatement("SELECT id, fname, lname, specialty FROM users " .
107 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
108 "AND ( authorized = 1 OR ( username = '' AND npi != '' ) ) " .
109 "ORDER BY lname, fname");
110 echo "<select name='form_$field_id' title='$description'>";
111 echo "<option value=''>" . xl('Unassigned') . "</option>";
112 while ($urow = sqlFetchArray($ures)) {
113 $uname = $urow['fname'] . ' ' . $urow['lname'];
114 echo "<option value='" . $urow['id'] . "'";
115 if ($urow['id'] == $currvalue) echo " selected";
116 echo ">$uname</option>";
118 echo "</select>";
121 // pharmacy list
122 else if ($data_type == 12) {
123 echo "<select name='form_$field_id' title='$description'>";
124 echo "<option value='0'></option>";
125 $pres = get_pharmacies();
126 while ($prow = sqlFetchArray($pres)) {
127 $key = $prow['id'];
128 echo "<option value='$key'";
129 if ($currvalue == $key) echo " selected";
130 echo '>' . $prow['name'] . ' ' . $prow['area_code'] . '-' .
131 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
132 $prow['line1'] . ' / ' . $prow['city'] . "</option>";
134 echo "</select>";
137 // squads
138 else if ($data_type == 13) {
139 echo "<select name='form_$field_id' title='$description'>";
140 echo "<option value=''>&nbsp;</option>";
141 $squads = acl_get_squads();
142 if ($squads) {
143 foreach ($squads as $key => $value) {
144 echo "<option value='$key'";
145 if ($currvalue == $key) echo " selected";
146 echo ">" . $value[3] . "</option>\n";
149 echo "</select>";
152 // address book, preferring organization name if it exists and is not in parentheses
153 else if ($data_type == 14) {
154 $ures = sqlStatement("SELECT id, fname, lname, organization FROM users " .
155 "WHERE active = 1 AND ( info IS NULL OR info NOT LIKE '%Inactive%' ) " .
156 "ORDER BY organization, lname, fname");
157 echo "<select name='form_$field_id' title='$description'>";
158 echo "<option value=''>" . xl('Unassigned') . "</option>";
159 while ($urow = sqlFetchArray($ures)) {
160 $uname = $urow['organization'];
161 if (empty($uname) || substr($uname, 0, 1) == '(') {
162 $uname = $urow['lname'];
163 if ($urow['fname']) $uname .= ", " . $urow['fname'];
165 echo "<option value='" . $urow['id'] . "'";
166 if ($urow['id'] == $currvalue) echo " selected";
167 echo ">$uname</option>";
169 echo "</select>";
172 // a billing code (only one of these allowed!)
173 else if ($data_type == 15) {
174 echo "<input type='text'" .
175 " name='form_$field_id'" .
176 " id='form_related_code'" .
177 " size='" . $frow['fld_length'] . "'" .
178 " maxlength='" . $frow['max_length'] . "'" .
179 " title='$description'" .
180 " value='$currescaped'" .
181 " onclick='sel_related()' readonly" .
182 " />";
185 // a set of labeled checkboxes
186 else if ($data_type == 21) {
187 $avalue = explode('|', $currvalue);
188 $lres = sqlStatement("SELECT * FROM list_options " .
189 "WHERE list_id = '$list_id' ORDER BY seq");
190 for ($count = 0; $lrow = sqlFetchArray($lres); ++$count) {
191 $option_id = $lrow['option_id'];
192 if ($count) echo "<br />";
193 echo "<input type='checkbox' name='form_{$field_id}[$option_id]' value='1'";
194 if (in_array($option_id, $avalue)) echo " checked";
195 echo ">" . $lrow['title'];
199 // a set of labeled text input fields
200 else if ($data_type == 22) {
201 $tmp = explode('|', $currvalue);
202 $avalue = array();
203 foreach ($tmp as $value) {
204 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
205 $avalue[$matches[1]] = $matches[2];
208 $lres = sqlStatement("SELECT * FROM list_options " .
209 "WHERE list_id = '$list_id' ORDER BY seq");
210 echo "<table cellpadding='0' cellspacing='0'>";
211 while ($lrow = sqlFetchArray($lres)) {
212 $option_id = $lrow['option_id'];
213 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
214 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
215 echo "<tr><td>" . $lrow['title'] . "&nbsp;</td>";
216 echo "<td><input type='text'" .
217 " name='form_{$field_id}[$option_id]'" .
218 " size='" . $frow['fld_length'] . "'" .
219 " maxlength='$maxlength'" .
220 " value='" . $avalue[$option_id] . "'";
221 echo " /></td></tr>";
223 echo "</table>";
226 // a set of exam results; 3 radio buttons and a text field:
227 else if ($data_type == 23) {
228 $tmp = explode('|', $currvalue);
229 $avalue = array();
230 foreach ($tmp as $value) {
231 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
232 $avalue[$matches[1]] = $matches[2];
235 $lres = sqlStatement("SELECT * FROM list_options " .
236 "WHERE list_id = '$list_id' ORDER BY seq");
237 echo "<table cellpadding='0' cellspacing='0'>";
238 echo "<tr><td>&nbsp;</td><td class='bold'>N/A&nbsp;</td><td class='bold'>Nor&nbsp;</td>" .
239 "<td class='bold'>Abn&nbsp;</td><td class='bold'>Date/Notes</td></tr>";
240 while ($lrow = sqlFetchArray($lres)) {
241 $option_id = $lrow['option_id'];
242 $maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
243 $fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
244 $restype = substr($avalue[$option_id], 0, 1);
245 $resnote = substr($avalue[$option_id], 2);
246 echo "<tr><td>" . $lrow['title'] . "&nbsp;</td>";
247 for ($i = 0; $i < 3; ++$i) {
248 echo "<td><input type='radio'" .
249 " name='radio_{$field_id}[$option_id]'" .
250 " value='$i'";
251 if ($restype === "$i") echo " checked";
252 echo " /></td>";
254 echo "<td><input type='text'" .
255 " name='form_{$field_id}[$option_id]'" .
256 " size='" . $frow['fld_length'] . "'" .
257 " maxlength='$maxlength'" .
258 " value='$resnote' /></td>";
259 echo "</tr>";
261 echo "</table>";
264 // the list of active allergies for the current patient
265 // this is read-only!
266 else if ($data_type == 24) {
267 $query = "SELECT title, comments FROM lists WHERE " .
268 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
269 "ORDER BY begdate";
270 // echo "<!-- $query -->\n"; // debugging
271 $lres = sqlStatement($query);
272 $count = 0;
273 while ($lrow = sqlFetchArray($lres)) {
274 if ($count++) echo "<br />";
275 echo $lrow['title'];
276 if ($lrow['comments']) echo ' (' . $lrow['comments'] . ')';
282 function generate_display_field($frow, $currvalue) {
283 $data_type = $frow['data_type'];
284 $field_id = $frow['field_id'];
285 $list_id = $frow['list_id'];
286 $s = '';
288 // generic selection list
289 if ($data_type == 1) {
290 $lrow = sqlQuery("SELECT title FROM list_options " .
291 "WHERE list_id = '$list_id' AND option_id = '$currvalue'");
292 $s = $lrow['title'];
295 // simple text field
296 else if ($data_type == 2) {
297 $s = $currvalue;
300 // long or multi-line text field
301 else if ($data_type == 3) {
302 $s = nl2br($currvalue);
305 // date
306 else if ($data_type == 4) {
307 $s = $currvalue;
310 // provider
311 else if ($data_type == 10 || $data_type == 11) {
312 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
313 "WHERE id = '$currvalue'");
314 $s = ucwords($urow['fname'] . " " . $urow['lname']);
317 // pharmacy list
318 else if ($data_type == 12) {
319 $pres = get_pharmacies();
320 while ($prow = sqlFetchArray($pres)) {
321 $key = $prow['id'];
322 if ($currvalue == $key) {
323 $s .= $prow['name'] . ' ' . $prow['area_code'] . '-' .
324 $prow['prefix'] . '-' . $prow['number'] . ' / ' .
325 $prow['line1'] . ' / ' . $prow['city'];
330 // squads
331 else if ($data_type == 13) {
332 $squads = acl_get_squads();
333 if ($squads) {
334 foreach ($squads as $key => $value) {
335 if ($currvalue == $key) {
336 $s .= $value[3];
342 // address book
343 else if ($data_type == 14) {
344 $urow = sqlQuery("SELECT fname, lname, specialty FROM users " .
345 "WHERE id = '$currvalue'");
346 $uname = $urow['lname'];
347 if ($urow['fname']) $uname .= ", " . $urow['fname'];
348 $s = $uname;
351 // billing code
352 else if ($data_type == 15) {
353 $s = $currvalue;
356 // a set of labeled checkboxes
357 else if ($data_type == 21) {
358 $avalue = explode('|', $currvalue);
359 $lres = sqlStatement("SELECT * FROM list_options " .
360 "WHERE list_id = '$list_id' ORDER BY seq");
361 $count = 0;
362 while ($lrow = sqlFetchArray($lres)) {
363 $option_id = $lrow['option_id'];
364 if (in_array($option_id, $avalue)) {
365 if ($count++) $s .= "<br />";
366 $s .= $lrow['title'];
371 // a set of labeled text input fields
372 else if ($data_type == 22) {
373 $tmp = explode('|', $currvalue);
374 $avalue = array();
375 foreach ($tmp as $value) {
376 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
377 $avalue[$matches[1]] = $matches[2];
380 $lres = sqlStatement("SELECT * FROM list_options " .
381 "WHERE list_id = '$list_id' ORDER BY seq");
382 $s .= "<table cellpadding='0' cellspacing='0'>";
383 while ($lrow = sqlFetchArray($lres)) {
384 $option_id = $lrow['option_id'];
385 if (empty($avalue[$option_id])) continue;
386 $s .= "<tr><td class='bold' valign='top'>" . $lrow['title'] . ":&nbsp;</td>";
387 $s .= "<td class='text' valign='top'>" . $avalue[$option_id] . "</td></tr>";
389 $s .= "</table>";
392 // a set of exam results; 3 radio buttons and a text field:
393 else if ($data_type == 23) {
394 $tmp = explode('|', $currvalue);
395 $avalue = array();
396 foreach ($tmp as $value) {
397 if (preg_match('/^(\w+?):(.*)$/', $value, $matches)) {
398 $avalue[$matches[1]] = $matches[2];
401 $lres = sqlStatement("SELECT * FROM list_options " .
402 "WHERE list_id = '$list_id' ORDER BY seq");
403 $s .= "<table cellpadding='0' cellspacing='0'>";
404 while ($lrow = sqlFetchArray($lres)) {
405 $option_id = $lrow['option_id'];
406 $restype = substr($avalue[$option_id], 0, 1);
407 $resnote = substr($avalue[$option_id], 2);
408 if (empty($restype) && empty($resnote)) continue;
409 $s .= "<tr><td class='bold' valign='top'>" . $lrow['title'] . "&nbsp;</td>";
410 $restype = ($restype == '1') ? 'Normal' : (($restype == '2') ? 'Abnormal' : 'N/A');
411 $s .= "<td class='text' valign='top'>$restype</td></tr>";
412 $s .= "<td class='text' valign='top'>$resnote</td></tr>";
413 $s .= "</tr>";
415 $s .= "</table>";
418 // the list of active allergies for the current patient
419 else if ($data_type == 24) {
420 $query = "SELECT title, comments FROM lists WHERE " .
421 "pid = '" . $GLOBALS['pid'] . "' AND type = 'allergy' AND enddate IS NULL " .
422 "ORDER BY begdate";
423 // echo "<!-- $query -->\n"; // debugging
424 $lres = sqlStatement($query);
425 $count = 0;
426 while ($lrow = sqlFetchArray($lres)) {
427 if ($count++) $s .= "<br />";
428 $s .= $lrow['title'];
429 if ($lrow['comments']) $s .= ' (' . $lrow['comments'] . ')';
433 return $s;
436 $CPR = 4; // cells per row of generic data
437 $last_group = '';
438 $cell_count = 0;
439 $item_count = 0;
441 function disp_end_cell() {
442 global $item_count, $cell_count;
443 if ($item_count > 0) {
444 echo "</td>";
445 $item_count = 0;
449 function disp_end_row() {
450 global $cell_count, $CPR;
451 disp_end_cell();
452 if ($cell_count > 0) {
453 for (; $cell_count < $CPR; ++$cell_count) echo "<td></td>";
454 echo "</tr>\n";
455 $cell_count = 0;
459 function disp_end_group() {
460 global $last_group;
461 if (strlen($last_group) > 0) {
462 disp_end_row();
466 function display_layout_rows($formtype, $result1, $result2='') {
467 global $item_count, $cell_count, $last_group, $CPR;
469 $fres = sqlStatement("SELECT * FROM layout_options " .
470 "WHERE form_id = '$formtype' AND uor > 0 " .
471 "ORDER BY group_name, seq");
473 while ($frow = sqlFetchArray($fres)) {
474 $this_group = $frow['group_name'];
475 $titlecols = $frow['titlecols'];
476 $datacols = $frow['datacols'];
477 $data_type = $frow['data_type'];
478 $field_id = $frow['field_id'];
479 $list_id = $frow['list_id'];
480 $currvalue = '';
482 if ($formtype == 'DEM') {
483 if ($GLOBALS['athletic_team']) {
484 // Skip fitness level and return-to-play date because those appear
485 // in a special display/update form on this page.
486 if ($field_id === 'fitness' || $field_id === 'userdate1') continue;
488 if (strpos($field_id, 'em_') === 0) {
489 $tmp = substr($field_id, 3);
490 if (isset($result2[$tmp])) $currvalue = $result2[$tmp];
492 else {
493 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
496 else {
497 if (isset($result1[$field_id])) $currvalue = $result1[$field_id];
500 // Handle a data category (group) change.
501 if (strcmp($this_group, $last_group) != 0) {
502 disp_end_group();
503 $group_name = substr($this_group, 1);
504 $last_group = $this_group;
507 // Handle starting of a new row.
508 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
509 disp_end_row();
510 echo " <tr><td class='bold' style='padding-right:5pt' valign='top'>";
511 if ($group_name) {
512 echo "<font color='#008800'>$group_name</font>";
513 $group_name = '';
514 } else {
515 echo '&nbsp;';
517 echo "</td>";
520 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
522 // Handle starting of a new label cell.
523 if ($titlecols > 0) {
524 disp_end_cell();
525 echo "<td class='bold' colspan='$titlecols' valign='top'";
526 if ($cell_count == 2) echo " style='padding-left:10pt'";
527 echo ">";
528 $cell_count += $titlecols;
530 ++$item_count;
532 if ($frow['title']) echo $frow['title'] . ":"; else echo "&nbsp;";
534 // Handle starting of a new data cell.
535 if ($datacols > 0) {
536 disp_end_cell();
537 echo "<td colspan='$datacols' class='text' valign='top'";
538 if ($cell_count > 0) echo " style='padding-left:5pt'";
539 echo ">";
540 $cell_count += $datacols;
543 ++$item_count;
544 echo generate_display_field($frow, $currvalue);
547 disp_end_group();