Focus the search term on load
[openemr.git] / interface / forms / LBF / new.php
blobe24aaca7dce54c47041b3027e5836b380de6fd4c
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 //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");
23 $CPR = 4; // cells per row
25 $pprow = array();
27 // $is_lbf is defined in trend_form.php and indicates that we are being
28 // invoked from there; in that case the current encounter is irrelevant.
29 if (empty($is_lbf) && !$encounter) {
30 die("Internal error: we do not seem to be in an encounter!");
33 function end_cell() {
34 global $item_count, $cell_count, $historical_ids;
35 if ($item_count > 0) {
36 echo "</td>";
38 foreach ($historical_ids as $key => $dummy) {
39 $historical_ids[$key] .= "</td>";
42 $item_count = 0;
46 function end_row() {
47 global $cell_count, $CPR, $historical_ids;
48 end_cell();
49 if ($cell_count > 0) {
50 for (; $cell_count < $CPR; ++$cell_count) {
51 echo "<td></td>";
52 foreach ($historical_ids as $key => $dummy) {
53 $historical_ids[$key] .= "<td></td>";
57 foreach ($historical_ids as $key => $dummy) {
58 echo $historical_ids[$key];
61 echo "</tr>\n";
62 $cell_count = 0;
66 function end_group() {
67 global $last_group;
68 if (strlen($last_group) > 0) {
69 end_row();
70 echo " </table>\n";
71 // No div for an empty group name.
72 if (strlen($last_group) > 1) echo "</div>\n";
76 $formname = isset($_GET['formname']) ? $_GET['formname'] : '';
77 $formid = 0 + (isset($_GET['id']) ? $_GET['id'] : '');
79 // Get title and number of history columns for this form.
80 $tmp = sqlQuery("SELECT title, option_value FROM list_options WHERE " .
81 "list_id = 'lbfnames' AND option_id = ?", array($formname) );
82 $formtitle = $tmp['title'];
83 $formhistory = 0 + $tmp['option_value'];
85 $newid = 0;
87 // If Save was clicked, save the info.
89 if ($_POST['bn_save']) {
90 $sets = "";
91 $fres = sqlStatement("SELECT * FROM layout_options " .
92 "WHERE form_id = ? AND uor > 0 AND field_id != '' AND " .
93 "edit_options != 'H' " .
94 "ORDER BY group_name, seq", array($formname) );
95 while ($frow = sqlFetchArray($fres)) {
96 $field_id = $frow['field_id'];
97 $value = get_layout_form_value($frow);
98 $sql_bind_array = array();
99 if ($formid) { // existing form
100 if ($value === '') {
101 $query = "DELETE FROM lbf_data WHERE " .
102 "form_id = ? AND field_id = ?";
103 array_push($sql_bind_array,$formid,$field_id);
105 else {
106 $query = "REPLACE INTO lbf_data SET field_value = ?, " .
107 "form_id = ?, field_id = ?";
108 array_push($sql_bind_array,$value,$formid,$field_id);
110 sqlStatement($query,$sql_bind_array);
112 else { // new form
113 if ($value !== '') {
114 if ($newid) {
115 sqlStatement("INSERT INTO lbf_data " .
116 "( form_id, field_id, field_value ) " .
117 " VALUES (?,?,?)", array($newid, $field_id, $value) );
119 else {
120 $newid = sqlInsert("INSERT INTO lbf_data " .
121 "( field_id, field_value ) " .
122 " VALUES (?,?)", array($field_id, $value) );
125 // Note that a completely empty form will not be created at all!
129 if (!$formid && $newid) {
130 addForm($encounter, $formtitle, $newid, $formname, $pid, $userauthorized);
133 formHeader("Redirecting....");
134 formJump();
135 formFooter();
136 exit;
139 if (empty($is_lbf)) {
140 $fname = $GLOBALS['OE_SITE_DIR'] . "/LBF/$formname.plugin.php";
141 if (file_exists($fname)) include_once($fname);
144 <html>
145 <head>
146 <?php html_header_show();?>
147 <link rel=stylesheet href="<?php echo $css_header;?>" type="text/css">
148 <style>
150 td, input, select, textarea {
151 font-family: Arial, Helvetica, sans-serif;
152 font-size: 10pt;
155 div.section {
156 border: solid;
157 border-width: 1px;
158 border-color: #0000ff;
159 margin: 0 0 0 10pt;
160 padding: 5pt;
163 </style>
165 <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>
167 <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.css" media="screen" />
168 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/dialog.js"></script>
169 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.1.3.2.js"></script>
170 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/common.js"></script>
171 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/fancybox/jquery.fancybox-1.2.6.js"></script>
172 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-ui.js"></script>
173 <script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery.easydrag.handler.beta2.js"></script>
174 <script type="text/javascript" src="../../../library/textformat.js"></script>
175 <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
176 <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
177 <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>
179 <script language="JavaScript">
180 $(document).ready(function(){
181 // fancy box
182 if(window.enable_modals){
183 enable_modals();
185 if(window.tabbify){
186 tabbify();
188 // special size for
189 $(".iframe_medium").fancybox( {
190 'overlayOpacity' : 0.0,
191 'showCloseButton' : true,
192 'frameHeight' : 580,
193 'frameWidth' : 900
196 $(function(){
197 // add drag and drop functionality to fancybox
198 $("#fancy_outer").easydrag();
201 var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';
203 // Supports customizable forms.
204 function divclick(cb, divid) {
205 var divstyle = document.getElementById(divid).style;
206 if (cb.checked) {
207 divstyle.display = 'block';
208 } else {
209 divstyle.display = 'none';
211 return true;
214 // This is for callback by the find-code popup.
215 // Appends to or erases the current list of related codes.
216 function set_related(codetype, code, selector, codedesc) {
217 var frc = document.getElementById('form_related_code');
218 var s = frc.value;
219 if (code) {
220 if (s.length > 0) s += ';';
221 s += codetype + ':' + code;
222 } else {
223 s = '';
225 frc.value = s;
228 // This invokes the find-code popup.
229 function sel_related() {
230 dlgopen('<?php echo $rootdir ?>/patient_file/encounter/find_code_popup.php', '_blank', 500, 400);
233 <?php if (function_exists($formname . '_javascript')) call_user_func($formname . '_javascript'); ?>
235 </script>
236 </head>
238 <body <?php echo $top_bg_line; ?> topmargin="0" rightmargin="0" leftmargin="2" bottommargin="0" marginwidth="2" marginheight="0">
239 <form method="post" action="<?php echo $rootdir ?>/forms/LBF/new.php?formname=<?php echo attr($formname) ?>&id=<?php echo attr($formid) ?>"
240 onsubmit="return top.restoreSession()">
242 <?php
243 if (empty($is_lbf)) {
244 $enrow = sqlQuery("SELECT p.fname, p.mname, p.lname, fe.date FROM " .
245 "form_encounter AS fe, forms AS f, patient_data AS p WHERE " .
246 "p.pid = ? AND f.pid = ? AND f.encounter = ? AND " .
247 "f.formdir = 'newpatient' AND f.deleted = 0 AND " .
248 "fe.id = f.form_id LIMIT 1", array($pid, $pid, $encounter) );
249 echo "<p class='title' style='margin-top:8px;margin-bottom:8px;text-align:center'>\n";
250 echo text($formtitle) . " " . xlt('for') . ' ';
251 echo text($enrow['fname']) . ' ' . text($enrow['mname']) . ' ' . text($enrow['lname']);
252 echo ' ' . xlt('on') . ' ' . text(substr($enrow['date'], 0, 10));
253 echo "</p>\n";
257 <!-- This is where a chart might display. -->
258 <div id="chart"></div>
260 <?php
261 $shrow = getHistoryData($pid);
263 $fres = sqlStatement("SELECT * FROM layout_options " .
264 "WHERE form_id = ? AND uor > 0 " .
265 "ORDER BY group_name, seq", array($formname) );
266 $last_group = '';
267 $cell_count = 0;
268 $item_count = 0;
269 $display_style = 'block';
271 // This is an array keyed on forms.form_id for other occurrences of this
272 // form type. The maximum number of such other occurrences to display is
273 // in list_options.option_value for this form's list item. Values in this
274 // array are work areas for building the ending HTML for each displayed row.
276 $historical_ids = array();
278 // True if any data items in this form can be graphed.
279 $form_is_graphable = false;
281 while ($frow = sqlFetchArray($fres)) {
282 $this_group = $frow['group_name'];
283 $titlecols = $frow['titlecols'];
284 $datacols = $frow['datacols'];
285 $data_type = $frow['data_type'];
286 $field_id = $frow['field_id'];
287 $list_id = $frow['list_id'];
288 $edit_options = $frow['edit_options'];
290 $graphable = strpos($edit_options, 'G') !== FALSE;
291 if ($graphable) $form_is_graphable = true;
293 $currvalue = '';
295 if ($frow['edit_options'] == 'H') {
296 // This data comes from static history
297 if (isset($shrow[$field_id])) $currvalue = $shrow[$field_id];
298 } else {
299 if ($formid) {
300 $pprow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
301 "form_id = ? AND field_id = ?", array($formid, $field_id) );
302 if (!empty($pprow)) $currvalue = $pprow['field_value'];
304 else {
305 // New form, see if there is a custom default from a plugin.
306 $fname = $formname . '_default_' . $field_id;
307 if (function_exists($fname)) {
308 $currvalue = call_user_func($fname);
313 // Handle a data category (group) change.
314 if (strcmp($this_group, $last_group) != 0) {
315 end_group();
316 $group_seq = 'lbf' . substr($this_group, 0, 1);
317 $group_name = substr($this_group, 1);
318 $last_group = $this_group;
320 // If group name is blank, no checkbox or div.
321 if (strlen($this_group) > 1) {
322 echo "<br /><span class='bold'><input type='checkbox' name='form_cb_" . attr($group_seq) . "' value='1' " .
323 "onclick='return divclick(this,\"div_" . attr(addslashes($group_seq)) . "\");'";
324 if ($display_style == 'block') echo " checked";
325 echo " /><b>" . text(xl_layout_label($group_name)) . "</b></span>\n";
326 echo "<div id='div_" . attr($group_seq) . "' class='section' style='display:" . attr($display_style) . ";'>\n";
328 // echo " <table border='0' cellpadding='0' width='100%'>\n";
329 echo " <table border='0' cellpadding='0' width='100%'>\n";
330 $display_style = 'none';
332 // Initialize historical data array and write date headers.
333 $historical_ids = array();
334 if ($formhistory > 0) {
335 echo " <tr>";
336 echo "<td colspan='" . attr($CPR) . "' align='right' class='bold'>";
337 if (empty($is_lbf)) echo xlt('Current');
338 echo "</td>\n";
339 $hres = sqlStatement("SELECT f.form_id, fe.date " .
340 "FROM forms AS f, form_encounter AS fe WHERE " .
341 "f.pid = ? AND f.formdir = ? AND " .
342 "f.form_id != ? AND f.deleted = 0 AND " .
343 "fe.pid = f.pid AND fe.encounter = f.encounter " .
344 "ORDER BY fe.date DESC, f.encounter DESC, f.date DESC " .
345 "LIMIT ?",
346 array($pid, $formname, $formid, $formhistory));
347 // For some readings like vitals there may be multiple forms per encounter.
348 // We sort these sensibly, however only the encounter date is shown here;
349 // at some point we may wish to show also the data entry date/time.
350 while ($hrow = sqlFetchArray($hres)) {
351 $historical_ids[$hrow['form_id']] = '';
352 echo "<td colspan='" . attr($CPR) . "' align='right' class='bold'>&nbsp;" .
353 text(oeFormatShortDate(substr($hrow['date'], 0, 10))) . "</td>\n";
355 echo " </tr>";
360 // Handle starting of a new row.
361 if (($titlecols > 0 && $cell_count >= $CPR) || $cell_count == 0) {
362 end_row();
363 echo " <tr>";
364 // Clear historical data string.
365 foreach ($historical_ids as $key => $dummy) {
366 $historical_ids[$key] = '';
370 if ($item_count == 0 && $titlecols == 0) $titlecols = 1;
372 // Handle starting of a new label cell.
373 if ($titlecols > 0) {
374 end_cell();
375 echo "<td valign='top' colspan='" . attr($titlecols) . "' width='1%' nowrap";
376 echo " class='";
377 echo ($frow['uor'] == 2) ? "required" : "bold";
378 if ($graphable) echo " graph";
379 echo "'";
380 if ($cell_count == 2) echo " style='padding-left:10pt'";
381 if ($graphable) echo " id='" . attr($field_id) . "'";
382 echo ">";
384 foreach ($historical_ids as $key => $dummy) {
385 $historical_ids[$key] .= "<td valign='top' colspan='" . attr($titlecols) . "' class='text' nowrap>";
388 $cell_count += $titlecols;
390 ++$item_count;
392 echo "<b>";
393 if ($frow['title']) echo text(xl_layout_label($frow['title']) . ":"); else echo "&nbsp;";
394 echo "</b>";
396 // Note the labels are not repeated in the history columns.
398 // Handle starting of a new data cell.
399 if ($datacols > 0) {
400 end_cell();
401 echo "<td valign='top' colspan='" . attr($datacols) . "' class='text'";
402 if ($cell_count > 0) echo " style='padding-left:5pt'";
403 echo ">";
405 foreach ($historical_ids as $key => $dummy) {
406 $historical_ids[$key] .= "<td valign='top' align='right' colspan='" . attr($datacols) . "' class='text'>";
409 $cell_count += $datacols;
412 ++$item_count;
414 // Skip current-value fields for the display-only case.
415 if (empty($is_lbf)) {
416 if ($frow['edit_options'] == 'H')
417 echo generate_display_field($frow, $currvalue);
418 else
419 generate_form_field($frow, $currvalue);
422 // Append to historical data of other dates for this item.
423 foreach ($historical_ids as $key => $dummy) {
424 $hvrow = sqlQuery("SELECT field_value FROM lbf_data WHERE " .
425 "form_id = ? AND field_id = ?", array($key, $field_id) );
426 $value = empty($hvrow) ? '' : $hvrow['field_value'];
427 $historical_ids[$key] .= generate_display_field($frow, $value);
432 end_group();
435 <p style='text-align:center'>
436 <?php if (empty($is_lbf)) { ?>
437 <input type='submit' name='bn_save' value='<?php echo xla('Save') ?>' />
438 &nbsp;
439 <input type='button' value='<?php echo xla('Cancel') ?>' onclick="top.restoreSession();location='<?php echo $GLOBALS['form_exit_url']; ?>'" />
440 &nbsp;
441 <?php if ($form_is_graphable) { ?>
442 <input type='button' value='<?php echo xla('Show Graph') ?>' onclick="top.restoreSession();location='../../patient_file/encounter/trend_form.php?formname=<?php echo attr($formname); ?>'" />
443 &nbsp;
444 <?php } ?>
445 <?php } else { ?>
446 <input type='button' value='<?php echo xla('Back') ?>' onclick='window.back();' />
447 <?php } ?>
448 </p>
450 </form>
452 <!-- include support for the list-add selectbox feature -->
453 <?php include $GLOBALS['fileroot'] . "/library/options_listadd.inc"; ?>
455 <script language="JavaScript">
456 <?php echo $date_init; ?>
457 <?php
458 if (function_exists($formname . '_javascript_onload')) {
459 call_user_func($formname . '_javascript_onload');
461 // TBD: If $alertmsg, display it with a JavaScript alert().
463 </script>
465 </body>
466 </html>