* @author Brady Miller * @copyright Copyright (c) 2006-2011 Rod Roark * @copyright Copyright (c) 2017 Brady Miller * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3 */ require_once(dirname(__FILE__) . '/api.inc'); require_once(dirname(__FILE__) . '/forms.inc'); require_once(dirname(__FILE__) . '/../interface/forms/fee_sheet/codes.php'); use OpenEMR\Core\Header; $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); return $fldval; } // Get the actual string from a form field. function form2real($fldval) { $fldval = trim($fldval); return $fldval; } // encode a plain string for html display. function real2form($fldval) { return attr($fldval); } 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 " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ? AND rownbr = -1 AND colnbr = -1", array($tempid)); $template_name = $trow['value']; } elseif ($formid) { $trow = sqlQuery("SELECT value FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ? AND rownbr = -1 AND colnbr = -1", array($formid)); 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 " . escape_table_name('form_' . $spreadsheet_form_name) . " SET value = ? WHERE id = ? AND rownbr = -1 AND colnbr = -1", array( $form_completed . '|' . $start_date . '|' . $template_name, $formid ) ); sqlStatement( "DELETE FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ? AND rownbr >= 0 AND colnbr >= 0", array($formid) ); } else { // If adding a new form... $tmprow = sqlQuery( "SELECT pid FROM form_encounter WHERE encounter = ? ORDER BY id DESC LIMIT 1", array($thisenc) ); $thispid = $tmprow['pid']; sqlStatement( "LOCK TABLES " . escape_table_name('form_' . $spreadsheet_form_name) . " WRITE, log WRITE" ); $tmprow = sqlQuery("SELECT MAX(id) AS maxid FROM " . escape_table_name('form_' . $spreadsheet_form_name)); $formid = $tmprow['maxid'] + 1; if ($formid <= 0) { $formid = 1; } sqlStatement( "INSERT INTO " . escape_table_name('form_' . $spreadsheet_form_name) . " ( " . "id, rownbr, colnbr, datatype, value " . ") VALUES ( ?, -1, -1, 0, ? )", array( $formid, $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 " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id < 0 AND rownbr = -1 AND colnbr = -1 AND value = ?", array($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 " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ? AND rownbr >= 0 AND colnbr >= 0", array($tempid) ); } else { // If adding a new template... sqlStatement( "LOCK TABLES " . escape_table_name('form_' . $spreadsheet_form_name) . " WRITE, log WRITE" ); $tmprow = sqlQuery("SELECT MIN(id) AS minid FROM " . escape_table_name('form_' . $spreadsheet_form_name)); $tempid = $tmprow['minid'] - 1; if ($tempid >= 0) { $tempid = -1; } sqlStatement( "INSERT INTO " . escape_table_name('form_' . $spreadsheet_form_name) . " ( " . "id, rownbr, colnbr, datatype, value " . ") VALUES ( ?, -1, -1, 0, ? )", array( $tempid, $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) { sqlStatement( "INSERT INTO " . escape_table_name('form_' . $spreadsheet_form_name) . " ( id, rownbr, colnbr, datatype, value ) " . "VALUES ( ?, ?, ?, ?, ? )", array($saveid, $i, $j, $celltype, $cellvalue) ); } } } } } elseif ($_POST['bn_delete_template'] && $tempid) { sqlStatement( "DELETE FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ?", array($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 " . escape_table_name('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 " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ? ORDER BY rownbr, colnbr", array($formid)); $tmprow = sqlQuery( "SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . "FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ?", array($formid) ); $num_used_rows = $tmprow['rowmax'] + 1; $num_used_cols = $tmprow['colmax'] + 1; } elseif ($tempid) { // Otherwise if we are editing a template, get it. $dres = sqlStatement( "SELECT * FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ? ORDER BY rownbr, colnbr", array($tempid) ); $tmprow = sqlQuery( "SELECT MAX(rownbr) AS rowmax, MAX(colnbr) AS colmax " . "FROM " . escape_table_name('form_' . $spreadsheet_form_name) . " WHERE id = ?", array($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; ?>
: />       />
\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 = $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 ""; } elseif ($celltype == '2') { echo ""; } elseif ($celltype == '3') { echo ""; } elseif ($celltype == '4') { echo ""; } echo ""; // new // echo "