Add access to chartable LBF forms from the demographics summary page, and remove...
[openemr.git] / interface / forms / LBF / new.php
blob687976dbb25699426802bb6c8a407cfe4ba85797
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 // if (! $encounter) { // comes from globals.php
21 // die("Internal error: we do not seem to be in an encounter!");
22 // }
24 function end_cell() {
25 global $item_count, $cell_count, $historical_ids;
26 if ($item_count > 0) {
27 echo "</td>";
29 foreach ($historical_ids as $key => $dummy) {
30 $historical_ids[$key] .= "</td>";
33 $item_count = 0;
37 function end_row() {
38 global $cell_count, $CPR, $historical_ids;
39 end_cell();
40 if ($cell_count > 0) {
41 for (; $cell_count < $CPR; ++$cell_count) {
42 echo "<td></td>";
43 foreach ($historical_ids as $key => $dummy) {
44 $historical_ids[$key] .= "<td></td>";
48 foreach ($historical_ids as $key => $dummy) {
49 echo $historical_ids[$key];
52 echo "</tr>\n";
53 $cell_count = 0;
57 function end_group() {
58 global $last_group;
59 if (strlen($last_group) > 0) {
60 end_row();
61 echo " </table>\n";
62 // No div for an empty group name.
63 if (strlen($last_group) > 1) echo "</div>\n";
67 $formname = formData('formname', 'G');
68 $formid = 0 + formData('id', 'G');
70 // Get title and number of history columns for this form.
71 $tmp = sqlQuery("SELECT title, option_value FROM list_options WHERE " .
72 "list_id = 'lbfnames' AND option_id = '$formname'");
73 $formtitle = $tmp['title'];
74 $formhistory = 0 + $tmp['option_value'];
76 $newid = 0;
78 // If Save was clicked, save the info.
80 if ($_POST['bn_save']) {
81 $sets = "";
82 $fres = sqlStatement("SELECT * FROM layout_options " .
83 "WHERE form_id = '$formname' AND uor > 0 AND field_id != '' AND " .
84 "edit_options != 'H' " .
85 "ORDER BY group_name, seq");
86 while ($frow = sqlFetchArray($fres)) {
87 $field_id = $frow['field_id'];
88 $value = get_layout_form_value($frow);
89 if ($formid) { // existing form
90 if ($value === '') {
91 $query = "DELETE FROM lbf_data WHERE " .
92 "form_id = '$formid' AND field_id = '$field_id'";
94 else {
95 $query = "REPLACE INTO lbf_data SET field_value = '$value', " .
96 "form_id = '$formid', field_id = '$field_id'";
98 sqlStatement($query);
100 else { // new form
101 if ($value !== '') {
102 if ($newid) {
103 sqlStatement("INSERT INTO lbf_data " .
104 "( form_id, field_id, field_value ) " .
105 " VALUES ( '$newid', '$field_id', '$value' )");
107 else {
108 $newid = sqlInsert("INSERT INTO lbf_data " .
109 "( field_id, field_value ) " .
110 " VALUES ( '$field_id', '$value' )");
113 // Note that a completely empty form will not be created at all!
117 if (!$formid && $newid) {
118 addForm($encounter, $formtitle, $newid, $formname, $pid, $userauthorized);
121 formHeader("Redirecting....");
122 formJump();
123 formFooter();
124 exit;
127 $fname = $GLOBALS['OE_SITE_DIR'] . "/LBF/$formname.plugin.php";
128 if (file_exists($fname)) include_once($fname);
130 <html>
131 <head>
132 <?php html_header_show();?>
133 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
134 <style>
136 td, input, select, textarea {
137 font-family: Arial, Helvetica, sans-serif;
138 font-size: 10pt;
141 div.section {
142 border: solid;
143 border-width: 1px;
144 border-color: #0000ff;
145 margin: 0 0 0 10pt;
146 padding: 5pt;
149 </style>
151 <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>
153 <script type="text/javascript" src="../../../library/dialog.js"></script>
154 <script type="text/javascript" src="../../../library/textformat.js"></script>
155 <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
156 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
157 <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>
158 <script type="text/javascript" src="../../../library/js/jquery.js"></script>
160 <script language="JavaScript">
162 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
164 // Supports customizable forms.
165 function divclick(cb, divid) {
166 var divstyle = document.getElementById(divid).style;
167 if (cb.checked) {
168 divstyle.display = 'block';
169 } else {
170 divstyle.display = 'none';
172 return true;
175 // This is for callback by the find-code popup.
176 // Appends to or erases the current list of related codes.
177 function set_related(codetype, code, selector, codedesc) {
178 var frc = document.getElementById('form_related_code');
179 var s = frc.value;
180 if (code) {
181 if (s.length > 0) s += ';';
182 s += codetype + ':' + code;
183 } else {
184 s = '';
186 frc.value = s;
189 // This invokes the find-code popup.
190 function sel_related() {
191 dlgopen('<?php echo $rootdir ?>/patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
194 <?php if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?>
196 </script>
197 </head>
199 <body <?php echo $top_bg_line; ?> topmargin="0" rightmargin="0" leftmargin="2" bottommargin="0" marginwidth="2" marginheight="0">
200 <form method="post" action="<?php echo $rootdir ?>/forms/LBF/new.php?formname=<?php echo $formname ?>&id=<?php echo $formid ?>"
201 onsubmit="return top.restoreSession()">
203 <?php
204 if ($encounter) {
205 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date FROM " .
206 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
207 "p.pid = '$pid' AND f.pid = '$pid' AND f.encounter = '$encounter' AND " .
208 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
209 "fe.id = f.form_id LIMIT 1");
210 echo "<p class='title' style='margin-top:8px;margin-bottom:8px;text-align:center'>\n";
211 echo "$formtitle " . xl('for') . ' ';
212 echo $enrow['fname'] . ' ' . $enrow['mname'] . ' ' . $enrow['lname'];
213 echo ' ' . htmlspecialchars(xl('on')) . ' ' . substr($enrow['date'], 0, 10);
214 echo "</p>\n";
218 <!-- This is where a chart might display. -->
219 <div id="chart"></div>
221 <?php
222 $shrow = getHistoryData($pid);
224 $fres = sqlStatement("SELECT * FROM layout_options " .
225 "WHERE form_id = '$formname' AND uor > 0 " .
226 "ORDER BY group_name, seq");
227 $last_group = '';
228 $cell_count = 0;
229 $item_count = 0;
230 $display_style = 'block';
232 // This is an array keyed on forms.form_id for other occurrences of this
233 // form type. The maximum number of such other occurrences to display is
234 // in list_options.option_value for this form's list item. Values in this
235 // array are work areas for building the ending HTML for each displayed row.
237 $historical_ids = array();
239 // True if any data items in this form can be graphed.
240 $form_is_graphable = false;
242 while ($frow = sqlFetchArray($fres)) {
243 $this_group = $frow['group_name'];
244 $titlecols = $frow['titlecols'];
245 $datacols = $frow['datacols'];
246 $data_type = $frow['data_type'];
247 $field_id = $frow['field_id'];
248 $list_id = $frow['list_id'];
249 $edit_options = $frow['edit_options'];
251 $graphable = strpos($edit_options, 'G') !== FALSE;
252 if ($graphable) $form_is_graphable = true;
254 $currvalue = '';
256 if ($frow['edit_options'] == 'H') {
257 // This data comes from static history
258 if (isset($shrow[$field_id])) $currvalue = $shrow[$field_id];
259 } else {
260 if ($formid) {
261 $pprow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
262 "form_id = '$formid' AND field_id = '$field_id'");
263 if (!empty($pprow)) $currvalue = $pprow['field_value'];
265 else {
266 // New form, see if there is a custom default from a plugin.
267 $fname = $formname . '_default_' . $field_id;
268 if (function_exists($fname)) {
269 $currvalue = call_user_func($fname);
274 // Handle a data category (group) change.
275 if (strcmp($this_group, $last_group) != 0) {
276 end_group();
277 $group_seq = 'lbf' . substr($this_group, 0, 1);
278 $group_name = substr($this_group, 1);
279 $last_group = $this_group;
281 // If group name is blank, no checkbox or div.
282 if (strlen($this_group > 1)) {
283 echo "<br /><span class='bold'><input type='checkbox' name='form_cb_$group_seq' value='1' " .
284 "onclick='return divclick(this,\"div_$group_seq\");'";
285 if ($display_style == 'block') echo " checked";
286 echo " /><b>" . htmlspecialchars(xl_layout_label($group_name)) . "</b></span>\n";
287 echo "<div id='div_$group_seq' class='section' style='display:$display_style;'>\n";
289 // echo " <table border='0' cellpadding='0' width='100%'>\n";
290 echo " <table border='0' cellpadding='0'>\n";
291 $display_style = 'none';
293 // Initialize historical data array and write date headers.
294 $historical_ids = array();
295 if ($formhistory > 0) {
296 echo " <tr>";
297 echo "<td colspan='$CPR' align='right' class='bold'>" . htmlspecialchars(xl('Current')) . "</th>\n";
298 $hres = sqlStatement("SELECT date, form_id FROM forms WHERE " .
299 "pid = '$pid' AND formdir = '$formname' AND " .
300 "form_id != '$formid' AND deleted = 0 " .
301 "ORDER BY date DESC LIMIT $formhistory");
302 while ($hrow = sqlFetchArray($hres)) {
303 $historical_ids[$hrow['form_id']] = '';
304 echo "<td colspan='$CPR' align='right' class='bold'>&nbsp;" . $hrow['date'] . "</th>\n";
305 // TBD: Format date per globals.
307 echo " </tr>";
312 // Handle starting of a new row.
313 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
314 end_row();
315 echo " <tr>";
316 // Clear historical data string.
317 foreach ($historical_ids as $key => $dummy) {
318 $historical_ids[$key] = '';
322 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
324 // Handle starting of a new label cell.
325 if ($titlecols > 0) {
326 end_cell();
327 echo "<td valign='top' colspan='$titlecols' nowrap";
328 echo " class='";
329 echo ($frow['uor'] == 2) ? "required" : "bold";
330 if ($graphable) echo " graph";
331 echo "'";
332 if ($cell_count == 2) echo " style='padding-left:10pt'";
333 if ($graphable) echo " id='$field_id'";
334 echo ">";
336 foreach ($historical_ids as $key => $dummy) {
337 $historical_ids[$key] .= "<td valign='top' colspan='$titlecols' class='text' nowrap>";
340 $cell_count += $titlecols;
342 ++$item_count;
344 echo "<b>";
345 if ($frow['title']) echo htmlspecialchars(xl_layout_label($frow['title']) . ":"); else echo "&nbsp;";
346 echo "</b>";
348 // Note the labels are not repeated in the history columns.
350 // Handle starting of a new data cell.
351 if ($datacols > 0) {
352 end_cell();
353 echo "<td valign='top' align='right' colspan='$datacols' class='text'";
354 if ($cell_count > 0) echo " style='padding-left:5pt'";
355 echo ">";
357 foreach ($historical_ids as $key => $dummy) {
358 $historical_ids[$key] .= "<td valign='top' align='right' colspan='$datacols' class='text'>";
361 $cell_count += $datacols;
364 ++$item_count;
366 if ($frow['edit_options'] == 'H')
367 echo generate_display_field($frow, $currvalue);
368 else
369 generate_form_field($frow, $currvalue);
371 // Append to historical data of other dates for this item.
372 foreach ($historical_ids as $key => $dummy) {
373 $hvrow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
374 "form_id = '$key' AND field_id = '$field_id'");
375 $value = empty($hvrow) ? '' : $hvrow['field_value'];
376 $historical_ids[$key] .= generate_display_field($frow, $value);
381 end_group();
384 <p style='text-align:center'>
385 <?php if ($encounter) { ?>
386 <input type='submit' name='bn_save' value='<?php echo htmlspecialchars(xl('Save')) ?>' />
387 &nbsp;
388 <input type='button' value='<?php echo htmlspecialchars(xl('Cancel')) ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
389 &nbsp;
390 <?php } ?>
391 <?php if ($form_is_graphable) { ?>
392 <input type='button' value='<?php echo htmlspecialchars(xl('Show Graph')) ?>' onclick="top.restoreSession();location='../../patient_file/encounter/trend_form.php?formname=<?php echo $formname; ?>'" />
393 &nbsp;
394 <?php } ?>
395 </p>
397 </form>
399 <!-- include support for the list-add selectbox feature -->
400 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
402 <script language="JavaScript">
403 <?php echo $date_init; ?>
404 <?php
405 if (function_exists($formname . '_javascript_onload')) {
406 call_user_func($formname . '_javascript_onload');
408 // TBD: If $alertmsg, display it with a JavaScript alert().
410 </script>
412 </body>
413 </html>