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) {
24 $fldval = trim($fldval);
25 $fldval = strip_escape_custom($fldval);
29 // encode a plain string for database writing.
30 function real2db($fldval) {
31 return addslashes($fldval);
34 // Get the actual string from a form field.
35 function form2real($fldval) {
36 $fldval = trim($fldval);
37 $fldval = strip_escape_custom($fldval);
41 // encode a plain string for html display.
42 function real2form($fldval) {
43 return htmlspecialchars($fldval, ENT_QUOTES
);
46 if (empty($spreadsheet_title)) $spreadsheet_title = 'Injury Log';
48 // Putting an error message in here will result in a javascript alert.
51 // Determine the encounter that we are working with.
52 $thisenc = empty($_GET['thisenc']) ?
$encounter : $_GET['thisenc'] +
0;
54 // If we are invoked as a popup (not in an encounter):
55 $popup = $_GET['popup'];
57 // The form ID is passed to us when an existing encounter form is loaded.
58 $formid = $_GET['id'];
60 // $tempid is the currently selected template, if any.
61 $tempid = $_POST['form_template'] +
0;
63 // This is the start date to be saved with the spreadsheet.
66 $form_completed = '0';
68 if (!$popup && !$encounter) { // $encounter comes from globals.php
69 die("Internal error: we do not seem to be in an encounter!");
72 // Get the name of the template selected by the dropdown, if any;
73 // or if we are loading a form then it comes from that.
76 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
77 "id = $tempid AND rownbr = -1 AND colnbr = -1");
78 $template_name = $trow['value'];
81 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
82 "id = $formid AND rownbr = -1 AND colnbr = -1");
83 list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3);
86 if (!$start_date) $start_date = form2real($_POST['form_start_date']);
88 // Used rows and columns are those beyond which there are only unused cells.
92 // If we are saving...
94 if ($_POST['bn_save_form'] ||
$_POST['bn_save_template']) {
96 // The form data determines how many rows and columns are now used.
97 $cells = $_POST['cell'];
98 for ($i = 0; $i < count($cells); ++
$i) {
100 for ($j = 0; $j < count($row); ++
$j) {
101 if (substr($row[$j], 0, 1)) {
102 if ($i >= $num_used_rows) $num_used_rows = $i +
1;
103 if ($j >= $num_used_cols) $num_used_cols = $j +
1;
108 if ($_POST['bn_save_form']) {
109 $form_completed = $_POST['form_completed'] ?
'1' : '0';
111 // If updating an existing form...
113 sqlStatement("UPDATE form_$spreadsheet_form_name SET " .
114 "value = '$form_completed|$start_date|$template_name' " .
115 "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1");
116 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
117 "id = '$formid' AND rownbr >= 0 AND colnbr >= 0");
119 // If adding a new form...
121 $tmprow = sqlQuery("SELECT pid FROM form_encounter WHERE encounter = ? ORDER BY id DESC LIMIT 1",
123 $thispid = $tmprow['pid'];
124 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
125 $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_$spreadsheet_form_name");
126 $formid = $tmprow['maxid'] +
1;
127 if ($formid <= 0) $formid = 1;
128 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
129 "id, rownbr, colnbr, datatype, value " .
131 "$formid, -1, -1, 0, " .
132 "'$form_completed|$start_date|$template_name' " .
134 sqlStatement("UNLOCK TABLES");
135 addForm($thisenc, $spreadsheet_title, $formid, "$spreadsheet_form_name",
136 $thispid, $userauthorized);
140 else { // saving a template
141 // The rule is, we can update the original name, or insert a new name
142 // which must not match any existing template name.
143 $new_template_name = form2real($_POST['form_new_template_name']);
144 if ($new_template_name != $template_name) {
145 $trow = sqlQuery("SELECT id FROM form_$spreadsheet_form_name WHERE " .
146 "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" .
147 real2db($new_template_name) . "'");
149 $alertmsg = "Template \"" . real2form($new_template_name) .
150 "\" already exists!";
153 $tempid = 0; // to force insert of new template
154 $template_name = $new_template_name;
158 // If updating an existing template...
160 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
161 "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0");
163 // If adding a new template...
165 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE");
166 $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_$spreadsheet_form_name");
167 $tempid = $tmprow['minid'] - 1;
168 if ($tempid >= 0) $tempid = -1;
169 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
170 "id, rownbr, colnbr, datatype, value " .
172 "$tempid, -1, -1, 0, " .
173 "'" . real2db($template_name) . "' " .
175 sqlStatement("UNLOCK TABLES");
182 // Finally, save the table cells.
183 for ($i = 0; $i < $num_used_rows; ++
$i) {
184 for ($j = 0; $j < $num_used_cols; ++
$j) {
185 $tmp = $cells[$i][$j];
186 $celltype = substr($tmp, 0, 1) +
0;
187 $cellvalue = form2db(substr($tmp, 1));
189 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
190 "id, rownbr, colnbr, datatype, value " .
192 "$saveid, $i, $j, $celltype, '$cellvalue' )");
198 else if ($_POST['bn_delete_template'] && $tempid) {
199 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
205 if ($_POST['bn_save_form'] && !$alertmsg && !$popup) {
206 formHeader("Redirecting....");
212 // If we get here then we are displaying a spreadsheet, either a template or
213 // an encounter form.
215 // Get the array of template names.
216 $tres = sqlStatement("SELECT id, value FROM form_$spreadsheet_form_name WHERE " .
217 "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value");
221 # If we are reloading a form, get it.
223 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
224 "id = '$formid' ORDER BY rownbr, colnbr");
225 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
226 "FROM form_$spreadsheet_form_name WHERE id = '$formid'");
227 $num_used_rows = $tmprow['rowmax'] +
1;
228 $num_used_cols = $tmprow['colmax'] +
1;
230 # Otherwise if we are editing a template, get it.
232 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
233 "id = '$tempid' ORDER BY rownbr, colnbr");
234 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
235 "FROM form_$spreadsheet_form_name WHERE id = '$tempid'");
236 $num_used_rows = $tmprow['rowmax'] +
1;
237 $num_used_cols = $tmprow['colmax'] +
1;
240 // Virtual rows and columns are those available when in Edit Structure mode,
241 // and include some additional ones beyond those used. This allows quite a
242 // lot of stuff to be entered before having to save the template.
243 $num_virtual_rows = $num_used_rows ?
$num_used_rows +
5 : 10;
244 $num_virtual_cols = $num_used_cols ?
$num_used_cols +
5 : 10;
248 <?php
html_header_show();?
>
249 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
250 <style type
="text/css">@import
url(../../../library
/dynarch_calendar
.css
);</style
>
253 font
-family
: sans
-serif
;
258 font
-family
: sans
-serif
;
261 background
-color
: transparent
;
264 font
-family
: sans
-serif
;
267 background
-color
: transparent
;
270 font
-family
: sans
-serif
;
273 background
-color
: transparent
;
281 <script type
="text/javascript" src
="../../../library/textformat.js"></script
>
282 <script type
="text/javascript" src
="../../../library/dynarch_calendar.js"></script
>
283 <script type
="text/javascript" src
="../../../library/dynarch_calendar_en.js"></script
>
284 <script type
="text/javascript" src
="../../../library/dynarch_calendar_setup.js"></script
>
286 <script language
="JavaScript">
287 var mypcc
= '<?php echo $GLOBALS['phone_country_code
']; ?>';
288 var ssChanged
= false; // if they have changed anything in the spreadsheet
289 var startDate
= '<?php echo $start_date ? $start_date : date('Y
-m
-d
'); ?>';
291 // In case we are a popup (top level) window, handle top.restoreSession() calls.
292 function restoreSession() {
293 return opener
.top
.restoreSession();
296 // Helper function to set the contents of a block.
297 function setBlockContent(id
, content
) {
298 if (document
.getElementById
) {
299 var x
= document
.getElementById(id
);
301 x
.innerHTML
= content
;
303 else if (document
.all
) {
304 var x
= document
.all
[id
];
305 x
.innerHTML
= content
;
307 // alert("ID = \"" + id + "\", string = \"" + content + "\"");
310 // Called when a different template name is selected.
311 function newTemplate(sel
) {
312 if (ssChanged
&& !confirm('You have made changes that will be discarded ' +
313 'if you select a new template. Do you really want to do this?'))
315 // Restore the original template selection.
316 for (var i
= 0; i
< sel
.options
.length
; ++i
) {
317 if (sel
.options
[i
].value
== '<?php echo $tempid ?>') {
318 sel
.options
[i
].selected
= true;
323 top
.restoreSession();
324 document
.forms
[0].submit();
327 // Called when the Cancel button is clicked.
328 function doCancel() {
329 if (!ssChanged ||
confirm('You have made changes that will be discarded ' +
330 'if you close now. Click OK if you really want to exit this form.'))
332 <?php
if ($popup) { ?
>
335 top
.restoreSession();
336 location
='<?php echo $GLOBALS['form_exit_url
'] ?>';
341 // Called when the Edit Structure checkbox is clicked.
342 function editChanged() {
343 var f
= document
.forms
[0];
344 var newdisplay
= f
.form_edit_template
.checked ?
'' : 'none';
347 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
348 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
349 if (f
['cell['+i+
']['+j+
']'].value
.charAt(0) != '0') {
350 if (i
>= usedrows
) usedrows
= i +
1;
351 if (j
>= usedcols
) usedcols
= j +
1;
355 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
356 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
357 // document.getElementById('div_'+i+'_'+j).style.display = newdisplay;
358 document
.getElementById('sel_'+i+
'_'+j
).style
.display
= newdisplay
;
359 if (i
>= usedrows || j
>= usedcols
) {
360 document
.getElementById('td_'+i+
'_'+j
).style
.display
= newdisplay
;
366 // Prepare a string for use as an HTML value attribute in single quotes.
367 function escQuotes(s
) {
368 return s
.replace(/'/g, "'");
371 // Parse static text to evaluate possible functions.
372 function genStatic(s) {
376 while ((i = s.indexOf('%day
(')) >= 0) {
377 var s1 = s.substring(0, i);
379 var j = s.indexOf(')', i);
381 var dayinc = parseInt(s.substring(i,j));
382 var mydate = new Date(parseInt(startDate.substring(0,4)),
383 parseInt(startDate.substring(5,7))-1, parseInt(startDate.substring(8)));
384 mydate.setTime(1000 * 60 * 60 * 24 * dayinc + mydate.getTime());
385 var year = mydate.getYear(); if (year < 1900) year += 1900;
386 s = s1 + year + '-' +
387 ('' + (mydate.getMonth() + 101)).substring(1) + '-' +
388 ('' + (mydate.getDate() + 100)).substring(1) +
392 // Parse "%sel(first,second,third,...,default)".
393 while ((i = s.indexOf('%sel
(')) >= 0) {
394 var s1 = s.substring(0, i);
396 var j = s.indexOf(')', i);
398 var x = s.substring(0,j);
399 var k = x.lastIndexOf(',');
401 var dflt = s.substring(k+1, j);
402 x = "<select class='selgen
' onchange='newsel(this
)'>";
403 while ((k = s.indexOf(',', i)) > i) {
405 var elem = s.substring(i,k);
406 x += "<option value='" + elem + "'";
407 if (elem == dflt) x += " selected";
408 x += ">" + elem + "</option>";
412 s = s1 + x + s.substring(j + 1);
413 break; // only one %sel allowed
416 // Parse "%ptp(default)".
417 while ((i = s.indexOf('%ptp
(')) >= 0) {
418 var s1 = s.substring(0, i);
420 var j = s.indexOf(')', i);
422 var dflt = s.substring(i, j);
423 x = "<select class='selgen
' onchange='newptp(this
)'>";
424 x += "<option value=''>-- Select --</option>";
426 foreach ($bcodes['Phys
']['Physiotherapy Procedures
'] as $key => $value) {
427 echo " x += \"<option value='$key'\";\n";
428 echo " if (dflt == '$key') x += ' selected
';\n";
429 echo " x += '>$value</option
>';\n";
433 s = s1 + x + s.substring(j + 1);
434 break; // only one %ptp allowed
440 // Called when a cell type selector in the spreadsheet is clicked.
441 function newType(i,j) {
443 var f = document.forms[0];
444 var typeval = f['cell
['+i+']['+j+']'].value;
445 var thevalue = typeval.substring(1);
446 var thetype = document.getElementById('sel_
'+i+'_
'+j).value;
447 var s = "<input type='hidden
' name='cell
[" + i + "][" + j + "]' " +
448 "value='" + thetype + escQuotes(thevalue) + "' />";
450 if (thetype == '1') {
451 s += genStatic(thevalue);
453 else if (thetype == '2') {
454 s += "<input type='checkbox
' value='1' onclick='cbClick(this
," + i + "," + j + ")'";
455 if (thevalue) s += " checked";
458 else if (thetype == '3') {
459 s += "<input type='text
' onchange='textChange(this
," + i + "," + j + ")'" +
460 " class='intext
' value='" + escQuotes(thevalue) + "' size='12' />";
462 else if (thetype == '4') {
463 s += "<textarea rows='3' cols='25' wrap='virtual
' class='intext
' " +
464 "onchange='longChange(this
," + i + "," + j + ")'>" +
465 escQuotes(thevalue) + "</textarea>";
467 setBlockContent('vis_
' + i + '_
' + j, s);
470 // Called when a checkbox in the spreadsheet is clicked.
471 function cbClick(elem,i,j) {
473 var f = document.forms[0];
474 var cell = f['cell
['+i+']['+j+']'];
475 cell.value = '2' + (elem.checked ? '1' : '');
478 // Called when a text value in the spreadsheet is changed.
479 function textChange(elem,i,j) {
481 var f = document.forms[0];
482 var cell = f['cell
['+i+']['+j+']'];
483 cell.value = '3' + elem.value;
486 // Called when a textarea value in the spreadsheet is changed.
487 function longChange(elem,i,j) {
489 var f = document.forms[0];
490 var cell = f['cell
['+i+']['+j+']'];
491 cell.value = '4' + elem.value;
494 // Helper function to get the value element of a table cell given any
495 // other element within that cell.
496 function getHidden(sel) {
497 var p = sel.parentNode;
498 while (p.tagName != 'TD
') {
499 if (!p.parentNode || p.parentNode == p) {
500 alert("JavaScript error, cannot find TD element");
505 // Get the <input type=hidden> element within this table cell.
506 var f = document.forms[0];
507 var s = p.id.substring(3);
508 var uix = s.indexOf('_
');
509 var i = s.substring(0, uix);
510 var j = s.substring(uix+1);
511 return f['cell
[' + i + '][' + j + ']'];
514 // Called when a user-defined select list has a new selection.
515 // This rewrites the function definition for the select list.
516 function newsel(sel) {
517 var inelem = getHidden(sel);
518 var s = inelem.value;
519 var i = s.indexOf('%sel
(');
520 var j = s.indexOf(')', i);
521 var x = s.substring(0, j);
522 var k = x.lastIndexOf(',');
523 inelem.value = s.substring(0, k+1) + sel.value + s.substring(j);
526 // Called when a physiotherapy select list has a new selection.
527 // This rewrites the function definition for the select list.
528 function newptp(sel) {
529 var inelem = getHidden(sel);
530 var s = inelem.value;
531 var i = s.indexOf('%ptp
(') + 5;
532 var j = s.indexOf(')', i);
533 inelem.value = s.substring(0, i) + sel.value + s.substring(j);
540 <body class="body_top">
541 <form method="post" action="<?php echo "$rootdir/forms/$spreadsheet_form_name/new.php?id=$formid&thisenc=$thisenc"; if ($popup) echo '&popup
=1'; ?>"
542 onsubmit="return top.restoreSession()">
545 <table border='0' cellpadding='5' cellspacing='0' style='margin
:8pt
'>
546 <tr bgcolor='#ddddff'>
548 <?php
xl('Start Date','e'); ?
>:
549 <input type
='text' name
='form_start_date' id
='form_start_date'
550 size
='10' value
='<?php echo $start_date; ?>'
551 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'
552 <?php
if ($formid && $start_date) echo 'disabled '; ?
>/>
553 <?php
if (!$formid ||
!$start_date) { ?
>
554 <img src
='../../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
555 id
='img_start_date' border
='0' alt
='[?]' style
='cursor:pointer'
556 title
='Click here to choose a date'>
559 <?php
xl('Template:','e') ?
>
560 <select name
='form_template' onchange
='newTemplate(this)'<?php
if ($formid) echo ' disabled'; ?
>>
561 <option value
='0'>-- Select
--</option
>
563 while ($trow = sqlFetchArray($tres)) {
564 echo " <option value='" . $trow['id'] . "'";
565 if ($tempid && $tempid == $trow['id'] ||
566 $formid && $template_name == $trow['value'])
570 echo ">" . $trow['value'] . "</option>\n";
575 <input type
='checkbox' name
='form_edit_template'
576 onclick
='editChanged()'
577 title
='<?php xl("If you want to change data types, or add rows or columns","e") ?>' />
578 <?php
xl('Edit Structure','e') ?
>
579 <?php
if ($formid) { ?
>
581 <input type
='checkbox' name
='form_completed'
582 title
='<?php xl("If all data for all columns are complete for this form","e") ?>'
583 <?php
if ($form_completed) echo 'checked '; ?
>/>
584 <?php
xl('Completed','e') ?
>
590 <table border
='1' cellpadding
='2' cellspacing
='0' class='sstable'>
592 if ($dres) $drow = sqlFetchArray($dres);
593 $typeprompts = array('unused','static','checkbox','text');
595 for ($i = 0; $i < $num_virtual_rows; ++
$i) {
597 for ($j = 0; $j < $num_virtual_cols; ++
$j) {
599 // Match up with the database for cell type and value.
603 while ($drow && $drow['rownbr'] < $i)
604 $drow = sqlFetchArray($dres);
605 while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j)
606 $drow = sqlFetchArray($dres);
607 if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) {
608 $celltype = $drow['datatype'];
609 $cellvalue = real2form($drow['value']);
610 $cellstatic = addslashes($drow['value']);
614 echo " <td id='td_${i}_${j}' valign='top'";
615 if ($i >= $num_used_rows ||
$j >= $num_used_cols)
616 echo " style='display:none'";
619 /*****************************************************************
620 echo "<span id='div_${i}_${j}' ";
621 echo "style='float:right;cursor:pointer;display:none' ";
622 echo "onclick='newType($i,$j)'>[";
623 echo $typeprompts[$celltype];
625 *****************************************************************/
626 echo "<div class='seldiv'>";
627 echo "<select id='sel_${i}_${j}' class='seltype' style='display:none' " .
628 "onchange='newType($i,$j)'>";
629 foreach ($celltypes as $key => $value) {
630 echo "<option value='$key'";
631 if ($key == $celltype) echo " selected";
632 echo ">$value</option>";
636 /****************************************************************/
638 echo "<span id='vis_${i}_${j}'>"; // new //
640 echo "<input type='hidden' name='cell[$i][$j]' value='$celltype$cellvalue' />";
641 if ($celltype == '1') {
642 // So we don't have to write a PHP version of genStatic():
643 echo "<script language='JavaScript'>document.write(genStatic('$cellstatic'));</script>";
645 else if ($celltype == '2') {
646 echo "<input type='checkbox' value='1' onclick='cbClick(this,$i,$j)'";
647 if ($cellvalue) echo " checked";
650 else if ($celltype == '3') {
651 echo "<input type='text' class='intext' onchange='textChange(this,$i,$j)'";
652 echo " value='$cellvalue'";
653 echo " size='12' />";
655 else if ($celltype == '4') {
656 echo "<textarea rows='3' cols='25' wrap='virtual' class='intext' " .
657 "onchange='longChange(this,$i,$j)'>";
662 echo "</span>"; // new //
672 <input type
='submit' name
='bn_save_form' value
='Save Form' />
673 <?php
if (!$formid) { ?
>
675 <input type
='submit' name
='bn_save_template' value
='Save as Template:' />
677 <input type
='text' name
='form_new_template_name' value
='<?php echo $template_name ?>' />
679 <input type
='submit' name
='bn_delete_template' value
='Delete Template' />
682 <input type
='button' value
='Cancel' onclick
="doCancel()" />
687 <script language
='JavaScript'>
688 Calendar
.setup({inputField
:"form_start_date", ifFormat
:"%Y-%m-%d", button
:"img_start_date"});
690 if ($alertmsg) echo " alert('$alertmsg');\n";