6 * @link http://www.open-emr.org
7 * @author Rod Roark <rod@sunsetsystems.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2006-2011 Rod Roark <rod@sunsetsystems.com>
10 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
14 require_once(dirname(__FILE__
) . '/api.inc');
15 require_once(dirname(__FILE__
) . '/forms.inc');
16 require_once(dirname(__FILE__
) . '/../interface/forms/fee_sheet/codes.php');
27 // encode a string from a form field for database writing.
28 function form2db($fldval)
30 $fldval = trim($fldval);
31 $fldval = strip_escape_custom($fldval);
35 // encode a plain string for database writing.
36 function real2db($fldval)
38 return addslashes($fldval);
41 // Get the actual string from a form field.
42 function form2real($fldval)
44 $fldval = trim($fldval);
45 $fldval = strip_escape_custom($fldval);
49 // encode a plain string for html display.
50 function real2form($fldval)
52 return htmlspecialchars($fldval, ENT_QUOTES
);
55 if (empty($spreadsheet_title)) {
56 $spreadsheet_title = 'Injury Log';
59 // Putting an error message in here will result in a javascript alert.
62 // Determine the encounter that we are working with.
63 $thisenc = empty($_GET['thisenc']) ?
$encounter : $_GET['thisenc'] +
0;
65 // If we are invoked as a popup (not in an encounter):
66 $popup = $_GET['popup'];
68 // The form ID is passed to us when an existing encounter form is loaded.
69 $formid = $_GET['id'];
71 // $tempid is the currently selected template, if any.
72 $tempid = $_POST['form_template'] +
0;
74 // This is the start date to be saved with the spreadsheet.
77 $form_completed = '0';
79 if (!$popup && !$encounter) { // $encounter comes from globals.php
80 die("Internal error: we do not seem to be in an encounter!");
83 // Get the name of the template selected by the dropdown, if any;
84 // or if we are loading a form then it comes from that.
87 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
88 "id = $tempid AND rownbr = -1 AND colnbr = -1");
89 $template_name = $trow['value'];
91 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
92 "id = $formid AND rownbr = -1 AND colnbr = -1");
93 list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3);
97 $start_date = form2real($_POST['form_start_date']);
100 // Used rows and columns are those beyond which there are only unused cells.
104 // If we are saving...
106 if ($_POST['bn_save_form'] ||
$_POST['bn_save_template']) {
107 // The form data determines how many rows and columns are now used.
108 $cells = $_POST['cell'];
109 for ($i = 0; $i < count($cells); ++
$i) {
111 for ($j = 0; $j < count($row); ++
$j) {
112 if (substr($row[$j], 0, 1)) {
113 if ($i >= $num_used_rows) {
114 $num_used_rows = $i +
1;
117 if ($j >= $num_used_cols) {
118 $num_used_cols = $j +
1;
124 if ($_POST['bn_save_form']) {
125 $form_completed = $_POST['form_completed'] ?
'1' : '0';
127 // If updating an existing form...
129 sqlStatement("UPDATE form_$spreadsheet_form_name SET " .
130 "value = '$form_completed|$start_date|$template_name' " .
131 "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1");
132 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
133 "id = '$formid' AND rownbr >= 0 AND colnbr >= 0");
134 } // If adding a new form...
137 "SELECT pid FROM form_encounter WHERE encounter = ? ORDER BY id DESC LIMIT 1",
140 $thispid = $tmprow['pid'];
141 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
142 $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_$spreadsheet_form_name");
143 $formid = $tmprow['maxid'] +
1;
148 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
149 "id, rownbr, colnbr, datatype, value " .
151 "$formid, -1, -1, 0, " .
152 "'$form_completed|$start_date|$template_name' " .
154 sqlStatement("UNLOCK TABLES");
159 "$spreadsheet_form_name",
166 } else { // saving a template
167 // The rule is, we can update the original name, or insert a new name
168 // which must not match any existing template name.
169 $new_template_name = form2real($_POST['form_new_template_name']);
170 if ($new_template_name != $template_name) {
171 $trow = sqlQuery("SELECT id FROM form_$spreadsheet_form_name WHERE " .
172 "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" .
173 real2db($new_template_name) . "'");
175 $alertmsg = "Template \"" . real2form($new_template_name) .
176 "\" already exists!";
178 $tempid = 0; // to force insert of new template
179 $template_name = $new_template_name;
184 // If updating an existing template...
186 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
187 "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0");
188 } // If adding a new template...
190 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
191 $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_$spreadsheet_form_name");
192 $tempid = $tmprow['minid'] - 1;
197 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
198 "id, rownbr, colnbr, datatype, value " .
200 "$tempid, -1, -1, 0, " .
201 "'" . real2db($template_name) . "' " .
203 sqlStatement("UNLOCK TABLES");
211 // Finally, save the table cells.
212 for ($i = 0; $i < $num_used_rows; ++
$i) {
213 for ($j = 0; $j < $num_used_cols; ++
$j) {
214 $tmp = $cells[$i][$j];
215 $celltype = substr($tmp, 0, 1) +
0;
216 $cellvalue = form2db(substr($tmp, 1));
218 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
219 "id, rownbr, colnbr, datatype, value " .
221 "$saveid, $i, $j, $celltype, '$cellvalue' )");
226 } else if ($_POST['bn_delete_template'] && $tempid) {
227 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
233 if ($_POST['bn_save_form'] && !$alertmsg && !$popup) {
234 formHeader("Redirecting....");
240 // If we get here then we are displaying a spreadsheet, either a template or
241 // an encounter form.
243 // Get the array of template names.
244 $tres = sqlStatement("SELECT id, value FROM form_$spreadsheet_form_name WHERE " .
245 "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value");
249 # If we are reloading a form, get it.
251 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
252 "id = '$formid' ORDER BY rownbr, colnbr");
253 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
254 "FROM form_$spreadsheet_form_name WHERE id = '$formid'");
255 $num_used_rows = $tmprow['rowmax'] +
1;
256 $num_used_cols = $tmprow['colmax'] +
1;
257 } # Otherwise if we are editing a template, get it.
259 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
260 "id = '$tempid' ORDER BY rownbr, colnbr");
261 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
262 "FROM form_$spreadsheet_form_name WHERE id = '$tempid'");
263 $num_used_rows = $tmprow['rowmax'] +
1;
264 $num_used_cols = $tmprow['colmax'] +
1;
267 // Virtual rows and columns are those available when in Edit Structure mode,
268 // and include some additional ones beyond those used. This allows quite a
269 // lot of stuff to be entered before having to save the template.
270 $num_virtual_rows = $num_used_rows ?
$num_used_rows +
5 : 10;
271 $num_virtual_cols = $num_used_cols ?
$num_used_cols +
5 : 10;
275 <?php
html_header_show();?
>
276 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
277 <link rel
="stylesheet" href
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.min.css">
281 font
-family
: sans
-serif
;
286 font
-family
: sans
-serif
;
289 background
-color
: transparent
;
292 font
-family
: sans
-serif
;
295 background
-color
: transparent
;
298 font
-family
: sans
-serif
;
301 background
-color
: transparent
;
310 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-3-1-1/index.js"></script
>
311 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker-2-5-4/build/jquery.datetimepicker.full.min.js"></script
>
312 <script type
="text/javascript" src
="../../../library/textformat.js?v=<?php echo $v_js_includes; ?>"></script
>
314 <script language
="JavaScript">
316 var ssChanged
= false; // if they have changed anything in the spreadsheet
317 var startDate
= '<?php echo $start_date ? $start_date : date('Y
-m
-d
'); ?>';
319 // In case we are a popup (top level) window, handle top.restoreSession() calls.
320 function restoreSession() {
321 return opener
.top
.restoreSession();
324 // Helper function to set the contents of a block.
325 function setBlockContent(id
, content
) {
326 if (document
.getElementById
) {
327 var x
= document
.getElementById(id
);
329 x
.innerHTML
= content
;
331 else if (document
.all
) {
332 var x
= document
.all
[id
];
333 x
.innerHTML
= content
;
335 // alert("ID = \"" + id + "\", string = \"" + content + "\"");
338 // Called when a different template name is selected.
339 function newTemplate(sel
) {
340 if (ssChanged
&& !confirm('You have made changes that will be discarded ' +
341 'if you select a new template. Do you really want to do this?'))
343 // Restore the original template selection.
344 for (var i
= 0; i
< sel
.options
.length
; ++i
) {
345 if (sel
.options
[i
].value
== '<?php echo $tempid ?>') {
346 sel
.options
[i
].selected
= true;
351 top
.restoreSession();
352 document
.forms
[0].submit();
355 // Called when the Cancel button is clicked.
356 function doCancel() {
357 if (!ssChanged ||
confirm('You have made changes that will be discarded ' +
358 'if you close now. Click OK if you really want to exit this form.'))
360 <?php
if ($popup) { ?
>
363 top
.restoreSession();
364 location
='<?php echo $GLOBALS['form_exit_url
'] ?>';
369 // Called when the Edit Structure checkbox is clicked.
370 function editChanged() {
371 var f
= document
.forms
[0];
372 var newdisplay
= f
.form_edit_template
.checked ?
'' : 'none';
375 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
376 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
377 if (f
['cell['+i+
']['+j+
']'].value
.charAt(0) != '0') {
378 if (i
>= usedrows
) usedrows
= i +
1;
379 if (j
>= usedcols
) usedcols
= j +
1;
383 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
384 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
385 // document.getElementById('div_'+i+'_'+j).style.display = newdisplay;
386 document
.getElementById('sel_'+i+
'_'+j
).style
.display
= newdisplay
;
387 if (i
>= usedrows || j
>= usedcols
) {
388 document
.getElementById('td_'+i+
'_'+j
).style
.display
= newdisplay
;
394 // Prepare a string for use as an HTML value attribute in single quotes.
395 function escQuotes(s
) {
396 return s
.replace(/'/g, "'");
399 // Parse static text to evaluate possible functions.
400 function genStatic(s) {
404 while ((i = s.indexOf('%day
(')) >= 0) {
405 var s1 = s.substring(0, i);
407 var j = s.indexOf(')', i);
409 var dayinc = parseInt(s.substring(i,j));
410 var mydate = new Date(parseInt(startDate.substring(0,4)),
411 parseInt(startDate.substring(5,7))-1, parseInt(startDate.substring(8)));
412 mydate.setTime(1000 * 60 * 60 * 24 * dayinc + mydate.getTime());
413 var year = mydate.getYear(); if (year < 1900) year += 1900;
414 s = s1 + year + '-' +
415 ('' + (mydate.getMonth() + 101)).substring(1) + '-' +
416 ('' + (mydate.getDate() + 100)).substring(1) +
420 // Parse "%sel(first,second,third,...,default)".
421 while ((i = s.indexOf('%sel
(')) >= 0) {
422 var s1 = s.substring(0, i);
424 var j = s.indexOf(')', i);
426 var x = s.substring(0,j);
427 var k = x.lastIndexOf(',');
429 var dflt = s.substring(k+1, j);
430 x = "<select class='selgen
' onchange='newsel(this
)'>";
431 while ((k = s.indexOf(',', i)) > i) {
433 var elem = s.substring(i,k);
434 x += "<option value='" + elem + "'";
435 if (elem == dflt) x += " selected";
436 x += ">" + elem + "</option>";
440 s = s1 + x + s.substring(j + 1);
441 break; // only one %sel allowed
444 // Parse "%ptp(default)".
445 while ((i = s.indexOf('%ptp
(')) >= 0) {
446 var s1 = s.substring(0, i);
448 var j = s.indexOf(')', i);
450 var dflt = s.substring(i, j);
451 x = "<select class='selgen
' onchange='newptp(this
)'>";
452 x += "<option value=''>-- Select --</option>";
454 foreach ($bcodes['Phys
']['Physiotherapy Procedures
'] as $key => $value) {
455 echo " x += \"<option value='$key'\";\n";
456 echo " if (dflt == '$key') x += ' selected
';\n";
457 echo " x += '>$value</option
>';\n";
461 s = s1 + x + s.substring(j + 1);
462 break; // only one %ptp allowed
468 // Called when a cell type selector in the spreadsheet is clicked.
469 function newType(i,j) {
471 var f = document.forms[0];
472 var typeval = f['cell
['+i+']['+j+']'].value;
473 var thevalue = typeval.substring(1);
474 var thetype = document.getElementById('sel_
'+i+'_
'+j).value;
475 var s = "<input type='hidden
' name='cell
[" + i + "][" + j + "]' " +
476 "value='" + thetype + escQuotes(thevalue) + "' />";
478 if (thetype == '1') {
479 s += genStatic(thevalue);
481 else if (thetype == '2') {
482 s += "<input type='checkbox
' value='1' onclick='cbClick(this
," + i + "," + j + ")'";
483 if (thevalue) s += " checked";
486 else if (thetype == '3') {
487 s += "<input type='text
' onchange='textChange(this
," + i + "," + j + ")'" +
488 " class='intext
' value='" + escQuotes(thevalue) + "' size='12' />";
490 else if (thetype == '4') {
491 s += "<textarea rows='3' cols='25' wrap='virtual
' class='intext
' " +
492 "onchange='longChange(this
," + i + "," + j + ")'>" +
493 escQuotes(thevalue) + "</textarea>";
495 setBlockContent('vis_
' + i + '_
' + j, s);
498 // Called when a checkbox in the spreadsheet is clicked.
499 function cbClick(elem,i,j) {
501 var f = document.forms[0];
502 var cell = f['cell
['+i+']['+j+']'];
503 cell.value = '2' + (elem.checked ? '1' : '');
506 // Called when a text value in the spreadsheet is changed.
507 function textChange(elem,i,j) {
509 var f = document.forms[0];
510 var cell = f['cell
['+i+']['+j+']'];
511 cell.value = '3' + elem.value;
514 // Called when a textarea value in the spreadsheet is changed.
515 function longChange(elem,i,j) {
517 var f = document.forms[0];
518 var cell = f['cell
['+i+']['+j+']'];
519 cell.value = '4' + elem.value;
522 // Helper function to get the value element of a table cell given any
523 // other element within that cell.
524 function getHidden(sel) {
525 var p = sel.parentNode;
526 while (p.tagName != 'TD
') {
527 if (!p.parentNode || p.parentNode == p) {
528 alert("JavaScript error, cannot find TD element");
533 // Get the <input type=hidden> element within this table cell.
534 var f = document.forms[0];
535 var s = p.id.substring(3);
536 var uix = s.indexOf('_
');
537 var i = s.substring(0, uix);
538 var j = s.substring(uix+1);
539 return f['cell
[' + i + '][' + j + ']'];
542 // Called when a user-defined select list has a new selection.
543 // This rewrites the function definition for the select list.
544 function newsel(sel) {
545 var inelem = getHidden(sel);
546 var s = inelem.value;
547 var i = s.indexOf('%sel
(');
548 var j = s.indexOf(')', i);
549 var x = s.substring(0, j);
550 var k = x.lastIndexOf(',');
551 inelem.value = s.substring(0, k+1) + sel.value + s.substring(j);
554 // Called when a physiotherapy select list has a new selection.
555 // This rewrites the function definition for the select list.
556 function newptp(sel) {
557 var inelem = getHidden(sel);
558 var s = inelem.value;
559 var i = s.indexOf('%ptp
(') + 5;
560 var j = s.indexOf(')', i);
561 inelem.value = s.substring(0, i) + sel.value + s.substring(j);
564 $(document).ready(function() {
565 $('.datepicker
').datetimepicker({
566 <?php $datetimepicker_timepicker = false; ?>
567 <?php $datetimepicker_showseconds = false; ?>
568 <?php $datetimepicker_formatInput = false; ?>
569 <?php require($GLOBALS['srcdir
'] . '/js
/xl
/jquery
-datetimepicker
-2-5-4.js
.php
'); ?>
570 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
578 <body class="body_top">
579 <form method="post" action="<?php echo "$rootdir/forms/$spreadsheet_form_name/new.php?id=$formid&thisenc=$thisenc";
583 onsubmit="return top.restoreSession()">
586 <table border='0' cellpadding='5' cellspacing='0' style='margin
:8pt
'>
587 <tr bgcolor='#ddddff'>
589 <?php
xl('Start Date', 'e'); ?
>:
590 <input type
='text' class='datepicker' name
='form_start_date' id
='form_start_date'
591 size
='10' value
='<?php echo $start_date; ?>'
593 <?php
echo ($formid && $start_date) ?
'disabled ' : ''; ?
>/>
595 <?php
xl('Template:', 'e') ?
>
596 <select name
='form_template' onchange
='newTemplate(this)'<?php
echo ($formid) ?
' disabled' : ''; ?
>>
597 <option value
='0'>-- Select
--</option
>
599 while ($trow = sqlFetchArray($tres)) {
600 echo " <option value='" . $trow['id'] . "'";
601 if ($tempid && $tempid == $trow['id'] ||
602 $formid && $template_name == $trow['value']) {
606 echo ">" . $trow['value'] . "</option>\n";
611 <input type
='checkbox' name
='form_edit_template'
612 onclick
='editChanged()'
613 title
='<?php xl("If you want to change data types, or add rows or columns", "e") ?>' />
614 <?php
xl('Edit Structure', 'e') ?
>
615 <?php
if ($formid) { ?
>
617 <input type
='checkbox' name
='form_completed'
618 title
='<?php xl("If all data for all columns are complete for this form", "e") ?>'
619 <?php
echo ($form_completed) ?
'checked ' : ''; ?
>/>
620 <?php
xl('Completed', 'e') ?
>
626 <table border
='1' cellpadding
='2' cellspacing
='0' class='sstable'>
629 $drow = sqlFetchArray($dres);
632 $typeprompts = array('unused','static','checkbox','text');
634 for ($i = 0; $i < $num_virtual_rows; ++
$i) {
636 for ($j = 0; $j < $num_virtual_cols; ++
$j) {
637 // Match up with the database for cell type and value.
641 while ($drow && $drow['rownbr'] < $i) {
642 $drow = sqlFetchArray($dres);
645 while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j) {
646 $drow = sqlFetchArray($dres);
649 if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) {
650 $celltype = $drow['datatype'];
651 $cellvalue = real2form($drow['value']);
652 $cellstatic = addslashes($drow['value']);
656 echo " <td id='td_${i}_${j}' valign='top'";
657 if ($i >= $num_used_rows ||
$j >= $num_used_cols) {
658 echo " style='display:none'";
663 /*****************************************************************
664 echo "<span id='div_${i}_${j}' ";
665 echo "style='float:right;cursor:pointer;display:none' ";
666 echo "onclick='newType($i,$j)'>[";
667 echo $typeprompts[$celltype];
669 *****************************************************************/
670 echo "<div class='seldiv'>";
671 echo "<select id='sel_${i}_${j}' class='seltype' style='display:none' " .
672 "onchange='newType($i,$j)'>";
673 foreach ($celltypes as $key => $value) {
674 echo "<option value='$key'";
675 if ($key == $celltype) {
679 echo ">$value</option>";
684 /****************************************************************/
686 echo "<span id='vis_${i}_${j}'>"; // new //
688 echo "<input type='hidden' name='cell[$i][$j]' value='$celltype$cellvalue' />";
689 if ($celltype == '1') {
690 // So we don't have to write a PHP version of genStatic():
691 echo "<script language='JavaScript'>document.write(genStatic('$cellstatic'));</script>";
692 } else if ($celltype == '2') {
693 echo "<input type='checkbox' value='1' onclick='cbClick(this,$i,$j)'";
699 } else if ($celltype == '3') {
700 echo "<input type='text' class='intext' onchange='textChange(this,$i,$j)'";
701 echo " value='$cellvalue'";
702 echo " size='12' />";
703 } else if ($celltype == '4') {
704 echo "<textarea rows='3' cols='25' wrap='virtual' class='intext' " .
705 "onchange='longChange(this,$i,$j)'>";
710 echo "</span>"; // new //
721 <input type
='submit' name
='bn_save_form' value
='Save Form' />
722 <?php
if (!$formid) { ?
>
724 <input type
='submit' name
='bn_save_template' value
='Save as Template:' />
726 <input type
='text' name
='form_new_template_name' value
='<?php echo $template_name ?>' />
728 <input type
='submit' name
='bn_delete_template' value
='Delete Template' />
731 <input type
='button' value
='Cancel' onclick
="doCancel()" />
736 <script language
='JavaScript'>
739 echo " alert('$alertmsg');\n";