Fixes for encounter date display when showing historical LBF data.
[openemr.git] / interface / forms / LBF / new.php
blobb269681e67cd739faa939d2d3c74003683af54da
1 <?php
2 // Copyright (C) 2009-2011 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 require_once("../../globals.php");
10 require_once("$srcdir/api.inc");
11 require_once("$srcdir/forms.inc");
12 require_once("$srcdir/options.inc.php");
13 require_once("$srcdir/patient.inc");
14 require_once("$srcdir/formdata.inc.php");
15 require_once("$srcdir/formatting.inc.php");
17 $CPR = 4; // cells per row
19 $pprow = array();
21 // $is_lbf is defined in trend_form.php and indicates that we are being
22 // invoked from there; in that case the current encounter is irrelevant.
23 if (empty($is_lbf) && !$encounter) {
24 die("Internal error: we do not seem to be in an encounter!");
27 function end_cell() {
28 global $item_count, $cell_count, $historical_ids;
29 if ($item_count > 0) {
30 echo "</td>";
32 foreach ($historical_ids as $key => $dummy) {
33 $historical_ids[$key] .= "</td>";
36 $item_count = 0;
40 function end_row() {
41 global $cell_count, $CPR, $historical_ids;
42 end_cell();
43 if ($cell_count > 0) {
44 for (; $cell_count < $CPR; ++$cell_count) {
45 echo "<td></td>";
46 foreach ($historical_ids as $key => $dummy) {
47 $historical_ids[$key] .= "<td></td>";
51 foreach ($historical_ids as $key => $dummy) {
52 echo $historical_ids[$key];
55 echo "</tr>\n";
56 $cell_count = 0;
60 function end_group() {
61 global $last_group;
62 if (strlen($last_group) > 0) {
63 end_row();
64 echo " </table>\n";
65 // No div for an empty group name.
66 if (strlen($last_group) > 1) echo "</div>\n";
70 $formname = formData('formname', 'G');
71 $formid = 0 + formData('id', 'G');
73 // Get title and number of history columns for this form.
74 $tmp = sqlQuery("SELECT title, option_value FROM list_options WHERE " .
75 "list_id = 'lbfnames' AND option_id = '$formname'");
76 $formtitle = $tmp['title'];
77 $formhistory = 0 + $tmp['option_value'];
79 $newid = 0;
81 // If Save was clicked, save the info.
83 if ($_POST['bn_save']) {
84 $sets = "";
85 $fres = sqlStatement("SELECT * FROM layout_options " .
86 "WHERE form_id = '$formname' AND uor > 0 AND field_id != '' AND " .
87 "edit_options != 'H' " .
88 "ORDER BY group_name, seq");
89 while ($frow = sqlFetchArray($fres)) {
90 $field_id = $frow['field_id'];
91 $value = get_layout_form_value($frow);
92 if ($formid) { // existing form
93 if ($value === '') {
94 $query = "DELETE FROM lbf_data WHERE " .
95 "form_id = '$formid' AND field_id = '$field_id'";
97 else {
98 $query = "REPLACE INTO lbf_data SET field_value = '$value', " .
99 "form_id = '$formid', field_id = '$field_id'";
101 sqlStatement($query);
103 else { // new form
104 if ($value !== '') {
105 if ($newid) {
106 sqlStatement("INSERT INTO lbf_data " .
107 "( form_id, field_id, field_value ) " .
108 " VALUES ( '$newid', '$field_id', '$value' )");
110 else {
111 $newid = sqlInsert("INSERT INTO lbf_data " .
112 "( field_id, field_value ) " .
113 " VALUES ( '$field_id', '$value' )");
116 // Note that a completely empty form will not be created at all!
120 if (!$formid && $newid) {
121 addForm($encounter, $formtitle, $newid, $formname, $pid, $userauthorized);
124 formHeader("Redirecting....");
125 formJump();
126 formFooter();
127 exit;
130 if (empty($is_lbf)) {
131 $fname = $GLOBALS['OE_SITE_DIR'] . "/LBF/$formname.plugin.php";
132 if (file_exists($fname)) include_once($fname);
135 <html>
136 <head>
137 <?php html_header_show();?>
138 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
139 <style>
141 td, input, select, textarea {
142 font-family: Arial, Helvetica, sans-serif;
143 font-size: 10pt;
146 div.section {
147 border: solid;
148 border-width: 1px;
149 border-color: #0000ff;
150 margin: 0 0 0 10pt;
151 padding: 5pt;
154 </style>
156 <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>
158 <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
159 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
160 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.1.3.2.js"></script>
161 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js"></script>
162 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.js"></script>
163 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-ui.js"></script>
164 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.easydrag.handler.beta2.js"></script>
165 <script type="text/javascript" src="../../../library/textformat.js"></script>
166 <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
167 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
168 <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>
170 <script language="JavaScript">
171 $(document).ready(function(){
172 // fancy box
173 if(window.enable_modals){
174 enable_modals();
176 if(window.tabbify){
177 tabbify();
179 // special size for
180 $(".iframe_medium").fancybox( {
181 'overlayOpacity' : 0.0,
182 'showCloseButton' : true,
183 'frameHeight' : 580,
184 'frameWidth' : 900
187 $(function(){
188 // add drag and drop functionality to fancybox
189 $("#fancy_outer").easydrag();
192 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
194 // Supports customizable forms.
195 function divclick(cb, divid) {
196 var divstyle = document.getElementById(divid).style;
197 if (cb.checked) {
198 divstyle.display = 'block';
199 } else {
200 divstyle.display = 'none';
202 return true;
205 // This is for callback by the find-code popup.
206 // Appends to or erases the current list of related codes.
207 function set_related(codetype, code, selector, codedesc) {
208 var frc = document.getElementById('form_related_code');
209 var s = frc.value;
210 if (code) {
211 if (s.length > 0) s += ';';
212 s += codetype + ':' + code;
213 } else {
214 s = '';
216 frc.value = s;
219 // This invokes the find-code popup.
220 function sel_related() {
221 dlgopen('<?php echo $rootdir ?>/patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
224 <?php if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?>
226 </script>
227 </head>
229 <body <?php echo $top_bg_line; ?> topmargin="0" rightmargin="0" leftmargin="2" bottommargin="0" marginwidth="2" marginheight="0">
230 <form method="post" action="<?php echo $rootdir ?>/forms/LBF/new.php?formname=<?php echo $formname ?>&id=<?php echo $formid ?>"
231 onsubmit="return top.restoreSession()">
233 <?php
234 if (empty($is_lbf)) {
235 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date FROM " .
236 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
237 "p.pid = '$pid' AND f.pid = '$pid' AND f.encounter = '$encounter' AND " .
238 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
239 "fe.id = f.form_id LIMIT 1");
240 echo "<p class='title' style='margin-top:8px;margin-bottom:8px;text-align:center'>\n";
241 echo "$formtitle " . xl('for') . ' ';
242 echo $enrow['fname'] . ' ' . $enrow['mname'] . ' ' . $enrow['lname'];
243 echo ' ' . htmlspecialchars(xl('on')) . ' ' . substr($enrow['date'], 0, 10);
244 echo "</p>\n";
248 <!-- This is where a chart might display. -->
249 <div id="chart"></div>
251 <?php
252 $shrow = getHistoryData($pid);
254 $fres = sqlStatement("SELECT * FROM layout_options " .
255 "WHERE form_id = '$formname' AND uor > 0 " .
256 "ORDER BY group_name, seq");
257 $last_group = '';
258 $cell_count = 0;
259 $item_count = 0;
260 $display_style = 'block';
262 // This is an array keyed on forms.form_id for other occurrences of this
263 // form type. The maximum number of such other occurrences to display is
264 // in list_options.option_value for this form's list item. Values in this
265 // array are work areas for building the ending HTML for each displayed row.
267 $historical_ids = array();
269 // True if any data items in this form can be graphed.
270 $form_is_graphable = false;
272 while ($frow = sqlFetchArray($fres)) {
273 $this_group = $frow['group_name'];
274 $titlecols = $frow['titlecols'];
275 $datacols = $frow['datacols'];
276 $data_type = $frow['data_type'];
277 $field_id = $frow['field_id'];
278 $list_id = $frow['list_id'];
279 $edit_options = $frow['edit_options'];
281 $graphable = strpos($edit_options, 'G') !== FALSE;
282 if ($graphable) $form_is_graphable = true;
284 $currvalue = '';
286 if ($frow['edit_options'] == 'H') {
287 // This data comes from static history
288 if (isset($shrow[$field_id])) $currvalue = $shrow[$field_id];
289 } else {
290 if ($formid) {
291 $pprow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
292 "form_id = '$formid' AND field_id = '$field_id'");
293 if (!empty($pprow)) $currvalue = $pprow['field_value'];
295 else {
296 // New form, see if there is a custom default from a plugin.
297 $fname = $formname . '_default_' . $field_id;
298 if (function_exists($fname)) {
299 $currvalue = call_user_func($fname);
304 // Handle a data category (group) change.
305 if (strcmp($this_group, $last_group) != 0) {
306 end_group();
307 $group_seq = 'lbf' . substr($this_group, 0, 1);
308 $group_name = substr($this_group, 1);
309 $last_group = $this_group;
311 // If group name is blank, no checkbox or div.
312 if (strlen($this_group) > 1) {
313 echo "<br /><span class='bold'><input type='checkbox' name='form_cb_$group_seq' value='1' " .
314 "onclick='return divclick(this,\"div_$group_seq\");'";
315 if ($display_style == 'block') echo " checked";
316 echo " /><b>" . htmlspecialchars(xl_layout_label($group_name)) . "</b></span>\n";
317 echo "<div id='div_$group_seq' class='section' style='display:$display_style;'>\n";
319 // echo " <table border='0' cellpadding='0' width='100%'>\n";
320 echo " <table border='0' cellpadding='0' width='100%'>\n";
321 $display_style = 'none';
323 // Initialize historical data array and write date headers.
324 $historical_ids = array();
325 if ($formhistory > 0) {
326 echo " <tr>";
327 echo "<td colspan='$CPR' align='right' class='bold'>";
328 if (empty($is_lbf)) echo htmlspecialchars(xl('Current'));
329 echo "</td>\n";
330 $hres = sqlStatement("SELECT f.form_id, fe.date " .
331 "FROM forms AS f, form_encounter AS fe WHERE " .
332 "f.pid = ? AND f.formdir = ? AND " .
333 "f.form_id != ? AND f.deleted = 0 AND " .
334 "fe.pid = f.pid AND fe.encounter = f.encounter " .
335 "ORDER BY fe.date DESC, f.encounter DESC, f.date DESC " .
336 "LIMIT ?",
337 array($pid, $formname, $formid, $formhistory));
338 // For some readings like vitals there may be multiple forms per encounter.
339 // We sort these sensibly, however only the encounter date is shown here;
340 // at some point we may wish to show also the data entry date/time.
341 while ($hrow = sqlFetchArray($hres)) {
342 $historical_ids[$hrow['form_id']] = '';
343 echo "<td colspan='$CPR' align='right' class='bold'>&nbsp;" .
344 oeFormatShortDate(substr($hrow['date'], 0, 10)) . "</td>\n";
346 echo " </tr>";
351 // Handle starting of a new row.
352 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
353 end_row();
354 echo " <tr>";
355 // Clear historical data string.
356 foreach ($historical_ids as $key => $dummy) {
357 $historical_ids[$key] = '';
361 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
363 // Handle starting of a new label cell.
364 if ($titlecols > 0) {
365 end_cell();
366 echo "<td valign='top' colspan='$titlecols' width='1%' nowrap";
367 echo " class='";
368 echo ($frow['uor'] == 2) ? "required" : "bold";
369 if ($graphable) echo " graph";
370 echo "'";
371 if ($cell_count == 2) echo " style='padding-left:10pt'";
372 if ($graphable) echo " id='$field_id'";
373 echo ">";
375 foreach ($historical_ids as $key => $dummy) {
376 $historical_ids[$key] .= "<td valign='top' colspan='$titlecols' class='text' nowrap>";
379 $cell_count += $titlecols;
381 ++$item_count;
383 echo "<b>";
384 if ($frow['title']) echo htmlspecialchars(xl_layout_label($frow['title']) . ":"); else echo "&nbsp;";
385 echo "</b>";
387 // Note the labels are not repeated in the history columns.
389 // Handle starting of a new data cell.
390 if ($datacols > 0) {
391 end_cell();
392 echo "<td valign='top' colspan='$datacols' class='text'";
393 if ($cell_count > 0) echo " style='padding-left:5pt'";
394 echo ">";
396 foreach ($historical_ids as $key => $dummy) {
397 $historical_ids[$key] .= "<td valign='top' align='right' colspan='$datacols' class='text'>";
400 $cell_count += $datacols;
403 ++$item_count;
405 // Skip current-value fields for the display-only case.
406 if (empty($is_lbf)) {
407 if ($frow['edit_options'] == 'H')
408 echo generate_display_field($frow, $currvalue);
409 else
410 generate_form_field($frow, $currvalue);
413 // Append to historical data of other dates for this item.
414 foreach ($historical_ids as $key => $dummy) {
415 $hvrow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
416 "form_id = '$key' AND field_id = '$field_id'");
417 $value = empty($hvrow) ? '' : $hvrow['field_value'];
418 $historical_ids[$key] .= generate_display_field($frow, $value);
423 end_group();
426 <p style='text-align:center'>
427 <?php if (empty($is_lbf)) { ?>
428 <input type='submit' name='bn_save' value='<?php echo htmlspecialchars(xl('Save')) ?>' />
429 &nbsp;
430 <input type='button' value='<?php echo htmlspecialchars(xl('Cancel')) ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
431 &nbsp;
432 <?php if ($form_is_graphable) { ?>
433 <input type='button' value='<?php echo htmlspecialchars(xl('Show Graph')) ?>' onclick="top.restoreSession();location='../../patient_file/encounter/trend_form.php?formname=<?php echo $formname; ?>'" />
434 &nbsp;
435 <?php } ?>
436 <?php } else { ?>
437 <input type='button' value='<?php echo htmlspecialchars(xl('Back')) ?>' onclick='window.back();' />
438 <?php } ?>
439 </p>
441 </form>
443 <!-- include support for the list-add selectbox feature -->
444 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
446 <script language="JavaScript">
447 <?php echo $date_init; ?>
448 <?php
449 if (function_exists($formname . '_javascript_onload')) {
450 call_user_func($formname . '_javascript_onload');
452 // TBD: If $alertmsg, display it with a JavaScript alert().
454 </script>
456 </body>
457 </html>