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);
34 // encode a plain string for database writing.
35 function real2db($fldval)
37 return addslashes($fldval);
40 // Get the actual string from a form field.
41 function form2real($fldval)
43 $fldval = trim($fldval);
47 // encode a plain string for html display.
48 function real2form($fldval)
50 return htmlspecialchars($fldval, ENT_QUOTES
);
53 if (empty($spreadsheet_title)) {
54 $spreadsheet_title = 'Injury Log';
57 // Putting an error message in here will result in a javascript alert.
60 // Determine the encounter that we are working with.
61 $thisenc = empty($_GET['thisenc']) ?
$encounter : $_GET['thisenc'] +
0;
63 // If we are invoked as a popup (not in an encounter):
64 $popup = $_GET['popup'];
66 // The form ID is passed to us when an existing encounter form is loaded.
67 $formid = $_GET['id'];
69 // $tempid is the currently selected template, if any.
70 $tempid = $_POST['form_template'] +
0;
72 // This is the start date to be saved with the spreadsheet.
75 $form_completed = '0';
77 if (!$popup && !$encounter) { // $encounter comes from globals.php
78 die("Internal error: we do not seem to be in an encounter!");
81 // Get the name of the template selected by the dropdown, if any;
82 // or if we are loading a form then it comes from that.
85 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
86 "id = $tempid AND rownbr = -1 AND colnbr = -1");
87 $template_name = $trow['value'];
89 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
90 "id = $formid AND rownbr = -1 AND colnbr = -1");
91 list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3);
95 $start_date = form2real($_POST['form_start_date']);
98 // Used rows and columns are those beyond which there are only unused cells.
102 // If we are saving...
104 if ($_POST['bn_save_form'] ||
$_POST['bn_save_template']) {
105 // The form data determines how many rows and columns are now used.
106 $cells = $_POST['cell'];
107 for ($i = 0; $i < count($cells); ++
$i) {
109 for ($j = 0; $j < count($row); ++
$j) {
110 if (substr($row[$j], 0, 1)) {
111 if ($i >= $num_used_rows) {
112 $num_used_rows = $i +
1;
115 if ($j >= $num_used_cols) {
116 $num_used_cols = $j +
1;
122 if ($_POST['bn_save_form']) {
123 $form_completed = $_POST['form_completed'] ?
'1' : '0';
125 // If updating an existing form...
127 sqlStatement("UPDATE form_$spreadsheet_form_name SET " .
128 "value = '$form_completed|$start_date|$template_name' " .
129 "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1");
130 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
131 "id = '$formid' AND rownbr >= 0 AND colnbr >= 0");
132 } // If adding a new form...
135 "SELECT pid FROM form_encounter WHERE encounter = ? ORDER BY id DESC LIMIT 1",
138 $thispid = $tmprow['pid'];
139 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
140 $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_$spreadsheet_form_name");
141 $formid = $tmprow['maxid'] +
1;
146 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
147 "id, rownbr, colnbr, datatype, value " .
149 "$formid, -1, -1, 0, " .
150 "'$form_completed|$start_date|$template_name' " .
152 sqlStatement("UNLOCK TABLES");
157 "$spreadsheet_form_name",
164 } else { // saving a template
165 // The rule is, we can update the original name, or insert a new name
166 // which must not match any existing template name.
167 $new_template_name = form2real($_POST['form_new_template_name']);
168 if ($new_template_name != $template_name) {
169 $trow = sqlQuery("SELECT id FROM form_$spreadsheet_form_name WHERE " .
170 "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" .
171 real2db($new_template_name) . "'");
173 $alertmsg = "Template \"" . real2form($new_template_name) .
174 "\" already exists!";
176 $tempid = 0; // to force insert of new template
177 $template_name = $new_template_name;
182 // If updating an existing template...
184 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
185 "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0");
186 } // If adding a new template...
188 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
189 $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_$spreadsheet_form_name");
190 $tempid = $tmprow['minid'] - 1;
195 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
196 "id, rownbr, colnbr, datatype, value " .
198 "$tempid, -1, -1, 0, " .
199 "'" . real2db($template_name) . "' " .
201 sqlStatement("UNLOCK TABLES");
209 // Finally, save the table cells.
210 for ($i = 0; $i < $num_used_rows; ++
$i) {
211 for ($j = 0; $j < $num_used_cols; ++
$j) {
212 $tmp = $cells[$i][$j];
213 $celltype = substr($tmp, 0, 1) +
0;
214 $cellvalue = form2db(substr($tmp, 1));
216 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
217 "id, rownbr, colnbr, datatype, value " .
219 "$saveid, $i, $j, $celltype, '$cellvalue' )");
224 } else if ($_POST['bn_delete_template'] && $tempid) {
225 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
231 if ($_POST['bn_save_form'] && !$alertmsg && !$popup) {
232 formHeader("Redirecting....");
238 // If we get here then we are displaying a spreadsheet, either a template or
239 // an encounter form.
241 // Get the array of template names.
242 $tres = sqlStatement("SELECT id, value FROM form_$spreadsheet_form_name WHERE " .
243 "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value");
247 # If we are reloading a form, get it.
249 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
250 "id = '$formid' ORDER BY rownbr, colnbr");
251 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
252 "FROM form_$spreadsheet_form_name WHERE id = '$formid'");
253 $num_used_rows = $tmprow['rowmax'] +
1;
254 $num_used_cols = $tmprow['colmax'] +
1;
255 } # Otherwise if we are editing a template, get it.
257 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
258 "id = '$tempid' ORDER BY rownbr, colnbr");
259 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
260 "FROM form_$spreadsheet_form_name WHERE id = '$tempid'");
261 $num_used_rows = $tmprow['rowmax'] +
1;
262 $num_used_cols = $tmprow['colmax'] +
1;
265 // Virtual rows and columns are those available when in Edit Structure mode,
266 // and include some additional ones beyond those used. This allows quite a
267 // lot of stuff to be entered before having to save the template.
268 $num_virtual_rows = $num_used_rows ?
$num_used_rows +
5 : 10;
269 $num_virtual_cols = $num_used_cols ?
$num_used_cols +
5 : 10;
273 <?php
html_header_show();?
>
274 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
275 <link rel
="stylesheet" href
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker/build/jquery.datetimepicker.min.css">
279 font
-family
: sans
-serif
;
284 font
-family
: sans
-serif
;
287 background
-color
: transparent
;
290 font
-family
: sans
-serif
;
293 background
-color
: transparent
;
296 font
-family
: sans
-serif
;
299 background
-color
: transparent
;
308 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery/dist/jquery.min.js"></script
>
309 <script type
="text/javascript" src
="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-datetimepicker/build/jquery.datetimepicker.full.min.js"></script
>
310 <script type
="text/javascript" src
="../../../library/textformat.js?v=<?php echo $v_js_includes; ?>"></script
>
312 <script language
="JavaScript">
314 var ssChanged
= false; // if they have changed anything in the spreadsheet
315 var startDate
= '<?php echo $start_date ? $start_date : date('Y
-m
-d
'); ?>';
317 // In case we are a popup (top level) window, handle top.restoreSession() calls.
318 function restoreSession() {
319 return opener
.top
.restoreSession();
322 // Helper function to set the contents of a block.
323 function setBlockContent(id
, content
) {
324 if (document
.getElementById
) {
325 var x
= document
.getElementById(id
);
327 x
.innerHTML
= content
;
329 else if (document
.all
) {
330 var x
= document
.all
[id
];
331 x
.innerHTML
= content
;
333 // alert("ID = \"" + id + "\", string = \"" + content + "\"");
336 // Called when a different template name is selected.
337 function newTemplate(sel
) {
338 if (ssChanged
&& !confirm('You have made changes that will be discarded ' +
339 'if you select a new template. Do you really want to do this?'))
341 // Restore the original template selection.
342 for (var i
= 0; i
< sel
.options
.length
; ++i
) {
343 if (sel
.options
[i
].value
== '<?php echo $tempid ?>') {
344 sel
.options
[i
].selected
= true;
349 top
.restoreSession();
350 document
.forms
[0].submit();
353 // Called when the Cancel button is clicked.
354 function doCancel() {
355 if (!ssChanged ||
confirm('You have made changes that will be discarded ' +
356 'if you close now. Click OK if you really want to exit this form.'))
358 <?php
if ($popup) { ?
>
361 top
.restoreSession();
362 location
='<?php echo $GLOBALS['form_exit_url
'] ?>';
367 // Called when the Edit Structure checkbox is clicked.
368 function editChanged() {
369 var f
= document
.forms
[0];
370 var newdisplay
= f
.form_edit_template
.checked ?
'' : 'none';
373 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
374 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
375 if (f
['cell['+i+
']['+j+
']'].value
.charAt(0) != '0') {
376 if (i
>= usedrows
) usedrows
= i +
1;
377 if (j
>= usedcols
) usedcols
= j +
1;
381 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
382 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
383 // document.getElementById('div_'+i+'_'+j).style.display = newdisplay;
384 document
.getElementById('sel_'+i+
'_'+j
).style
.display
= newdisplay
;
385 if (i
>= usedrows || j
>= usedcols
) {
386 document
.getElementById('td_'+i+
'_'+j
).style
.display
= newdisplay
;
392 // Prepare a string for use as an HTML value attribute in single quotes.
393 function escQuotes(s
) {
394 return s
.replace(/'/g, "'");
397 // Parse static text to evaluate possible functions.
398 function genStatic(s) {
402 while ((i = s.indexOf('%day
(')) >= 0) {
403 var s1 = s.substring(0, i);
405 var j = s.indexOf(')', i);
407 var dayinc = parseInt(s.substring(i,j));
408 var mydate = new Date(parseInt(startDate.substring(0,4)),
409 parseInt(startDate.substring(5,7))-1, parseInt(startDate.substring(8)));
410 mydate.setTime(1000 * 60 * 60 * 24 * dayinc + mydate.getTime());
411 var year = mydate.getYear(); if (year < 1900) year += 1900;
412 s = s1 + year + '-' +
413 ('' + (mydate.getMonth() + 101)).substring(1) + '-' +
414 ('' + (mydate.getDate() + 100)).substring(1) +
418 // Parse "%sel(first,second,third,...,default)".
419 while ((i = s.indexOf('%sel
(')) >= 0) {
420 var s1 = s.substring(0, i);
422 var j = s.indexOf(')', i);
424 var x = s.substring(0,j);
425 var k = x.lastIndexOf(',');
427 var dflt = s.substring(k+1, j);
428 x = "<select class='selgen
' onchange='newsel(this
)'>";
429 while ((k = s.indexOf(',', i)) > i) {
431 var elem = s.substring(i,k);
432 x += "<option value='" + elem + "'";
433 if (elem == dflt) x += " selected";
434 x += ">" + elem + "</option>";
438 s = s1 + x + s.substring(j + 1);
439 break; // only one %sel allowed
442 // Parse "%ptp(default)".
443 while ((i = s.indexOf('%ptp
(')) >= 0) {
444 var s1 = s.substring(0, i);
446 var j = s.indexOf(')', i);
448 var dflt = s.substring(i, j);
449 x = "<select class='selgen
' onchange='newptp(this
)'>";
450 x += "<option value=''>-- Select --</option>";
452 foreach ($bcodes['Phys
']['Physiotherapy Procedures
'] as $key => $value) {
453 echo " x += \"<option value='$key'\";\n";
454 echo " if (dflt == '$key') x += ' selected
';\n";
455 echo " x += '>$value</option
>';\n";
459 s = s1 + x + s.substring(j + 1);
460 break; // only one %ptp allowed
466 // Called when a cell type selector in the spreadsheet is clicked.
467 function newType(i,j) {
469 var f = document.forms[0];
470 var typeval = f['cell
['+i+']['+j+']'].value;
471 var thevalue = typeval.substring(1);
472 var thetype = document.getElementById('sel_
'+i+'_
'+j).value;
473 var s = "<input type='hidden
' name='cell
[" + i + "][" + j + "]' " +
474 "value='" + thetype + escQuotes(thevalue) + "' />";
476 if (thetype == '1') {
477 s += genStatic(thevalue);
479 else if (thetype == '2') {
480 s += "<input type='checkbox
' value='1' onclick='cbClick(this
," + i + "," + j + ")'";
481 if (thevalue) s += " checked";
484 else if (thetype == '3') {
485 s += "<input type='text
' onchange='textChange(this
," + i + "," + j + ")'" +
486 " class='intext
' value='" + escQuotes(thevalue) + "' size='12' />";
488 else if (thetype == '4') {
489 s += "<textarea rows='3' cols='25' wrap='virtual
' class='intext
' " +
490 "onchange='longChange(this
," + i + "," + j + ")'>" +
491 escQuotes(thevalue) + "</textarea>";
493 setBlockContent('vis_
' + i + '_
' + j, s);
496 // Called when a checkbox in the spreadsheet is clicked.
497 function cbClick(elem,i,j) {
499 var f = document.forms[0];
500 var cell = f['cell
['+i+']['+j+']'];
501 cell.value = '2' + (elem.checked ? '1' : '');
504 // Called when a text value in the spreadsheet is changed.
505 function textChange(elem,i,j) {
507 var f = document.forms[0];
508 var cell = f['cell
['+i+']['+j+']'];
509 cell.value = '3' + elem.value;
512 // Called when a textarea value in the spreadsheet is changed.
513 function longChange(elem,i,j) {
515 var f = document.forms[0];
516 var cell = f['cell
['+i+']['+j+']'];
517 cell.value = '4' + elem.value;
520 // Helper function to get the value element of a table cell given any
521 // other element within that cell.
522 function getHidden(sel) {
523 var p = sel.parentNode;
524 while (p.tagName != 'TD
') {
525 if (!p.parentNode || p.parentNode == p) {
526 alert("JavaScript error, cannot find TD element");
531 // Get the <input type=hidden> element within this table cell.
532 var f = document.forms[0];
533 var s = p.id.substring(3);
534 var uix = s.indexOf('_
');
535 var i = s.substring(0, uix);
536 var j = s.substring(uix+1);
537 return f['cell
[' + i + '][' + j + ']'];
540 // Called when a user-defined select list has a new selection.
541 // This rewrites the function definition for the select list.
542 function newsel(sel) {
543 var inelem = getHidden(sel);
544 var s = inelem.value;
545 var i = s.indexOf('%sel
(');
546 var j = s.indexOf(')', i);
547 var x = s.substring(0, j);
548 var k = x.lastIndexOf(',');
549 inelem.value = s.substring(0, k+1) + sel.value + s.substring(j);
552 // Called when a physiotherapy select list has a new selection.
553 // This rewrites the function definition for the select list.
554 function newptp(sel) {
555 var inelem = getHidden(sel);
556 var s = inelem.value;
557 var i = s.indexOf('%ptp
(') + 5;
558 var j = s.indexOf(')', i);
559 inelem.value = s.substring(0, i) + sel.value + s.substring(j);
562 $(document).ready(function() {
563 $('.datepicker
').datetimepicker({
564 <?php $datetimepicker_timepicker = false; ?>
565 <?php $datetimepicker_showseconds = false; ?>
566 <?php $datetimepicker_formatInput = false; ?>
567 <?php require($GLOBALS['srcdir
'] . '/js
/xl
/jquery
-datetimepicker
-2-5-4.js
.php
'); ?>
568 <?php // can add any additional javascript settings to datetimepicker here; need to prepend first setting with a comma ?>
576 <body class="body_top">
577 <form method="post" action="<?php echo "$rootdir/forms/$spreadsheet_form_name/new.php?id=$formid&thisenc=$thisenc";
581 onsubmit="return top.restoreSession()">
584 <table border='0' cellpadding='5' cellspacing='0' style='margin
:8pt
'>
585 <tr bgcolor='#ddddff'>
587 <?php
xl('Start Date', 'e'); ?
>:
588 <input type
='text' class='datepicker' name
='form_start_date' id
='form_start_date'
589 size
='10' value
='<?php echo $start_date; ?>'
591 <?php
echo ($formid && $start_date) ?
'disabled ' : ''; ?
>/>
593 <?php
xl('Template:', 'e') ?
>
594 <select name
='form_template' onchange
='newTemplate(this)'<?php
echo ($formid) ?
' disabled' : ''; ?
>>
595 <option value
='0'>-- Select
--</option
>
597 while ($trow = sqlFetchArray($tres)) {
598 echo " <option value='" . $trow['id'] . "'";
599 if ($tempid && $tempid == $trow['id'] ||
600 $formid && $template_name == $trow['value']) {
604 echo ">" . $trow['value'] . "</option>\n";
609 <input type
='checkbox' name
='form_edit_template'
610 onclick
='editChanged()'
611 title
='<?php xl("If you want to change data types, or add rows or columns", "e") ?>' />
612 <?php
xl('Edit Structure', 'e') ?
>
613 <?php
if ($formid) { ?
>
615 <input type
='checkbox' name
='form_completed'
616 title
='<?php xl("If all data for all columns are complete for this form", "e") ?>'
617 <?php
echo ($form_completed) ?
'checked ' : ''; ?
>/>
618 <?php
xl('Completed', 'e') ?
>
624 <table border
='1' cellpadding
='2' cellspacing
='0' class='sstable'>
627 $drow = sqlFetchArray($dres);
630 $typeprompts = array('unused','static','checkbox','text');
632 for ($i = 0; $i < $num_virtual_rows; ++
$i) {
634 for ($j = 0; $j < $num_virtual_cols; ++
$j) {
635 // Match up with the database for cell type and value.
639 while ($drow && $drow['rownbr'] < $i) {
640 $drow = sqlFetchArray($dres);
643 while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j) {
644 $drow = sqlFetchArray($dres);
647 if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) {
648 $celltype = $drow['datatype'];
649 $cellvalue = real2form($drow['value']);
650 $cellstatic = addslashes($drow['value']);
654 echo " <td id='td_${i}_${j}' valign='top'";
655 if ($i >= $num_used_rows ||
$j >= $num_used_cols) {
656 echo " style='display:none'";
661 /*****************************************************************
662 echo "<span id='div_${i}_${j}' ";
663 echo "style='float:right;cursor:pointer;display:none' ";
664 echo "onclick='newType($i,$j)'>[";
665 echo $typeprompts[$celltype];
667 *****************************************************************/
668 echo "<div class='seldiv'>";
669 echo "<select id='sel_${i}_${j}' class='seltype' style='display:none' " .
670 "onchange='newType($i,$j)'>";
671 foreach ($celltypes as $key => $value) {
672 echo "<option value='$key'";
673 if ($key == $celltype) {
677 echo ">$value</option>";
682 /****************************************************************/
684 echo "<span id='vis_${i}_${j}'>"; // new //
686 echo "<input type='hidden' name='cell[$i][$j]' value='$celltype$cellvalue' />";
687 if ($celltype == '1') {
688 // So we don't have to write a PHP version of genStatic():
689 echo "<script language='JavaScript'>document.write(genStatic('$cellstatic'));</script>";
690 } else if ($celltype == '2') {
691 echo "<input type='checkbox' value='1' onclick='cbClick(this,$i,$j)'";
697 } else if ($celltype == '3') {
698 echo "<input type='text' class='intext' onchange='textChange(this,$i,$j)'";
699 echo " value='$cellvalue'";
700 echo " size='12' />";
701 } else if ($celltype == '4') {
702 echo "<textarea rows='3' cols='25' wrap='virtual' class='intext' " .
703 "onchange='longChange(this,$i,$j)'>";
708 echo "</span>"; // new //
719 <input type
='submit' name
='bn_save_form' value
='Save Form' />
720 <?php
if (!$formid) { ?
>
722 <input type
='submit' name
='bn_save_template' value
='Save as Template:' />
724 <input type
='text' name
='form_new_template_name' value
='<?php echo $template_name ?>' />
726 <input type
='submit' name
='bn_delete_template' value
='Delete Template' />
729 <input type
='button' value
='Cancel' onclick
="doCancel()" />
734 <script language
='JavaScript'>
737 echo " alert('$alertmsg');\n";