Option for showing description instead of codes in LBF Billing Code fields, and some...
[openemr.git] / interface / forms / LBF / new.php
blob895c697dbdb250303f117c1902ef18ff798d6e3d
1 <?php
2 // Copyright (C) 2009-2014 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 //SANITIZE ALL ESCAPES
10 $sanitize_all_escapes=true;
12 //STOP FAKE REGISTER GLOBALS
13 $fake_register_globals=false;
15 require_once("../../globals.php");
16 require_once("$srcdir/api.inc");
17 require_once("$srcdir/forms.inc");
18 require_once("$srcdir/options.inc.php");
19 require_once("$srcdir/patient.inc");
20 require_once("$srcdir/formdata.inc.php");
21 require_once("$srcdir/formatting.inc.php");
22 if ($GLOBALS['gbl_portal_cms_enable']) {
23 require_once("$include_root/cmsportal/portal.inc.php");
26 $CPR = 4; // cells per row
28 $pprow = array();
30 // $is_lbf is defined in trend_form.php and indicates that we are being
31 // invoked from there; in that case the current encounter is irrelevant.
32 if (empty($is_lbf) && !$encounter) {
33 die("Internal error: we do not seem to be in an encounter!");
36 function end_cell() {
37 global $item_count, $cell_count, $historical_ids;
38 if ($item_count > 0) {
39 echo "&nbsp;</td>";
41 foreach ($historical_ids as $key => $dummy) {
42 $historical_ids[$key] .= "&nbsp;</td>";
45 $item_count = 0;
49 function end_row() {
50 global $cell_count, $CPR, $historical_ids;
51 end_cell();
52 if ($cell_count > 0) {
53 for (; $cell_count < $CPR; ++$cell_count) {
54 echo "<td></td>";
55 foreach ($historical_ids as $key => $dummy) {
56 $historical_ids[$key] .= "<td></td>";
60 foreach ($historical_ids as $key => $dummy) {
61 echo $historical_ids[$key];
64 echo "</tr>\n";
65 $cell_count = 0;
69 function end_group() {
70 global $last_group;
71 if (strlen($last_group) > 0) {
72 end_row();
73 echo " </table>\n";
74 // No div for an empty group name.
75 if (strlen($last_group) > 1) echo "</div>\n";
79 $formname = isset($_GET['formname']) ? $_GET['formname'] : '';
80 $formid = isset($_GET['id'] ) ? intval($_GET['id']) : 0;
81 $portalid = isset($_GET['portalid']) ? intval($_GET['portalid']) : 0;
83 // Get title and number of history columns for this form.
84 $tmp = sqlQuery("SELECT title, option_value FROM list_options WHERE " .
85 "list_id = 'lbfnames' AND option_id = ?", array($formname) );
86 $formtitle = $tmp['title'];
87 $formhistory = 0 + $tmp['option_value'];
89 $newid = 0;
91 if (empty($is_lbf)) {
92 $fname = $GLOBALS['OE_SITE_DIR'] . "/LBF/$formname.plugin.php";
93 if (file_exists($fname)) include_once($fname);
96 // If Save was clicked, save the info.
98 if ($_POST['bn_save']) {
99 $sets = "";
100 $fres = sqlStatement("SELECT * FROM layout_options " .
101 "WHERE form_id = ? AND uor > 0 AND field_id != '' AND " .
102 "edit_options != 'H' " .
103 "ORDER BY group_name, seq", array($formname) );
104 while ($frow = sqlFetchArray($fres)) {
105 $field_id = $frow['field_id'];
106 $value = get_layout_form_value($frow);
107 $sql_bind_array = array();
108 if ($formid) { // existing form
109 if ($value === '') {
110 $query = "DELETE FROM lbf_data WHERE " .
111 "form_id = ? AND field_id = ?";
112 array_push($sql_bind_array,$formid,$field_id);
114 else {
115 $query = "REPLACE INTO lbf_data SET field_value = ?, " .
116 "form_id = ?, field_id = ?";
117 array_push($sql_bind_array,$value,$formid,$field_id);
119 sqlStatement($query,$sql_bind_array);
121 else { // new form
122 if ($value !== '') {
123 if ($newid) {
124 sqlStatement("INSERT INTO lbf_data " .
125 "( form_id, field_id, field_value ) " .
126 " VALUES (?,?,?)", array($newid, $field_id, $value) );
128 else {
129 $newid = sqlInsert("INSERT INTO lbf_data " .
130 "( field_id, field_value ) " .
131 " VALUES (?,?)", array($field_id, $value) );
134 // Note that a completely empty form will not be created at all!
139 if (!$formid && $newid) {
140 addForm($encounter, $formtitle, $newid, $formname, $pid, $userauthorized);
143 if ($portalid) {
144 // Delete the request from the portal.
145 $result = cms_portal_call(array('action' => 'delpost', 'postid' => $portalid));
146 if ($result['errmsg']) {
147 die(text($result['errmsg']));
151 // Support custom behavior at save time, such as going to another form.
152 if (function_exists($formname . '_save_exit')) {
153 if (call_user_func($formname . '_save_exit')) exit;
155 formHeader("Redirecting....");
156 formJump();
157 formFooter();
158 exit;
162 <html>
163 <head>
164 <?php html_header_show();?>
165 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
166 <style>
168 td, input, select, textarea {
169 font-family: Arial, Helvetica, sans-serif;
170 font-size: 10pt;
173 div.section {
174 border: solid;
175 border-width: 1px;
176 border-color: #0000ff;
177 margin: 0 0 0 10pt;
178 padding: 5pt;
181 </style>
183 <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>
185 <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
186 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
187 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.1.3.2.js"></script>
188 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js"></script>
189 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.js"></script>
190 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-ui.js"></script>
191 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.easydrag.handler.beta2.js"></script>
192 <script type="text/javascript" src="../../../library/textformat.js"></script>
193 <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
194 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
195 <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>
197 <script language="JavaScript">
198 $(document).ready(function() {
199 // fancy box
200 if (window.enable_modals) {
201 enable_modals();
203 if(window.tabbify){
204 tabbify();
206 // special size for
207 $(".iframe_medium").fancybox({
208 'overlayOpacity' : 0.0,
209 'showCloseButton' : true,
210 'frameHeight' : 580,
211 'frameWidth' : 900
213 $(function() {
214 // add drag and drop functionality to fancybox
215 $("#fancy_outer").easydrag();
219 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
221 // Supports customizable forms.
222 function divclick(cb, divid) {
223 var divstyle = document.getElementById(divid).style;
224 if (cb.checked) {
225 divstyle.display = 'block';
226 } else {
227 divstyle.display = 'none';
229 return true;
232 // The ID of the input element to receive a found code.
233 var current_sel_name = '';
235 // This is for callback by the find-code popup.
236 // Appends to or erases the current list of related codes.
237 function set_related(codetype, code, selector, codedesc) {
238 var f = document.forms[0];
239 // frc will be the input element containing the codes.
240 // frcd, if set, will be the input element containing their descriptions.
241 var frc = f[current_sel_name];
242 var frcd;
243 var matches = current_sel_name.match(/^(.*)__desc$/);
244 if (matches) {
245 frcd = frc;
246 frc = f[matches[1]];
248 var s = frc.value;
249 var sd = frcd ? frcd.value : s;
250 if (code) {
251 if (codetype != 'PROD') {
252 if (s.indexOf(codetype + ':') == 0 || s.indexOf(';' + codetype + ':') > 0) {
253 return '<?php echo xl('A code of this type is already selected. Erase the field first if you need to replace it.') ?>';
256 if (s.length > 0) {
257 s += ';';
258 sd += ';';
260 s += codetype + ':' + code;
261 sd += codedesc;
262 } else {
263 s = '';
264 sd = '';
266 frc.value = s;
267 if (frcd) frcd.value = sd;
268 return '';
271 // This invokes the find-code popup.
272 function sel_related(elem, codetype) {
273 current_sel_name = elem.name;
274 var url = '<?php echo $rootdir ?>/patient_file/encounter/find_code_popup.php';
275 if (codetype) url += '?codetype=' + codetype;
276 dlgopen(url, '_blank', 500, 400);
279 // Validation logic for form submission.
280 function validate(f) {
281 <?php generate_layout_validation($formname); ?>
282 top.restoreSession();
283 return true;
285 <?php if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?>
287 </script>
288 </head>
290 <body <?php echo $top_bg_line; ?> topmargin="0" rightmargin="0" leftmargin="2" bottommargin="0" marginwidth="2" marginheight="0">
292 <?php
293 echo "<form method='post' " .
294 "action='$rootdir/forms/LBF/new.php?formname=$formname&id=$formid&portalid=$portalid' " .
295 "onsubmit='return validate(this)'>\n";
297 $cmsportal_login = '';
298 $portalres = FALSE;
299 if (empty($is_lbf)) {
300 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, p.cmsportal_login, " .
301 "fe.date FROM " .
302 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
303 "p.pid = ? AND f.pid = p.pid AND f.encounter = ? AND " .
304 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
305 "fe.id = f.form_id LIMIT 1", array($pid, $encounter));
306 echo "<p class='title' style='margin-top:8px;margin-bottom:8px;text-align:center'>\n";
307 echo text($formtitle) . " " . xlt('for') . ' ';
308 echo text($enrow['fname']) . ' ' . text($enrow['mname']) . ' ' . text($enrow['lname']);
309 echo ' ' . xlt('on') . ' ' . text(oeFormatShortDate(substr($enrow['date'], 0, 10)));
310 echo "</p>\n";
311 $cmsportal_login = $enrow['cmsportal_login'];
313 // If loading data from portal, get the data.
314 if ($GLOBALS['gbl_portal_cms_enable'] && $portalid) {
315 $portalres = cms_portal_call(array('action' => 'getpost', 'postid' => $portalid));
316 if ($portalres['errmsg']) {
317 die(text($portalres['errmsg']));
322 <!-- This is where a chart might display. -->
323 <div id="chart"></div>
325 <?php
326 $shrow = getHistoryData($pid);
328 $fres = sqlStatement("SELECT * FROM layout_options " .
329 "WHERE form_id = ? AND uor > 0 " .
330 "ORDER BY group_name, seq", array($formname) );
331 $last_group = '';
332 $cell_count = 0;
333 $item_count = 0;
334 $display_style = 'block';
336 // This is an array keyed on forms.form_id for other occurrences of this
337 // form type. The maximum number of such other occurrences to display is
338 // in list_options.option_value for this form's list item. Values in this
339 // array are work areas for building the ending HTML for each displayed row.
341 $historical_ids = array();
343 // True if any data items in this form can be graphed.
344 $form_is_graphable = false;
346 while ($frow = sqlFetchArray($fres)) {
347 $this_group = $frow['group_name'];
348 $titlecols = $frow['titlecols'];
349 $datacols = $frow['datacols'];
350 $data_type = $frow['data_type'];
351 $field_id = $frow['field_id'];
352 $list_id = $frow['list_id'];
353 $edit_options = $frow['edit_options'];
355 $graphable = strpos($edit_options, 'G') !== FALSE;
356 if ($graphable) $form_is_graphable = true;
358 $currvalue = '';
360 if ($frow['edit_options'] == 'H') {
361 // This data comes from static history
362 if (isset($shrow[$field_id])) $currvalue = $shrow[$field_id];
363 } else {
364 if ($formid) {
365 $pprow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
366 "form_id = ? AND field_id = ?", array($formid, $field_id) );
367 if (!empty($pprow)) $currvalue = $pprow['field_value'];
369 else {
370 // This is a new form.
371 // Get data from the CMS portal if applicable.
372 /*************************************************************
373 if ($portalres && isset($portalres['fields'][$field_id])) {
374 $currvalue = $portalres['fields'][$field_id];
375 $currvalue = cms_field_to_lbf($currvalue, $data_type, $field_id);
377 *************************************************************/
378 if ($portalres) {
379 $currvalue = cms_field_to_lbf($data_type, $field_id, $portalres['fields']);
381 if ($currvalue === '') {
382 // Still no data. See if there is a custom default from a plugin.
383 $fname = $formname . '_default_' . $field_id;
384 if (function_exists($fname)) {
385 $currvalue = call_user_func($fname);
391 // Handle a data category (group) change.
392 if (strcmp($this_group, $last_group) != 0) {
393 end_group();
394 $group_seq = 'lbf' . substr($this_group, 0, 1);
395 $group_name = substr($this_group, 1);
396 $last_group = $this_group;
398 // If group name is blank, no checkbox or div.
399 if (strlen($this_group) > 1) {
400 echo "<br /><span class='bold'><input type='checkbox' name='form_cb_" . attr($group_seq) . "' value='1' " .
401 "onclick='return divclick(this,\"div_" . attr(addslashes($group_seq)) . "\");'";
402 if ($display_style == 'block') echo " checked";
403 echo " /><b>" . text(xl_layout_label($group_name)) . "</b></span>\n";
404 echo "<div id='div_" . attr($group_seq) . "' class='section' style='display:" . attr($display_style) . ";'>\n";
406 // echo " <table border='0' cellpadding='0' width='100%'>\n";
407 echo " <table border='0' cellspacing='0' cellpadding='0'>\n";
408 $display_style = 'none';
410 // Initialize historical data array and write date headers.
411 $historical_ids = array();
412 if ($formhistory > 0) {
413 echo " <tr>";
414 echo "<td colspan='" . attr($CPR) . "' align='right' class='bold'>";
415 if (empty($is_lbf)){
416 // Including actual date per IPPF request 2012-08-23.
417 echo oeFormatShortDate(substr($enrow['date'], 0, 10));
418 echo ' (' . htmlspecialchars(xl('Current')) . ')';
420 echo "&nbsp;</td>\n";
421 $hres = sqlStatement("SELECT f.form_id, fe.date " .
422 "FROM forms AS f, form_encounter AS fe WHERE " .
423 "f.pid = ? AND f.formdir = ? AND " .
424 "f.form_id != ? AND f.deleted = 0 AND " .
425 "fe.pid = f.pid AND fe.encounter = f.encounter " .
426 "ORDER BY fe.date DESC, f.encounter DESC, f.date DESC " .
427 "LIMIT ?",
428 array($pid, $formname, $formid, $formhistory));
429 // For some readings like vitals there may be multiple forms per encounter.
430 // We sort these sensibly, however only the encounter date is shown here;
431 // at some point we may wish to show also the data entry date/time.
432 while ($hrow = sqlFetchArray($hres)) {
434 echo "<td colspan='".attr($CPR)."' align='right' class='bold' style='";
435 echo "border-top:1px solid black;";
436 echo "border-right:1px solid black;";
437 echo "border-bottom:1px solid black;";
438 if (empty($historical_ids)) { echo "border-left:1px solid black;"; }
439 echo "'>" .
440 oeFormatShortDate(substr($hrow['date'], 0, 10)) . "&nbsp;</td>\n";
441 $historical_ids[$hrow['form_id']] = '';
443 echo " </tr>";
448 // Handle starting of a new row.
449 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
450 end_row();
451 echo " <tr>";
452 // Clear historical data string.
453 foreach ($historical_ids as $key => $dummy) {
454 $historical_ids[$key] = '';
458 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
460 // First item is on the "left-border"
461 $leftborder = true;
463 // Handle starting of a new label cell.
464 if ($titlecols > 0) {
465 end_cell();
466 echo "<td valign='top' colspan='" . attr($titlecols) . "' nowrap";
467 echo " class='";
468 echo ($frow['uor'] == 2) ? "required" : "bold";
469 if ($graphable) echo " graph";
470 echo "'";
471 if ($cell_count == 2) echo " style='padding-left:10pt'";
472 if ($graphable) echo " id='" . attr($field_id) . "'";
473 echo ">";
475 foreach ($historical_ids as $key => $dummy) {
476 $historical_ids[$key] .= "<td valign='top' colspan='" . attr($titlecols) . "' class='text' style='";
477 $historical_ids[$key] .= "border-bottom:1px solid black;";
478 if ($leftborder) $historical_ids[$key] .= "border-left:1px solid black;";
479 if (!$datacols ) $historical_ids[$key] .= "border-right:1px solid black;";
480 $historical_ids[$key] .= "' nowrap>";
481 $leftborder = false;
485 $cell_count += $titlecols;
487 ++$item_count;
489 echo "<b>";
490 if ($frow['title']) echo text(xl_layout_label($frow['title']) . ":"); else echo "&nbsp;";
491 echo "</b>";
493 // Note the labels are not repeated in the history columns.
495 // Handle starting of a new data cell.
496 if ($datacols > 0) {
497 end_cell();
498 echo "<td valign='top' colspan='" . attr($datacols) . "' class='text'";
499 if ($cell_count > 0) echo " style='padding-left:5pt'";
500 echo ">";
502 foreach ($historical_ids as $key => $dummy) {
503 $historical_ids[$key] .= "<td valign='top' align='right' colspan='" . attr($datacols) . "' class='text' style='";
504 $historical_ids[$key] .= "border-bottom:1px solid black;";
505 $historical_ids[$key] .= "border-right:1px solid black;";
506 if ($leftborder) $historical_ids[$key] .= "border-left:1px solid black;";
507 $historical_ids[$key] .= "'>";
508 $leftborder = false;
511 $cell_count += $datacols;
514 ++$item_count;
516 // Skip current-value fields for the display-only case.
517 if (empty($is_lbf)) {
518 if ($frow['edit_options'] == 'H')
519 echo generate_display_field($frow, $currvalue);
520 else
521 generate_form_field($frow, $currvalue);
524 // Append to historical data of other dates for this item.
525 foreach ($historical_ids as $key => $dummy) {
526 $hvrow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
527 "form_id = ? AND field_id = ?", array($key, $field_id) );
528 $value = empty($hvrow) ? '' : $hvrow['field_value'];
529 $historical_ids[$key] .= generate_display_field($frow, $value);
534 end_group();
537 <p style='text-align:center'>
538 <?php if (empty($is_lbf)) { ?>
539 <input type='submit' name='bn_save' value='<?php echo xla('Save') ?>' />
540 <?php
541 if (function_exists($formname . '_additional_buttons')) {
542 // Allow the plug-in to insert more action buttons here.
543 call_user_func($formname . '_additional_buttons');
546 &nbsp;
547 <input type='button' value='<?php echo xla('Cancel') ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
548 &nbsp;
549 <?php if ($form_is_graphable) { ?>
550 <input type='button' value='<?php echo xla('Show Graph') ?>' onclick="top.restoreSession();location='../../patient_file/encounter/trend_form.php?formname=<?php echo attr($formname); ?>'" />
551 &nbsp;
552 <?php } ?>
553 <?php } else { ?>
554 <input type='button' value='<?php echo xla('Back') ?>' onclick='window.history.back();' />
555 <?php } ?>
556 </p>
558 </form>
560 <!-- include support for the list-add selectbox feature -->
561 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
563 <script language="JavaScript">
564 <?php echo $date_init; ?>
565 <?php
566 if (function_exists($formname . '_javascript_onload')) {
567 call_user_func($formname . '_javascript_onload');
570 // TBD: If $alertmsg, display it with a JavaScript alert().
572 // New form and this patient has a portal login and we have not loaded portal data.
573 // Check if there is portal data pending for this patient and form type.
574 if (!$formid && $GLOBALS['gbl_portal_cms_enable'] && $cmsportal_login && !$portalid) {
575 $portalres = cms_portal_call(array('action' => 'checkptform', 'form' => $formname, 'patient' => $cmsportal_login));
576 if ($portalres['errmsg']) {
577 die(text($portalres['errmsg'])); // TBD: Change to alertmsg
579 $portalid = $portalres['postid'];
580 if ($portalid) {
581 echo "if (confirm('" . xls('The portal has data for this patient and form. Load it now?') . "')) {\n";
582 echo " top.restoreSession();\n";
583 echo " document.location.href = 'load_form.php?formname=$formname&portalid=$portalid';\n";
584 echo "}\n";
588 </script>
590 </body>
591 </html>