7 * @link https://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2006-2011 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once(dirname(__FILE__
) . '/api.inc.php');
16 require_once(dirname(__FILE__
) . '/forms.inc.php');
17 require_once(dirname(__FILE__
) . '/../interface/forms/fee_sheet/codes.php');
19 use OpenEMR\Core\Header
;
30 // encode a string from a form field for database writing.
31 function form2db($fldval)
33 $fldval = trim($fldval);
37 // Get the actual string from a form field.
38 function form2real($fldval)
40 $fldval = trim($fldval);
44 // encode a plain string for html display.
45 function real2form($fldval)
50 if (empty($spreadsheet_title)) {
51 $spreadsheet_title = 'Injury Log';
54 // Putting an error message in here will result in a javascript alert.
57 // Determine the encounter that we are working with.
58 $thisenc = empty($_GET['thisenc']) ?
$encounter : $_GET['thisenc'] +
0;
60 // If we are invoked as a popup (not in an encounter):
61 $popup = $_GET['popup'];
63 // The form ID is passed to us when an existing encounter form is loaded.
64 $formid = $_GET['id'];
66 // $tempid is the currently selected template, if any.
67 $tempid = $_POST['form_template'] +
0;
69 // This is the start date to be saved with the spreadsheet.
72 $form_completed = '0';
74 if (!$popup && !$encounter) { // $encounter comes from globals.php
75 die("Internal error: we do not seem to be in an encounter!");
78 // Get the name of the template selected by the dropdown, if any;
79 // or if we are loading a form then it comes from that.
82 $trow = sqlQuery("SELECT value FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
83 " WHERE id = ? AND rownbr = -1 AND colnbr = -1", array($tempid));
84 $template_name = $trow['value'];
86 $trow = sqlQuery("SELECT value FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
87 " WHERE id = ? AND rownbr = -1 AND colnbr = -1", array($formid));
88 list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3);
92 $start_date = form2real($_POST['form_start_date']);
95 // Used rows and columns are those beyond which there are only unused cells.
99 // If we are saving...
101 if ($_POST['bn_save_form'] ||
$_POST['bn_save_template']) {
102 // The form data determines how many rows and columns are now used.
103 $cells = $_POST['cell'];
104 for ($i = 0; $i < count($cells); ++
$i) {
106 for ($j = 0; $j < count($row); ++
$j) {
107 if (substr($row[$j], 0, 1)) {
108 if ($i >= $num_used_rows) {
109 $num_used_rows = $i +
1;
112 if ($j >= $num_used_cols) {
113 $num_used_cols = $j +
1;
119 if ($_POST['bn_save_form']) {
120 $form_completed = $_POST['form_completed'] ?
'1' : '0';
122 // If updating an existing form...
125 "UPDATE " . escape_table_name('form_' . $spreadsheet_form_name) .
126 " SET value = ? WHERE id = ? AND rownbr = -1 AND colnbr = -1",
128 $form_completed . '|' . $start_date . '|' . $template_name,
133 "DELETE FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
134 " WHERE id = ? AND rownbr >= 0 AND colnbr >= 0",
137 } else { // If adding a new form...
139 "SELECT pid FROM form_encounter WHERE encounter = ? ORDER BY id DESC LIMIT 1",
142 $thispid = $tmprow['pid'];
144 "LOCK TABLES " . escape_table_name('form_' . $spreadsheet_form_name) .
147 $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM " .
148 escape_table_name('form_' . $spreadsheet_form_name));
149 $formid = $tmprow['maxid'] +
1;
155 "INSERT INTO " . escape_table_name('form_' . $spreadsheet_form_name) . " ( " .
156 "id, rownbr, colnbr, datatype, value " .
157 ") VALUES ( ?, -1, -1, 0, ? )",
160 $form_completed . '|' . $start_date . '|' . $template_name
163 sqlStatement("UNLOCK TABLES");
168 "$spreadsheet_form_name",
175 } else { // saving a template
176 // The rule is, we can update the original name, or insert a new name
177 // which must not match any existing template name.
178 $new_template_name = form2real($_POST['form_new_template_name']);
179 if ($new_template_name != $template_name) {
181 "SELECT id FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
182 " WHERE id < 0 AND rownbr = -1 AND colnbr = -1 AND value = ?",
183 array($new_template_name)
186 $alertmsg = "Template \"" . real2form($new_template_name) .
187 "\" already exists!";
189 $tempid = 0; // to force insert of new template
190 $template_name = $new_template_name;
195 // If updating an existing template...
198 "DELETE FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
199 " WHERE id = ? AND rownbr >= 0 AND colnbr >= 0",
202 } else { // If adding a new template...
204 "LOCK TABLES " . escape_table_name('form_' . $spreadsheet_form_name) .
207 $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM " .
208 escape_table_name('form_' . $spreadsheet_form_name));
209 $tempid = $tmprow['minid'] - 1;
215 "INSERT INTO " . escape_table_name('form_' . $spreadsheet_form_name) . " ( " .
216 "id, rownbr, colnbr, datatype, value " .
217 ") VALUES ( ?, -1, -1, 0, ? )",
223 sqlStatement("UNLOCK TABLES");
231 // Finally, save the table cells.
232 for ($i = 0; $i < $num_used_rows; ++
$i) {
233 for ($j = 0; $j < $num_used_cols; ++
$j) {
234 $tmp = $cells[$i][$j];
235 $celltype = substr($tmp, 0, 1) +
0;
236 $cellvalue = form2db(substr($tmp, 1));
239 "INSERT INTO " . escape_table_name('form_' . $spreadsheet_form_name) .
240 " ( id, rownbr, colnbr, datatype, value ) " .
241 "VALUES ( ?, ?, ?, ?, ? )",
242 array($saveid, $i, $j, $celltype, $cellvalue)
248 } elseif ($_POST['bn_delete_template'] && $tempid) {
250 "DELETE FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
258 if ($_POST['bn_save_form'] && !$alertmsg && !$popup) {
259 formHeader("Redirecting....");
265 // If we get here then we are displaying a spreadsheet, either a template or
266 // an encounter form.
268 // Get the array of template names.
269 $tres = sqlStatement("SELECT id, value FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
270 " WHERE id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value");
274 # If we are reloading a form, get it.
276 $dres = sqlStatement("SELECT * FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
277 " WHERE id = ? ORDER BY rownbr, colnbr", array($formid));
279 "SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
280 "FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ?",
283 $num_used_rows = $tmprow['rowmax'] +
1;
284 $num_used_cols = $tmprow['colmax'] +
1;
285 } elseif ($tempid) { // Otherwise if we are editing a template, get it.
286 $dres = sqlStatement(
287 "SELECT * FROM " . escape_table_name('form_' . $spreadsheet_form_name) .
288 " WHERE id = ? ORDER BY rownbr, colnbr",
292 "SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
293 "FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ?",
296 $num_used_rows = $tmprow['rowmax'] +
1;
297 $num_used_cols = $tmprow['colmax'] +
1;
300 // Virtual rows and columns are those available when in Edit Structure mode,
301 // and include some additional ones beyond those used. This allows quite a
302 // lot of stuff to be entered before having to save the template.
303 $num_virtual_rows = $num_used_rows ?
$num_used_rows +
5 : 10;
304 $num_virtual_cols = $num_used_cols ?
$num_used_cols +
5 : 10;
308 <?php Header
::setupHeader('datetime-picker'); ?
>
312 font
-family
: sans
-serif
;
317 font
-family
: sans
-serif
;
320 background
-color
: transparent
;
323 font
-family
: sans
-serif
;
326 background
-color
: transparent
;
329 font
-family
: sans
-serif
;
332 background
-color
: transparent
;
343 var ssChanged
= false; // if they have changed anything in the spreadsheet
344 var startDate
= <?php
echo js_escape(($start_date ?
$start_date : date('Y-m-d'))); ?
>;
346 // In case we are a popup (top level) window, handle top.restoreSession() calls.
347 function restoreSession() {
348 return opener
.top
.restoreSession();
351 // Helper function to set the contents of a block.
352 function setBlockContent(id
, content
) {
353 if (document
.getElementById
) {
354 var x
= document
.getElementById(id
);
356 x
.innerHTML
= content
;
358 else if (document
.all
) {
359 var x
= document
.all
[id
];
360 x
.innerHTML
= content
;
362 // alert("ID = \"" + id + "\", string = \"" + content + "\"");
365 // Called when a different template name is selected.
366 function newTemplate(sel
) {
367 if (ssChanged
&& !confirm('You have made changes that will be discarded ' +
368 'if you select a new template. Do you really want to do this?'))
370 // Restore the original template selection.
371 for (var i
= 0; i
< sel
.options
.length
; ++i
) {
372 if (sel
.options
[i
].value
== '<?php echo $tempid ?>') {
373 sel
.options
[i
].selected
= true;
378 top
.restoreSession();
379 document
.forms
[0].submit();
382 // Called when the Cancel button is clicked.
383 function doCancel() {
384 if (!ssChanged ||
confirm('You have made changes that will be discarded ' +
385 'if you close now. Click OK if you really want to exit this form.'))
387 <?php
if ($popup) { ?
>
390 top
.restoreSession();
391 location
='<?php echo $GLOBALS['form_exit_url
'] ?>';
396 // Called when the Edit Structure checkbox is clicked.
397 function editChanged() {
398 var f
= document
.forms
[0];
399 var newdisplay
= f
.form_edit_template
.checked ?
'' : 'none';
402 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
403 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
404 if (f
['cell['+i+
']['+j+
']'].value
.charAt(0) != '0') {
405 if (i
>= usedrows
) usedrows
= i +
1;
406 if (j
>= usedcols
) usedcols
= j +
1;
410 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
411 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
412 // document.getElementById('div_'+i+'_'+j).style.display = newdisplay;
413 document
.getElementById('sel_'+i+
'_'+j
).style
.display
= newdisplay
;
414 if (i
>= usedrows || j
>= usedcols
) {
415 document
.getElementById('td_'+i+
'_'+j
).style
.display
= newdisplay
;
421 // Prepare a string for use as an HTML value attribute in single quotes.
422 function escQuotes(s
) {
423 return s
.replace(/'/g, "'");
426 // Parse static text to evaluate possible functions.
427 function genStatic(s) {
431 while ((i = s.indexOf('%day
(')) >= 0) {
432 var s1 = s.substring(0, i);
434 var j = s.indexOf(')', i);
436 var dayinc = parseInt(s.substring(i,j));
437 var mydate = new Date(parseInt(startDate.substring(0,4)),
438 parseInt(startDate.substring(5,7))-1, parseInt(startDate.substring(8)));
439 mydate.setTime(1000 * 60 * 60 * 24 * dayinc + mydate.getTime());
440 var year = mydate.getYear(); if (year < 1900) year += 1900;
441 s = s1 + year + '-' +
442 ('' + (mydate.getMonth() + 101)).substring(1) + '-' +
443 ('' + (mydate.getDate() + 100)).substring(1) +
447 // Parse "%sel(first,second,third,...,default)".
448 while ((i = s.indexOf('%sel
(')) >= 0) {
449 var s1 = s.substring(0, i);
451 var j = s.indexOf(')', i);
453 var x = s.substring(0,j);
454 var k = x.lastIndexOf(',');
456 var dflt = s.substring(k+1, j);
457 x = "<select class='selgen
' onchange='newsel(this
)'>";
458 while ((k = s.indexOf(',', i)) > i) {
460 var elem = s.substring(i,k);
461 x += "<option value='" + elem + "'";
462 if (elem == dflt) x += " selected";
463 x += ">" + elem + "</option>";
467 s = s1 + x + s.substring(j + 1);
468 break; // only one %sel allowed
471 // Parse "%ptp(default)".
472 while ((i = s.indexOf('%ptp
(')) >= 0) {
473 var s1 = s.substring(0, i);
475 var j = s.indexOf(')', i);
477 var dflt = s.substring(i, j);
478 x = "<select class='selgen
' onchange='newptp(this
)'>";
479 x += "<option value=''>-- Select --</option>";
481 foreach ($bcodes['Phys
']['Physiotherapy Procedures
'] as $key => $value) {
482 echo " x += \"<option value='$key'\";\n";
483 echo " if (dflt == '$key') x += ' selected
';\n";
484 echo " x += '>$value</option
>';\n";
488 s = s1 + x + s.substring(j + 1);
489 break; // only one %ptp allowed
495 // Called when a cell type selector in the spreadsheet is clicked.
496 function newType(i,j) {
498 var f = document.forms[0];
499 var typeval = f['cell
['+i+']['+j+']'].value;
500 var thevalue = typeval.substring(1);
501 var thetype = document.getElementById('sel_
'+i+'_
'+j).value;
502 var s = "<input type='hidden
' name='cell
[" + i + "][" + j + "]' " +
503 "value='" + thetype + escQuotes(thevalue) + "' />";
505 if (thetype == '1') {
506 s += genStatic(thevalue);
508 else if (thetype == '2') {
509 s += "<input type='checkbox
' value='1' onclick='cbClick(this
," + i + "," + j + ")'";
510 if (thevalue) s += " checked";
513 else if (thetype == '3') {
514 s += "<input type='text
' onchange='textChange(this
," + i + "," + j + ")'" +
515 " class='intext
' value='" + escQuotes(thevalue) + "' size='12' />";
517 else if (thetype == '4') {
518 s += "<textarea rows='3' cols='25' wrap='virtual
' class='intext
' " +
519 "onchange='longChange(this
," + i + "," + j + ")'>" +
520 escQuotes(thevalue) + "</textarea>";
522 setBlockContent('vis_
' + i + '_
' + j, s);
525 // Called when a checkbox in the spreadsheet is clicked.
526 function cbClick(elem,i,j) {
528 var f = document.forms[0];
529 var cell = f['cell
['+i+']['+j+']'];
530 cell.value = '2' + (elem.checked ? '1' : '');
533 // Called when a text value in the spreadsheet is changed.
534 function textChange(elem,i,j) {
536 var f = document.forms[0];
537 var cell = f['cell
['+i+']['+j+']'];
538 cell.value = '3' + elem.value;
541 // Called when a textarea value in the spreadsheet is changed.
542 function longChange(elem,i,j) {
544 var f = document.forms[0];
545 var cell = f['cell
['+i+']['+j+']'];
546 cell.value = '4' + elem.value;
549 // Helper function to get the value element of a table cell given any
550 // other element within that cell.
551 function getHidden(sel) {
552 var p = sel.parentNode;
553 while (p.tagName != 'TD
') {
554 if (!p.parentNode || p.parentNode == p) {
555 alert("JavaScript error, cannot find TD element");
560 // Get the <input type=hidden> element within this table cell.
561 var f = document.forms[0];
562 var s = p.id.substring(3);
563 var uix = s.indexOf('_
');
564 var i = s.substring(0, uix);
565 var j = s.substring(uix+1);
566 return f['cell
[' + i + '][' + j + ']'];
569 // Called when a user-defined select list has a new selection.
570 // This rewrites the function definition for the select list.
571 function newsel(sel) {
572 var inelem = getHidden(sel);
573 var s = inelem.value;
574 var i = s.indexOf('%sel
(');
575 var j = s.indexOf(')', i);
576 var x = s.substring(0, j);
577 var k = x.lastIndexOf(',');
578 inelem.value = s.substring(0, k+1) + sel.value + s.substring(j);
581 // Called when a physiotherapy select list has a new selection.
582 // This rewrites the function definition for the select list.
583 function newptp(sel) {
584 var inelem = getHidden(sel);
585 var s = inelem.value;
586 var i = s.indexOf('%ptp
(') + 5;
587 var j = s.indexOf(')', i);
588 inelem.value = s.substring(0, i) + sel.value + s.substring(j);
592 $('.datepicker
').datetimepicker({
593 <?php $datetimepicker_timepicker = false; ?>
594 <?php $datetimepicker_showseconds = false; ?>
595 <?php $datetimepicker_formatInput = false; ?>
596 <?php require($GLOBALS['srcdir
'] . '/js
/xl
/jquery
-datetimepicker
-2-5-4.js
.php
'); ?>
597 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
605 <body class="body_top">
606 <form method="post" action="<?php echo "$rootdir/forms/$spreadsheet_form_name/new.php?id=" . attr_url($formid) . "&thisenc=" . attr_url($thisenc);
610 onsubmit="return top.restoreSession()">
613 <table border='0' cellpadding='5' cellspacing='0' style='margin
:8pt
'>
614 <tr bgcolor='#ddddff'>
616 <?php
echo xlt('Start Date'); ?
>:
617 <input type
='text' class='datepicker' name
='form_start_date' id
='form_start_date'
618 size
='10' value
='<?php echo attr($start_date); ?>'
620 <?php
echo ($formid && $start_date) ?
'disabled ' : ''; ?
>/>
622 <?php
echo xlt('Template:'); ?
>
623 <select name
='form_template' onchange
='newTemplate(this)'<?php
echo ($formid) ?
' disabled' : ''; ?
>>
624 <option value
='0'>-- Select
--</option
>
626 while ($trow = sqlFetchArray($tres)) {
627 echo " <option value='" . attr($trow['id']) . "'";
629 $tempid && $tempid == $trow['id'] ||
630 $formid && $template_name == $trow['value']
635 echo ">" . text($trow['value']) . "</option>\n";
640 <input type
='checkbox' name
='form_edit_template'
641 onclick
='editChanged()'
642 title
='<?php echo xla("If you want to change data types, or add rows or columns"); ?>' />
643 <?php
echo xlt('Edit Structure'); ?
>
644 <?php
if ($formid) { ?
>
646 <input type
='checkbox' name
='form_completed'
647 title
='<?php echo xla("If all data for all columns are complete for this form"); ?>'
648 <?php
echo ($form_completed) ?
'checked ' : ''; ?
>/>
649 <?php
echo xlt('Completed'); ?
>
655 <table border
='1' cellpadding
='2' cellspacing
='0' class='sstable'>
658 $drow = sqlFetchArray($dres);
661 $typeprompts = array('unused','static','checkbox','text');
663 for ($i = 0; $i < $num_virtual_rows; ++
$i) {
665 for ($j = 0; $j < $num_virtual_cols; ++
$j) {
666 // Match up with the database for cell type and value.
670 while ($drow && $drow['rownbr'] < $i) {
671 $drow = sqlFetchArray($dres);
674 while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j) {
675 $drow = sqlFetchArray($dres);
678 if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) {
679 $celltype = $drow['datatype'];
680 $cellvalue = $drow['value'];
681 $cellstatic = addslashes($drow['value']);
685 echo " <td id='td_${i}_${j}' valign='top'";
686 if ($i >= $num_used_rows ||
$j >= $num_used_cols) {
687 echo " style='display:none'";
692 /*****************************************************************
693 echo "<span id='div_${i}_${j}' ";
694 echo "style='float:right;cursor:pointer;display:none' ";
695 echo "onclick='newType($i,$j)'>[";
696 echo $typeprompts[$celltype];
698 *****************************************************************/
699 echo "<div class='seldiv'>";
700 echo "<select id='sel_${i}_${j}' class='seltype' style='display:none' " .
701 "onchange='newType($i,$j)'>";
702 foreach ($celltypes as $key => $value) {
703 echo "<option value='" . attr($key) . "'";
704 if ($key == $celltype) {
708 echo ">" . text($value) . "</option>";
713 /****************************************************************/
715 echo "<span id='vis_${i}_${j}'>"; // new //
717 echo "<input type='hidden' name='cell[$i][$j]' value='" . attr($celltype) . attr($cellvalue) . "' />";
718 if ($celltype == '1') {
719 // So we don't have to write a PHP version of genStatic():
720 echo "<script>document.write(genStatic('$cellstatic'));</script>";
721 } elseif ($celltype == '2') {
722 echo "<input type='checkbox' value='1' onclick='cbClick(this,$i,$j)'";
728 } elseif ($celltype == '3') {
729 echo "<input type='text' class='intext' onchange='textChange(this,$i,$j)'";
730 echo " value='" . attr($cellvalue) . "'";
731 echo " size='12' />";
732 } elseif ($celltype == '4') {
733 echo "<textarea rows='3' cols='25' wrap='virtual' class='intext' " .
734 "onchange='longChange(this,$i,$j)'>";
735 echo text($cellvalue);
739 echo "</span>"; // new //
750 <input type
='submit' name
='bn_save_form' value
='Save Form' />
751 <?php
if (!$formid) { ?
>
753 <input type
='submit' name
='bn_save_template' value
='Save as Template:' />
755 <input type
='text' name
='form_new_template_name' value
='<?php echo attr($template_name); ?>' />
757 <input type
='submit' name
='bn_delete_template' value
='Delete Template' />
760 <input type
='button' value
='Cancel' onclick
="doCancel()" />
768 echo " alert(" . js_escape($alertmsg) . ");\n";