<?php
#Copyright 2008 Aaron Day-Williams <hide@address.com>
#
#This file is part of Generic Genetic Studies Database (GGSD).
#
#GGSD 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 3 of the License, or
#(at your option) any later version.
#
#GGSD is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with GGSD. If not, see <http://www.gnu.org/licenses/>.
header('Content-type: text/html');
session_cache_limiter('private');
session_start();
require 'sql_lib.inc';
require 'html_lib.inc';
require 'file_system_lib.inc';
#NAME insert_maps.php
#TYPE PHP
#USES sql_lib.inc(authenticate insertMap insertMapMarkerLink tableQuery distinctTableQuery compoundTableQuery updateRecord)
# html_lib.inc(??)
#PURPOSE To allow the user to insert maps and their markers into the database.
#USAGE insert_maps.php
#PARAMETERS
# authorized user auth_user int optional
# insert from file insert_from_file int optional
# map name map_name string optional
# map id map_id int optional
# chromosome id chr char optional
# number of markers num_markers int optional
# number inserted markers num_inserted_markers int optional
# distance measure dist_measure string optional
# map description map_desc string optional
# marker name marker_name string optional
# marker_id marker_id int optional
# marker number in map marker_num_in_map int optional
# distance from previous marker dist_prev_marker float optional
#RETURNS
# success message string if success
# error message string if error
#DATASTRUCTURES No special databastructures.
#FUNCTIONAL DESCRIPTION Authenticate the user. Then determine if they want to insert
# from a file or not. If not from a file, give a web form. Get the
# data from the web form and insert. If from a file, give form to upload file.
# Then parse file and insert.
#ASSUMPTIONS The file is formatted correctly and the data is correct.
#LIMITATIONS No way of telling if the data is in the correct format.
#COMMENTS
#PRELIMINARY DESIGN
# 1 Get Args
# 2 Authenticate
# 3 Determine if insert from file
# 4 If not from file, give web form
# 5 Get web form data and insert
# 6 If from file, give file upload form
# 7 Get file, parse, and insert
echo "<html>\n";
echo "<head>\n";
if (isset($_POST['end_session'])){echo " <META HTTP-EQUIV=Refresh CONTENT=\"$logout_refresh_time;URL=../right.html\">\n";}
else {echo " <META HTTP-EQUIV=Refresh CONTENT=\"$normal_refresh_time; URL=$_SERVER[SCRIPT_NAME]\">\n";}
echo " <title>Insert Maps</title>\n";
echo "</head>\n";
echo "<body bgcolor=\"white\">\n";
echo "<center><h3>Insert Maps</h3></center>\n";
echo "<hr>\n";
printHomeNavBar();
echo "<br>\n";
#1 Get Args
$DEBUG = 0;
$access_type_required = 'administrative';
$insert_from_file = $_POST['insert_from_file'];
$map_id = $_POST['map_id'];
$map_name = $_POST['map_name'];
$marker_id = $_POST['marker_id'];
$marker_name = $_POST['marker_name'];
$chr = $_POST['chromosome_id'];
$num_markers = $_POST['num_markers'];
$num_inserted_markers = $_POST['num_inserted_markers'];
$num_marker_in_map = $_POST['num_marker_in_map'];
$dist_measure = $_POST['dist_measure'];
$map_desc = $_POST['map_description'];
$dist_prev_marker = $_POST['dist_prev_marker'];
$file_format = $_POST['file_format'];
$upload_file = $_POST['upload_file'];
$file_on_server = $_POST['file_name'];
if ($DEBUG)
{
echo "<table>\n";
echo "<tr><td>insert_from_file</td><td>$_POST[insert_from_file]</td></tr>\n";
echo "<tr><td>map_id</td><td>$_POST[map_id]</td></tr>\n";
echo "<tr><td>map_name</td><td>$_POST[map_name]</td></tr>\n";
echo "<tr><td>marker_id</td><td>$_POST[marker_id]</td></tr>\n";
echo "<tr><td>marker_name</td><td>$_POST[marker_name]</td></tr>\n";
echo "<tr><td>chr</td><td>$_POST[chromosome_id]</td></tr>\n";
echo "<tr><td>num_markers</td><td>$_POST[num_markers]</td></tr>\n";
echo "<tr><td>num_inserted_markers</td><td>$_POST[num_inserted_markers]</td></tr>\n";
echo "<tr><td>num_marker_in_map</td><td>$_POST[num_marker_in_map]</td></tr>\n";
echo "<tr><td>dist_measure</td><td>$_POST[dist_measure]</td></tr>\n";
echo "<tr><td>map_desc</td><td>$_POST[map_description]</td></tr>\n";
echo "<tr><td>dist_prev_marker</td><td>$_POST[dist_prev_marker]</td></tr>\n";
echo "<tr><td>file_format</td><td>$_POST[file_format]</td></tr>\n";
echo "</table>\n";
}#end if DEBUG on
$ret_val = connect();
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #1: $date<br>\n";
echo "Error in call to connect from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
exit;
}
#2 Authenticate
if (isset($_POST['end_session']))#if want to end session
{
#if (key_exists('files_on_server',$_SESSION))
# {
# $files_to_delete_array = $_SESSION['files_on_server'];
# $ret_val = deleteFiles($files_to_delete_array);
# if ($ret_val != '1')
# {
# $date = date('Y-m-d H:i:s');
# echo "Error #2: $date<br>\n";
# echo "Error in call to deleteFiles from insert_maps.php<br>\n";
# echo "$ret_val<br><br>\n";
# echo "Please email the above error using Comments/Help/Support link \n";
# echo "in the left navigation bar.<br>\n";
# echo "</body></html>\n";
# session_unset();
# session_destroy();
# $ret_val = disconnect();
# exit;
# }#end error in call to deleteFiles
# }#end if files that need to be deleted
if (is_dir("../data/download_data/$_SESSION[user_name]"))
{
$ret_val = deleteExpiredFiles($_SESSION['user_name']);
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #3: $date<br>\n";
echo "Error in call to deleteExpiredFiles from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}#end error in call to deleteExpiredFiles
}#end if user has own directory in download_data directory
session_unset();
session_destroy();
echo "You have successfully logged out of the system.<br>\n";
#echo "To log back in click on any of the links on the left navigation bar.<br>\n";
$ret_val = disconnect();
exit;
}#end if want to end session
else#don't want to end session
{
if ((!session_is_registered('user_name')) and (!session_is_registered('user_passwd')))
{
if ((!preg_match('/\w+/',$_POST['user_name'])) and (!preg_match('/\w+/',$_POST['user_passwd'])))
{
$ret_val = printLoginPage('insert_maps.php');
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #4: $date<br>\n";
echo "Error in call to printLoginPage from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}#end if error in call to printLoginPage
}#end if haven't started a session yet
else #have started a session
{
$auth_user = authenticate($_POST['user_name'],$_POST['user_passwd'],$_POST['rand'],$access_type_required,$_POST['database_id']);
if (($auth_user != '1') and ($auth_user != '0'))
{
$date = date('Y-m-d H:i:s');
echo "Error #5: $date<br>\n";
echo "Error in call to authenticate from insert_maps.php<br>\n";
echo "$auth_user<br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}
if ($auth_user == '0')
{
echo "You are not an authorized user of this website!!<br><br>\n";
echo "You must be an authorized user to access the data contained in this site.\n";
echo "If you feel like you should be an authorized user, or want to see if you can\n";
echo "become an authorized user please contact $contact_person at \n";
echo "<a href=\"mailto:$contact_email\">$contact_email</a>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}
if ($auth_user == '1')
{
$_SESSION['user_name'] = $_POST['user_name'];
$_SESSION['user_passwd'] = $_POST['user_passwd'];
$_SESSION['database_id'] = $_POST['database_id'];
$_SESSION['rand'] = $_POST['rand'];
$_SESSION['entry_time'] = time();
session_write_close();
}
}#end else have user name an passwd
}#end if session variables not registered
else #session variable are registered
{
if ((time() - $_SESSION['entry_time']) > $session_expiration_time)#if session has expired
{
if (key_exists('files_on_server',$_SESSION))
{
$files_to_delete_array = $_SESSION['files_on_server'];
$ret_val = deleteFiles($files_to_delete_array);
if ($ret_val != 1)
{
$date = date('Y-m-d H:i:s');
echo "Error #6: $date<br>\n";
echo "Error in call to deleteFiles from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}#end if error in call to deleteFiles
}#end if files that need to be deleted
if (is_dir("../data/download_data/$_SESSION[user_name]"))
{
$ret_val = deleteExpiredFiles($_SESSION['user_name']);
if ($ret_val != 1)
{
$date = date('Y-m-d H:i:s');
echo "Error #7: $date<br>\n";
echo "Error in call to deleteExpiredFiles from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}#end if error in call to deleteExpiredFiles
}#end if user has a data directory
session_unset();
session_destroy();
echo "<b>Your Session has expired. You must log back into the system.</b><br><br><br>\n";
$ret_val = printLoginPage('insert_maps.php');
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #8: $date<br>\n";
echo "Error in call to printLoginPage from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}#end if error in call to printLoginPage
}#end if session has expired
else #session hasn't expired
{
if ((!session_is_registered('database_id')) and (!isset($_POST['database_id'])))
{
$ret_val = printDatabaseSelectPage('insert_maps.php');
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #9: $date<br>\n";
echo "Error in call to printDatabaseSelectPage from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}#end if error in call to printDatabaseSelectPage
}#end if the database id session variable isn't set and haven't selected
else #all the session variables are set
{
$user_name = $_SESSION['user_name'];
$user_passwd = $_SESSION['user_passwd'];
if (session_is_registered('database_id')){$database_id = $_SESSION['database_id'];}
else #set database id session variable
{
$database_id = $_POST['database_id'];
$_SESSION['database_id'] = $_POST['database_id'];
}#end else set database id session variable
$rand = $_SESSION['rand'];
$_SESSION['entry_time'] = time();
$auth_user = authenticate($user_name,$user_passwd,$rand,$access_type_required,$database_id);
if (($auth_user != '1') and ($auth_user != '0'))
{
$date = date('Y-m-d H:i:s');
echo "Error #10: $date<br>\n";
echo "Error in call to authenticate from insert_maps.php<br>\n";
echo "$auth_user<br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}
if ($auth_user == '0')
{
echo "You are not an authorized user of this website!!<br><br>\n";
echo "You must be an authorized user to access the data contained in this site.\n";
echo "If you feel like you should be an authorized user, or want to see if you can\n";
echo "become an authorized user please contact $contact_person \n";
echo "at <a href=\"mailto:$contact_email\">$contact_email</a>\n";
echo "</body></html>\n";
session_unset();
session_destroy();
$ret_val = disconnect();
exit;
}
}#end else all the session variables are set
}#end else session hasn't expired
}#end else registered values
}#end else not ending session
if ($auth_user == '1')
{
$database_id = $_SESSION['database_id'];
#3 Deterime if insert from file or not
if (!preg_match('/\d/',$insert_from_file))
{
echo "<form action=\"insert_maps.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
echo "This form allows you to insert new maps and their associated marker information into the database. There are two options \n";
echo "for the way to do this. You can store the marker and distance information in a file and upload the file to insert the information, \n";
echo "or you can select the markers and input the distance information through web forms.<br>\n";
echo "Please choose the method you would like to choose below:<br><br>\n";
echo "<input type=\"radio\" name=\"insert_from_file\" value=\"1\" checked>Insert Map Data Stored in a File<br>\n";
echo "<input type=\"radio\" name=\"insert_from_file\" value=\"0\">Insert Map Data Using a Web Form<br>\n";
echo "<center>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Insert New Map\">\n";
echo "<input type=\"reset\" name=\"reset\" value=\"Reset Selection\">\n";
echo "</form>\n";
echo "</center>\n";
echo "<br><br>\n\n";
$ret_val = printLogOutButton('insert_maps.php');
}#end if (!preg_match('/\d/',insert_from_file))
#4 If not from file, give web form
if (($insert_from_file == '0') and
(!preg_match('/\w+/',$map_name)) and
(!preg_match('/\w+/',$dist_measure)) and
(!preg_match('/\d+/',$num_markers))
)
{
echo "<form action=\"insert_maps.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
echo "Please fill in the form below and then press the Submit button. The data will then be inserted into the \n";
echo "database. (Required fields are in bold type)<br><br>\n";
echo "<table>\n";
echo "<tr><td><b>Map Name</b>:</td><td><input type=\"text\" name=\"map_name\" maxlength=\"52\" size=\"15\"></td></tr>\n";
echo "<tr><td><b>Distance Measure</b>:</td><td><select name=\"dist_measure\">\n";
echo "<option>Select a Distance Measure\n";
echo "<option value=\"base_pairs\">Base Pairs\n";
echo "<option value=\"haldane_centiMorgans\">Haldance centiMorgans\n";
echo "<option value=\"kosambi_centiMorgans\">Kosambi centiMorgans\n";
echo "<option value=\"recombination_fraction\">Recombination Fraction\n";
echo "</select>\n";
echo "</td></tr>\n";
echo "<tr><td><b>Number of Markers in Map</b>:</td><td><input type=\"text\" name=\"num_markers\" maxlength=\"11\" size=\"15\"></td></tr>\n";
echo "<tr><td>Map Description:</td><td><textarea name=\"map_description\" cols=\"50\" rows=\"10\" wrap=\"virtual\"></textarea></td></tr>\n";
echo "</table>\n";
echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"0\">\n";
echo "<center>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Insert Map into Database\">\n";
echo "<input type=\"reset\" name=\"reset\" value=\"Reset Values\">\n";
echo "</form>\n";
echo "</center>\n";
echo "<br><br>\n\n";
$ret_val = printLogOutButton('insert_maps.php');
echo "\n\n";
echo "<script>\n";
echo "function Validate(form)\n";
echo "{\n";
#echo " var number_reg = new RegExp(\"^\d+$\");\n";
echo " var num_markers = form.num_markers.value;\n";
echo " if (form.map_name.value == \"\"){\n";
echo " alert(\"You must enter a Map Name\");\n";
echo " form.map_name.focus();\n";
echo " return(false);}\n";
echo "\n";
echo " if (form.dist_measure.selectedIndex == 0){\n";
echo " alert(\"You must select a Distance Measure\");\n";
echo " form.dist_measure.focus();\n";
echo " return(false);}\n";
echo "\n";
echo " if (form.num_markers.value == \"\"){\n";
echo " alert(\"You must enter the Number of Markers in Map\");\n";
echo " form.num_markers.focus();\n";
echo " return(false);}\n";
echo "\n";
echo " if (!num_markers.match(/^\d+/)){\n";
echo " alert(\"You must enter a number in the Number of Markers in Map field\");\n";
echo " form.num_markers.focus();\n";
echo " return(false);}\n";
echo " return(true);\n";
echo "}\n";
echo "</script>\n";
}#end if not from a file, and not web data
#5 Get Web form data and insert
if (($insert_from_file == '0') and
(preg_match('/\w+/',$map_name)) and
(preg_match('/\w+/',$dist_measure)) and
(preg_match('/\d+/',$num_markers))
)
{
$rand = rand();
$tmp_log_file = "log_file_"."$rand".".txt";
$log_file_handle = fopen("../data/$tmp_log_file","w");
if (!$log_file_handle)
{
$date = date('Y-m-d H:i:s');
echo "Error #11: $date<br>\n";
echo "Error in call to fopen from insert_maps.php<br>\n";
echo "Unable to open log file to keep track of when, what, and by whom data is altered in the database<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to fopen
$log_file_statement = "";
$field_array = array('map_name');
$map_name = strtolower($map_name);
$map_name = mysql_escape_string($map_name);
$value_array = array($map_name);
$exists_map = existsValue('map_table',$field_array,$value_array,$database_id);
if (!preg_match('/\d+/',$exists_map))
{
$date = date('Y-m-d H:i:s');
echo "Error #12: $date<br>\n";
echo "Error in call to existsValue from insert_maps.php<br>\n";
echo "$exists_map<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to existsValue
if ($exists_map > '0')
{
echo "The map ".stripslashes($map_name)." is already in the database. Please make sure that you \n";
echo "spelled the map name correctly. Each map in the database must have a unique name.<br>\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end if map already exists
$ret_val = insertMap($database_id,$map_name,$num_markers,$dist_measure);
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #13: $date<br>\n";
echo "Error in call to insertMarker from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to insertMap
$field_list_array = array('map_name');
$map_id_array = distinctTableQuery('map_id','map_table',$database_id,$field_list_array,$map_name,'equal','all');
if (!is_array($map_id_array))
{
$date = date('Y-m-d H:i:s');
echo "Error #14: $date<br>\n";
echo "Error in call to distinctTableQuery from insert_maps.php<br>\n";
echo "$map_id_array<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to distinctTableQuery
if (sizeof($map_id_array) == '0')
{
$date = date('Y-m-d H:i:s');
echo "Error #15: $date<br>\n";
echo "Error in execution of insert_maps.php<br>\n";
echo "The call to insertMap returned success for map name $map_name<br>\n";
echo "But when query database id $database_id, do not find the record.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end no map record for inserted map
elseif (sizeof($map_id_array) == '1'){$map_id = $map_id_array[0];}
else #error in data integrity
{
$date = date('Y-m-d H:i:s');
echo "Error #16: $date<br>\n";
echo "!!!Error in Database Data Integrity!!!!<br>\n";
echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
echo "Error in execution of insert_maps.php<br>\n";
echo "The call to insertMap returned success for map name $map_name<br>\n";
echo "But when query database id $database_id, multiple records are returned.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in data integrity
$date = date('Y-m-d H:i:s');
$log_file_statement.= "$date:\t$_SESSION[user_name] Inserted a New Map ID $map_id into Database ID $database_id, ";
$log_file_statement.= "new Map Name $map_name has $num_markers Markers on is Measured in $dist_measure.\n";
if (preg_match('/\w+/',$map_desc))
{
$update_field_array = array('map_description');
$update_value_array = array($map_desc);
$ret_val = updateRecord('map_table',$map_id,$update_field_array,$update_value_array);
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #17: $date<br>\n";
echo "Error in call to updateRecord from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fwrite($log_file_handle,$log_file_statement);
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
exec($command,$output_array,$return_code);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to updateRecord
$date = date('Y-m-d H:i:s');
$log_file_statement.= "$date:\t$_SESSION[user_name] Updated Map ID $map_id in Database ID $database_id, ";
$log_file_statement.= "inserted Map Description $map_desc.\n";
}#end if have map_description
fwrite($log_file_handle,$log_file_statement);
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
exec($command,$output_array,$return_code);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
echo "Successfully inserted the map $map_name into the database.<br>\n";
echo "Now you will be prompted for the marker and distance information for this map.<br><br>\n";
echo "<form action=\"insert_maps.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"0\">\n";
echo "<input type=\"hidden\" name=\"num_inserted_markers\" value=\"0\">\n";
echo "<input type=\"hidden\" name=\"num_markers\" value=\"$num_markers\">\n";
echo "<input type=\"hidden\" name=\"map_id\" value=\"$map_id\">\n";
echo "<center>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Insert Marker Information for Map $map_name into Database\">\n";
echo "</center>\n";
echo "</form>\n";
echo "<br><br>\n";
$ret_val = printLogOutButton('insert_maps.php');
echo "</body>\n";
echo "</html>\n";
$ret_val = disconnect();
exit;
}#end if have web map data
if (($insert_from_file == '0') and
(preg_match('/\d+/',$map_id)) and
($num_inserted_markers != $num_markers)
)
{
$field_list_array = array('map_id');
$map_name_array = distinctTableQuery('map_name','map_table',$database_id,$field_list_array,$map_id,'equal','all');
if (!is_array($map_name_array))
{
$date = date('Y-m-d H:i:s');
echo "Error #18: $date<br>\n";
echo "Error in call to distinctTableQuery from insert_maps.php<br>\n";
echo "$map_name_array<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to distinctTableQuery
if (sizeof($map_name_array) == '0')
{
$date = date('Y-m-d H:i:s');
echo "The Map with map id $map_id is no longer being tracked in the database.<br>\n";
echo "Therefore, you can no longer add markers to the map.<br>\n";
echo "The message was generated at $date.<br><br>\n";
$ret_val = disconnect();
exit;
}#end if no longer a map
elseif (sizeof($map_name_array) == '1'){$map_name = $map_name_array[0];}
else #error in data integrity
{
$date = date('Y-m-d H:i:s');
echo "Error #19: $date<br>\n";
echo "!!!Error in Database Data Integrity!!!!<br>\n";
echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
echo "Error in execution of insert_maps.php<br>\n";
echo "The user selected to insert marker id $marker id into the map id $map_id in database id $database_id<br>\n";
echo "But when query the map table with map id $map_id, multiple records are returned.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in data integrity
if ((!preg_match('/\w+/',$marker_id)) and (preg_match('/\w+/',$chr)))
{
echo "<form action=\"insert_maps.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
$current_marker = ($num_inserted_markers + 1);
echo "Please input the Marker information for the $current_marker";
if ($current_marker == '1') {echo "st ";}
elseif ($current_marker == '2') {echo "nd ";}
elseif ($current_marker == '3') {echo "rd ";}
else {echo "th ";}
echo "marker on Chromosome $chr. (Required information in bold)<br><br>\n";
echo "<table>\n";
echo "<tr><td><b>Marker Name on Chromosome $chr</b>:</td><td>\n";
$jscript_2 = printMarkerSelectList($database_id,$chr);
if ($jscript_2 == '0')
{
$date = date('Y-m-d H:i:s');
echo "Error #20: $date<br>\n";
echo "Error in call to printMarkerSelectList from insert_maps.php<br>\n";
echo "Please see the error message from printMarkerSelectList above for an explanation.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if error in call to printMarkerSelectList
echo "</td></tr>\n";
echo "<tr>\n";
echo "<td><b>Distance from Previous Marker</b>:</td>\n";
echo "<td><input type=\"text\" name=\"dist_prev_marker\" maxlength=\"255\" size=\"15\"></td>\n";
echo "<td><b>(If First Marker in Map -or- On Different Chr from previous Marker enter 0.0)</b></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"0\">\n";
echo "<input type=\"hidden\" name=\"map_id\" value=\"$map_id\">\n";
echo "<input type=\"hidden\" name=\"num_markers\" value=\"$num_markers\">\n";
echo "<input type=\"hidden\" name=\"num_inserted_markers\" value=\"$num_inserted_markers\">\n";
echo "<input type=\"hidden\" name=\"chromosome_id\" value=\"$chr\">\n";
echo "<center>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit Marker Information\">\n";
echo "<input type=\"reset\" name=\"reset\" value=\"Reset Marker Information\">\n";
echo "</form>\n";
echo "</center>\n";
echo "<br><br>\n\n";
$ret_val = printLogOutButton('insert_maps.php');
echo "\n\n";
$jscript = preg_replace('/else \{return true;\}
\}/','',$jscript);
echo "<script>\n";
echo "function Validate(form)\n";
echo "$jscript";
echo " if (form.dist_prev_marker.value == \"\"){\n";
echo " alert(\"You must enter the distance from the previous marker\");\n";
echo " form.dist_prev_marker.focus();\n";
echo " return false;}\n";
echo " return true;\n";
echo "}\n";
echo "</script>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if have chromosome of next marker but not marker name and distance
if ((preg_match('/\w+/',$marker_id)) and (preg_match('/\w+/',$chr)))
{
$rand = rand();
$tmp_log_file = "log_file_"."$rand".".txt";
$log_file_handle = fopen("../data/$tmp_log_file","w");
if (!$log_file_handle)
{
$date = date('Y-m-d H:i:s');
echo "Error #21: $date<br>\n";
echo "Error in call to fopen from insert_maps.php<br>\n";
echo "Unable to open log file to keep track of when, what, and by whom data is altered in the database<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to fopen
$log_file_statement = "";
$chr = strtolower($chr);
$field_list_array = array('marker_id');
$marker_name_array = distinctTableQuery('marker_name','marker_table',$database_id,$field_list_array,$marker_id,'equal','all');
if (!is_array($marker_name_array))
{
$date = date('Y-m-d H:i:s');
echo "Error #22: $date<br>\n";
echo "Error in call to distinctTableQuery from insert_maps.php<br>\n";
echo "$marker_name_array<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to distinctTableQuery
if (sizeof($marker_name_array) == '0')
{
$insert_num = $num_inserted_markers + 1;
echo "The Marker you selected to insert as the $insert_num marker in the map \n";
echo "is no longer in the database. Please select a new marker to input into the map below.<br><br>\n";
}
elseif (sizeof($marker_name_array) == '1'){$marker_name = $marker_name_array[0];}
else #error in data integrity
{
$date = date('Y-m-d H:i:s');
echo "Error #23: $date<br>\n";
echo "!!!Error in Database Data Integrity!!!!<br>\n";
echo "Stop using the data in the database until Database Administrator tells you all data is okay.<br>\n";
echo "Error in execution of insert_maps.php<br>\n";
echo "The user selected to insert marker id $marker id into the map id $map_id in database id $database_id<br>\n";
echo "But when query the marker table, multiple records are returned.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in data integrity
if (sizeof($marker_name_array) == '1')
{
$field_array = array('marker_id','map_id');
$value_array = array($marker_id,$map_id);
$exists_map_marker_link = existsValue('map_marker_link_table',$field_array,$value_array,$database_id);
if (!preg_match('/\d+/',$exists_map_marker_link))
{
$date = date('Y-m-d H:i:s');
echo "Error #24: $date<br>\n";
echo "Error in call to existsValue from insert_maps.php<br>\n";
echo "$exists_map_marker_link<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to existsValue
if ($exists_map_marker_link > '0')
{
$insert_num = $num_inserted_markers + 1;
echo "Marker $marker_name is already linked to Map $map_name. Please choose another marker \n";
echo "to be the $insert_num marker in the map.<br><br>\n";
}#end if map marker link already exists
else #not already linked to map
{
$marker_num = $num_inserted_markers + 1;
if (!preg_match('/^\d+$/',$dist_prev_marker)){$dist_prev_marker = "0";}#{$dist_prev_marker = "$dist_prev_marker".".00000";}
$ret_val = insertMapMarkerLink($database_id,$map_id,$marker_id,$chr,$dist_prev_marker,$marker_num);
if ($ret_val != '1')
{
$date = date('Y-m-d H:i:s');
echo "Error #25: $date<br>\n";
echo "Error in call to insertMapMarkerLink from insert_maps.php<br>\n";
echo "$ret_val<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to insertMapMarkerLink
$field_list_array = array('marker_id','map_id');
$term_array = array($marker_id,$map_id);
$mode_array = array('equal','equal');
$map_marker_link_rec_list = compoundTableQuery('map_marker_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
if (!is_array($map_marker_link_rec_list))
{
$date = date('Y-m-d H:i:s');
echo "Error #26: $date<br>\n";
echo "Error in call to compoundTableQuery from insert_maps.php<br>\n";
echo "$map_marker_link_rec_list<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end error in call to compoundTableQuery
if (sizeof($map_marker_link_rec_list) == '0')
{
$date = date('Y-m-d H:i:s');
echo "Error #27: $date<br>\n";
echo "Error in insert_maps.php<br>\n";
echo "insertMapMarkerLink returned success, but when query Map-Marker Link Table \n";
echo "does not find link for Marker $marker_name with Map $map_name<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
$ret_val = disconnect();
exit;
}#end if can't find map-marker link
$map_marker_link_rec = $map_marker_link_rec_list[0];
$map_marker_link_id = $map_marker_link_rec['map_marker_link_id'];
$date = date('Y-m-d H:i:s');
$log_file_statement.= "$date:\t$_SESSION[user_name] Inserted Map Marker Link ID $map_marker_link_id into Database ID $database_id, ";
$log_file_statement.= "Linked Map ID $map_id to Marker ID $marker_id.\n";
$num_inserted_markers++;
}#end else not a linked marker
}#end if still a marker
fwrite($log_file_handle,$log_file_statement);
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
exec($command,$output_array,$return_code);
$command = "rm ../data/$tmp_log_file";
exec($command,$output_array,$return_code);
}#end if have marker_id and chromosome
if ($num_inserted_markers != $num_markers)
{
echo "<form action=\"insert_maps.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
$current_marker = ($num_inserted_markers + 1);
echo "Please input the Chromosome of the Marker to be the $current_marker";
if ($current_marker == '1') {echo "st ";}
elseif ($current_marker == '2') {echo "nd ";}
elseif ($current_marker == '3') {echo "rd ";}
else {echo "th ";}
echo "marker. (Required information in bold)<br><br>\n";
echo "<table>\n";
echo "<tr><td><b>Chromosome of Marker</b>:</td><td>\n";
$jscript = printChromosomeSelectList($database_id);
if ($jscript == '0')
{
$date = date('Y-m-d H:i:s');
echo "Error #28: $date<br>\n";
echo "Error in call to printChromosomeSelectList from insert_maps.php<br>\n";
echo "Please see the error message from printChromosomeSelectList above for an explanation.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if error in call to printChromosomeSelectList
echo "</td></tr>\n";
echo "</table>\n";
echo "<br><br>\n";
echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"0\">\n";
echo "<input type=\"hidden\" name=\"map_id\" value=\"$map_id\">\n";
echo "<input type=\"hidden\" name=\"num_markers\" value=\"$num_markers\">\n";
echo "<input type=\"hidden\" name=\"num_inserted_markers\" value=\"$num_inserted_markers\">\n";
echo "<center>\n";
echo "<input type=\"submit\" name=\"submit\" value=\"Submit Marker Information\">\n";
echo "<input type=\"reset\" name=\"reset\" value=\"Reset Marker Information\">\n";
echo "</form>\n";
echo "</center>\n";
echo "<br><br>\n\n";
$ret_val = printLogOutButton('insert_maps.php');
echo "\n\n";
echo "<script>\n";
echo "function Validate(form)\n";
echo "$jscript";
echo "</script>\n";
}#end if not inserted all the markers
else #inserted all the markers
{
echo "Successfully inserted Map $map_name and all of its $num_markers associated Markers.<br><br>\n";
echo "Now emailing all the users of the database to inform them of the new information in the database.<br>\n";
$database_rec_list = getDatabase($database_id);
if (!is_array($database_rec_list))
{
$date = date('Y-m-d H:i:s');
echo "Error #29: $date<br>\n";
echo "Error in call to getDatabase from insert_maps.php<br>\n";
echo "$database_rec_list<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to getDatabase
$database_rec = $database_rec_list[0];
$user_rec_list = getAllUsers();
if (!is_array($user_rec_list))
{
$date = date('Y-m-d H:i:s');
echo "Error #30: $date<br>\n";
echo "Error in call to getAllUsers from insert_maps.php<br>\n";
echo "$user_rec_list<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to getAllUsers
foreach ($user_rec_list as $user_rec)
{
if ($user_rec['user_name'] == 'root') {$root_email = $user_rec['user_email'];}
}#end foreach user
$subject = "New Map in your Genetic Studies Database";
$message = "The Map $map_name and its associated Markers were inserted into the database $database_rec[database_name]";
$header = "From: $root_email";
$field_list = array('user_database_link_id');
$user_database_link_rec_list = tableQuery('user_database_link_table',$database_id,$field_list,'_','wildcard','all');
if (!is_array($user_database_link_rec_list))
{
$date = date('Y-m-d H:i:s');
echo"Error #31: $date<br>\n";
echo "Error in call to tableQuery from insert_maps.php<br>\n";
echo "$user_database_link_rec_list<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to tableQuery
if (sizeof($user_database_link_rec_list) > '0')
{
foreach ($user_database_link_rec_list as $user_database_link_rec)
{
$user_id = $user_database_link_rec['user_id'];
$field_list = array('user_id');
$user_rec_list = getUser($user_id);
if (!is_array($user_rec_list))
{
$date = date('Y-m-d H:i:s');
echo "Error #32: $date<br>\n";
echo "Error in call to getUser from insert_maps.php<br>\n";
echo "$user_rec_list<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to getUser
if (sizeof($user_rec_list) == '0') {continue;}
elseif (sizeof($user_rec_list) == '1') {$user_rec = $user_rec_list[0];}
else
{
$date = date('Y-m-d H:i:s');
echo "Error #33: $date<br>\n";
echo "Tried to send an update email to user id $user_id, but when queried the user table \n";
echo "with that id multiple records were returned.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in data integrity
$user_email_address = $user_rec['user_email'];
$ret_val = mail($user_email_address,$subject,$message,$header);
#if (!$ret_val) {echo "Unable to send an update email to user $user_rec[user_name]<br>\n";}
#else {echo "Sent an update email to user $user_name<br>\n";}
}#end foreach user associated with database
}#end if have users associated with database
echo "Finished sending update emails to all the users<br>\n";
}#end else inserted all markers
}#end have web marker information
#6 Determine if file upload from users computer or on server
if (($insert_from_file == '1') and
(!preg_match('/^\d+$/',$upload_file)) and
(!preg_match('/\w+/',$map_name)) and
((!is_uploaded_file($_FILES['map_file']['tmp_name'])) and (!preg_match('/\w+/',$file_on_server)))
)
{
echo "Please select whether you would like to upload a file from your computer, or \n";
echo "select a file that is already in your 'Uploaded Data' directory on the server.<br><br>\n";
echo "<form action=\"insert_maps.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"$insert_from_file\">\n";
echo "<table align=\"center\">\n";
echo "<tr><td><input type=\"radio\" name=\"upload_file\" value=\"0\" checked></td><td>Select File in 'Uploaded Data' directory</td></tr>\n";
echo "<tr><td><input type=\"radio\" name=\"upload_file\" value=\"1\"></td><td>Upload File from Your Computer</td></tr>\n";
echo "</table>\n";
echo "<br>\n";
echo "<table align=\"center\">\n";
echo "<tr>\n";
echo "<td><input type=\"submit\" name=\"submit\" value=\"Select File to Insert From\"></td>\n";
echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Method to Select File\"></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "<br><br>\n";
printLogOutButton('insert_maps.php');
}#end if determine if file upload from users computer or on server
#7 Want to insert from file
if (($insert_from_file == '1') and
(preg_match('/^\d+$/',$upload_file)) and
(!preg_match('/\w+/',$map_name)) and
((!is_uploaded_file($_FILES['map_file']['tmp_name'])) and (!preg_match('/\w+/',$file_on_server)))
)
{
if ($upload_file == 1){echo "<form action=\"insert_maps.php\" enctype = \"multipart/form-data\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";}
else {echo "<form action=\"insert_maps.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";}
echo "This form will allow you to upload a file that contains the markers and their distancte information \n";
echo "for the map you would like to insert into the database.<br>\n";
echo "There are two different file formats that are allowed.<br><br>\n";
echo "<b>File Format 1:</b> \n";
echo "This file format is very similar to the map file used as input into the Mega2 software. The file contains 3 columns of data separated \n";
echo "by commas. The first column is the chromosome of the marker on a line. The second column is the relative position of the marker in \n";
echo "centiMorgans. The last column is the Marker Name to be inserted into the map. The file can contain markers from different chromosomes \n";
echo "as long as the markers are ordered along a chromosome. <b>Do not</b> place a blank line between markers on different chromosomes. \n";
echo "The important differences from Mega2 map input \n";
echo "files is that it is <b>comma-separated</b> and there is <b>No Header line</b>.<br>\n";
echo "(Click <a href=\"../data/test_data/example_mega2_map_file.txt\" target=\"_blank\">here</a> for an example file.)<br><br>\n\n";
echo "<b>File Format 2:</b> \n";
echo "This file format is similar to the format of a comma-separated map file for the Mendel software. \n";
echo "The file consists of alternating lines of \n";
echo "Marker Names and its Chromosome separated by a comma, followed by a line with a distance value for the distance separating \n";
echo "the two markers on the line above and below the distance value. The file can contain markers from different chromosomes \n";
echo "as long as the markers are ordered along a chromosome. For the distance line between\n";
echo "two markers that are on different chromosomes <b>leave the line blank</b>.\n";
echo "The first and last line of the file <br>must</b> be a marker name.<br>\n";
echo "(Click <a href=\"../data/test_data/example_mendel_map_file.txt\" target=\"_blank\">here</a> for an example file.)<br><br>\n\n";
echo "It is important to note that all the markers in file must already have been inserted into the database.<br>\n";
echo "This utility is only to link markers together in maps, <b>NOT</b> to insert markers into the database.<br>\n";
echo "Please input :<br>\n";
echo "(1) the name of the map you are creating<br>\n";
echo "(2) how distance is measured between adjacent markers<br>\n";
echo "(3) a description of the map<br>\n";
echo "(4) the name of the file that contains the map information<br>\n";
echo "(5) the file format your file is in.<br>\n";
echo "Required fields are in <b>bold</b>.<br>\n";
echo "Upload your file below:<br><br>\n";
echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"1\">\n";
echo "<input type=\"hidden\" name=\"upload_file\" value=\"$upload_file\">\n";
if ($upload_file == 1){echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"1000000000\">\n";}
echo "<table align=\"center\">\n";
echo "<tr><td><b>Map Name</b>:</td><td><input type=\"text\" name=\"map_name\" maxlength=\"52\" size=\"20\"></td></tr>\n";
echo "<tr><td><b>Number of Markers in Map</b>:</td><td><input type=\"text\" name=\"num_markers\" maxlength=\"11\" size=\"20\"></td></tr>\n";
echo "<tr><td><b>Distance Measure</b>:</td><td><select name=\"dist_measure\">\n";
echo "<option>Select a Distance Measure\n";
echo "<option value=\"base_pairs\">Base Pairs\n";
echo "<option value=\"haldane_centiMorgans\">Haldance centiMorgans\n";
echo "<option value=\"kosambi_centiMorgans\">Kosambi centiMorgans\n";
echo "<option value=\"recombination_fraction\">Recombination Fraction\n";
echo "</select>\n";
echo "</td></tr>\n";
echo "<tr><td>Map Description:</td><td><textarea name=\"map_description\" cols=\"50\" rows=\"10\" wrap=\"virtual\"></textarea></td></tr>\n";
if ($upload_file == 1){echo "<tr><td><b>File Name</b>:</td><td><input type=\"file\" name=\"map_file\" size=\"20\"></td></tr>\n";}
else #file on server
{
echo "<tr><td><b>File Name</b>:</td><td align=\"left\">\n";
$jscript = printFileSelectList($_SESSION[user_name],$database_id,'upload');
if ($jscript == '0')
{
echo "</td></tr>\n";
echo "</table>\n";
echo "</form>\n";
echo "<br><br>\n";
$date = date('Y-m-d H:i:s');
echo "Error #34: $date<br>\n";
echo "Error in call to printFileSelectList from insert_maps.php<br>\n";
echo "Please see the above error message from printFileSelectList for an explanation.<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link \n";
echo "on the left navigation bar.\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to printFileSelectList
if ($jscript == '1')
{
echo "</td></tr>\n";
echo "</table>\n";
echo "</form>\n";
echo "<br><br>\n";
$date = date('Y-m-d H:i:s');
echo "<center>\n";
echo "<b>There are no files stored for user $_SESSION[user_name] in their uploaded_data directory.</b><br>\n";
echo "This means either you have not transfered the file to the server, or the file has expired and has been \n";
echo "deleted from the server.<br> So you need to either transfer the file you are looking for to the server and put it \n";
echo "in your uploaded_data directory, or upload the file directly from your computer in order to insert the data.<br><br>\n";
echo "</center>\n\n";
$ret_val = printLogOutButton('insert_maps.php');
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if no files in users directory to upload
echo "</td></tr>\n";
}#end if file on server
echo "<tr><td> </td><td><input type=\"radio\" name=\"file_format\" value=\"mega2\"> Comma-Separated Mega2 Format</td></tr>\n";
echo "<tr><td> <td><input type=\"radio\" name=\"file_format\" value=\"mendel\"> Comma-Separated Mendel Format</td></tr>\n";
echo "</table>\n";
echo "<br><br>\n";
echo "<center><input type=\"submit\" name=\"submit\" value=\"Insert Map\"></center>\n";
echo "</form>\n";
echo "<br><br>\n\n";
$ret_val = printLogOutButton('insert_maps.php');
echo "\n\n";
if ($upload_file == 0)
{
$jscript = preg_replace('/^\{
/','',$jscript);
$jscript = preg_replace('/ else \{return true;\}
\}/','',$jscript);
}#end if file on server
echo "<script>\n";
echo "function Validate(form)\n";
echo "{\n";
echo " if (form.map_name.value == \"\"){\n";
echo " alert(\"You must enter a Map Name\");\n";
echo " form.map_name.focus();\n";
echo " return false;}\n";
echo " if (form.num_markers.value == \"\"){\n";
echo " alert(\"You must enter the Number of Markers in the Map\");\n";
echo " form.num_markers.focus();\n";
echo " return false;}\n";
echo " if (form.dist_measure.selectedIndex == \"0\"){\n";
echo " alert(\"You must select a Distance Measure\");\n";
echo " form.dist_measure.focus();\n";
echo " return false;}\n";
if ($upload_file == 1)
{
echo " if (form.map_file.value == \"\"){\n";
echo " alert(\"You must upload a File\");\n";
echo " form.map_file.focus();\n";
echo " return false;}\n";
}#end if file upload
else {echo "$jscript\n";} #file on server
echo " var rv = false;\n";
echo " for (var i=0; i < form.file_format.length; i++){\n";
echo " if (form.file_format[i].checked){rv = true;}}\n";
echo " if (!rv){\n";
echo " alert(\"You must select the file format of the file\");\n";
echo " return false;}\n";
echo " return true;\n";
echo "}\n";
echo "</script>\n";
}#end get file
#8 Get file, parse and insert
if ((($insert_from_file == '1') and
($upload_file == '0') and
(preg_match('/\w+/',$map_name)) and
(preg_match('/\w+/',$file_on_server))) or
(($insert_from_file == '1') and
($upload_file == '1') and
(preg_match('/\w+/',$map_name)) and
(is_uploaded_file($_FILES['marker_file']['tmp_name'])))
)
{
echo "Now inserting the markers and distance information in the file into your map.<br>\n";
echo "Depending on the size of the file you uploaded, this could take a few minutes. Please be patient.<br><br>\n";
if ($upload_file == 1)
{
$rand_num = rand();
$file_name = "map_marker_data_upload_".$rand_num.".txt";
$ret_val = move_uploaded_file($_FILES['map_file']['tmp_name'],"../data/uploaded_data/$_SESSION[user_name]/$file_name");
if (!$ret_val)
{
$date = date('Y-m-d H:i:s');
echo "Error #35: $date<br>\n";
echo "Error in call to insert_maps.php<br>\n";
echo "Unable to move uploaded file inorder to read it.<br><br>\n";
echo "Please email the above error message using the Comments/Help/Support link on \n";
echo "the left navigation bar.\n";
echo "</body></html>\n";
fclose($log_file_handle);
chmod("../data/$tmp_log_file",0777);
$ret_val = disconnect();
exit;
}#end if (!ret_val)
$uploaded_file_name = "../../data/uploaded_data/$_SESSION[user_name]/$file_name";
$uploaded_file_size = @filesize("../data/uploaded_data/$_SESSION[user_name]/$file_name");
if (!$uploaded_file_size)
{
$command = "stat -c %s ../data/uploaded_data/$_SESSION[user_name]/$file_name";
exec($command,$output_array);
$uploaded_file_size = $output_array[0];
}#end if file too big for php
}#end if file uploaded from users computer
else #file was on server
{
$uploaded_file_name = "../"."$file_on_server";
$uploaded_file_size = @filesize("$file_on_server");
if (!$uploaded_file_size)
{
$command = "stat -c %s $file_on_server";
exec($command,$output_array);
$uploaded_file_size = $output_array[0];
}#end if file too big for php
}#end else file on server
$ret_val = chdir("../bin/database_query_scripts/");
if (!$ret_val)
{
$date = date('Y-m-d H:i:s');
echo "Error #36: $date<br>\n";
echo "Error in call to chdir from insert_maps.php<br>\n";
echo "Unable to change directory to database query scripts directory<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to chdir
if ($uploaded_file_size > $max_file_size_to_wait_to_run)
{
$background_flag = 1;
if ($uploaded_file_size >= $max_file_size_not_to_split)
{
$found = preg_match('/^(.+\/)([^\/]+)$/',$uploaded_file_name,$captured);
$path = $captured[1];
$file_name = $captured[2];
$sub_file_name = "sub_"."$file_name";
$prefix = "$path"."$sub_file_name";
$prefix = preg_replace('/\.\w+$/','_',$prefix);
$command = "split -a5 --lines=$num_lines_to_split_files $uploaded_file_name $prefix";
if (sizeof($output_array) > '0'){$output_array = array();}
exec($command,$output_array,$return_code);
if ($return_code)
{
$err_msg = implode("\n",$output_array);
$date = date('Y-m-d H:i:s');
echo "Error #37: $date<br>\n";
echo "Error in call to exec to split file from insert_maps.php<br>\n";
echo "$err_msg<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</form>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if error in exec call to split file
$file_array = glob("$prefix*");
$num_files_split = sizeof($file_array);
sort($file_array);
foreach ($file_array as $key=>$file)
{
$command = "./insert_data_into_database.php '$uploaded_file_name' '$database_id' 'Insert Genes' '$_SESSION[user_name]' '$background_flag' ";
$command.= "'$map_name' '$num_markers' '$dist_measure' '$map_desc' '$file_format' >> dummy_insert_data_background_output.txt &";
$last_line = system($command,$sys_ret_val);
if ($sys_ret_val != '0')
{
$date = date('Y-m-d H:i:s');
echo "Error #38: $date<br>\n";
echo "Error in system call to insert_data_into_database.php from insert_maps.php<br>\n";
echo "The return value of the sytem call was $sys_ret_val<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</form>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if error in system call
}#end foreach sub file
}#end if file too big for php
else #don't need to split file
{
$command = "./insert_data_into_database.php '$uploaded_file_name' '$database_id' 'Insert Genes' '$_SESSION[user_name]' '$background_flag' ";
$command.= "'$map_name' '$num_markers' '$dist_measure' '$map_desc' '$file_format' >> dummy_insert_data_background_output.txt &";
$last_line = system($command,$sys_ret_val);
if ($sys_ret_val != '0')
{
$date = date('Y-m-d H:i:s');
echo "Error #39: $date<br>\n";
echo "Error in system call to insert_data_into_database.php from insert_maps.php<br>\n";
echo "The return value of the sytem call was $sys_ret_val<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</form>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if error in system call
}#end else don't need to split file
}#end if run in background
else #run in foreground
{
$background_flag = 0;
$command = "./insert_data_into_database.php '$uploaded_file_name' '$database_id' 'Insert Maps' '$_SESSION[user_name]' '$background_flag' ";
$command.= "'$map_name' '$num_markers' '$dist_measure' '$map_desc' '$file_format'";
passthru($command,$sys_ret_val);
if ($sys_ret_val != '0')
{
echo "<br><br>\n";
$date = date('Y-m-d H:i:s');
echo "Error #40: $date<br>\n";
echo "Error in system call to insert_data_into_database.php from insert_maps.php<br>\n";
echo "See the error message above for an explanation of the error<br><br>\n";
echo "Please email the above error using the Comments/Help/Support link on the \n";
echo "left navigation bar.\n";
echo "</form>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if error in system call
}#end else run in foreground
$ret_val = chdir("../../cgi-bin/");
if (!$ret_val)
{
$date = date('Y-m-d H:i:s');
echo "Error #41: $date<br>\n";
echo "Error in call to chdir from insert_maps.php<br>\n";
echo "Unable to change directory to cgi-bin directory<br><br>\n";
echo "Please email the above error using Comments/Help/Support link \n";
echo "in the left navigation bar.<br>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end error in call to chdir
if ($background_flag)
{
echo "<center>\n";
echo "<b>The data you uploaded is being inserted into the database.<br>\n";
if ($uploaded_file_size >= $max_file_size_not_to_split)
{
echo "The file was too large to handle, so it was split into $num_files_split sub files, and each \n";
echo "sub file is being processed separately. So you will recieve an email when the data in each sub file \n";
echo "has been inserted into the database \n";
}#end if split file
else {echo "You will receive an email when all the data has been inserted into the database \n";}
echo "detailing the results of the file upload and any errors.</b><br><br>\n";
echo "</center>\n";
echo "</body></html>\n";
$ret_val = disconnect();
exit;
}#end if running in background
if (!$background_flag){echo "Finished sending update emails to all the users<br>\n";}
}#end parse file and insert
}#end if auth_user
$ret_val = disconnect();
echo "</body></html>\n";
?>