LBF plugin and layout updates
[openemr.git] / interface / forms / LBF / new.php
blobf88b60bef4d14008f02e0c27ffc493a6cb9aa314
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
214 $(function(){
215 // add drag and drop functionality to fancybox
216 $("#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 // This is for callback by the find-code popup.
233 // Appends to or erases the current list of related codes.
234 function set_related(codetype, code, selector, codedesc) {
235 var frc = document.getElementById('form_related_code');
236 var s = frc.value;
237 if (code) {
238 if (codetype != 'PROD') {
239 if (s.indexOf(codetype + ':') == 0 || s.indexOf(';' + codetype + ':') > 0) {
240 return '<?php echo xl('A code of this type is already selected. Erase the field first if you need to replace it.') ?>';
243 if (s.length > 0) s += ';';
244 s += codetype + ':' + code;
245 } else {
246 s = '';
248 frc.value = s;
249 return '';
252 // This invokes the find-code popup.
253 function sel_related(elem, codetype) {
254 var url = '<?php echo $rootdir ?>/patient_file/encounter/find_code_popup.php';
255 if (codetype) url += '?codetype=' + codetype;
256 dlgopen(url, '_blank', 500, 400);
259 // Validation logic for form submission.
260 function validate(f) {
261 <?php generate_layout_validation($formname); ?>
262 top.restoreSession();
263 return true;
265 <?php if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?>
267 </script>
268 </head>
270 <body <?php echo $top_bg_line; ?> topmargin="0" rightmargin="0" leftmargin="2" bottommargin="0" marginwidth="2" marginheight="0">
272 <?php
273 echo "<form method='post' " .
274 "action='$rootdir/forms/LBF/new.php?formname=$formname&id=$formid&portalid=$portalid' " .
275 "onsubmit='return validate(this)'>\n";
277 $cmsportal_login = '';
278 $portalres = FALSE;
279 if (empty($is_lbf)) {
280 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, p.cmsportal_login, " .
281 "fe.date FROM " .
282 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
283 "p.pid = ? AND f.pid = p.pid AND f.encounter = ? AND " .
284 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
285 "fe.id = f.form_id LIMIT 1", array($pid, $encounter));
286 echo "<p class='title' style='margin-top:8px;margin-bottom:8px;text-align:center'>\n";
287 echo text($formtitle) . " " . xlt('for') . ' ';
288 echo text($enrow['fname']) . ' ' . text($enrow['mname']) . ' ' . text($enrow['lname']);
289 echo ' ' . xlt('on') . ' ' . text(oeFormatShortDate(substr($enrow['date'], 0, 10)));
290 echo "</p>\n";
291 $cmsportal_login = $enrow['cmsportal_login'];
293 // If loading data from portal, get the data.
294 if ($GLOBALS['gbl_portal_cms_enable'] && $portalid) {
295 $portalres = cms_portal_call(array('action' => 'getpost', 'postid' => $portalid));
296 if ($portalres['errmsg']) {
297 die(text($portalres['errmsg']));
302 <!-- This is where a chart might display. -->
303 <div id="chart"></div>
305 <?php
306 $shrow = getHistoryData($pid);
308 $fres = sqlStatement("SELECT * FROM layout_options " .
309 "WHERE form_id = ? AND uor > 0 " .
310 "ORDER BY group_name, seq", array($formname) );
311 $last_group = '';
312 $cell_count = 0;
313 $item_count = 0;
314 $display_style = 'block';
316 // This is an array keyed on forms.form_id for other occurrences of this
317 // form type. The maximum number of such other occurrences to display is
318 // in list_options.option_value for this form's list item. Values in this
319 // array are work areas for building the ending HTML for each displayed row.
321 $historical_ids = array();
323 // True if any data items in this form can be graphed.
324 $form_is_graphable = false;
326 while ($frow = sqlFetchArray($fres)) {
327 $this_group = $frow['group_name'];
328 $titlecols = $frow['titlecols'];
329 $datacols = $frow['datacols'];
330 $data_type = $frow['data_type'];
331 $field_id = $frow['field_id'];
332 $list_id = $frow['list_id'];
333 $edit_options = $frow['edit_options'];
335 $graphable = strpos($edit_options, 'G') !== FALSE;
336 if ($graphable) $form_is_graphable = true;
338 $currvalue = '';
340 if ($frow['edit_options'] == 'H') {
341 // This data comes from static history
342 if (isset($shrow[$field_id])) $currvalue = $shrow[$field_id];
343 } else {
344 if ($formid) {
345 $pprow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
346 "form_id = ? AND field_id = ?", array($formid, $field_id) );
347 if (!empty($pprow)) $currvalue = $pprow['field_value'];
349 else {
350 // This is a new form.
351 // Get data from the CMS portal if applicable.
352 /*************************************************************
353 if ($portalres && isset($portalres['fields'][$field_id])) {
354 $currvalue = $portalres['fields'][$field_id];
355 $currvalue = cms_field_to_lbf($currvalue, $data_type, $field_id);
357 *************************************************************/
358 if ($portalres) {
359 $currvalue = cms_field_to_lbf($data_type, $field_id, $portalres['fields']);
361 if ($currvalue === '') {
362 // Still no data. See if there is a custom default from a plugin.
363 $fname = $formname . '_default_' . $field_id;
364 if (function_exists($fname)) {
365 $currvalue = call_user_func($fname);
371 // Handle a data category (group) change.
372 if (strcmp($this_group, $last_group) != 0) {
373 end_group();
374 $group_seq = 'lbf' . substr($this_group, 0, 1);
375 $group_name = substr($this_group, 1);
376 $last_group = $this_group;
378 // If group name is blank, no checkbox or div.
379 if (strlen($this_group) > 1) {
380 echo "<br /><span class='bold'><input type='checkbox' name='form_cb_" . attr($group_seq) . "' value='1' " .
381 "onclick='return divclick(this,\"div_" . attr(addslashes($group_seq)) . "\");'";
382 if ($display_style == 'block') echo " checked";
383 echo " /><b>" . text(xl_layout_label($group_name)) . "</b></span>\n";
384 echo "<div id='div_" . attr($group_seq) . "' class='section' style='display:" . attr($display_style) . ";'>\n";
386 // echo " <table border='0' cellpadding='0' width='100%'>\n";
387 echo " <table border='0' cellspacing='0' cellpadding='0'>\n";
388 $display_style = 'none';
390 // Initialize historical data array and write date headers.
391 $historical_ids = array();
392 if ($formhistory > 0) {
393 echo " <tr>";
394 echo "<td colspan='" . attr($CPR) . "' align='right' class='bold'>";
395 if (empty($is_lbf)){
396 // Including actual date per IPPF request 2012-08-23.
397 echo oeFormatShortDate(substr($enrow['date'], 0, 10));
398 echo ' (' . htmlspecialchars(xl('Current')) . ')';
400 echo "&nbsp;</td>\n";
401 $hres = sqlStatement("SELECT f.form_id, fe.date " .
402 "FROM forms AS f, form_encounter AS fe WHERE " .
403 "f.pid = ? AND f.formdir = ? AND " .
404 "f.form_id != ? AND f.deleted = 0 AND " .
405 "fe.pid = f.pid AND fe.encounter = f.encounter " .
406 "ORDER BY fe.date DESC, f.encounter DESC, f.date DESC " .
407 "LIMIT ?",
408 array($pid, $formname, $formid, $formhistory));
409 // For some readings like vitals there may be multiple forms per encounter.
410 // We sort these sensibly, however only the encounter date is shown here;
411 // at some point we may wish to show also the data entry date/time.
412 while ($hrow = sqlFetchArray($hres)) {
414 echo "<td colspan='".attr($CPR)."' align='right' class='bold' style='";
415 echo "border-top:1px solid black;";
416 echo "border-right:1px solid black;";
417 echo "border-bottom:1px solid black;";
418 if (empty($historical_ids)) { echo "border-left:1px solid black;"; }
419 echo "'>" .
420 oeFormatShortDate(substr($hrow['date'], 0, 10)) . "&nbsp;</td>\n";
421 $historical_ids[$hrow['form_id']] = '';
423 echo " </tr>";
428 // Handle starting of a new row.
429 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
430 end_row();
431 echo " <tr>";
432 // Clear historical data string.
433 foreach ($historical_ids as $key => $dummy) {
434 $historical_ids[$key] = '';
438 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
440 // First item is on the "left-border"
441 $leftborder = true;
443 // Handle starting of a new label cell.
444 if ($titlecols > 0) {
445 end_cell();
446 echo "<td valign='top' colspan='" . attr($titlecols) . "' nowrap";
447 echo " class='";
448 echo ($frow['uor'] == 2) ? "required" : "bold";
449 if ($graphable) echo " graph";
450 echo "'";
451 if ($cell_count == 2) echo " style='padding-left:10pt'";
452 if ($graphable) echo " id='" . attr($field_id) . "'";
453 echo ">";
455 foreach ($historical_ids as $key => $dummy) {
456 $historical_ids[$key] .= "<td valign='top' colspan='" . attr($titlecols) . "' class='text' style='";
457 $historical_ids[$key] .= "border-bottom:1px solid black;";
458 if ($leftborder) $historical_ids[$key] .= "border-left:1px solid black;";
459 if (!$datacols ) $historical_ids[$key] .= "border-right:1px solid black;";
460 $historical_ids[$key] .= "' nowrap>";
461 $leftborder = false;
465 $cell_count += $titlecols;
467 ++$item_count;
469 echo "<b>";
470 if ($frow['title']) echo text(xl_layout_label($frow['title']) . ":"); else echo "&nbsp;";
471 echo "</b>";
473 // Note the labels are not repeated in the history columns.
475 // Handle starting of a new data cell.
476 if ($datacols > 0) {
477 end_cell();
478 echo "<td valign='top' colspan='" . attr($datacols) . "' class='text'";
479 if ($cell_count > 0) echo " style='padding-left:5pt'";
480 echo ">";
482 foreach ($historical_ids as $key => $dummy) {
483 $historical_ids[$key] .= "<td valign='top' align='right' colspan='" . attr($datacols) . "' class='text' style='";
484 $historical_ids[$key] .= "border-bottom:1px solid black;";
485 $historical_ids[$key] .= "border-right:1px solid black;";
486 if ($leftborder) $historical_ids[$key] .= "border-left:1px solid black;";
487 $historical_ids[$key] .= "'>";
488 $leftborder = false;
491 $cell_count += $datacols;
494 ++$item_count;
496 // Skip current-value fields for the display-only case.
497 if (empty($is_lbf)) {
498 if ($frow['edit_options'] == 'H')
499 echo generate_display_field($frow, $currvalue);
500 else
501 generate_form_field($frow, $currvalue);
504 // Append to historical data of other dates for this item.
505 foreach ($historical_ids as $key => $dummy) {
506 $hvrow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
507 "form_id = ? AND field_id = ?", array($key, $field_id) );
508 $value = empty($hvrow) ? '' : $hvrow['field_value'];
509 $historical_ids[$key] .= generate_display_field($frow, $value);
514 end_group();
517 <p style='text-align:center'>
518 <?php if (empty($is_lbf)) { ?>
519 <input type='submit' name='bn_save' value='<?php echo xla('Save') ?>' />
520 <?php
521 if (function_exists($formname . '_additional_buttons')) {
522 // Allow the plug-in to insert more action buttons here.
523 call_user_func($formname . '_additional_buttons');
526 &nbsp;
527 <input type='button' value='<?php echo xla('Cancel') ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
528 &nbsp;
529 <?php if ($form_is_graphable) { ?>
530 <input type='button' value='<?php echo xla('Show Graph') ?>' onclick="top.restoreSession();location='../../patient_file/encounter/trend_form.php?formname=<?php echo attr($formname); ?>'" />
531 &nbsp;
532 <?php } ?>
533 <?php } else { ?>
534 <input type='button' value='<?php echo xla('Back') ?>' onclick='window.history.back();' />
535 <?php } ?>
536 </p>
538 </form>
540 <!-- include support for the list-add selectbox feature -->
541 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
543 <script language="JavaScript">
544 <?php echo $date_init; ?>
545 <?php
546 if (function_exists($formname . '_javascript_onload')) {
547 call_user_func($formname . '_javascript_onload');
550 // TBD: If $alertmsg, display it with a JavaScript alert().
552 // New form and this patient has a portal login and we have not loaded portal data.
553 // Check if there is portal data pending for this patient and form type.
554 if (!$formid && $GLOBALS['gbl_portal_cms_enable'] && $cmsportal_login && !$portalid) {
555 $portalres = cms_portal_call(array('action' => 'checkptform', 'form' => $formname, 'patient' => $cmsportal_login));
556 if ($portalres['errmsg']) {
557 die(text($portalres['errmsg'])); // TBD: Change to alertmsg
559 $portalid = $portalres['postid'];
560 if ($portalid) {
561 echo "if (confirm('" . xls('The portal has data for this patient and form. Load it now?') . "')) {\n";
562 echo " top.restoreSession();\n";
563 echo " document.location.href = 'load_form.php?formname=$formname&portalid=$portalid';\n";
564 echo "}\n";
568 </script>
570 </body>
571 </html>