// // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. include_once(dirname(__FILE__) . '/api.inc'); include_once(dirname(__FILE__) . '/forms.inc'); include_once(dirname(__FILE__) . '/../interface/forms/fee_sheet/codes.php'); $celltypes = array( '0' => 'Unused', '1' => 'Static', '2' => 'Checkbox', '3' => 'Text', '4' => 'Longtext', // '5' => 'Function', ); // encode a string from a form field for database writing. function form2db($fldval) { $fldval = trim($fldval); if (!get_magic_quotes_gpc()) $fldval = addslashes($fldval); return $fldval; } // encode a plain string for database writing. function real2db($fldval) { return addslashes($fldval); } // Get the actual string from a form field. function form2real($fldval) { $fldval = trim($fldval); if (get_magic_quotes_gpc()) $fldval = stripslashes($fldval); return $fldval; } // encode a plain string for html display. function real2form($fldval) { return htmlspecialchars($fldval, ENT_QUOTES); } if (empty($spreadsheet_title)) $spreadsheet_title = 'Injury Log'; // Putting an error message in here will result in a javascript alert. $alertmsg = ''; // Determine the encounter that we are working with. $thisenc = empty($_GET['thisenc']) ? $encounter : $_GET['thisenc'] + 0; // If we are invoked as a popup (not in an encounter): $popup = $_GET['popup']; // The form ID is passed to us when an existing encounter form is loaded. $formid = $_GET['id']; // $tempid is the currently selected template, if any. $tempid = $_POST['form_template'] + 0; // This is the start date to be saved with the spreadsheet. $start_date = ''; $form_completed = '0'; if (!$popup && !$encounter) { // $encounter comes from globals.php die("Internal error: we do not seem to be in an encounter!"); } // Get the name of the template selected by the dropdown, if any; // or if we are loading a form then it comes from that. $template_name = ''; if ($tempid) { $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " . "id = $tempid AND rownbr = -1 AND colnbr = -1"); $template_name = $trow['value']; } else if ($formid) { $trow = sqlQuery("SELECT value FROM form_$spreadsheet_form_name WHERE " . "id = $formid AND rownbr = -1 AND colnbr = -1"); list($form_completed, $start_date, $template_name) = explode('|', $trow['value'], 3); } if (!$start_date) $start_date = form2real($_POST['form_start_date']); // Used rows and columns are those beyond which there are only unused cells. $num_used_rows = 0; $num_used_cols = 0; // If we are saving... // if ($_POST['bn_save_form'] || $_POST['bn_save_template']) { // The form data determines how many rows and columns are now used. $cells = $_POST['cell']; for ($i = 0; $i < count($cells); ++$i) { $row = $cells[$i]; for ($j = 0; $j < count($row); ++$j) { if (substr($row[$j], 0, 1)) { if ($i >= $num_used_rows) $num_used_rows = $i + 1; if ($j >= $num_used_cols) $num_used_cols = $j + 1; } } } if ($_POST['bn_save_form']) { $form_completed = $_POST['form_completed'] ? '1' : '0'; // If updating an existing form... if ($formid) { sqlStatement("UPDATE form_$spreadsheet_form_name SET " . "value = '$form_completed|$start_date|$template_name' " . "WHERE id = '$formid' AND rownbr = -1 AND colnbr = -1"); sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " . "id = '$formid' AND rownbr >= 0 AND colnbr >= 0"); } // If adding a new form... else { $tmprow = sqlQuery("SELECT pid FROM form_encounter WHERE encounter = ? ORDER BY id DESC LIMIT 1", array($thisenc)); $thispid = $tmprow['pid']; sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE"); $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM form_$spreadsheet_form_name"); $formid = $tmprow['maxid'] + 1; if ($formid <= 0) $formid = 1; sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " . "id, rownbr, colnbr, datatype, value " . ") VALUES ( " . "$formid, -1, -1, 0, " . "'$form_completed|$start_date|$template_name' " . ")"); sqlStatement("UNLOCK TABLES"); addForm($thisenc, $spreadsheet_title, $formid, "$spreadsheet_form_name", $thispid, $userauthorized); } $saveid = $formid; } else { // saving a template // The rule is, we can update the original name, or insert a new name // which must not match any existing template name. $new_template_name = form2real($_POST['form_new_template_name']); if ($new_template_name != $template_name) { $trow = sqlQuery("SELECT id FROM form_$spreadsheet_form_name WHERE " . "id < 0 AND rownbr = -1 AND colnbr = -1 AND value = '" . real2db($new_template_name) . "'"); if ($trow['id']) { $alertmsg = "Template \"" . real2form($new_template_name) . "\" already exists!"; } else { $tempid = 0; // to force insert of new template $template_name = $new_template_name; } } if (!$alertmsg) { // If updating an existing template... if ($tempid) { sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " . "id = '$tempid' AND rownbr >= 0 AND colnbr >= 0"); } // If adding a new template... else { sqlStatement("LOCK TABLES form_$spreadsheet_form_name WRITE, log WRITE"); $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM form_$spreadsheet_form_name"); $tempid = $tmprow['minid'] - 1; if ($tempid >= 0) $tempid = -1; sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " . "id, rownbr, colnbr, datatype, value " . ") VALUES ( " . "$tempid, -1, -1, 0, " . "'" . real2db($template_name) . "' " . ")"); sqlStatement("UNLOCK TABLES"); } $saveid = $tempid; } } if (!$alertmsg) { // Finally, save the table cells. for ($i = 0; $i < $num_used_rows; ++$i) { for ($j = 0; $j < $num_used_cols; ++$j) { $tmp = $cells[$i][$j]; $celltype = substr($tmp, 0, 1) + 0; $cellvalue = form2db(substr($tmp, 1)); if ($celltype) { sqlInsert("INSERT INTO form_$spreadsheet_form_name ( " . "id, rownbr, colnbr, datatype, value " . ") VALUES ( " . "$saveid, $i, $j, $celltype, '$cellvalue' )"); } } } } } else if ($_POST['bn_delete_template'] && $tempid) { sqlStatement("DELETE FROM form_$spreadsheet_form_name WHERE " . "id = '$tempid'"); $tempid = 0; $template_name = ''; } if ($_POST['bn_save_form'] && !$alertmsg && !$popup) { formHeader("Redirecting...."); formJump(); formFooter(); exit; } // If we get here then we are displaying a spreadsheet, either a template or // an encounter form. // Get the array of template names. $tres = sqlStatement("SELECT id, value FROM form_$spreadsheet_form_name WHERE " . "id < 0 AND rownbr = -1 AND colnbr = -1 ORDER BY value"); $dres = false; # If we are reloading a form, get it. if ($formid) { $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " . "id = '$formid' ORDER BY rownbr, colnbr"); $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . "FROM form_$spreadsheet_form_name WHERE id = '$formid'"); $num_used_rows = $tmprow['rowmax'] + 1; $num_used_cols = $tmprow['colmax'] + 1; } # Otherwise if we are editing a template, get it. else if ($tempid) { $dres = sqlStatement("SELECT * FROM form_$spreadsheet_form_name WHERE " . "id = '$tempid' ORDER BY rownbr, colnbr"); $tmprow = sqlQuery("SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . "FROM form_$spreadsheet_form_name WHERE id = '$tempid'"); $num_used_rows = $tmprow['rowmax'] + 1; $num_used_cols = $tmprow['colmax'] + 1; } // Virtual rows and columns are those available when in Edit Structure mode, // and include some additional ones beyond those used. This allows quite a // lot of stuff to be entered before having to save the template. $num_virtual_rows = $num_used_rows ? $num_used_rows + 5 : 10; $num_virtual_cols = $num_used_cols ? $num_used_cols + 5 : 10; ?>
" onsubmit="return top.restoreSession()">
: /> [?]       />
\n"; for ($j = 0; $j < $num_virtual_cols; ++$j) { // Match up with the database for cell type and value. $celltype = '0'; $cellvalue = ''; if ($dres) { while ($drow && $drow['rownbr'] < $i) $drow = sqlFetchArray($dres); while ($drow && $drow['rownbr'] == $i && $drow['colnbr'] < $j) $drow = sqlFetchArray($dres); if ($drow && $drow['rownbr'] == $i && $drow['colnbr'] == $j) { $celltype = $drow['datatype']; $cellvalue = real2form($drow['value']); $cellstatic = addslashes($drow['value']); } } echo " \n"; } echo " \n"; } ?>
= $num_used_rows || $j >= $num_used_cols) echo " style='display:none'"; echo ">"; /***************************************************************** echo "["; echo $typeprompts[$celltype]; echo "]"; *****************************************************************/ echo "
"; echo ""; echo "
"; /****************************************************************/ echo ""; // new // echo ""; if ($celltype == '1') { // So we don't have to write a PHP version of genStatic(): echo ""; } else if ($celltype == '2') { echo ""; } else if ($celltype == '3') { echo ""; } else if ($celltype == '4') { echo ""; } echo ""; // new // echo "