Location: PHPKode > projects > Generic Genetic Studies Database > ggsd-2.19/cgi-bin/unlink_marker_to_map.php
<?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	unlink_marker_to_map.php

#TYPE	PHP

#USES	sql_lib(authenticate updateRecord tableQuery deleteMapMarkerLink)
#	html_lib(printMapSelectList printMarkerSelectList)

#PURPOSE	To give the user a form to delete one or more markers from a map from the database.

#USAGE	unlink_marker_to_map.php

#PARAMETERS
#	authorized user			auth_user		int	optional
#	map id				map_id			int	optional
#	marker id			marker_id		int	optional
#	distance from prev marker	dist_prev_marker	float	optional
#	map marker link id		map_marker_link_id	int	optional


#RETURNS
#	success message			string	if success
#	error message			string	if error

#DATASTRUCTURES No special databastructures.

#FUNCTIONAL DESCRIPTION Authenticate the user.  Then determine which map want to alter.  The get the marker to delete from map.
#		        Get the data and delete marker from map and update other markers in map.  Get updated distance info.

#ASSUMPTIONS  The user knows that deleteing a marker from a map will not delete the marker from the database.

#LIMITATIONS No way to check, besides telling the user on the form.

#COMMENTS

#PRELIMINARY DESIGN
#	    1	Get Args
#	    2	Authenticate
#	    3	Determine which map to alter
#	    4	Determine which marker to delete
#	    5	Delete and update rest of markers



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>Delete Marker from a Map</title>\n";
echo "</head>\n";
echo "<body bgcolor=\"white\">\n";
echo "<center><h3>Delete Marker from a  Map</h3></center>\n";
echo "<hr>\n";
printHomeNavBar();
echo "<br>\n";


#1     Get Args
$access_type_required  = 'administrative';
$map_id                = $_POST['map_id'];
$marker_id             = $_POST['marker_id'];
$map_marker_link_id    = $_POST['map_marker_link_id'];
$dist_prev_marker      = $_POST['dist_prev_marker'];

$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 unlink_marker_to_map.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 user
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 unlink_marker_to_map.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 unlink_marker_to_map.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('unlink_marker_to_map.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 unlink_marker_to_map.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 unlink_marker_to_map.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 unlink_marker_to_map.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 unlink_marker_to_map.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('unlink_marker_to_map.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 unlink_marker_to_map.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('unlink_marker_to_map.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 unlink_marker_to_map.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 unlink_marker_to_map.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	Determine which Map to alter
    if ((!preg_match('/^\d+$/',$map_id)) and
        (!preg_match('/^\d+$/',$marker_id)) and 
        (!preg_match('/^\d+$/',$map_marker_link_id)) and
        (!preg_match('/\d+/',$dist_prev_marker))
       )
      {
        echo "This form will allow you to delete a marker from a  map record and update the rest of the \n";
        echo "information in the altered map.<br><br>\n";
        echo "Please select which Map you want to delete a marker from:<br><br>\n";

        echo "<form action=\"unlink_marker_to_map.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
        echo "<table align=\"center\">\n";
        echo "<tr>\n";
        echo "<td>Map Name:</td>\n";
        echo "<td>\n";
        $jscript = printMapSelectList($database_id);
        if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #11: $date<br>\n";
            echo "Error in call to printMapSelectList from unlink_marker_to_map.php<br>\n";
            echo "See the error message from printMapSelectList above for an explanation.<br><br>\n";
            echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	    echo "navigation bar\n";
            echo "</td></tr>\n";
            echo "</table>\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in call to printMapSelectList
        if ($jscript == 'no_defined_maps')
          {
            $date = date('Y-m-d H:i:s');
            echo "</td></tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "This statement generated at $date<br><br>\n";
            echo "</form>\n";

            $ret_val = printLogOutButton('unlink_marker_to_map.php');
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
        echo "</table>\n";
        echo "<center>\n";
        echo "<input type=\"submit\" name=\"submit\" value=\"Delete Marker from Map\">\n";
        echo "<input type=\"reset\" name=\"reset\" value=\"Reset Map Name\">\n";
        echo "</form>\n";
        echo "</center>\n";
        echo "<br><br>\n";

        $ret_val = printLogOutButton('unlink_marker_to_map.php');
        echo "\n\n";
                                                                                                    
        echo "<script>\n";
        echo "function Validate(form)\n";
        echo "$jscript\n";
        echo "</script>\n";
      }#end if don't have map id

    #4	Determine which Marker to delete from map
    if ((preg_match('/^\d+$/',$map_id)) and
        (!preg_match('/^\d+$/',$marker_id)) and 
        (!preg_match('/^\d+$/',$map_marker_link_id)) and
        (!preg_match('/\d+/',$dist_prev_marker))
       )
      {
        $field_list_array = array('map_id');
        $map_rec_list = tableQuery('map_table',$database_id,$field_list_array,$map_id,'equal','all');
        if (!is_array($map_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #12: $date<br>\n";
            echo "Error in call to tableQuery from unlink_marker_to_map.php<br>\n";
            echo "$map_rec_list<br><br>\n";
            echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	    echo "navigation bar\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if error in call to tableQuery
        if (sizeof($map_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "The map you wanted to update is no longer in the database.<br>\n";
            echo "This message was generated at $date.<br>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if no map
        if (sizeof($map_rec_list) == '1'){$map_rec = $map_rec_list[0];}
        else #error in data integrity
          {
            $date = date('Y-m-d H:i:s');
	    echo "Error #13: $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 unlink_marker_to_map.php<br>\n";
	    echo "The user wants to delete a marker from map with map_id $map_id in database_id $database_id, \n";
	    echo "but when query the map table with that data 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
        $map_name = $map_rec['map_name'];

        $map_marker_link_rec_list = tableQuery('map_marker_link_table',$database_id,$field_list_array,$map_id,'equal','all','marker_number_in_map');
        if (!is_array($map_marker_link_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #14: $date<br>\n";
            echo "Error in call to tableQuery from unlink_marker_to_map.php<br>\n";
            echo "$map_marker_link_rec_list<br><br>\n";
            echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	    echo "navigation bar\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if error in call to tableQuery
        if (sizeof($map_marker_link_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "The map you wanted to update does not have any markers associated with it in the database.<br>\n";
            echo "This message was generated at $date.<br><br>\n";

            echo "A Map can't exist without having Markers associated with it, so Map $map_name will now be deleted \n";
            echo "from the database.<br>\n";

            $rand = rand();
            $tmp_flog_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 #15: $date<br>\n";
                echo "Error in call to fopen from unlink_marker_to_map.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 = "";

            $ret_val = deleteMap($map_rec['map_id']);
            if ($ret_val != '1')
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #16: $date<br>\n";
                echo "Error in call to deleteMap from unlink_marker_to_map.php<br>\n";
                echo "$ret_val<br><br>\n";
                echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	        echo "navigation bar\n";
                echo "</body></html>\n";
                $ret_val = disconnect();
                exit;
              }#end if error in call to deleteMap
            $date = date('Y-m-d H:i:s');
            $log_file_statement.= "$date:\t$_SESSION[user_name] Deleted Map ID $map_rec[map_id] from Database ID $database_id ";
            $log_file_statement.= "because it no longer had any markers associated with it.\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);
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if no map

        echo "Please select which Marker you want to delete from the Map $map_name.<br><br>\n";

        echo "<form action=\"unlink_marker_to_map.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
        echo "<table align=\"center\">\n";
        echo "<tr>\n";
        echo "<td>Marker Name:</td>\n";
        echo "<td><select name=\"marker_id\">\n";
        echo "<option>Select a Marker\n";
        foreach ($map_marker_link_rec_list as $map_marker_link_rec)
          {
            $marker_id = $map_marker_link_rec['marker_id'];
            $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 #17: $date<br>\n";
                echo "Error in call to distinctTableQuery from unlink_marker_to_map.php<br>\n";
                echo "$marker_name_array<br><br>\n";
                echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	        echo "navigation bar\n";
                echo "</body></html>\n";
                $ret_val = disconnect();
                exit;
              }#end if error in call to distinctTableQuery
            if (sizeof($marker_name_array) == '0'){continue;}
            if (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 #18: $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 unlink_marker_to_map.php<br>\n";
	        echo "The user wants to delete marker_id $marker_id from map with map_id $map_id in database_id $database_id, \n";
	        echo "but when query the marker table with that data 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
            echo "<option value=\"$marker_id\">$marker_name\n";
          }#end foreach marker in map
        echo "</select></td>\n";
        echo "</tr>\n";
        echo "</table>\n\n";

        echo "<input type=\"hidden\" name=\"map_id\" value=\"$map_id\">\n";
        echo "<center>\n";
        echo "<input type=\"submit\" name=\"submit\" value=\"Delete Marker from Map\">\n";
        echo "<input type=\"reset\" name=\"reset\" value=\"Reset Marker Name\">\n";
        echo "</form>\n";
        echo "</center>\n";
        echo "<br><br>\n";

        $ret_val = printLogOutButton('unlink_marker_to_map.php');
        echo "\n\n";
                                                                                                    
        echo "<script>\n";
        echo "function Validate(form)\n";
        echo "{\n";
        echo "  if (form.marker_id.selectedIndex == '0'){\n";
        echo "    alert(\"You must select a Marker to Delete\");\n";
        echo "    form.marker_id.focus();\n";
        echo "    return false;}\n";
        echo "  return true;\n";
        echo "}\n";
        echo "</script>\n";        
      }#end if have map but not marker

    #5	Delete marker and update rest of markers in map
    if ((preg_match('/^\d+$/',$map_id)) and
        (preg_match('/^\d+$/',$marker_id)) and
        (!preg_match('/^\d+$/',$map_marker_link_id)) and
        (!preg_match('/\d+/',$dist_prev_marker))
       )
      {
        $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 #19: $date<br>\n";
            echo "Error in call to fopen from unlink_marker_to_map.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_list_array = array('map_id');
        $map_rec_list = tableQuery('map_table',$database_id,$field_list_array,$map_id,'equal','all');
        if (!is_array($map_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #20: $date<br>\n";
            echo "Error in call to compoundTableQuery from unlink_marker_to_map.php<br>\n";
            echo "$map_marker_link_rec_list<br><br>\n";
            echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	    echo "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 error in call to tableQuery
        if (sizeof($map_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "The map you want to update is no longer in the database.<br>\n";
            echo "This message was generated at $date<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 no map
        elseif (sizeof($map_rec_list) == '1'){$map_rec = $map_rec_list[0];}
        else #error in data integrity
          {
            $date = date('Y-m-d H:i:s');
	    echo "Error #21: $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 unlink_marker_to_map.php<br>\n";
	    echo "The user wants to delete a marker from map with map_id $map_id in database_id $database_id, \n";
	    echo "but when query the map table with that data 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
        $map_name             = $map_rec['map_name'];
        $num_markers_in_map   = $map_rec['num_markers_in_map'];
        $map_distance_measure = $map_rec['distance_measure'];

        $field_list_array = array('map_id','marker_id');
        $term_array       = array($map_id,$marker_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 #22: $date<br>\n";
            echo "Error in call to compoundTableQuery from unlink_marker_to_map.php<br>\n";
            echo "$map_marker_link_rec_list<br><br>\n";
            echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	    echo "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 error in call to compoundTableQuery
        if (sizeof($map_marker_link_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "The marker you wanted to delete is no longer in the map.<br>\n";
            echo "This message generated at $date.<br>\n";
            echo "</body></html>\n";
            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;
          }
        if (sizeof($map_marker_link_rec_list) == '1')
          {
            $map_marker_link_rec       = $map_marker_link_rec_list[0];
            $delete_map_marker_link_id = $map_marker_link_rec['map_marker_link_id'];
            $delete_marker_num_in_map  = $map_marker_link_rec['marker_number_in_map'];
            $delete_marker_chr         = $map_marker_link_rec['chromosome'];
            $delete_dist_from_prev     = $map_marker_link_rec['distance_from_previous_marker'];
            unset($map_marker_link_rec,$map_marker_link_rec_list);
          }
        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 unlink_marker_to_map.php<br>\n";
	    echo "The user wants to delete marker_id $marker_id from map with map_id $map_id in database_id $database_id, \n";
	    echo "but when query the map marker link table with that data 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

        $ret_val = deleteMapMarkerLink($delete_map_marker_link_id);
        if ($ret_val != '1')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #24: $date<br>\n";
            echo "Error in call to deleteMapMarkerLink from unlink_marker_to_map.php<br>\n";
            echo "$ret_val<br><br>\n";
            echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	    echo "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 error in call to deleteMapMarkerLink
        $date = date('Y-m-d H:i:s');
        $log_file_statement.= "$date:\t$_SESSION[user_name] Deleted Map Marker Link ID $delete_map_marker_link_id from Database ID $database_id ";
        $log_file_statement.= "that linked Map ID $map_rec[map_id] and Marker ID $marker_id.\n";

        if ($delete_marker_num_in_map != $num_markers_in_map)
          {
            $field_list_array = array('map_id','marker_number_in_map');
            $term_array       = array($map_id,$delete_marker_num_in_map);
            $mode_array       = array('equal','greater_than');
            $map_marker_link_rec_list = compoundTableQuery('map_marker_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all','marker_number_in_map');
            if (!is_array($map_marker_link_rec_list))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #25: $date<br>\n";
                echo "Error in call to compoundTableQuery from unlink_marker_to_map.php<br>\n";
                echo "$map_marker_link_rec_list<br><br>\n";
                echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	        echo "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 if error in call to compoundTableQuery
            foreach ($map_marker_link_rec_list as $map_marker_link_rec)
              {
                $new_map_location = $map_marker_link_rec['marker_number_in_map'] - 1;
                if ($new_map_location == $delete_marker_num_in_map)
                  {
                    if ($delete_marker_chr == $map_marker_link_rec['chromosome'])
                      {
                        if ($delete_dist_from_prev == 0){$new_dist_from_prev = $delete_dist_from_prev;}
                        else #not replacing the first marker
                          {
                            if ($map_distance_measure == 'recombination_fraction')
                              {
                                $delete_dist_from_prev_log_component = 1 - (2 * $delete_dist_from_prev);
                                if ($delete_dist_from_prev_log_component <= 0)
                                  {
                                    $delete_dist_from_prev_map_unit = 18;
                                  }#end if can't take log
                                else #can take log
                                  {
                                    $delete_dist_from_prev_map_unit = (-0.5)*(log($dist_from_prev_log_component));
                                  }#end else can take log

                                $update_dist_from_prev_log_component = 1 - (2*$map_marker_link_rec['distance_from_previous_marker']);
                                if ($update_dist_from_prev_log_component <= 0)
                                  {
                                    $update_dist_from_prev_map_unit = 18;
                                  }#end if can't take log of 0
                                else #can take log
                                  {
                                    $update_dist_from_prev_map_unit = (-0.5)*(log($update_dist_from_prev_log_component));
                                  }#end else can take log

                                $new_map_unit = $delete_dist_from_prev_map_unit + $update_dist_from_prev_map_unit;
                                $new_dist_from_prev = (0.5)*(1 - exp(-2*$new_map_unit));
                              }#end if distance measure is recomb fraction, need to convert to genetic dist and then back
                            else #map distance that is additive
                              {
                                $new_dist_from_prev = $dist_from_prev + $map_marker_link_rec['distance_from_previous_marker'];
                              }#end else map distance that is additive
                          }#end else have to calculate new distance
                        $update_field_array = array('marker_number_in_map','distance_from_previous_marker');
                        $update_value_array = array($new_map_location,$new_dist_from_prev);
                      }#end if deleted marker and marker replacing on same chr
                    else #deleted marker and one replacing it on different chr
                      {
                        $update_field_array = array('marker_number_in_map');
                        $update_value_array = array($new_map_location);
                      }#end else deleted marker and one replacing it on different chr

                    $ret_val = updateRecord('map_marker_link_table',$map_marker_link_rec['map_marker_link_id'],$update_field_array,$update_value_array);
                    if ($ret_val != '1')
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #26: $date<br>\n";
                        echo "Error in call to updateRecord from unlink_marker_to_map.php<br>\n";
                        echo "$ret_val<br><br>\n";
                        echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	                echo "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 if error in call to updateRecord
                    $log_file_statement.= "$date:\t$_SESSION[user_name] Updated Map Marker Link ID $map_marker_link_rec[map_marker_link_id] ";
                    $log_file_statement.= "from Database ID for Map ID $map_marker_link_rec[map_id] and ";
                    $log_file_statement.= "Marker ID $map_marker_link_rec[marker_id], set marker_number_in_map to $new_map_location ";
                    $log_file_statement.= "from $map_marker_link_rep[marker_number_in_map] ";
                    if ($delete_marker_chr == $map_marker_link_rec['chromosome'])
                      {
                        $log_file_statement.= "AND set distance_from_previous_marker to ";
                        $log_file_statement.= "$new_dist_from_prev from $map_marker_link_rec[distance_from_previous_marker].\n";
                      }#end if deleted marker and one replacing it on same chr
                    else {$log_file_statement.= "\n";}
                  }#end if current marker taking place of deleted marker
                else #not replacing deleted marker
                  {
                    $update_field_array = array('marker_number_in_map');
                    $update_value_array = array($new_map_location);
                    $ret_val = updateRecord('map_marker_link_table',$map_marker_link_rec['map_marker_link_id'],$update_field_array,$update_value_array);
                    if ($ret_val != '1')
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #27: $date<br>\n";
                        echo "Error in call to updateRecord from unlink_marker_to_map.php<br>\n";
                        echo "$ret_val<br><br>\n";
                        echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	                echo "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 if error in call to updateRecord
                    $date = date('Y-m-d H:i:s');
                    $log_file_statement.= "$date:\t$_SESSION[user_name] Updated Map Marker Link ID $map_marker_link_rec[map_marker_link_id] ";
                    $log_file_statement.= "from Database ID, set marker_number_in_map to $new_map_location ";
                    $log_file_statement.= "from $map_marker_link_rep[marker_number_in_map].\n";
                  }#end else not replacing deleted marker
              }#end foreach map marker link
          }#end if deleted marker wasn't the last marker in map

        $new_num_markers_in_map = $num_markers_in_map - 1;
        if ($new_num_markers_in_map == 0)
          {
            $ret_val = deleteMap($map_id);
            if ($ret_val != '1')
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #28: $date<br>\n";
                echo "Error in call to unlink_marker_to_map.php<br>\n";
                echo "$ret_val<br><br>\n";
                echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	        echo "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 if error in call to deleteMap
            $date = date('Y-m-d H:i:s');
            $log_file_statement.= "$date:\t$_SESSION[user_name] Deleted Map ID $map_id from Database ID $database_id ";
            $log_file_statement.= "because it no longer had any markers associated with it.\n";
            echo "The marker has successfully been deleted from Map $map_name.<br>\n";
            echo "This resulted in no Markers being associated with Map $map_name, so it was also deleted from the database<br><br>\n";
          }#end if no more markers in map
        else #still have markers in map
          {
            $update_field_array = array('num_markers_in_map');
            $update_value_array = array($new_num_markers_in_map);
            $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 #29: $date<br>\n";
                echo "Error in call to updateRecord from unlink_marker_to_map.php<br>\n";
                echo "$ret_val<br><br>\n";
                echo "Please email the above error message using the Comments/Help/Support link on the left \n";
	        echo "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 if 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 from Database ID $database_id, ";
            $log_file_statement.= "set num_markers_in_map to $new_num_markers_in_map from $num_markers_in_map.\n";
            echo "The marker has successfully been deleted from Map $map_name.<br>\n";
            echo "All the information foreach Marker still in Map $map_name has also been updated.<br><br>\n";
          }#end else still have markers in map

        $ret_val = printLogOutButton('unlink_marker_to_map');
        echo "\n\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);
      }#end if have map and marker, delete and update
  }#end if auth_user
$ret_val = disconnect();
echo "</body></html>\n";
?>
Return current item: Generic Genetic Studies Database