2 // Copyright (C) 2009-2014 Rod Roark <rod@sunsetsystems.com>
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.
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
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!");
37 global $item_count, $cell_count, $historical_ids;
38 if ($item_count > 0) {
41 foreach ($historical_ids as $key => $dummy) {
42 $historical_ids[$key] .= " </td>";
50 global $cell_count, $CPR, $historical_ids;
52 if ($cell_count > 0) {
53 for (; $cell_count < $CPR; ++
$cell_count) {
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];
69 function end_group() {
71 if (strlen($last_group) > 0) {
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'];
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']) {
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
110 $query = "DELETE FROM lbf_data WHERE " .
111 "form_id = ? AND field_id = ?";
112 array_push($sql_bind_array,$formid,$field_id);
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);
124 sqlStatement("INSERT INTO lbf_data " .
125 "( form_id, field_id, field_value ) " .
126 " VALUES (?,?,?)", array($newid, $field_id, $value) );
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);
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....");
164 <?php
html_header_show();?
>
165 <link rel
=stylesheet href
="<?php echo $css_header;?>" type
="text/css">
168 td
, input
, select
, textarea
{
169 font
-family
: Arial
, Helvetica
, sans
-serif
;
176 border
-color
: #0000ff;
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() {
200 if (window
.enable_modals
) {
207 $
(".iframe_medium").fancybox({
208 'overlayOpacity' : 0.0,
209 'showCloseButton' : true,
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
;
225 divstyle
.display
= 'block';
227 divstyle
.display
= 'none';
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
];
243 var matches
= current_sel_name
.match(/^
(.*)__desc$
/);
249 var sd
= frcd ? frcd
.value
: s
;
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
.') ?>';
260 s +
= codetype +
':' + code
;
267 if (frcd
) frcd
.value
= sd
;
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();
285 <?php
if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?
>
290 <body
<?php
echo $top_bg_line; ?
> topmargin
="0" rightmargin
="0" leftmargin
="2" bottommargin
="0" marginwidth
="2" marginheight
="0">
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 = '';
299 if (empty($is_lbf)) {
300 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, p.cmsportal_login, " .
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)));
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
>
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) );
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;
360 if ($frow['edit_options'] == 'H') {
361 // This data comes from static history
362 if (isset($shrow[$field_id])) $currvalue = $shrow[$field_id];
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'];
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 *************************************************************/
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) {
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) {
414 echo "<td colspan='" . attr($CPR) . "' align='right' class='bold'>";
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 " </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 " .
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;"; }
440 oeFormatShortDate(substr($hrow['date'], 0, 10)) . " </td>\n";
441 $historical_ids[$hrow['form_id']] = '';
448 // Handle starting of a new row.
449 if (($titlecols > 0 && $cell_count >= $CPR) ||
$cell_count == 0) {
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"
463 // Handle starting of a new label cell.
464 if ($titlecols > 0) {
466 echo "<td valign='top' colspan='" . attr($titlecols) . "' nowrap";
468 echo ($frow['uor'] == 2) ?
"required" : "bold";
469 if ($graphable) echo " graph";
471 if ($cell_count == 2) echo " style='padding-left:10pt'";
472 if ($graphable) echo " id='" . attr($field_id) . "'";
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>";
485 $cell_count +
= $titlecols;
490 if ($frow['title']) echo text(xl_layout_label($frow['title']) . ":"); else echo " ";
493 // Note the labels are not repeated in the history columns.
495 // Handle starting of a new data cell.
498 echo "<td valign='top' colspan='" . attr($datacols) . "' class='text'";
499 if ($cell_count > 0) echo " style='padding-left:5pt'";
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] .= "'>";
511 $cell_count +
= $datacols;
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);
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);
537 <p style
='text-align:center'>
538 <?php
if (empty($is_lbf)) { ?
>
539 <input type
='submit' name
='bn_save' value
='<?php echo xla('Save
') ?>' />
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');
547 <input type
='button' value
='<?php echo xla('Cancel
') ?>' onclick
="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
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); ?>'" />
554 <input type
='button' value
='<?php echo xla('Back
') ?>' onclick
='window.history.back();' />
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; ?
>
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'];
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";