2 // Copyright (C) 2006-2007 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("$srcdir/api.inc");
10 include_once("$srcdir/forms.inc");
11 include_once("$include_root/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 if (!get_magic_quotes_gpc()) $fldval = addslashes($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 if (get_magic_quotes_gpc()) $fldval = stripslashes($fldval);
41 // encode a plain string for html display.
42 function real2form($fldval) {
43 return htmlspecialchars($fldval, ENT_QUOTES
);
46 // Putting an error message in here will result in a javascript alert.
49 // If we are invoked as a popup (not in an encounter):
50 $popup = $_GET['popup'];
52 // The form ID is passed to us when an existing encounter form is loaded.
53 $formid = $_GET['id'];
55 // $tempid is the currently selected template, if any.
56 $tempid = $_POST['form_template'] +
0;
58 // This is the start date to be saved with the spreadsheet.
61 $form_completed = '0';
63 if (!$popup && !$encounter) { // $encounter comes from globals.php
64 die("Internal error: we do not seem to be in an encounter!");
67 // Get the name of the template selected by the dropdown, if any;
68 // or if we are loading a form then it comes from that.
71 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
72 "id = $tempid AND rownbr = -1 AND colnbr = -1");
73 $template_name = $trow['value'];
76 $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " .
77 "id = $formid AND rownbr = -1 AND colnbr = -1");
78 list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3);
81 if (!$start_date) $start_date = form2real($_POST['form_start_date']);
83 // Used rows and columns are those beyond which there are only unused cells.
87 // If we are saving...
89 if ($_POST['bn_save_form'] ||
$_POST['bn_save_template']) {
91 // The form data determines how many rows and columns are now used.
92 $cells = $_POST['cell'];
93 for ($i = 0; $i < count($cells); ++
$i) {
95 for ($j = 0; $j < count($row); ++
$j) {
96 if (substr($row[$j], 0, 1)) {
97 if ($i >= $num_used_rows) $num_used_rows = $i +
1;
98 if ($j >= $num_used_cols) $num_used_cols = $j +
1;
103 if ($_POST['bn_save_form']) {
104 $form_completed = $_POST['form_completed'] ?
'1' : '0';
106 // If updating an existing form...
108 sqlStatement("UPDATE form_$spreadsheet_form_name SET " .
109 "value = '$form_completed|$start_date|$template_name' " .
110 "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1");
111 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
112 "id = '$formid' AND rownbr >= 0 AND colnbr >= 0");
114 // If adding a new form...
116 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE");
117 $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_$spreadsheet_form_name");
118 $formid = $tmprow['maxid'] +
1;
119 if ($formid <= 0) $formid = 1;
120 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
121 "id, rownbr, colnbr, datatype, value " .
123 "$formid, -1, -1, 0, " .
124 "'$form_completed|$start_date|$template_name' " .
126 sqlStatement("UNLOCK TABLES");
127 addForm($encounter, "Injury Log", $formid, "$spreadsheet_form_name",
128 $pid, $userauthorized);
132 else { // saving a template
133 // The rule is, we can update the original name, or insert a new name
134 // which must not match any existing template name.
135 $new_template_name = form2real($_POST['form_new_template_name']);
136 if ($new_template_name != $template_name) {
137 $trow = sqlQuery("SELECT id FROM form_$spreadsheet_form_name WHERE " .
138 "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" .
139 real2db($new_template_name) . "'");
141 $alertmsg = "Template \"" . real2form($new_template_name) .
142 "\" already exists!";
145 $tempid = 0; // to force insert of new template
146 $template_name = $new_template_name;
150 // If updating an existing template...
152 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
153 "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0");
155 // If adding a new template...
157 sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE");
158 $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_$spreadsheet_form_name");
159 $tempid = $tmprow['minid'] - 1;
160 if ($tempid >= 0) $tempid = -1;
161 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
162 "id, rownbr, colnbr, datatype, value " .
164 "$tempid, -1, -1, 0, " .
165 "'" . real2db($template_name) . "' " .
167 sqlStatement("UNLOCK TABLES");
174 // Finally, save the table cells.
175 for ($i = 0; $i < $num_used_rows; ++
$i) {
176 for ($j = 0; $j < $num_used_cols; ++
$j) {
177 $tmp = $cells[$i][$j];
178 $celltype = substr($tmp, 0, 1) +
0;
179 $cellvalue = form2db(substr($tmp, 1));
181 sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " .
182 "id, rownbr, colnbr, datatype, value " .
184 "$saveid, $i, $j, $celltype, '$cellvalue' )");
190 else if ($_POST['bn_delete_template'] && $tempid) {
191 sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " .
197 if ($_POST['bn_save_form'] && !$alertmsg && !$popup) {
198 formHeader("Redirecting....");
204 // If we get here then we are displaying a spreadsheet, either a template or
205 // an encounter form.
207 // Get the array of template names.
208 $tres = sqlStatement("SELECT id, value FROM form_$spreadsheet_form_name WHERE " .
209 "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value");
213 # If we are reloading a form, get it.
215 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
216 "id = '$formid' ORDER BY rownbr, colnbr");
217 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
218 "FROM form_$spreadsheet_form_name WHERE id = '$formid'");
219 $num_used_rows = $tmprow['rowmax'] +
1;
220 $num_used_cols = $tmprow['colmax'] +
1;
222 # Otherwise if we are editing a template, get it.
224 $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " .
225 "id = '$tempid' ORDER BY rownbr, colnbr");
226 $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " .
227 "FROM form_$spreadsheet_form_name WHERE id = '$tempid'");
228 $num_used_rows = $tmprow['rowmax'] +
1;
229 $num_used_cols = $tmprow['colmax'] +
1;
232 // Virtual rows and columns are those available when in Edit Structure mode,
233 // and include some additional ones beyond those used. This allows quite a
234 // lot of stuff to be entered before having to save the template.
235 $num_virtual_rows = $num_used_rows ?
$num_used_rows +
5 : 10;
236 $num_virtual_cols = $num_used_cols ?
$num_used_cols +
5 : 10;
240 <link rel
=stylesheet href
="<?echo $css_header;?>" type
="text/css">
241 <style type
="text/css">@import
url(../../../library
/dynarch_calendar
.css
);</style
>
244 font
-family
: sans
-serif
;
249 font
-family
: sans
-serif
;
252 background
-color
: transparent
;
255 font
-family
: sans
-serif
;
258 background
-color
: transparent
;
261 font
-family
: sans
-serif
;
264 background
-color
: transparent
;
272 <script type
="text/javascript" src
="../../../library/textformat.js"></script
>
273 <script type
="text/javascript" src
="../../../library/dynarch_calendar.js"></script
>
274 <script type
="text/javascript" src
="../../../library/dynarch_calendar_en.js"></script
>
275 <script type
="text/javascript" src
="../../../library/dynarch_calendar_setup.js"></script
>
277 <script language
="JavaScript">
278 var mypcc
= '<?php echo $GLOBALS['phone_country_code
']; ?>';
279 var ssChanged
= false; // if they have changed anything in the spreadsheet
280 var startDate
= '<?php echo $start_date ? $start_date : date('Y
-m
-d
'); ?>';
282 // Helper function to set the contents of a block.
283 function setBlockContent(id
, content
) {
284 if (document
.getElementById
) {
285 var x
= document
.getElementById(id
);
287 x
.innerHTML
= content
;
289 else if (document
.all
) {
290 var x
= document
.all
[id
];
291 x
.innerHTML
= content
;
293 // alert("ID = \"" + id + "\", string = \"" + content + "\"");
296 // Called when a different template name is selected.
297 function newTemplate(sel
) {
298 if (ssChanged
&& !confirm('You have made changes that will be discarded ' +
299 'if you select a new template. Do you really want to do this?'))
301 // Restore the original template selection.
302 for (var i
= 0; i
< sel
.options
.length
; ++i
) {
303 if (sel
.options
[i
].value
== '<?php echo $tempid ?>') {
304 sel
.options
[i
].selected
= true;
309 document
.forms
[0].submit();
312 // Called when the Cancel button is clicked.
313 function doCancel() {
314 if (!ssChanged ||
confirm('You have made changes that will be discarded ' +
315 'if you close now. Click OK if you really want to exit this form.'))
317 <?php
if ($popup) { ?
>
320 location
='<?php echo $GLOBALS['form_exit_url
'] ?>';
325 // Called when the Edit Structure checkbox is clicked.
326 function editChanged() {
327 var f
= document
.forms
[0];
328 var newdisplay
= f
.form_edit_template
.checked ?
'' : 'none';
331 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
332 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
333 if (f
['cell['+i+
']['+j+
']'].value
.charAt(0) != '0') {
334 if (i
>= usedrows
) usedrows
= i +
1;
335 if (j
>= usedcols
) usedcols
= j +
1;
339 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
340 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
341 // document.getElementById('div_'+i+'_'+j).style.display = newdisplay;
342 document
.getElementById('sel_'+i+
'_'+j
).style
.display
= newdisplay
;
343 if (i
>= usedrows || j
>= usedcols
) {
344 document
.getElementById('td_'+i+
'_'+j
).style
.display
= newdisplay
;
350 // Prepare a string for use as an HTML value attribute in single quotes.
351 function escQuotes(s
) {
352 return s
.replace(/'/g, "'");
355 // Parse static text to evaluate possible functions.
356 function genStatic(s) {
360 while ((i = s.indexOf('%day
(')) >= 0) {
361 var s1 = s.substring(0, i);
363 var j = s.indexOf(')', i);
365 var dayinc = parseInt(s.substring(i,j));
366 var mydate = new Date(parseInt(startDate.substring(0,4)),
367 parseInt(startDate.substring(5,7))-1, parseInt(startDate.substring(8)));
368 mydate.setTime(1000 * 60 * 60 * 24 * dayinc + mydate.getTime());
369 var year = mydate.getYear(); if (year < 1900) year += 1900;
370 s = s1 + year + '-' +
371 ('' + (mydate.getMonth() + 101)).substring(1) + '-' +
372 ('' + (mydate.getDate() + 100)).substring(1) +
376 // Parse "%sel(first,second,third,...,default)".
377 while ((i = s.indexOf('%sel
(')) >= 0) {
378 var s1 = s.substring(0, i);
380 var j = s.indexOf(')', i);
382 var x = s.substring(0,j);
383 var k = x.lastIndexOf(',');
385 var dflt = s.substring(k+1, j);
386 x = "<select class='selgen
' onchange='newsel(this
)'>";
387 while ((k = s.indexOf(',', i)) > i) {
389 var elem = s.substring(i,k);
390 x += "<option value='" + elem + "'";
391 if (elem == dflt) x += " selected";
392 x += ">" + elem + "</option>";
396 s = s1 + x + s.substring(j + 1);
397 break; // only one %sel allowed
400 // Parse "%ptp(default)".
401 while ((i = s.indexOf('%ptp
(')) >= 0) {
402 var s1 = s.substring(0, i);
404 var j = s.indexOf(')', i);
406 var dflt = s.substring(i, j);
407 x = "<select class='selgen
' onchange='newptp(this
)'>";
408 x += "<option value=''>-- Select --</option>";
410 foreach ($bcodes['PTCJ
']['Physiotherapy Procedures
'] as $key => $value) {
411 echo " x += \"<option value='$key'\";\n";
412 echo " if (dflt == '$key') x += ' selected
';\n";
413 echo " x += '>$value</option
>';\n";
417 s = s1 + x + s.substring(j + 1);
418 break; // only one %ptp allowed
424 // Called when a cell type selector in the spreadsheet is clicked.
425 function newType(i,j) {
427 var f = document.forms[0];
428 var typeval = f['cell
['+i+']['+j+']'].value;
429 var thevalue = typeval.substring(1);
430 var thetype = document.getElementById('sel_
'+i+'_
'+j).value;
431 var s = "<input type='hidden
' name='cell
[" + i + "][" + j + "]' " +
432 "value='" + thetype + escQuotes(thevalue) + "' />";
434 if (thetype == '1') {
435 s += genStatic(thevalue);
437 else if (thetype == '2') {
438 s += "<input type='checkbox
' value='1' onclick='cbClick(this
," + i + "," + j + ")'";
439 if (thevalue) s += " checked";
442 else if (thetype == '3') {
443 s += "<input type='text
' onchange='textChange(this
," + i + "," + j + ")'" +
444 " class='intext
' value='" + escQuotes(thevalue) + "' size='12' />";
446 else if (thetype == '4') {
447 s += "<textarea rows='3' cols='25' wrap='virtual
' class='intext
' " +
448 "onchange='longChange(this
," + i + "," + j + ")'>" +
449 escQuotes(thevalue) + "</textarea>";
451 setBlockContent('vis_
' + i + '_
' + j, s);
454 // Called when a checkbox in the spreadsheet is clicked.
455 function cbClick(elem,i,j) {
457 var f = document.forms[0];
458 var cell = f['cell
['+i+']['+j+']'];
459 cell.value = '2' + (elem.checked ? '1' : '');
462 // Called when a text value in the spreadsheet is changed.
463 function textChange(elem,i,j) {
465 var f = document.forms[0];
466 var cell = f['cell
['+i+']['+j+']'];
467 cell.value = '3' + elem.value;
470 // Called when a textarea value in the spreadsheet is changed.
471 function longChange(elem,i,j) {
473 var f = document.forms[0];
474 var cell = f['cell
['+i+']['+j+']'];
475 cell.value = '4' + elem.value;
478 // Helper function to get the value element of a table cell given any
479 // other element within that cell.
480 function getHidden(sel) {
481 var p = sel.parentNode;
482 while (p.tagName != 'TD
') {
483 if (!p.parentNode || p.parentNode == p) {
484 alert("JavaScript error, cannot find TD element");
489 // Get the <input type=hidden> element within this table cell.
490 var f = document.forms[0];
491 var s = p.id.substring(3);
492 var uix = s.indexOf('_
');
493 var i = s.substring(0, uix);
494 var j = s.substring(uix+1);
495 return f['cell
[' + i + '][' + j + ']'];
498 // Called when a user-defined select list has a new selection.
499 // This rewrites the function definition for the select list.
500 function newsel(sel) {
501 var inelem = getHidden(sel);
502 var s = inelem.value;
503 var i = s.indexOf('%sel
(');
504 var j = s.indexOf(')', i);
505 var x = s.substring(0, j);
506 var k = x.lastIndexOf(',');
507 inelem.value = s.substring(0, k+1) + sel.value + s.substring(j);
510 // Called when a physiotherapy select list has a new selection.
511 // This rewrites the function definition for the select list.
512 function newptp(sel) {
513 var inelem = getHidden(sel);
514 var s = inelem.value;
515 var i = s.indexOf('%ptp
(') + 5;
516 var j = s.indexOf(')', i);
517 inelem.value = s.substring(0, i) + sel.value + s.substring(j);
524 <body <?echo $top_bg_line;?> topmargin="0" rightmargin="0" leftmargin="0"
525 bottommargin="0" marginwidth="0" marginheight="0">
526 <form method="post" action="<?php echo "$rootdir/forms/$spreadsheet_form_name/new.php?id=$formid"; if ($popup) echo '&popup
=1'; ?>">
529 <table border='0' cellpadding='5' cellspacing='0' style='margin
:8pt
'>
530 <tr bgcolor='#ddddff'>
532 <?php
xl('Start Date','e'); ?
>:
533 <input type
='text' name
='form_start_date' id
='form_start_date'
534 size
='10' value
='<?php echo $start_date; ?>'
535 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'
536 <?php
if ($formid && $start_date) echo 'disabled '; ?
>/>
537 <?php
if (!$formid ||
!$start_date) { ?
>
538 <img src
='../../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
539 id
='img_start_date' border
='0' alt
='[?]' style
='cursor:pointer'
540 title
='Click here to choose a date'>
543 <?php
xl('Template:','e') ?
>
544 <select name
='form_template' onchange
='newTemplate(this)'<?php
if ($formid) echo ' disabled'; ?
>>
545 <option value
='0'>-- Select
--</option
>
547 while ($trow = sqlFetchArray($tres)) {
548 echo " <option value='" . $trow['id'] . "'";
549 if ($tempid && $tempid == $trow['id'] ||
550 $formid && $template_name == $trow['value'])
554 echo ">" . $trow['value'] . "</option>\n";
559 <input type
='checkbox' name
='form_edit_template'
560 onclick
='editChanged()'
561 title
='<?php xl("If you want to change data types, or add rows or columns","e") ?>' />
562 <?php
xl('Edit Structure','e') ?
>
563 <?php
if ($formid) { ?
>
565 <input type
='checkbox' name
='form_completed'
566 title
='<?php xl("If all data for all columns are complete for this form","e") ?>'
567 <?php
if ($form_completed) echo 'checked '; ?
>/>
568 <?php
xl('Completed','e') ?
>
574 <table border
='1' cellpadding
='2' cellspacing
='0' class='sstable'>
576 if ($dres) $drow = sqlFetchArray($dres);
577 $typeprompts = array('unused','static','checkbox','text');
579 for ($i = 0; $i < $num_virtual_rows; ++
$i) {
581 for ($j = 0; $j < $num_virtual_cols; ++
$j) {
583 // Match up with the database for cell type and value.
587 while ($drow && $drow['rownbr'] < $i)
588 $drow = sqlFetchArray($dres);
589 while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j)
590 $drow = sqlFetchArray($dres);
591 if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) {
592 $celltype = $drow['datatype'];
593 $cellvalue = real2form($drow['value']);
594 $cellstatic = addslashes($drow['value']);
598 echo " <td id='td_${i}_${j}' valign='top'";
599 if ($i >= $num_used_rows ||
$j >= $num_used_cols)
600 echo " style='display:none'";
603 /*****************************************************************
604 echo "<span id='div_${i}_${j}' ";
605 echo "style='float:right;cursor:pointer;display:none' ";
606 echo "onclick='newType($i,$j)'>[";
607 echo $typeprompts[$celltype];
609 *****************************************************************/
610 echo "<div class='seldiv'>";
611 echo "<select id='sel_${i}_${j}' class='seltype' style='display:none' " .
612 "onchange='newType($i,$j)'>";
613 foreach ($celltypes as $key => $value) {
614 echo "<option value='$key'";
615 if ($key == $celltype) echo " selected";
616 echo ">$value</option>";
620 /****************************************************************/
622 echo "<span id='vis_${i}_${j}'>"; // new //
624 echo "<input type='hidden' name='cell[$i][$j]' value='$celltype$cellvalue' />";
625 if ($celltype == '1') {
626 // So we don't have to write a PHP version of genStatic():
627 echo "<script language='JavaScript'>document.write(genStatic('$cellstatic'));</script>";
629 else if ($celltype == '2') {
630 echo "<input type='checkbox' value='1' onclick='cbClick(this,$i,$j)'";
631 if ($cellvalue) echo " checked";
634 else if ($celltype == '3') {
635 echo "<input type='text' class='intext' onchange='textChange(this,$i,$j)'";
636 echo " value='$cellvalue'";
637 echo " size='12' />";
639 else if ($celltype == '4') {
640 echo "<textarea rows='3' cols='25' wrap='virtual' class='intext' " .
641 "onchange='longChange(this,$i,$j)'>";
646 echo "</span>"; // new //
656 <input type
='submit' name
='bn_save_form' value
='Save Form' />
657 <?php
if (!$formid) { ?
>
659 <input type
='submit' name
='bn_save_template' value
='Save as Template:' />
661 <input type
='text' name
='form_new_template_name' value
='<?php echo $template_name ?>' />
663 <input type
='submit' name
='bn_delete_template' value
='Delete Template' />
666 <input type
='button' value
='Cancel' onclick
="doCancel()" />
671 <script language
='JavaScript'>
672 Calendar
.setup({inputField
:"form_start_date", ifFormat
:"%Y-%m-%d", button
:"img_start_date"});
674 if ($alertmsg) echo " alert('$alertmsg');\n";