Nation Notes module contributed by Z&H Healthcare.
[openemr.git] / interface / forms / LBF / new.php
bloba4c25eecbcf65f0aee0f97b1cd3d0838890dfda5
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");
16 $CPR = 4; // cells per row
18 $pprow = array();
20 // $is_lbf is defined in trend_form.php and indicates that we are being
21 // invoked from there; in that case the current encounter is irrelevant.
22 if (empty($is_lbf) && !$encounter) {
23 die("Internal error: we do not seem to be in an encounter!");
26 function end_cell() {
27 global $item_count, $cell_count, $historical_ids;
28 if ($item_count > 0) {
29 echo "</td>";
31 foreach ($historical_ids as $key => $dummy) {
32 $historical_ids[$key] .= "</td>";
35 $item_count = 0;
39 function end_row() {
40 global $cell_count, $CPR, $historical_ids;
41 end_cell();
42 if ($cell_count > 0) {
43 for (; $cell_count < $CPR; ++$cell_count) {
44 echo "<td></td>";
45 foreach ($historical_ids as $key => $dummy) {
46 $historical_ids[$key] .= "<td></td>";
50 foreach ($historical_ids as $key => $dummy) {
51 echo $historical_ids[$key];
54 echo "</tr>\n";
55 $cell_count = 0;
59 function end_group() {
60 global $last_group;
61 if (strlen($last_group) > 0) {
62 end_row();
63 echo " </table>\n";
64 // No div for an empty group name.
65 if (strlen($last_group) > 1) echo "</div>\n";
69 $formname = formData('formname', 'G');
70 $formid = 0 + formData('id', 'G');
72 // Get title and number of history columns for this form.
73 $tmp = sqlQuery("SELECT title, option_value FROM list_options WHERE " .
74 "list_id = 'lbfnames' AND option_id = '$formname'");
75 $formtitle = $tmp['title'];
76 $formhistory = 0 + $tmp['option_value'];
78 $newid = 0;
80 // If Save was clicked, save the info.
82 if ($_POST['bn_save']) {
83 $sets = "";
84 $fres = sqlStatement("SELECT * FROM layout_options " .
85 "WHERE form_id = '$formname' AND uor > 0 AND field_id != '' AND " .
86 "edit_options != 'H' " .
87 "ORDER BY group_name, seq");
88 while ($frow = sqlFetchArray($fres)) {
89 $field_id = $frow['field_id'];
90 $value = get_layout_form_value($frow);
91 if ($formid) { // existing form
92 if ($value === '') {
93 $query = "DELETE FROM lbf_data WHERE " .
94 "form_id = '$formid' AND field_id = '$field_id'";
96 else {
97 $query = "REPLACE INTO lbf_data SET field_value = '$value', " .
98 "form_id = '$formid', field_id = '$field_id'";
100 sqlStatement($query);
102 else { // new form
103 if ($value !== '') {
104 if ($newid) {
105 sqlStatement("INSERT INTO lbf_data " .
106 "( form_id, field_id, field_value ) " .
107 " VALUES ( '$newid', '$field_id', '$value' )");
109 else {
110 $newid = sqlInsert("INSERT INTO lbf_data " .
111 "( field_id, field_value ) " .
112 " VALUES ( '$field_id', '$value' )");
115 // Note that a completely empty form will not be created at all!
119 if (!$formid && $newid) {
120 addForm($encounter, $formtitle, $newid, $formname, $pid, $userauthorized);
123 formHeader("Redirecting....");
124 formJump();
125 formFooter();
126 exit;
129 if (empty($is_lbf)) {
130 $fname = $GLOBALS['OE_SITE_DIR'] . "/LBF/$formname.plugin.php";
131 if (file_exists($fname)) include_once($fname);
134 <html>
135 <head>
136 <?php html_header_show();?>
137 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
138 <style>
140 td, input, select, textarea {
141 font-family: Arial, Helvetica, sans-serif;
142 font-size: 10pt;
145 div.section {
146 border: solid;
147 border-width: 1px;
148 border-color: #0000ff;
149 margin: 0 0 0 10pt;
150 padding: 5pt;
153 </style>
155 <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>
157 <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
158 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
159 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.1.3.2.js"></script>
160 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js"></script>
161 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.js"></script>
162 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-ui.js"></script>
163 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.easydrag.handler.beta2.js"></script>
164 <script type="text/javascript" src="../../../library/textformat.js"></script>
165 <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
166 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
167 <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>
169 <script language="JavaScript">
170 $(document).ready(function(){
171 // fancy box
172 if(window.enable_modals){
173 enable_modals();
175 if(window.tabbify){
176 tabbify();
178 // special size for
179 $(".iframe_medium").fancybox( {
180 'overlayOpacity' : 0.0,
181 'showCloseButton' : true,
182 'frameHeight' : 580,
183 'frameWidth' : 900
186 $(function(){
187 // add drag and drop functionality to fancybox
188 $("#fancy_outer").easydrag();
191 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
193 // Supports customizable forms.
194 function divclick(cb, divid) {
195 var divstyle = document.getElementById(divid).style;
196 if (cb.checked) {
197 divstyle.display = 'block';
198 } else {
199 divstyle.display = 'none';
201 return true;
204 // This is for callback by the find-code popup.
205 // Appends to or erases the current list of related codes.
206 function set_related(codetype, code, selector, codedesc) {
207 var frc = document.getElementById('form_related_code');
208 var s = frc.value;
209 if (code) {
210 if (s.length > 0) s += ';';
211 s += codetype + ':' + code;
212 } else {
213 s = '';
215 frc.value = s;
218 // This invokes the find-code popup.
219 function sel_related() {
220 dlgopen('<?php echo $rootdir ?>/patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
223 <?php if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?>
225 </script>
226 </head>
228 <body <?php echo $top_bg_line; ?> topmargin="0" rightmargin="0" leftmargin="2" bottommargin="0" marginwidth="2" marginheight="0">
229 <form method="post" action="<?php echo $rootdir ?>/forms/LBF/new.php?formname=<?php echo $formname ?>&id=<?php echo $formid ?>"
230 onsubmit="return top.restoreSession()">
232 <?php
233 if (empty($is_lbf)) {
234 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date FROM " .
235 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
236 "p.pid = '$pid' AND f.pid = '$pid' AND f.encounter = '$encounter' AND " .
237 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
238 "fe.id = f.form_id LIMIT 1");
239 echo "<p class='title' style='margin-top:8px;margin-bottom:8px;text-align:center'>\n";
240 echo "$formtitle " . xl('for') . ' ';
241 echo $enrow['fname'] . ' ' . $enrow['mname'] . ' ' . $enrow['lname'];
242 echo ' ' . htmlspecialchars(xl('on')) . ' ' . substr($enrow['date'], 0, 10);
243 echo "</p>\n";
247 <!-- This is where a chart might display. -->
248 <div id="chart"></div>
250 <?php
251 $shrow = getHistoryData($pid);
253 $fres = sqlStatement("SELECT * FROM layout_options " .
254 "WHERE form_id = '$formname' AND uor > 0 " .
255 "ORDER BY group_name, seq");
256 $last_group = '';
257 $cell_count = 0;
258 $item_count = 0;
259 $display_style = 'block';
261 // This is an array keyed on forms.form_id for other occurrences of this
262 // form type. The maximum number of such other occurrences to display is
263 // in list_options.option_value for this form's list item. Values in this
264 // array are work areas for building the ending HTML for each displayed row.
266 $historical_ids = array();
268 // True if any data items in this form can be graphed.
269 $form_is_graphable = false;
271 while ($frow = sqlFetchArray($fres)) {
272 $this_group = $frow['group_name'];
273 $titlecols = $frow['titlecols'];
274 $datacols = $frow['datacols'];
275 $data_type = $frow['data_type'];
276 $field_id = $frow['field_id'];
277 $list_id = $frow['list_id'];
278 $edit_options = $frow['edit_options'];
280 $graphable = strpos($edit_options, 'G') !== FALSE;
281 if ($graphable) $form_is_graphable = true;
283 $currvalue = '';
285 if ($frow['edit_options'] == 'H') {
286 // This data comes from static history
287 if (isset($shrow[$field_id])) $currvalue = $shrow[$field_id];
288 } else {
289 if ($formid) {
290 $pprow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
291 "form_id = '$formid' AND field_id = '$field_id'");
292 if (!empty($pprow)) $currvalue = $pprow['field_value'];
294 else {
295 // New form, see if there is a custom default from a plugin.
296 $fname = $formname . '_default_' . $field_id;
297 if (function_exists($fname)) {
298 $currvalue = call_user_func($fname);
303 // Handle a data category (group) change.
304 if (strcmp($this_group, $last_group) != 0) {
305 end_group();
306 $group_seq = 'lbf' . substr($this_group, 0, 1);
307 $group_name = substr($this_group, 1);
308 $last_group = $this_group;
310 // If group name is blank, no checkbox or div.
311 if (strlen($this_group) > 1) {
312 echo "<br /><span class='bold'><input type='checkbox' name='form_cb_$group_seq' value='1' " .
313 "onclick='return divclick(this,\"div_$group_seq\");'";
314 if ($display_style == 'block') echo " checked";
315 echo " /><b>" . htmlspecialchars(xl_layout_label($group_name)) . "</b></span>\n";
316 echo "<div id='div_$group_seq' class='section' style='display:$display_style;'>\n";
318 // echo " <table border='0' cellpadding='0' width='100%'>\n";
319 echo " <table border='0' cellpadding='0' width='100%'>\n";
320 $display_style = 'none';
322 // Initialize historical data array and write date headers.
323 $historical_ids = array();
324 if ($formhistory > 0) {
325 echo " <tr>";
326 echo "<td colspan='$CPR' align='right' class='bold'>";
327 if (empty($is_lbf)) echo htmlspecialchars(xl('Current'));
328 echo "</td>\n";
329 $hres = sqlStatement("SELECT date, form_id FROM forms WHERE " .
330 "pid = '$pid' AND formdir = '$formname' AND " .
331 "form_id != '$formid' AND deleted = 0 " .
332 "ORDER BY date DESC LIMIT $formhistory");
333 while ($hrow = sqlFetchArray($hres)) {
334 $historical_ids[$hrow['form_id']] = '';
335 echo "<td colspan='$CPR' align='right' class='bold'>&nbsp;" . $hrow['date'] . "</td>\n";
336 // TBD: Format date per globals.
338 echo " </tr>";
343 // Handle starting of a new row.
344 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
345 end_row();
346 echo " <tr>";
347 // Clear historical data string.
348 foreach ($historical_ids as $key => $dummy) {
349 $historical_ids[$key] = '';
353 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
355 // Handle starting of a new label cell.
356 if ($titlecols > 0) {
357 end_cell();
358 echo "<td valign='top' colspan='$titlecols' width='1%' nowrap";
359 echo " class='";
360 echo ($frow['uor'] == 2) ? "required" : "bold";
361 if ($graphable) echo " graph";
362 echo "'";
363 if ($cell_count == 2) echo " style='padding-left:10pt'";
364 if ($graphable) echo " id='$field_id'";
365 echo ">";
367 foreach ($historical_ids as $key => $dummy) {
368 $historical_ids[$key] .= "<td valign='top' colspan='$titlecols' class='text' nowrap>";
371 $cell_count += $titlecols;
373 ++$item_count;
375 echo "<b>";
376 if ($frow['title']) echo htmlspecialchars(xl_layout_label($frow['title']) . ":"); else echo "&nbsp;";
377 echo "</b>";
379 // Note the labels are not repeated in the history columns.
381 // Handle starting of a new data cell.
382 if ($datacols > 0) {
383 end_cell();
384 echo "<td valign='top' colspan='$datacols' class='text'";
385 if ($cell_count > 0) echo " style='padding-left:5pt'";
386 echo ">";
388 foreach ($historical_ids as $key => $dummy) {
389 $historical_ids[$key] .= "<td valign='top' align='right' colspan='$datacols' class='text'>";
392 $cell_count += $datacols;
395 ++$item_count;
397 // Skip current-value fields for the display-only case.
398 if (empty($is_lbf)) {
399 if ($frow['edit_options'] == 'H')
400 echo generate_display_field($frow, $currvalue);
401 else
402 generate_form_field($frow, $currvalue);
405 // Append to historical data of other dates for this item.
406 foreach ($historical_ids as $key => $dummy) {
407 $hvrow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
408 "form_id = '$key' AND field_id = '$field_id'");
409 $value = empty($hvrow) ? '' : $hvrow['field_value'];
410 $historical_ids[$key] .= generate_display_field($frow, $value);
415 end_group();
418 <p style='text-align:center'>
419 <?php if (empty($is_lbf)) { ?>
420 <input type='submit' name='bn_save' value='<?php echo htmlspecialchars(xl('Save')) ?>' />
421 &nbsp;
422 <input type='button' value='<?php echo htmlspecialchars(xl('Cancel')) ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
423 &nbsp;
424 <?php if ($form_is_graphable) { ?>
425 <input type='button' value='<?php echo htmlspecialchars(xl('Show Graph')) ?>' onclick="top.restoreSession();location='../../patient_file/encounter/trend_form.php?formname=<?php echo $formname; ?>'" />
426 &nbsp;
427 <?php } ?>
428 <?php } else { ?>
429 <input type='button' value='<?php echo htmlspecialchars(xl('Back')) ?>' onclick='window.back();' />
430 <?php } ?>
431 </p>
433 </form>
435 <!-- include support for the list-add selectbox feature -->
436 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
438 <script language="JavaScript">
439 <?php echo $date_init; ?>
440 <?php
441 if (function_exists($formname . '_javascript_onload')) {
442 call_user_func($formname . '_javascript_onload');
444 // TBD: If $alertmsg, display it with a JavaScript alert().
446 </script>
448 </body>
449 </html>