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(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 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 <?php
html_header_show();?
>
241 <link rel
="stylesheet" href
="<?php echo $css_header;?>" type
="text/css">
242 <style type
="text/css">@import
url(../../../library
/dynarch_calendar
.css
);</style
>
245 font
-family
: sans
-serif
;
250 font
-family
: sans
-serif
;
253 background
-color
: transparent
;
256 font
-family
: sans
-serif
;
259 background
-color
: transparent
;
262 font
-family
: sans
-serif
;
265 background
-color
: transparent
;
273 <script type
="text/javascript" src
="../../../library/textformat.js"></script
>
274 <script type
="text/javascript" src
="../../../library/dynarch_calendar.js"></script
>
275 <script type
="text/javascript" src
="../../../library/dynarch_calendar_en.js"></script
>
276 <script type
="text/javascript" src
="../../../library/dynarch_calendar_setup.js"></script
>
278 <script language
="JavaScript">
279 var mypcc
= '<?php echo $GLOBALS['phone_country_code
']; ?>';
280 var ssChanged
= false; // if they have changed anything in the spreadsheet
281 var startDate
= '<?php echo $start_date ? $start_date : date('Y
-m
-d
'); ?>';
283 // Helper function to set the contents of a block.
284 function setBlockContent(id
, content
) {
285 if (document
.getElementById
) {
286 var x
= document
.getElementById(id
);
288 x
.innerHTML
= content
;
290 else if (document
.all
) {
291 var x
= document
.all
[id
];
292 x
.innerHTML
= content
;
294 // alert("ID = \"" + id + "\", string = \"" + content + "\"");
297 // Called when a different template name is selected.
298 function newTemplate(sel
) {
299 if (ssChanged
&& !confirm('You have made changes that will be discarded ' +
300 'if you select a new template. Do you really want to do this?'))
302 // Restore the original template selection.
303 for (var i
= 0; i
< sel
.options
.length
; ++i
) {
304 if (sel
.options
[i
].value
== '<?php echo $tempid ?>') {
305 sel
.options
[i
].selected
= true;
310 top
.restoreSession();
311 document
.forms
[0].submit();
314 // Called when the Cancel button is clicked.
315 function doCancel() {
316 if (!ssChanged ||
confirm('You have made changes that will be discarded ' +
317 'if you close now. Click OK if you really want to exit this form.'))
319 <?php
if ($popup) { ?
>
322 top
.restoreSession();
323 location
='<?php echo $GLOBALS['form_exit_url
'] ?>';
328 // Called when the Edit Structure checkbox is clicked.
329 function editChanged() {
330 var f
= document
.forms
[0];
331 var newdisplay
= f
.form_edit_template
.checked ?
'' : 'none';
334 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
335 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
336 if (f
['cell['+i+
']['+j+
']'].value
.charAt(0) != '0') {
337 if (i
>= usedrows
) usedrows
= i +
1;
338 if (j
>= usedcols
) usedcols
= j +
1;
342 for (var i
= 0; i
< <?php
echo $num_virtual_rows; ?
>; ++i
) {
343 for (var j
= 0; j
< <?php
echo $num_virtual_cols; ?
>; ++j
) {
344 // document.getElementById('div_'+i+'_'+j).style.display = newdisplay;
345 document
.getElementById('sel_'+i+
'_'+j
).style
.display
= newdisplay
;
346 if (i
>= usedrows || j
>= usedcols
) {
347 document
.getElementById('td_'+i+
'_'+j
).style
.display
= newdisplay
;
353 // Prepare a string for use as an HTML value attribute in single quotes.
354 function escQuotes(s
) {
355 return s
.replace(/'/g, "'");
358 // Parse static text to evaluate possible functions.
359 function genStatic(s) {
363 while ((i = s.indexOf('%day
(')) >= 0) {
364 var s1 = s.substring(0, i);
366 var j = s.indexOf(')', i);
368 var dayinc = parseInt(s.substring(i,j));
369 var mydate = new Date(parseInt(startDate.substring(0,4)),
370 parseInt(startDate.substring(5,7))-1, parseInt(startDate.substring(8)));
371 mydate.setTime(1000 * 60 * 60 * 24 * dayinc + mydate.getTime());
372 var year = mydate.getYear(); if (year < 1900) year += 1900;
373 s = s1 + year + '-' +
374 ('' + (mydate.getMonth() + 101)).substring(1) + '-' +
375 ('' + (mydate.getDate() + 100)).substring(1) +
379 // Parse "%sel(first,second,third,...,default)".
380 while ((i = s.indexOf('%sel
(')) >= 0) {
381 var s1 = s.substring(0, i);
383 var j = s.indexOf(')', i);
385 var x = s.substring(0,j);
386 var k = x.lastIndexOf(',');
388 var dflt = s.substring(k+1, j);
389 x = "<select class='selgen
' onchange='newsel(this
)'>";
390 while ((k = s.indexOf(',', i)) > i) {
392 var elem = s.substring(i,k);
393 x += "<option value='" + elem + "'";
394 if (elem == dflt) x += " selected";
395 x += ">" + elem + "</option>";
399 s = s1 + x + s.substring(j + 1);
400 break; // only one %sel allowed
403 // Parse "%ptp(default)".
404 while ((i = s.indexOf('%ptp
(')) >= 0) {
405 var s1 = s.substring(0, i);
407 var j = s.indexOf(')', i);
409 var dflt = s.substring(i, j);
410 x = "<select class='selgen
' onchange='newptp(this
)'>";
411 x += "<option value=''>-- Select --</option>";
413 foreach ($bcodes['PTCJ
']['Physiotherapy Procedures
'] as $key => $value) {
414 echo " x += \"<option value='$key'\";\n";
415 echo " if (dflt == '$key') x += ' selected
';\n";
416 echo " x += '>$value</option
>';\n";
420 s = s1 + x + s.substring(j + 1);
421 break; // only one %ptp allowed
427 // Called when a cell type selector in the spreadsheet is clicked.
428 function newType(i,j) {
430 var f = document.forms[0];
431 var typeval = f['cell
['+i+']['+j+']'].value;
432 var thevalue = typeval.substring(1);
433 var thetype = document.getElementById('sel_
'+i+'_
'+j).value;
434 var s = "<input type='hidden
' name='cell
[" + i + "][" + j + "]' " +
435 "value='" + thetype + escQuotes(thevalue) + "' />";
437 if (thetype == '1') {
438 s += genStatic(thevalue);
440 else if (thetype == '2') {
441 s += "<input type='checkbox
' value='1' onclick='cbClick(this
," + i + "," + j + ")'";
442 if (thevalue) s += " checked";
445 else if (thetype == '3') {
446 s += "<input type='text
' onchange='textChange(this
," + i + "," + j + ")'" +
447 " class='intext
' value='" + escQuotes(thevalue) + "' size='12' />";
449 else if (thetype == '4') {
450 s += "<textarea rows='3' cols='25' wrap='virtual
' class='intext
' " +
451 "onchange='longChange(this
," + i + "," + j + ")'>" +
452 escQuotes(thevalue) + "</textarea>";
454 setBlockContent('vis_
' + i + '_
' + j, s);
457 // Called when a checkbox in the spreadsheet is clicked.
458 function cbClick(elem,i,j) {
460 var f = document.forms[0];
461 var cell = f['cell
['+i+']['+j+']'];
462 cell.value = '2' + (elem.checked ? '1' : '');
465 // Called when a text value in the spreadsheet is changed.
466 function textChange(elem,i,j) {
468 var f = document.forms[0];
469 var cell = f['cell
['+i+']['+j+']'];
470 cell.value = '3' + elem.value;
473 // Called when a textarea value in the spreadsheet is changed.
474 function longChange(elem,i,j) {
476 var f = document.forms[0];
477 var cell = f['cell
['+i+']['+j+']'];
478 cell.value = '4' + elem.value;
481 // Helper function to get the value element of a table cell given any
482 // other element within that cell.
483 function getHidden(sel) {
484 var p = sel.parentNode;
485 while (p.tagName != 'TD
') {
486 if (!p.parentNode || p.parentNode == p) {
487 alert("JavaScript error, cannot find TD element");
492 // Get the <input type=hidden> element within this table cell.
493 var f = document.forms[0];
494 var s = p.id.substring(3);
495 var uix = s.indexOf('_
');
496 var i = s.substring(0, uix);
497 var j = s.substring(uix+1);
498 return f['cell
[' + i + '][' + j + ']'];
501 // Called when a user-defined select list has a new selection.
502 // This rewrites the function definition for the select list.
503 function newsel(sel) {
504 var inelem = getHidden(sel);
505 var s = inelem.value;
506 var i = s.indexOf('%sel
(');
507 var j = s.indexOf(')', i);
508 var x = s.substring(0, j);
509 var k = x.lastIndexOf(',');
510 inelem.value = s.substring(0, k+1) + sel.value + s.substring(j);
513 // Called when a physiotherapy select list has a new selection.
514 // This rewrites the function definition for the select list.
515 function newptp(sel) {
516 var inelem = getHidden(sel);
517 var s = inelem.value;
518 var i = s.indexOf('%ptp
(') + 5;
519 var j = s.indexOf(')', i);
520 inelem.value = s.substring(0, i) + sel.value + s.substring(j);
527 <body class="body_top">
528 <form method="post" action="<?php echo "$rootdir/forms/$spreadsheet_form_name/new.php?id=$formid"; if ($popup) echo '&popup
=1'; ?>"
529 onsubmit="return top.restoreSession()">
532 <table border='0' cellpadding='5' cellspacing='0' style='margin
:8pt
'>
533 <tr bgcolor='#ddddff'>
535 <?php
xl('Start Date','e'); ?
>:
536 <input type
='text' name
='form_start_date' id
='form_start_date'
537 size
='10' value
='<?php echo $start_date; ?>'
538 onkeyup
='datekeyup(this,mypcc)' onblur
='dateblur(this,mypcc)' title
='yyyy-mm-dd'
539 <?php
if ($formid && $start_date) echo 'disabled '; ?
>/>
540 <?php
if (!$formid ||
!$start_date) { ?
>
541 <img src
='../../pic/show_calendar.gif' align
='absbottom' width
='24' height
='22'
542 id
='img_start_date' border
='0' alt
='[?]' style
='cursor:pointer'
543 title
='Click here to choose a date'>
546 <?php
xl('Template:','e') ?
>
547 <select name
='form_template' onchange
='newTemplate(this)'<?php
if ($formid) echo ' disabled'; ?
>>
548 <option value
='0'>-- Select
--</option
>
550 while ($trow = sqlFetchArray($tres)) {
551 echo " <option value='" . $trow['id'] . "'";
552 if ($tempid && $tempid == $trow['id'] ||
553 $formid && $template_name == $trow['value'])
557 echo ">" . $trow['value'] . "</option>\n";
562 <input type
='checkbox' name
='form_edit_template'
563 onclick
='editChanged()'
564 title
='<?php xl("If you want to change data types, or add rows or columns","e") ?>' />
565 <?php
xl('Edit Structure','e') ?
>
566 <?php
if ($formid) { ?
>
568 <input type
='checkbox' name
='form_completed'
569 title
='<?php xl("If all data for all columns are complete for this form","e") ?>'
570 <?php
if ($form_completed) echo 'checked '; ?
>/>
571 <?php
xl('Completed','e') ?
>
577 <table border
='1' cellpadding
='2' cellspacing
='0' class='sstable'>
579 if ($dres) $drow = sqlFetchArray($dres);
580 $typeprompts = array('unused','static','checkbox','text');
582 for ($i = 0; $i < $num_virtual_rows; ++
$i) {
584 for ($j = 0; $j < $num_virtual_cols; ++
$j) {
586 // Match up with the database for cell type and value.
590 while ($drow && $drow['rownbr'] < $i)
591 $drow = sqlFetchArray($dres);
592 while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j)
593 $drow = sqlFetchArray($dres);
594 if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) {
595 $celltype = $drow['datatype'];
596 $cellvalue = real2form($drow['value']);
597 $cellstatic = addslashes($drow['value']);
601 echo " <td id='td_${i}_${j}' valign='top'";
602 if ($i >= $num_used_rows ||
$j >= $num_used_cols)
603 echo " style='display:none'";
606 /*****************************************************************
607 echo "<span id='div_${i}_${j}' ";
608 echo "style='float:right;cursor:pointer;display:none' ";
609 echo "onclick='newType($i,$j)'>[";
610 echo $typeprompts[$celltype];
612 *****************************************************************/
613 echo "<div class='seldiv'>";
614 echo "<select id='sel_${i}_${j}' class='seltype' style='display:none' " .
615 "onchange='newType($i,$j)'>";
616 foreach ($celltypes as $key => $value) {
617 echo "<option value='$key'";
618 if ($key == $celltype) echo " selected";
619 echo ">$value</option>";
623 /****************************************************************/
625 echo "<span id='vis_${i}_${j}'>"; // new //
627 echo "<input type='hidden' name='cell[$i][$j]' value='$celltype$cellvalue' />";
628 if ($celltype == '1') {
629 // So we don't have to write a PHP version of genStatic():
630 echo "<script language='JavaScript'>document.write(genStatic('$cellstatic'));</script>";
632 else if ($celltype == '2') {
633 echo "<input type='checkbox' value='1' onclick='cbClick(this,$i,$j)'";
634 if ($cellvalue) echo " checked";
637 else if ($celltype == '3') {
638 echo "<input type='text' class='intext' onchange='textChange(this,$i,$j)'";
639 echo " value='$cellvalue'";
640 echo " size='12' />";
642 else if ($celltype == '4') {
643 echo "<textarea rows='3' cols='25' wrap='virtual' class='intext' " .
644 "onchange='longChange(this,$i,$j)'>";
649 echo "</span>"; // new //
659 <input type
='submit' name
='bn_save_form' value
='Save Form' />
660 <?php
if (!$formid) { ?
>
662 <input type
='submit' name
='bn_save_template' value
='Save as Template:' />
664 <input type
='text' name
='form_new_template_name' value
='<?php echo $template_name ?>' />
666 <input type
='submit' name
='bn_delete_template' value
='Delete Template' />
669 <input type
='button' value
='Cancel' onclick
="doCancel()" />
674 <script language
='JavaScript'>
675 Calendar
.setup({inputField
:"form_start_date", ifFormat
:"%Y-%m-%d", button
:"img_start_date"});
677 if ($alertmsg) echo " alert('$alertmsg');\n";