2 // Copyright (C) 2006-2011 Rod Roark <rod@sunsetsystems.com>
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 include_once(dirname(__FILE__
) . '/api.inc');
10 include_once(dirname(__FILE__
) . '/forms.inc');
11 include_once(dirname(__FILE__
) . '/../interface/forms/fee_sheet/codes.php');
22 // encode a string from a form field for database writing.
23 function form2db($fldval)
25 $fldval = trim($fldval);
26 $fldval = strip_escape_custom($fldval);
30 // encode a plain string for database writing.
31 function real2db($fldval)
33 return addslashes($fldval);
36 // Get the actual string from a form field.
37 function form2real($fldval)
39 $fldval = trim($fldval);
40 $fldval = strip_escape_custom($fldval);
44 // encode a plain string for html display.
45 function real2form($fldval)
47 return htmlspecialchars($fldval, ENT_QUOTES
);
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 form_$spreadsheet_form_name WHERE " .
83 "id = $tempid AND rownbr = -1 AND colnbr = -1");
84 $template_name = $trow['value'];
86 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
87 "id = $formid AND rownbr = -1 AND colnbr = -1");
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...
124 sqlStatement("UPDATE form_$spreadsheet_form_name SET " .
125 "value = '$form_completed|$start_date|$template_name' " .
126 "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1");
127 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
128 "id = '$formid' AND rownbr >= 0 AND colnbr >= 0");
129 } // If adding a new form...
132 "SELECT pid FROM form_encounter WHERE encounter = ? ORDER BY id DESC LIMIT 1",
135 $thispid = $tmprow['pid'];
136 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
137 $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_$spreadsheet_form_name");
138 $formid = $tmprow['maxid'] +
1;
143 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
144 "id, rownbr, colnbr, datatype, value " .
146 "$formid, -1, -1, 0, " .
147 "'$form_completed|$start_date|$template_name' " .
149 sqlStatement("UNLOCK TABLES");
154 "$spreadsheet_form_name",
161 } else { // saving a template
162 // The rule is, we can update the original name, or insert a new name
163 // which must not match any existing template name.
164 $new_template_name = form2real($_POST['form_new_template_name']);
165 if ($new_template_name != $template_name) {
166 $trow = sqlQuery("SELECT id FROM form_$spreadsheet_form_name WHERE " .
167 "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" .
168 real2db($new_template_name) . "'");
170 $alertmsg = "Template \"" . real2form($new_template_name) .
171 "\" already exists!";
173 $tempid = 0; // to force insert of new template
174 $template_name = $new_template_name;
179 // If updating an existing template...
181 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
182 "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0");
183 } // If adding a new template...
185 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
186 $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_$spreadsheet_form_name");
187 $tempid = $tmprow['minid'] - 1;
192 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
193 "id, rownbr, colnbr, datatype, value " .
195 "$tempid, -1, -1, 0, " .
196 "'" . real2db($template_name) . "' " .
198 sqlStatement("UNLOCK TABLES");
206 // Finally, save the table cells.
207 for ($i = 0; $i < $num_used_rows; ++
$i) {
208 for ($j = 0; $j < $num_used_cols; ++
$j) {
209 $tmp = $cells[$i][$j];
210 $celltype = substr($tmp, 0, 1) +
0;
211 $cellvalue = form2db(substr($tmp, 1));
213 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
214 "id, rownbr, colnbr, datatype, value " .
216 "$saveid, $i, $j, $celltype, '$cellvalue' )");
221 } else if ($_POST['bn_delete_template'] && $tempid) {
222 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
228 if ($_POST['bn_save_form'] && !$alertmsg && !$popup) {
229 formHeader("Redirecting....");
235 // If we get here then we are displaying a spreadsheet, either a template or
236 // an encounter form.
238 // Get the array of template names.
239 $tres = sqlStatement("SELECT id, value FROM form_$spreadsheet_form_name WHERE " .
240 "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value");
244 # If we are reloading a form, get it.
246 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
247 "id = '$formid' ORDER BY rownbr, colnbr");
248 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
249 "FROM form_$spreadsheet_form_name WHERE id = '$formid'");
250 $num_used_rows = $tmprow['rowmax'] +
1;
251 $num_used_cols = $tmprow['colmax'] +
1;
252 } # Otherwise if we are editing a template, get it.
254 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
255 "id = '$tempid' ORDER BY rownbr, colnbr");
256 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
257 "FROM form_$spreadsheet_form_name WHERE id = '$tempid'");
258 $num_used_rows = $tmprow['rowmax'] +
1;
259 $num_used_cols = $tmprow['colmax'] +
1;
262 // Virtual rows and columns are those available when in Edit Structure mode,
263 // and include some additional ones beyond those used. This allows quite a
264 // lot of stuff to be entered before having to save the template.
265 $num_virtual_rows = $num_used_rows ?
$num_used_rows +
5 : 10;
266 $num_virtual_cols = $num_used_cols ?
$num_used_cols +
5 : 10;
270 <?php
html_header_show();?
>
271 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
272 <style type
="text/css">@import
url(../../../library
/dynarch_calendar
.css
);</style
>
275 font
-family
: sans
-serif
;
280 font
-family
: sans
-serif
;
283 background
-color
: transparent
;
286 font
-family
: sans
-serif
;
289 background
-color
: transparent
;
292 font
-family
: sans
-serif
;
295 background
-color
: transparent
;
303 <script type
="text/javascript" src
="../../../library/textformat.js"></script
>
304 <script type
="text/javascript" src
="../../../library/dynarch_calendar.js"></script
>
305 <script type
="text/javascript" src
="../../../library/dynarch_calendar_en.js"></script
>
306 <script type
="text/javascript" src
="../../../library/dynarch_calendar_setup.js"></script
>
308 <script language
="JavaScript">
309 var mypcc
= '<?php echo $GLOBALS['phone_country_code
']; ?>';
310 var ssChanged
= false; // if they have changed anything in the spreadsheet
311 var startDate
= '<?php echo $start_date ? $start_date : date('Y
-m
-d
'); ?>';
313 // In case we are a popup (top level) window, handle top.restoreSession() calls.
314 function restoreSession() {
315 return opener
.top
.restoreSession();
318 // Helper function to set the contents of a block.
319 function setBlockContent(id
, content
) {
320 if (document
.getElementById
) {
321 var x
= document
.getElementById(id
);
323 x
.innerHTML
= content
;
325 else if (document
.all
) {
326 var x
= document
.all
[id
];
327 x
.innerHTML
= content
;
329 // alert("ID = \"" + id + "\", string = \"" + content + "\"");
332 // Called when a different template name is selected.
333 function newTemplate(sel
) {
334 if (ssChanged
&& !confirm('You have made changes that will be discarded ' +
335 'if you select a new template. Do you really want to do this?'))
337 // Restore the original template selection.
338 for (var i
= 0; i
< sel
.options
.length
; ++i
) {
339 if (sel
.options
[i
].value
== '<?php echo $tempid ?>') {
340 sel
.options
[i
].selected
= true;
345 top
.restoreSession();
346 document
.forms
[0].submit();
349 // Called when the Cancel button is clicked.
350 function doCancel() {
351 if (!ssChanged ||
confirm('You have made changes that will be discarded ' +
352 'if you close now. Click OK if you really want to exit this form.'))
354 <?php
if ($popup) { ?
>
357 top
.restoreSession();
358 location
='<?php echo $GLOBALS['form_exit_url
'] ?>';
363 // Called when the Edit Structure checkbox is clicked.
364 function editChanged() {
365 var f
= document
.forms
[0];
366 var newdisplay
= f
.form_edit_template
.checked ?
'' : 'none';
369 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
370 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
371 if (f
['cell['+i+
']['+j+
']'].value
.charAt(0) != '0') {
372 if (i
>= usedrows
) usedrows
= i +
1;
373 if (j
>= usedcols
) usedcols
= j +
1;
377 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
378 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
379 // document.getElementById('div_'+i+'_'+j).style.display = newdisplay;
380 document
.getElementById('sel_'+i+
'_'+j
).style
.display
= newdisplay
;
381 if (i
>= usedrows || j
>= usedcols
) {
382 document
.getElementById('td_'+i+
'_'+j
).style
.display
= newdisplay
;
388 // Prepare a string for use as an HTML value attribute in single quotes.
389 function escQuotes(s
) {
390 return s
.replace(/'/g, "'");
393 // Parse static text to evaluate possible functions.
394 function genStatic(s) {
398 while ((i = s.indexOf('%day
(')) >= 0) {
399 var s1 = s.substring(0, i);
401 var j = s.indexOf(')', i);
403 var dayinc = parseInt(s.substring(i,j));
404 var mydate = new Date(parseInt(startDate.substring(0,4)),
405 parseInt(startDate.substring(5,7))-1, parseInt(startDate.substring(8)));
406 mydate.setTime(1000 * 60 * 60 * 24 * dayinc + mydate.getTime());
407 var year = mydate.getYear(); if (year < 1900) year += 1900;
408 s = s1 + year + '-' +
409 ('' + (mydate.getMonth() + 101)).substring(1) + '-' +
410 ('' + (mydate.getDate() + 100)).substring(1) +
414 // Parse "%sel(first,second,third,...,default)".
415 while ((i = s.indexOf('%sel
(')) >= 0) {
416 var s1 = s.substring(0, i);
418 var j = s.indexOf(')', i);
420 var x = s.substring(0,j);
421 var k = x.lastIndexOf(',');
423 var dflt = s.substring(k+1, j);
424 x = "<select class='selgen
' onchange='newsel(this
)'>";
425 while ((k = s.indexOf(',', i)) > i) {
427 var elem = s.substring(i,k);
428 x += "<option value='" + elem + "'";
429 if (elem == dflt) x += " selected";
430 x += ">" + elem + "</option>";
434 s = s1 + x + s.substring(j + 1);
435 break; // only one %sel allowed
438 // Parse "%ptp(default)".
439 while ((i = s.indexOf('%ptp
(')) >= 0) {
440 var s1 = s.substring(0, i);
442 var j = s.indexOf(')', i);
444 var dflt = s.substring(i, j);
445 x = "<select class='selgen
' onchange='newptp(this
)'>";
446 x += "<option value=''>-- Select --</option>";
448 foreach ($bcodes['Phys
']['Physiotherapy Procedures
'] as $key => $value) {
449 echo " x += \"<option value='$key'\";\n";
450 echo " if (dflt == '$key') x += ' selected
';\n";
451 echo " x += '>$value</option
>';\n";
455 s = s1 + x + s.substring(j + 1);
456 break; // only one %ptp allowed
462 // Called when a cell type selector in the spreadsheet is clicked.
463 function newType(i,j) {
465 var f = document.forms[0];
466 var typeval = f['cell
['+i+']['+j+']'].value;
467 var thevalue = typeval.substring(1);
468 var thetype = document.getElementById('sel_
'+i+'_
'+j).value;
469 var s = "<input type='hidden
' name='cell
[" + i + "][" + j + "]' " +
470 "value='" + thetype + escQuotes(thevalue) + "' />";
472 if (thetype == '1') {
473 s += genStatic(thevalue);
475 else if (thetype == '2') {
476 s += "<input type='checkbox
' value='1' onclick='cbClick(this
," + i + "," + j + ")'";
477 if (thevalue) s += " checked";
480 else if (thetype == '3') {
481 s += "<input type='text
' onchange='textChange(this
," + i + "," + j + ")'" +
482 " class='intext
' value='" + escQuotes(thevalue) + "' size='12' />";
484 else if (thetype == '4') {
485 s += "<textarea rows='3' cols='25' wrap='virtual
' class='intext
' " +
486 "onchange='longChange(this
," + i + "," + j + ")'>" +
487 escQuotes(thevalue) + "</textarea>";
489 setBlockContent('vis_
' + i + '_
' + j, s);
492 // Called when a checkbox in the spreadsheet is clicked.
493 function cbClick(elem,i,j) {
495 var f = document.forms[0];
496 var cell = f['cell
['+i+']['+j+']'];
497 cell.value = '2' + (elem.checked ? '1' : '');
500 // Called when a text value in the spreadsheet is changed.
501 function textChange(elem,i,j) {
503 var f = document.forms[0];
504 var cell = f['cell
['+i+']['+j+']'];
505 cell.value = '3' + elem.value;
508 // Called when a textarea value in the spreadsheet is changed.
509 function longChange(elem,i,j) {
511 var f = document.forms[0];
512 var cell = f['cell
['+i+']['+j+']'];
513 cell.value = '4' + elem.value;
516 // Helper function to get the value element of a table cell given any
517 // other element within that cell.
518 function getHidden(sel) {
519 var p = sel.parentNode;
520 while (p.tagName != 'TD
') {
521 if (!p.parentNode || p.parentNode == p) {
522 alert("JavaScript error, cannot find TD element");
527 // Get the <input type=hidden> element within this table cell.
528 var f = document.forms[0];
529 var s = p.id.substring(3);
530 var uix = s.indexOf('_
');
531 var i = s.substring(0, uix);
532 var j = s.substring(uix+1);
533 return f['cell
[' + i + '][' + j + ']'];
536 // Called when a user-defined select list has a new selection.
537 // This rewrites the function definition for the select list.
538 function newsel(sel) {
539 var inelem = getHidden(sel);
540 var s = inelem.value;
541 var i = s.indexOf('%sel
(');
542 var j = s.indexOf(')', i);
543 var x = s.substring(0, j);
544 var k = x.lastIndexOf(',');
545 inelem.value = s.substring(0, k+1) + sel.value + s.substring(j);
548 // Called when a physiotherapy select list has a new selection.
549 // This rewrites the function definition for the select list.
550 function newptp(sel) {
551 var inelem = getHidden(sel);
552 var s = inelem.value;
553 var i = s.indexOf('%ptp
(') + 5;
554 var j = s.indexOf(')', i);
555 inelem.value = s.substring(0, i) + sel.value + s.substring(j);
562 <body class="body_top">
563 <form method="post" action="<?php echo "$rootdir/forms/$spreadsheet_form_name/new.php?id=$formid&thisenc=$thisenc";
567 onsubmit="return top.restoreSession()">
570 <table border='0' cellpadding='5' cellspacing='0' style='margin
:8pt
'>
571 <tr bgcolor='#ddddff'>
573 <?php
xl('Start Date', 'e'); ?
>:
574 <input type
='text' name
='form_start_date' id
='form_start_date'
575 size
='10' value
='<?php echo $start_date; ?>'
576 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'
577 <?php
if ($formid && $start_date) {
580 <?php
if (!$formid ||
!$start_date) { ?
>
581 <img src
='../../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
582 id
='img_start_date' border
='0' alt
='[?]' style
='cursor:pointer'
583 title
='Click here to choose a date'>
586 <?php
xl('Template:', 'e') ?
>
587 <select name
='form_template' onchange
='newTemplate(this)'<?php
if ($formid) {
590 <option value
='0'>-- Select
--</option
>
592 while ($trow = sqlFetchArray($tres)) {
593 echo " <option value='" . $trow['id'] . "'";
594 if ($tempid && $tempid == $trow['id'] ||
595 $formid && $template_name == $trow['value']) {
599 echo ">" . $trow['value'] . "</option>\n";
604 <input type
='checkbox' name
='form_edit_template'
605 onclick
='editChanged()'
606 title
='<?php xl("If you want to change data types, or add rows or columns", "e") ?>' />
607 <?php
xl('Edit Structure', 'e') ?
>
608 <?php
if ($formid) { ?
>
610 <input type
='checkbox' name
='form_completed'
611 title
='<?php xl("If all data for all columns are complete for this form", "e") ?>'
612 <?php
if ($form_completed) {
615 <?php
xl('Completed', 'e') ?
>
621 <table border
='1' cellpadding
='2' cellspacing
='0' class='sstable'>
624 $drow = sqlFetchArray($dres);
627 $typeprompts = array('unused','static','checkbox','text');
629 for ($i = 0; $i < $num_virtual_rows; ++
$i) {
631 for ($j = 0; $j < $num_virtual_cols; ++
$j) {
632 // Match up with the database for cell type and value.
636 while ($drow && $drow['rownbr'] < $i) {
637 $drow = sqlFetchArray($dres);
640 while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j) {
641 $drow = sqlFetchArray($dres);
644 if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) {
645 $celltype = $drow['datatype'];
646 $cellvalue = real2form($drow['value']);
647 $cellstatic = addslashes($drow['value']);
651 echo " <td id='td_${i}_${j}' valign='top'";
652 if ($i >= $num_used_rows ||
$j >= $num_used_cols) {
653 echo " style='display:none'";
658 /*****************************************************************
659 echo "<span id='div_${i}_${j}' ";
660 echo "style='float:right;cursor:pointer;display:none' ";
661 echo "onclick='newType($i,$j)'>[";
662 echo $typeprompts[$celltype];
664 *****************************************************************/
665 echo "<div class='seldiv'>";
666 echo "<select id='sel_${i}_${j}' class='seltype' style='display:none' " .
667 "onchange='newType($i,$j)'>";
668 foreach ($celltypes as $key => $value) {
669 echo "<option value='$key'";
670 if ($key == $celltype) {
674 echo ">$value</option>";
679 /****************************************************************/
681 echo "<span id='vis_${i}_${j}'>"; // new //
683 echo "<input type='hidden' name='cell[$i][$j]' value='$celltype$cellvalue' />";
684 if ($celltype == '1') {
685 // So we don't have to write a PHP version of genStatic():
686 echo "<script language='JavaScript'>document.write(genStatic('$cellstatic'));</script>";
687 } else if ($celltype == '2') {
688 echo "<input type='checkbox' value='1' onclick='cbClick(this,$i,$j)'";
694 } else if ($celltype == '3') {
695 echo "<input type='text' class='intext' onchange='textChange(this,$i,$j)'";
696 echo " value='$cellvalue'";
697 echo " size='12' />";
698 } else if ($celltype == '4') {
699 echo "<textarea rows='3' cols='25' wrap='virtual' class='intext' " .
700 "onchange='longChange(this,$i,$j)'>";
705 echo "</span>"; // new //
716 <input type
='submit' name
='bn_save_form' value
='Save Form' />
717 <?php
if (!$formid) { ?
>
719 <input type
='submit' name
='bn_save_template' value
='Save as Template:' />
721 <input type
='text' name
='form_new_template_name' value
='<?php echo $template_name ?>' />
723 <input type
='submit' name
='bn_delete_template' value
='Delete Template' />
726 <input type
='button' value
='Cancel' onclick
="doCancel()" />
731 <script language
='JavaScript'>
732 Calendar
.setup({inputField
:"form_start_date", ifFormat
:"%Y-%m-%d", button
:"img_start_date"});
735 echo " alert('$alertmsg');\n";