Location: PHPKode > projects > Generic Genetic Studies Database > ggsd-2.19/cgi-bin/link_pedigrees_to_pedigree_groups.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 'html_lib.inc';
require 'sql_lib.inc';
require 'file_system_lib.inc';

#NAME	link_pedigrees_to_pedigree_groups.php

#TYPE	PHP

#USES	html_lib.inc(??)
#	sql_lib.inc(authenticate existsValue insertPedigreePedigreeGroupLink)

#PURPOSE To get pedigree data to insert into a pedigree group in the database

#USAGE	link_pedigrees_to_pedigree_groups.php

#PARAMETERS
#	authorized user		auth_user		int	optional
#	insert_from_file	insert_from_file	int	optional
#       pedigree group id       ped_group_id            int     optional
#       pedigree table id       ped_id                  list    optional
	

#RETURNS
#	nothing

#DATASTRUCTURES No special datastructures

#FUNCTIONAL DESCRIPTION The script will authenticate the user.  Then determine
#		       whether they want to insert from a file or from the web
#		       site.  If from a file, parse the file and insert pedigree pedigre group links.
#		       If from website, give them a form to fill out and insert the
#		       values they supply.

#ASSUMPTIONS

#LIMITATIONS

#COMMENTS

#PRELIMINARY DESIGN
#	    1	Get Args
#	    2	Authenticate User
#	    3	Determine Whether they want to upload file
#	    4	If from Website give form
#	    5	Insert Pedigree Pedigree Group Link from webform
#	    6	Get File
#	    7	Parse File 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>Populate Pedigree Groups</title>\n";
echo " </head>\n";
 
echo "<body bgcolor = \"white\">\n";
echo "<center><h3>Populate Pedigree Groups</h3></center>\n";
echo "<hr>\n";
printHomeNavBar();
echo "<br>\n";


#1    Get Args
$access_type_required  = 'administrative';
$DEBUG                 = '0';
$start_time            = date('Y-m-d H:i:s');
$insert_from_file      = $_POST['insert_from_file'];
$upload_file           = $_POST['upload_file'];
$file_on_server        = $_POST['file_name'];
$ped_group_id          = $_POST['pedigree_group_id'];
$ped_table_id          = $_POST['pedigree_id'];       #Value from printPedigreeSelectList
$ped_ids               = $_POST['ped_id'];            #Value from printPedigreeCheckList
$ped_multiple_pages    = $_POST['ped_multiple_pages'];#Binary let knowing if more pedigrees to look at
$function_to_call      = $_POST['function_to_call'];
$table_name            = $_POST['table_name'];
$field_list            = $_POST['field_list'];
$term                  = $_POST['term'];
$mode                  = $_POST['mode'];
$order_by              = $_POST['order_by'];
$last_record_retrieved = $_POST['last_record_retrieved'];


$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 link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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('link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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('link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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('link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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 link_pedigrees_to_pedigree_groups.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 Whether Insert from File or Web
    if (!preg_match('/\d+/',$insert_from_file))
      {
	echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
	echo "This form allows you to populate Pedigree Groups with Pedigrees that are in the database.  There are two options \n";
	echo "for the way to do this.  If you have multiple pedigrees to link, you can store their \n";
	echo "information in a comma-separated file and upload that file to the site.  Or if you only have \n";
	echo "one or two pedigrees to link, you can use a web form to insert their data.  Please choose the method \n";
	echo "you would like to choose below.<br><br>\n";

        echo "<table>\n";
	echo "<tr><td><input type=\"radio\" name=\"insert_from_file\" value=\"0\"></td><td>Populate Pedigree Group with a Single Pedigree</td></tr>\n";
        echo "<tr><td><input type=\"radio\" name=\"insert_from_file\" value=\"1\"></td><td>Populate Pedigree Group with Several Pedigrees</td></tr>\n";
        echo "<tr><td><input type=\"radio\" name=\"insert_from_file\" value=\"2\" checked</td><td>Populate Pedigree Group from a File</td></tr>\n";
        echo "</table>\n";
        echo "<br>\n";
	echo "<center>\n";
	echo "<input type=\"submit\" name=\"submit\" value=\"Populate Pedigree Group\">\n";
	echo "<input type=\"reset\" name=\"reset\" value=\"Reset Selection\">\n";
        echo "</center>\n";
	echo "</form>\n";
        echo "<br><br>\n";

        $ret_val = printLogOutButton('link_pedigrees_to_pedigree_groups.php');
      }#end if (!preg_match('/\d+/',insert_from_file))

    #4    If Insert from Web form
    if (($insert_from_file == '0') and
	(!preg_match('/^\d+$/',$ped_group_id)) and
        (!preg_match('/^\d+$/',$ped_table_id))
       )
      {
	echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
	echo "Please select the Pedigree Group to populate and the Pedigree to populate it with, then press the submit button.  The \n";
	echo "information will then be entered into the database.<br><br>\n";
	echo "<table>\n";
        echo "<tr><td>Pedigree Group:</td><td>\n";
        $jscript_1 = printPedigreeGroupSelectList($database_id);
        if ($jscript_1 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #11: $date<br>\n";
            echo "Error in call to printPedigreeGroupSelectList from link_pedigrees_to_pedigree_groups.php<br>\n";
            echo "See the error message from printPedigreeGroupSelectList above for an explanation.<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 "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in call to printPedigreeGroupSelectList
        if ($jscript_1 == 'no_defined_pedigree_groups')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date.\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
        echo "<tr><td>&nbsp;</td></tr>\n";
        echo "<tr><td>Pedigree ID:</td><td>\n";
        $jscript_2 = printPedigreeSelectList($database_id);
        if ($jscript_2 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #12: $date<br>\n";
            echo "Error in call to printPedigreeSelectList from link_pedigrees_to_pedigree_groups.php<br>\n";
            echo "See the error message from printPedigreeSelectList above for an explanation.<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 "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in call to printPedigreeSelectList
        if ($jscript_2 == 'no_defined_pedigrees')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date.\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
	echo "</table>\n";
	echo "<br><br>\n";

	echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"0\">\n";
	echo "<center>\n";
	echo "<input type=\"submit\" name=\"submit\" value=\"Insert Pedigree into Pedigree Group\">\n";
	echo "<input type=\"reset\" name=\"reset\" value=\"Reset Values\">\n";
        echo "</form>\n";
        echo "</center>\n";
        echo "<br><br>\n";
        
        $ret_val = printLogOutButton('link_pedigrees_to_pedigree_groups.php');
	echo "\n\n";
	
        $jscript_1 = preg_replace('/ else \{return true;\}\n\}/','',$jscript_1);
        $jscript_2 = preg_replace('/^\{\n/','',$jscript_2);
        $jscript_2 = preg_replace('/ else \{return true;\}\n\}/','',$jscript_2);
        $jscript = "$jscript_1\n$jscript_2\n return true;\n}";
	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
        echo "</script>\n";
      }#end if from web but no values

    #If Select Multiple Pedigrees, but no pedigree group or pedigrees
    if (($insert_from_file == '1') and
        (!preg_match('/^\d+$/',$ped_group_id)) and
        (!preg_match('/^\d$/',$ped_multiple_pages)) and
        (sizeof($indv_ids) == '0')
       )
      {
        $field_list = array('pedigree_table_id');
        $num_peds = countTableQuery('pedigree_table',$database_id,$field_list,'_','wildcard');
        if (!preg_match('/^\d+$/',$num_peds))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #13: $date<br>\n";
            echo "Error in call to countTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
            echo "$num_peds<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 countTableQuery
        if ($num_peds == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Currently there are no Pedigrees in the database, so you cannot populate an Pedigree Group.<br>\n";
            echo "This message was generated at $date.<br><br>\n";
            printLogOutButton('link_pedigrees_to_pedigree_groups.php');
            $ret_val = disconnect();
            exit;
          }

        if ($num_peds > $max_records_to_print_per_page)
          {
            $indiv_multiple_pages = 1;
            $from                 = 0;
            $to                   = $max_records_to_print_per_page - 1;
            $range                = "$from"."-"."$to";
            if ($db_type == 'mysql'){$order_by = 'pedigree_id+0';}
            if ($db_type == 'pgsql'){$order_by = 'pedigree_id::text::integer';}
            $ped_rec_list = tableQuery('pedigree_table',$database_id,$field_list,'_','wildcard',$range,$order_by);
            if (!is_array($ped_rec_list))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #14: $date<br>\n";
                echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                echo "$ped_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";
                $ret_val = disconnect();
                exit;
              }#end if error in call to tableQuery
            $final_function_call   = 'tableQuery';
            $final_table_name      = 'pedigree_table';
            $final_field_list      = $field_list;
            $final_term            = '_';
            $final_mode            = 'wildcard';
            $final_order_by        = $order_by;
            $last_record_retrieved = $max_records_to_print_per_page;
          }#end if multiple pages
        else #only a single page of pedigrees
          {
            $ped_multiple_pages = 0;
            $range                = "all";
            if ($db_type == 'mysql'){$order_by = 'pedigree_id+0';}
            if ($db_type == 'pgsql'){$order_by = 'pedigree_id::text::integer';}
            $ped_rec_list = tableQuery('pedigree_table',$database_id,$field_list,'_','wildcard',$range,$order_by);
            if (!is_array($ped_rec_list))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #15: $date<br>\n";
                echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                echo "$ped_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";
                $ret_val = disconnect();
                exit;
              }#end if error in call to tableQuery
          }#end else only a single page of pedigrees

        echo "Select the Pedigree Group you want to insert Pedigrees into, and the Pedigrees to insert.<br><br>\n";
        $ret_val = printLogOutButton('link_pedigrees_to_pedigree_groups.php');
        echo "<br><br>\n";

        if ($ped_multiple_pages == '1')
          {
            echo "<table align=\"center\">\n";
            echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"$insert_from_file\">\n";
            echo "<input type=\"hidden\" name=\"pedigree_group_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"ped_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"ped_mulitple_pages\" value=\"0\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Selected Pedigree(s)\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "</form>\n";

            echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
            $records_left = $num_peds - $max_records_to_print_per_page;
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Pedigrees\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Pedigrees\"></td>\n";}
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple pages of pedigrees
        else #only a single page of pedigrees
          {
            echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Selected Pedigree(s)\"></td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end else only a single page of pedigrees
        echo "<br><br>\n";

        echo "<table align=\"center\">\n";
        echo "<tr><td><b>Pedigree Group:</td><td>\n";
        $jscript_1 = printPedigreeGroupSelectList($database_id);
        if ($jscript_1 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #16: $date<br>\n";
            echo "Error in call to printPedigreeGroupSelectList from link_pedigrees_to_pedigree_groups.php<br>\n";
            echo "See the error message from printPedigreeGroupSelectList above for an explanation.<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 "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in call to printPedigreeGroupSelectList
        if ($jscript_1 == 'no_defined_pedigree_groups')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date.\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
        echo "</table>\n";
        echo "<br>\n";

        $jscript_2 = printPedigreeCheckList($ped_rec_list,$database_id);
        if ($jscript_2 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #17: $date $date<br>\n";
	    echo "Error in call to printPedigreeCheckList from link_pedigrees_to_pedigree_groups.php<br>\n";
	    echo "See above error message from printPedigreeCheckList for an explanation<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 call to printPedigreeCheckList
	echo "<br><br>\n";

        echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"$insert_from_file\">\n";
        echo "<input type=\"hidden\" name=\"ped_multiple_pages\" value=\"$ped_multiple_pages\">\n";
        if ($ped_multiple_pages)
          {
            echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$final_function_call\">\n";
            echo "<input type=\"hidden\" name=\"table_name\" value=\"$final_table_name\">\n";
            foreach ($final_field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

            if (is_array($final_term)){foreach ($final_term as $term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$term\">\n";}}
            else {echo "<input type=\"hidden\" name=\"term\" value=\"$final_term\">\n";}

            if (is_array($final_mode)){foreach ($final_mode as $mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$mode\">\n";}}
            else {echo "<input type=\"hidden\" name=\"mode\" value=\"$final_mode\">\n";}

            echo "<input type=\"hidden\" name=\"order_by\" value=\"$final_order_by\">\n";
            echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";

            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Pedigrees\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Pedigrees\"></td>\n";}
            echo "</form>\n";

            echo "<form action=\"unlink_pedigrees_to_pedigree_groups.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
            echo "<input type=\"hidden\" name=\"pedigree_group_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"ped_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"ped_mulitple_pages\" value=\"0\">\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Selected Pedigree(s)\" onClick=\"copyData('skip_form2')\"></td>\n";
            echo "</form>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple_pages
        else #only a single page
          {
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Pedigree(s) from Pedigree Group\"></td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</form>\n";
            echo "</table>\n";
          }#end else only a single page
        echo "<br><br>\n";

        $ret_val = printLogOutButton('unlink_pedigrees_to_pedigree_groups.php');
        echo "\n\n";
            
        $jscript_1 = preg_replace('/ else \{return true;\}\n\}/','',$jscript_1);
        $jscript_2 = preg_replace('/^\{\n/','',$jscript_2);
        $jscript = "$jscript_1\n$jscript_2";                                                                                      
        echo "<script>\n";
        echo "function Validate(form)\n";
        echo "$jscript\n";
        if ($ped_multiple_pages)
          {
            echo "\n";
            echo "function copyData(form_to_copy_to)\n";
            echo "{\n";
            echo " var ped_string = new Array();\n";
            echo " var length = 0;\n";
            echo " for (var i=0; i<document.form.elements['ped_id[]'].length; i++){\n";
            echo "   if (document.form.elements['ped_id[]'][i].checked){\n";
            echo "     length = ped_string.length;\n";
            echo "     ped_string[length] = document.form.elements['ped_id[]'][i].value;}}\n";
            echo " if (form_to_copy_to == \"skip_form1\"){\n";
            echo "   document.skip_form1.pedigree_group_id.value = document.form.pedigree_group_id.value;\n";
            echo "   document.skip_form1.ped_id.value = ped_string;}\n";
            echo " if (form_to_copy_to == \"skip_form2\"){\n";
            echo "   document.skip_form2.pedigree_group_id.value = document.form.pedigree_group_id.value;\n";
            echo "   document.skip_form2.ped_id.value = ped_string;}\n";
            echo "}\n";
          }#end if multiple pages
        echo "</script>\n";        
      }#end if Select Multiple Pedigrees, but not pedigree group or pedigrees

    #If Select Multiple Pedigrees, have pedigree group but more pedigrees to see
    if (($insert_from_file == '1') and
        (preg_match('/^\d+$/',$ped_group_id)) and
        ($ped_multiple_pages == '1')
       )
      {
        $ped_group_field_list = array('pedigree_group_id');
        $ped_group_name_array = distinctTableQuery('pedigree_group_name','pedigree_group_table',$database_id,$ped_group_field_list,$ped_group_id,'equal','all');
        if (!is_array($ped_group_name_array))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #18: $date<br>\n";
            echo "Error in call to distinctTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
            echo "$ped_group_name_array<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 error in call to distinctTableQuery
        if (sizeof($ped_group_name_array) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "The Pedigree Group you selected to delete Pedigrees from appears to have been deleted from the database.  \n";
            echo "Therefore, there are no longer any pedigrees associated with it.<br>\n";
            echo "This message was generated at $date.<br><br>\n";
            printLogOutButton('link_pedigrees_to_pedigree_groups.php');
            $ret_val = disconnect();
            exit;
          }#end if pedigree group has been deleted
        elseif (sizeof($ped_group_name_array) == '1'){$ped_group_name = $ped_group_name_array[0];unset($ped_group_name_array);}
        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 link_pedigrees_to_pedigree_groups.php<br>\n";
	    echo "The user wants to delete an pedigree from pedigree group with pedigree_group_id $ped_group_id in database_id $database_id, \n";
	    echo "but when query the pedigree group 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

        if ($function_to_call == 'tableQuery')
	  {
	    $num_of_peds = countTableQuery($table_name,$database_id,$field_list,$term,$mode);
	    if (!preg_match('/^\d+$/',$num_of_peds))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #20: $date<br>\n";
		echo "Error in call to countTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
		echo "$num_of_peds<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 error in call to countTableQuery
	    $records_yet_to_be_retrieved = $num_of_peds - $last_record_retrieved;

            if ($records_yet_to_be_retrieved > $max_records_to_print_per_page)
              {
                $ped_multiple_pages = 1;
                $from                 = $last_record_retrieved;
                $to                   = ($last_record_retrieved-1) + $max_records_to_print_per_page;
                $range                = "$from"."-"."$to";
                $ped_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
                if (!is_array($ped_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #21: $date<br>\n";
		    echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
		    echo "$ped_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 "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if (!is_array(ped_rec_list))
		if (sizeof($ped_rec_list) == 0)
		  {
		    $date = date('Y-m-d H:i:s');
                    echo "Error #22: $date<br>\n";
		    echo "Error in call to link_pedigrees_to_pedigree_groups.php<br>\n";
		    echo "There should be records returned for tableQuery search with parameters:<br>\n";
                    echo "table_name = $table_name<br>\n";
                    echo "field_list = ".join(',',$field_list)."<br>\n";
                    echo "term       = $term<br>\n";
                    echo "mode       = $mode<br>\n";
                    echo "range      = $range<br>\n";
                    echo "order_by   = $order_by<br>\n";
                    echo "But no 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 "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if no records returned
                $last_record_retrieved = $to + 1;
	      }#end if more than a single page
	    else #only a single page
	      {
                $ped_multiple_pages = 0;
		$from                 = $last_record_retrieved;
                $to                   = $num_of_peds - 1;
                $range                = "$from"."-"."$to";
		$ped_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
                if (!is_array($ped_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #23: $date<br>\n";
		    echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
		    echo "$ped_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 "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if (!is_array(ped_rec_list))
                if (sizeof($ped_rec_list) == 0)
		  {
		    $date = date('Y-m-d H:i:s');
                    echo "Error #24: $date<br>\n";
		    echo "Error in call to link_pedigrees_to_pedigree_groups.php<br>\n";
		    echo "There should be records returned for tableQuery search with parameters:<br>\n";
                    echo "table_name = $table_name<br>\n";
                    echo "field_list = ".join(',',$field_list)."<br>\n";
                    echo "term       = $term<br>\n";
                    echo "mode       = $mode<br>\n";
                    echo "range      = $range<br>\n";
                    echo "order_by   = $order_by<br>\n";
                    echo "But no 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 "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if no records returned
	      }#end else only a single page
	  }#end if tableQuery

        echo "Please select which Pedigree(s) you want to insert into the Pedigree Group $ped_group_name.<br><br>\n";
        $ret_val = printLogOutButton('link_pedigrees_to_pedigree_groups.php');
        echo "<br><br>\n";

        if ($ped_multiple_pages)
          {
            echo "<table align=\"center\">\n";
            echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"$insert_from_file\">\n";
            echo "<input type=\"hidden\" name=\"pedigree_group_id\" value=\"$ped_group_id\">\n";
            echo "<input type=\"hidden\" name=\"ped_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"ped_mulitple_pages\" value=\"0\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Selected Pedigree(s)\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "</form>\n";

            echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
            $records_left = $num_peds - $last_record_retrieved;
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Pedigrees\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Pedigrees\"></td>\n";}
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple_pages
        else #only a single page
          {
            echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Selected Pedigree(s)\"></td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end else only a single page
        echo "<br><br>\n\n";

        $jscript = printPedigreeCheckList($ped_rec_list,$database_id);
        if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
	    echo "Error #25: $date<br>\n";
	    echo "Error in call to printPedigreeCheckList from link_pedigrees_to_pedigree_groups.php<br>\n";
	    echo "See above error message from printPedigreeCheckList for an explanation<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 (jscript == '0')
	echo "<br><br>\n";

        echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"$insert_from_file\">\n";
        echo "<input type=\"hidden\" name=\"pedigree_group_id\" value=\"$ped_group_id\">\n";
        echo "<input type=\"hidden\" name=\"ped_multiple_pages\" value=\"$ped_multiple_pages\">\n";
        if (sizeof($ped_id) > '0')
          {
            if (!is_array($ped_id)){echo "<input type=\"hidden\" name=\"ped_id[]\" value=\"$ped_id\">\n";}
            else
              {
                $term = join($ped_id,',');
                echo "<input type=\"hidden\" name=\"ped_id[]\" value=\"$term\">\n";
                unset($term);
              }
          }#end if have ids from previous page(s)

        if ($ped_multiple_pages)
          {
            echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$function_to_call\">\n";
            echo "<input type=\"hidden\" name=\"table_name\" value=\"$table_name\">\n";
            foreach ($field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

            if (is_array($term)){foreach ($term as $sub_term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$sub_term\">\n";}}
            else {echo "<input type=\"hidden\" name=\"term\" value=\"$term\">\n";}

            if (is_array($mode)){foreach ($mode as $sub_mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$sub_mode\">\n";}}
            else {echo "<input type=\"hidden\" name=\"mode\" value=\"$final_mode\">\n";}

            echo "<input type=\"hidden\" name=\"order_by\" value=\"$order_by\">\n";
            echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";

            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Pedigrees\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Pedigrees\"></td>\n";}
            echo "</form>\n";

            echo "<form action=\"unlink_pedigrees_to_pedigree_groups.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"$insert_from_file\">\n";
            echo "<input type=\"hidden\" name=\"pedigree_group_id\" value=\"$ped_group_id\">\n";
            echo "<input type=\"hidden\" name=\"ped_id\" value=\"\">\n";
            echo "<input type=\"hidden\" name=\"ped_mulitple_pages\" value=\"0\">\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Selected Pedigree(s)\" onClick=\"copyData('skip_form2')\"></td>\n";
            echo "</form>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple_pages
        else #only a single page
          {
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Insert Pedigree(s) from Pedigree Group\"></td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Pedigree(s)\"></td>\n";
            echo "</tr>\n";
            echo "</form>\n";
            echo "</table>\n";
          }#end else only a single page
        echo "<br><br>\n";

        $ret_val = printLogOutButton('link_pedigrees_to_pedigree_groups.php');
        echo "\n\n";
                                                                                                    
        echo "<script>\n";
        echo "function Validate(form)\n";
        echo "$jscript\n";
        if ($ped_multiple_pages)
          {
            echo "\n";
            echo "function copyData(form_to_copy_to)\n";
            echo "{\n";
            echo " var ped_string = new Array();\n";
            echo " var length = 0;\n";
            echo " for (var i=0; i<document.form.elements['ped_id[]'].length; i++){\n";
            echo "   if (document.form.elements['ped_id[]'][i].type == \"hidden\"){\n";
            echo "     length = ped_string.length;\n";
            echo "     ped_string[length] = document.form.elements['ped_id[]'][i].value;}\n";
            echo "   else{\n";
            echo "     if (document.form.elements['ped_id[]'][i].checked){\n";
            echo "       length = ped_string.length;\n";
            echo "       ped_string[length] = document.form.elements['ped_id[]'][i].value;}}}\n";
            echo " if (form_to_copy_to == \"skip_form1\"){\n";
            echo "   document.skip_form1.ped_id.value = ped_string;}\n";
            echo " if (form_to_copy_to == \"skip_form2\"){\n";
            echo "   document.skip_form2.ped_id.value = ped_string;}\n";
            echo "}\n";
          }#end if multiple pages
        echo "</script>\n";
      }#end if select multiple pedigrees, have pedigree group but more pedigrees to see

    #5    Insert values from Web form
    if ((($insert_from_file == '0') and
	 (preg_match('/^\d+$/',$ped_group_id)) and
         (preg_match('/^\d+$/',$ped_table_id))
        )or
        (($insert_from_file == '1') and
	 (preg_match('/^\d+$/',$ped_group_id)) and
         ($ped_multiple_pages == '0') and
         (sizeof($ped_ids) > '0')
        )
       )
      {
        $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 #26: $date<br>\n";
            echo "Error in call to fopen from link_pedigrees_to_pedigree_groups.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 = "";

	$pedigree_field_array = array('pedigree_group_id');
	$pedigree_value_array = array($ped_group_id);
	$exists_pedigree_group = existsValue('pedigree_group_table',$pedigree_field_array,$pedigree_value_array,$database_id);
	if (!preg_match('/\d+/',$exists_pedigree_group))
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #27: $date<br>\n";
	    echo "Error in call to existsValue from link_pedigrees_to_pedigree_groups.php<br>\n";
	    echo "$exists_pedigree_group<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 "</form>\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 existsValue
	if ($exists_pedigree_group == '0')
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Pedigree Group ID $ped_group_id is not a pedigree group in the database.  It appears that it was deleted \n";
            echo "from the database before you submitted your request to add pedigrees to it.<br>\n";
	    echo "This message was generated at $date.\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 pedigree group not already in the database

        $field_list = array('pedigree_group_id');
        $ped_group_rec_list = tableQuery('pedigree_group_table',$database_id,$field_list,$ped_group_id,'equal','all');
        if (!is_array($ped_group_rec_list))
          {
            $date = date('Y-m-d H:i:s');
	    echo "Error #28: $date<br>\n";
	    echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
	    echo "$ped_group_rec_list<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 "</form>\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
        $ped_group_rec = $ped_group_rec_list[0];

        if ($insert_from_file == '0')
          {
	    $pedigree_field_array = array('pedigree_table_id');
	    $pedigree_value_array = array($ped_table_id);
	    $exists_pedigree = existsValue('pedigree_table',$pedigree_field_array,$pedigree_value_array,$database_id);
	    if (!preg_match('/\d+/',$exists_pedigree))
	      {
                $date = date('Y-m-d H:i:s');
	        echo "Error #29: $date<br>\n";
	        echo "Error in call to existsValue from link_pedigrees_to_pedigree_groups.php<br>\n";
	        echo "$exists_pedigree<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 "</form>\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 existsValue
	    if ($exists_pedigree == '0')
	      {
                $date = date('Y-m-d H:i:s');
	        echo "Pedigree Table ID $ped_table_id is not a pedigree in the database.  It appears that it was deleted \n";
                echo "from the database before you submitted your request to add it to the pedigree group $ped_group_rec[pedigree_group_name].<br>\n";
	        echo "This message was generated at $date.\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 pedigree not already in the database

            $field_list = array('pedigree_table_id');
            $ped_rec_list = tableQuery('pedigree_table',$database_id,$field_list,$ped_table_id,'equal','all');
            if (!is_array($ped_rec_list))
              {
                $date = date('Y-m-d H:i:s');
	        echo "Error #30: $date<br>\n";
	        echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
	        echo "$ped_rec_list<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 "</form>\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
            $ped_rec = $ped_rec_list[0];

            #Test to see if pedigree already in the pedigree group
            $field_list_array = array('pedigree_group_id','pedigree_table_id');
            $term_array       = array($ped_group_id,$ped_table_id);
            $mode_array       = array('equal','equal');
            $linked = countCompoundTableQuery('pedigree_pedigree_group_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and');
            if (!preg_match('/^\d+$/',$linked))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #31: $date<br>\n";
                echo "Error in call to countCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                echo "$linked<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 "</form>\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 countCompoundTableQuery
            if ($linked > '0')
              {
                $date = date('Y-m-d H:i:s');
                echo "The Pedigree $ped_rec[pedigree_id] is already in the Pedigree Group $ped_group_rec[pedigree_group_name].<br>\n";
                echo "This message was generated at $date.<br><br>\n";
                echo "</form>\n";

                printLogOutButton('link_pedigrees_to_pedigree_groups.php');
	        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 pedigree already linked to pedigree group

            $ret_val = insertPedigreePedigreeGroupLink($database_id,$ped_table_id,$ped_group_id);
            if ($ret_val != '1')
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #32: $date<br>\n";
                echo "Error in call to insertPedigreePedigreeGroupLink from link_pedigrees_to_pedigree_groups.php<br>\n";
                echo "$ret_val<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 "</form>\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 insertPedigreePedigreeGroupLink

            $field_list = array('pedigree_table_id','pedigree_group_id');
            $term_array = array($ped_table_id,$ped_group_id);
            $mode_array = array('equal','equal');
            $ped_ped_group_link_id_array = distinctCompoundTableQuery('pedigree_pedigree_group_link_id','pedigree_pedigree_group_link_table',$database_id,$field_list,$term_array,$mode_array,'and','all');
            if (!is_array($ped_ped_group_link_id_array))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #33: $date<br>\n";
                echo "Error in call to distinctCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                echo "$ped_ped_group_link_id_array<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 "</form>\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 distinctCompoundTableQuery
            if ((sizeof($ped_ped_group_link_id_array) == 0) or (sizeof($ped_ped_group_link_id_array) > 1))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #34: $date<br>\n";
	        echo "Error in execution of link_pedigrees_to_pedigree_groups.php<br>\n";
	        echo "The call to insertPedigreePedigreeGroupLink returned success for Pedigree Table ID $ped_table_id and ";
                echo "Pedigree Group ID $ped_group_id.<br>\n";
	        echo "But when query database ".sizeof($ped_ped_group_link_id_array)." records are returned.<br>\n";
                echo "Please email the above error using the Comments/Help/Support link on the \n";
                echo "left navigation bar.<br><br>\n";
                echo "</form>\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 data integrity
            $date = date('Y-m-d H:i:s');
            $log_file_statement.= "$date:\t$_SESSION[user_name] Inserted a new Pedigree-Pedigree Group Link into Database ID $database_id for ";
            $log_file_statement.= "Pedigree Table ID $ped_table_id and Pedigree Group ID $ped_group_id, assigned Pedigree-Pedigree Group Link ID ";
            $log_file_statement.= "$ped_ped_group_link_id_array[0].\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 "Successfully added Pedigree $ped_rec[pedigree_id] to Pedigree Group $ped_group_rec[pedigree_group_name]<br><br>\n";
          }#end if inserting only a single pedigree
        if ($insert_from_file == '1')
          {
            $log_file_statement = "";
            $inserted_peds = array();
            $skipped_peds  = array();
            if (!is_array($ped_ids))
              {
                if (preg_match('/,/',$ped_ids))#if a comma-separated list
                  {
                    $insert_array = explode(',',$ped_ids);
                    foreach ($insert_array as $ped_table_id)
                      {
                        $pedigree_field_array = array('pedigree_table_id');
	                $pedigree_value_array = array($ped_table_id);
	                $exists_pedigree = existsValue('pedigree_table',$pedigree_field_array,$pedigree_value_array,$database_id);
	                if (!preg_match('/\d+/',$exists_pedigree))
	                  {
                            $date = date('Y-m-d H:i:s');
	                    echo "Error #35: $date<br>\n";
	                    echo "Error in call to existsValue from link_pedigrees_to_pedigree_groups.php<br>\n";
	                    echo "$exists_pedigree<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 "</form>\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;
	                  }#end if error in call to existsValue
	                if ($exists_pedigree == '0')
                          {
                            $skipped_peds[] = $ped_table_id;
                            continue;
                          }#end if pedigree not already in the database

                        $field_list = array('pedigree_table_id');
                        $ped_rec_list = tableQuery('pedigree_table',$database_id,$field_list,$ped_table_id,'equal','all');
                        if (!is_array($ped_rec_list))
                          {
                            $date = date('Y-m-d H:i:s');
	                    echo "Error #36: $date<br>\n";
	                    echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
	                    echo "$ped_rec_list<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 "</form>\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
                        $ped_rec = $ped_rec_list[0]; 

                        #Test to see if pedigree already in the pedigree group
                        $field_list_array = array('pedigree_group_id','pedigree_table_id');
                        $term_array       = array($ped_group_id,$ped_table_id);
                        $mode_array       = array('equal','equal');
                        $linked = countCompoundTableQuery('pedigree_pedigree_group_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and');
                        if (!preg_match('/^\d+$/',$linked))
                          {
                            $date = date('Y-m-d H:i:s');
                            echo "Error #37: $date<br>\n";
                            echo "Error in call to countCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                            echo "$linked<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 "</form>\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 countCompoundTableQuery
                       if ($linked > '0')
                         {
                           $skipped_peds[] = $ped_rec['pedigree_id'];
                           continue;
                         }#end if pedigree already linked to pedigree group

                        $ret_val = insertPedigreePedigreeGroupLink($database_id,$ped_table_id,$ped_group_id);
                        if ($ret_val != '1')
                          {
                            $date = date('Y-m-d H:i:s');
                            echo "Error #38: $date<br>\n";
                            echo "Error in call to insertPedigreePedigreeGroupLink from link_pedigrees_to_pedigree_groups.php<br>\n";
                            echo "$ret_val<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 "</form>\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;
	                  }#end if error in call to insertPedigreePedigreeGroupLink
                        $date = date('Y-m-d H:i:s');

                        $field_list = array('pedigree_table_id','pedigree_group_id');
                        $term_array = array($ped_table_id,$ped_group_id);
                        $mode_array = array('equal','equal');
                        $ped_ped_group_link_id_array = distinctCompoundTableQuery('pedigree_pedigree_group_link_id','pedigree_pedigree_group_link_table',$database_id,$field_list,$term_array,$mode_array,'and','all');
                        if (!is_array($ped_ped_group_link_id_array))
                          {
                            $date = date('Y-m-d H:i:s');
                            echo "Error #39: $date<br>\n";
                            echo "Error in call to distinctCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                            echo "$ped_ped_group_link_id_array<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 "</form>\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;
	                  }#end if error in call to distinctCompoundTableQuery
                        if ((sizeof($ped_ped_group_link_id_array) == 0) or (sizeof($ped_ped_group_link_id_array) > 1))
                          {
                            $date = date('Y-m-d H:i:s');
                            echo "Error #40: $date<br>\n";
	                    echo "Error in execution of link_pedigrees_to_pedigree_groups.php<br>\n";
	                    echo "The call to insertPedigreePedigreeGroupLink returned success for Pedigree Table ID $ped_table_id and ";
                            echo "Pedigree Group ID $ped_group_id.<br>\n";
	                    echo "But when query database ".sizeof($ped_ped_group_link_id_array)." records are returned.<br>\n";
                            echo "Please email the above error using the Comments/Help/Support link on the \n";
                            echo "left navigation bar.<br><br>\n";
                            echo "</form>\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;
                          }#end if error in data integrity
                        $log_file_statement = "$date:\t$_SESSION[user_name] Inserted a new Pedigree-Pedigree Group Link ID ";
                        $log_file_statement.= "$ped_ped_group_link_id_array[0] into Database ID $database_id ";
                        $log_file_statement.= "for Pedigree Group ID $ped_group_id and Pedigree Table ID $ped_table_id.\n";
                        fwrite($log_file_handle,$log_file_statement);
                        $inserted_peds[] = $ped_rec['pedigree_id'];
                      }#end foreach pedigree to insert
                  }#end if ped_ids is a comma-separated list
                else #only a single ped_id
                  {
                    $pedigree_field_array = array('pedigree_table_id');
	            $pedigree_value_array = array($ped_ids);
	            $exists_pedigree = existsValue('pedigree_table',$pedigree_field_array,$pedigree_value_array,$database_id);
	            if (!preg_match('/\d+/',$exists_pedigree))
	              {
                        $date = date('Y-m-d H:i:s');
	                echo "Error #41: $date<br>\n";
	                echo "Error in call to existsValue from link_pedigrees_to_pedigree_groups.php<br>\n";
	                echo "$exists_pedigree<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 "</form>\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 existsValue
	            if ($exists_pedigree == '0')
                      {
                        $date = date('Y-m-d H:i:s');
	                echo "Pedigree Table ID $ped_ids is not an pedigree in the database.  It appears that it was deleted \n";
                        echo "from the database before you submitted your request to add it to the pedigree group $ped_group_rec[pedigree_group_name].<br>\n";
	                echo "This message was generated at $date.\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 pedigree not already in the database

                    $field_list = array('pedigree_table_id');
                    $ped_rec_list = tableQuery('pedigree_table',$database_id,$field_list,$ped_table_id,'equal','all');
                    if (!is_array($ped_rec_list))
                      {
                        $date = date('Y-m-d H:i:s');
	                echo "Error #42: $date<br>\n";
	                echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
	                echo "$ped_rec_list<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 "</form>\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
                    $ped_rec = $ped_rec_list[0]; 

                    #Test to see if pedigree already in the pedigree group
                    $field_list_array = array('pedigree_group_id','pedigree_table_id');
                    $term_array       = array($ped_group_id,$ped_table_id);
                    $mode_array       = array('equal','equal');
                    $linked = countCompoundTableQuery('pedigree_pedigree_group_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and');
                    if (!preg_match('/^\d+$/',$linked))
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #43: $date<br>\n";
                        echo "Error in call to countCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                        echo "$linked<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 "</form>\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 countCompoundTableQuery
                    if ($linked > '0')
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "The Pedigree $ped_rec[pedigree_id] is already in the Pedigree Group $ped_group_rec[pedigree_group_name].<br>\n";
                        echo "This message was generated at $date.<br><br>\n";
                        echo "</form>\n";

                        printLogOutButton('link_pedigrees_to_pedigree_groups.php');
	                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 pedigree already linked to pedigree group

                    $ret_val = insertPedigreePedigreeGroupLink($database_id,$ped_ids,$ped_group_id);
                    if ($ret_val != '1')
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #44: $date<br>\n";
                        echo "Error in call to insertPedigreePedigreeGroupLink from link_pedigrees_to_pedigree_groups.php<br>\n";
                        echo "$ret_val<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 "</form>\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 insertPedigreePedigreeGroupLink
                    $date = date('Y-m-d H:i:s');

                    $field_list = array('pedigree_table_id','pedigree_group_id');
                    $term_array = array($ped_ids,$ped_group_id);
                    $mode_array = array('equal','equal');
                    $ped_ped_group_link_id_array = distinctCompoundTableQuery('pedigree_pedigree_group_link_id','pedigree_pedigree_group_link_table',$database_id,$field_list,$term_array,$mode_array,'and','all');
                    if (!is_array($ped_ped_group_link_id_array))
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #45: $date<br>\n";
                        echo "Error in call to distinctCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                        echo "$ped_ped_group_link_id_array<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 "</form>\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 distinctCompoundTableQuery
                    if ((sizeof($ped_ped_group_link_id_array) == 0) or (sizeof($ped_ped_group_link_id_array) > 1))
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #46: $date<br>\n";
	                echo "Error in execution of link_pedigrees_to_pedigree_groups.php<br>\n";
	                echo "The call to insertPedigreePedigreeGroupLink returned success for Pedigree Table ID $ped_ids and ";
                        echo "Pedigree Group ID $ped_group_id.<br>\n";
	                echo "But when query database ".sizeof($ped_ped_group_link_id_array)." records are returned.<br>\n";
                        echo "Please email the above error using the Comments/Help/Support link on the \n";
                        echo "left navigation bar.<br><br>\n";
                        echo "</form>\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 data integrity
                    $log_file_statement = "$date:\t$_SESSION[user_name] Inserted Pedigree-Pedigree Group Link ID ";
                    $log_file_statement.= "$ped_ped_group_link_id_array[0] into Database ID $database_id ";
                    $log_file_statement.= "for Pedigree Group ID $ped_group_id and Pedigree Table ID $ped_ids.\n";
                    fwrite($log_file_handle,$log_file_statement);
                    $inserted_peds[] = $ped_rec['pedigree_id'];
                  }#end only single ped_id
              }#end if ped_ids is not an array
            else #ped_ids is an array
              {
                $insert_term = join($ped_ids,',');
                $insert_array = explode(',',$insert_term);
                foreach ($insert_array as $ped_table_id)
                  {
                    $pedigree_field_array = array('pedigree_table_id');
	            $pedigree_value_array = array($ped_table_id);
	            $exists_pedigree = existsValue('pedigree_table',$pedigree_field_array,$pedigree_value_array,$database_id);
	            if (!preg_match('/\d+/',$exists_pedigree))
	              {
                        $date = date('Y-m-d H:i:s');
	                echo "Error #47: $date<br>\n";
	                echo "Error in call to existsValue from link_pedigrees_to_pedigree_groups.php<br>\n";
	                echo "$exists_pedigree<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 "</form>\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;
	              }#end if error in call to existsValue
	            if ($exists_pedigree == '0')
                      {
                        $skipped_peds[] = $ped_table_id;
                        continue;
                      }#end if pedigree not already in the database

                    $field_list = array('pedigree_table_id');
                    $ped_rec_list = tableQuery('pedigree_table',$database_id,$field_list,$ped_table_id,'equal','all');
                    if (!is_array($ped_rec_list))
                      {
                        $date = date('Y-m-d H:i:s');
	                echo "Error #48: $date<br>\n";
	                echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
	                echo "$ped_rec_list<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 "</form>\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
                    $ped_rec = $ped_rec_list[0]; 

                    #Test to see if pedigree already in the pedigree group
                    $field_list_array = array('pedigree_group_id','pedigree_table_id');
                    $term_array       = array($ped_group_id,$ped_table_id);
                    $mode_array       = array('equal','equal');
                    $linked = countCompoundTableQuery('pedigree_pedigree_group_link_table',$database_id,$field_list_array,$term_array,$mode_array,'and');
                    if (!preg_match('/^\d+$/',$linked))
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #49: $date<br>\n";
                        echo "Error in call to countCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                        echo "$linked<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 "</form>\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 countCompoundTableQuery
                    if ($linked > '0')
                      {
                        $skipped_peds[] = $ped_rec['pedigree_id'];
                        continue;
                      }#end if pedigree already linked to pedigree group

                    $ret_val = insertPedigreePedigreeGroupLink($database_id,$ped_table_id,$ped_group_id);
                    if ($ret_val != '1')
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #50: $date<br>\n";
                        echo "Error in call to insertPedigreePedigreeGroupLink from link_pedigrees_to_pedigree_groups.php<br>\n";
                        echo "$ret_val<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 "</form>\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;
	              }#end if error in call to insertPedigreePedigreeGroupLink

                    $field_list = array('pedigree_table_id','pedigree_group_id');
                    $term_array = array($ped_table_id,$ped_group_id);
                    $mode_array = array('equal','equal');
                    $ped_ped_group_link_id_array = distinctCompoundTableQuery('pedigree_pedigree_group_link_id','pedigree_pedigree_group_link_table',$database_id,$field_list,$term_array,$mode_array,'and','all');
                    if (!is_array($ped_ped_group_link_id_array))
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #51: $date<br>\n";
                        echo "Error in call to distinctCompoundTableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                        echo "$ped_ped_group_link_id_array<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 "</form>\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;
	              }#end if error in call to distinctCompoundTableQuery
                    if ((sizeof($ped_ped_group_link_id_array) == 0) or (sizeof($ped_ped_group_link_id_array) > 1))
                      {
                        $date = date('Y-m-d H:i:s');
                        echo "Error #52: $date<br>\n";
	                echo "Error in execution of link_pedigrees_to_pedigree_groups.php<br>\n";
	                echo "The call to insertPedigreePedigreeGroupLink returned success for Pedigree Table ID $ped_table_id and ";
                        echo "Pedigree Group ID $ped_group_id.<br>\n";
	                echo "But when query database ".sizeof($ped_ped_group_link_id_array)." records are returned.<br>\n";
                        echo "Please email the above error using the Comments/Help/Support link on the \n";
                        echo "left navigation bar.<br><br>\n";
                        echo "</form>\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;
                      }#end if error in data integrity
                    $date = date('Y-m-d H:i:s');
                    $log_file_statement = "$date:\t$_SESSION[user_name] Inserted Pedigree-Pedigree Group Link ID ";
                    $log_file_statement.= "$ped_ped_ped_group_link_array[0] into Database ID $database_id for ";
                    $log_file_statement.= "Pedigree Group ID $ped_group_id and Pedigree Table ID $ped_table_id.\n";
                    fwrite($log_file_handle,$log_file_statement);
                    $inserted_peds[] = $ped_rec['pedigree_id'];
                  }#end foreach pedigree to insert
              }#end else ped_ids is an array
            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 added the following ".sizeof($inserted_peds)." Pedigrees to Pedigree Group $ped_group_rec[pedigree_group_name]:<br>\n";
            echo "<table border=\"1\">\n";
            echo "<tr><th>Pedigree ID</th></tr>\n";
            foreach ($inserted_peds as $inserted_ped){echo "<tr><td align=\"center\">$inserted_ped</td></tr>\n";}
            echo "</table>\n";
            echo "<br><br>\n";
            if (sizeof($skipped_peds) > '0')
              {
                echo "The remaining ".sizeof($skipped_peds)." Pedigrees <b>were not</b> inserted into the pedigree group becasue they \n";
                echo "were no longer in the database OR because they were already in the pedigree group.<br>\n";
              }#end if pedigrees skipped<br><br>\n";
          }#end if inserting from checklist

	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 #53: $date<br>\n";
            echo "Error in call to getDatabase from link_pedigrees_to_pedigree_groups.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 #54: $date<br>\n";
            echo "Error in call to getAllUsers from link_pedigrees_to_pedigree_groups.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

        if ($insert_from_file == '0')
          {
            $subject = "Added new Pedigree to a Pedigree Group in your Genetic Studies Database";
            $message = "The Pedigree $ped_rec[pedigree_id] was added to Pedigree Group $ped_group_rec[pedigree_group_name] ";
            $message.= "in database $database_rec[database_name].\n";
          }#end if only inserted a single pedigree
        if ($insert_from_file == '1')
          {
            $subject = "New Pedigrees added to a Pedigree Group in your Genetic Studies Database";
            $message = "There were ".sizeof($inserted_peds)." Pedigrees inserted into Pedigree Group $ped_group_rec[pedigree_group_name] ";
            $message.= "in database $database_rec[database_name]";
          }#end if inserted from checklist
        $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 #55: $date<br>\n";
            echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.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 #56: $date<br>\n";
                    echo "Error in call to getUser from link_pedigrees_to_pedigree_groups.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 #57: $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><br>\n";

        if ($insert_from_file == '0')
          {
            echo " <form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
	    echo " If you would like to insert more pedigrees please click the button below.<br><br>\n";
	    echo "<center>\n";
	    echo "<input type=\"submit\" name=\"submit\" value=\"Insert More Pedigrees\">\n";
            echo "</center>\n";
	    echo "</form>\n";
            echo "<br><br>\n";
          }#end if only inserted a single pedigree

        $ret_val = printLogOutButton('link_pedigrees_to_pedigree_groups.php');
      }#end if insert from web and have values

    #6 Want to insert from file, but haven't said if file on server or users computer
    if (($insert_from_file == '2') and
        (!preg_match('/^\d$/',$upload_file)) and
	((!is_uploaded_file($_FILES['ped_group_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=\"link_pedigrees_to_pedigree_groups.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('link_pedigrees_to_pedigree_groups.php');
      }#end if want to upload file but haven't select way to select

    #7    Get File
    if (($insert_from_file == '2') and
        (preg_match('/^\d$/',$upload_file)) and
	((!is_uploaded_file($_FILES['ped_group_file']['tmp_name'])) and (!preg_match('/\w+/',$file_on_server)))
       )
      {
	if ($upload_file == 1)
          {echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" enctype=\"multipart/form-data\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";}
        else #file on server
          {echo "<form action=\"link_pedigrees_to_pedigree_groups.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";}
	echo "This form will allow you to upload a file that contains the pedigrees you would like to populate the selected pedigree group with.<br>  \n";
	echo "The file must be in a specific format for the insertion to be done properly.  The file must contain 1 \n";
	echo "column of pedigree ids.  Each pedigree must be on its own line, and all the pedigrees must \n";
        echo "already be in the database. (Click <a href=\"../data/test_data/pedigree_pedigree_group_link_test_data.txt\" target=\"_blank\">here</a> for an example data file.)<br> ";
        echo "<center><b>Upload your file below:</b></center> <br><br>\n";

	echo "<input type=\"hidden\" name=\"insert_from_file\" value=\"$insert_from_file\">\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>Pedigree Group:</td><td>\n";
        $jscript_1 = printPedigreeGroupSelectList($database_id);
        if ($jscript_1 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #58: $date<br>\n";
            echo "Error in call to printPedigreeGroupSelectList from link_pedigrees_to_pedigree_groups.php<br>\n";
            echo "See error from printPedigreeGroupSelectList above for an explanation.<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 "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in call to printPedigreeGroupSelectList
        if ($jscript_1 == 'no_defined_pedigree_groups')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date.<br>\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
        echo "<tr><td>&nbsp;</td></tr>\n";
        echo "<tr><td>&nbsp;</td></tr>\n";
	echo "<tr><td>File Name:</td><td>\n";
        if ($upload_file == 1){echo "<input type=\"file\" name=\"ped_group_file\" size=\"20\"></td></tr>\n";}
        else #file on server
          {
            $jscript_2 = printFileSelectList($_SESSION['user_name'],$database_id,'upload');
            if ($jscript_2 == '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 #59: $date<br>\n";
                echo "Error in call to printFileSelectList from link_pedigrees_to_pedigree_groups.php<br>\n";
                echo "See above error message from printFileSelectList for an explanation.<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";
                $ret_val = disconnect();
                exit;
              }#end if error in call to printFileSelectList
            if ($jscript_2 == '1')
              {
                echo "</td></tr>\n";
                echo "</table>\n";
                echo "</form>\n";
                echo "<br><br>\n";

                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('link_pedigrees_to_pedigree_groups.php');
                echo "</body></html>\n";
                $ret_val = disconnect();
                exit;
              }#end if no files in users directory to upload
            echo "</td></tr>\n";
          }#end else file on server
        echo "</table>\n";
        echo "<br>\n";
        echo "<table align=\"center\">\n";
        echo "<tr>\n";
	echo "<td><input type=\"submit\" name=\"submit\" value=\"Upload File\"></td>\n";
        echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected File\"></td>\n";
        echo "</tr>\n";
	echo "</form>\n";
        echo "</table>\n";
        echo "<br><br>\n";
        
        $ret_val = printLogOutButton('link_pedigrees_to_pedigree_groups.php');
        echo "\n\n";

        $jscript_1 = preg_replace('/ else \{return true;\}\n\}/','',$jscript_1);
        echo "<script>\n";
        echo "function Validate(form)\n";
        echo "$jscript\n";
        if ($upload_file == 1)
          {
            echo " if (form.ped_group_file.value == \"\"){\n";
            echo "   alert(\"You must select a file to upload.\");\n";
            echo "   return false;}\n";
            echo " return true;\n";
            echo "}\n";
          }#end if file upload
        else #file on server
          {
            $jscript_2 = preg_replace('/^\{/','',$jscript_2);
            echo "$jscript_2\n";
          }#end else file on server
        echo "</script>\n";
      }#end get file

    #8    Parse File and Insert
    if ((
         ($insert_from_file == '2') and
         (preg_match('/^\d$/',$upload_file)) and
         (preg_match('/^\d+$/',$ped_group_id)) and
	 (is_uploaded_file($_FILES['ped_group_file']['tmp_name']))
        ) or
        (
         ($insert_from_file == '2') and
         (preg_match('/^\d$/',$upload_file)) and
         (preg_match('/^\d+$/',$ped_group_id)) and
         (preg_match('/\w+/',$file_on_server))
        )
       )
      {
        if ($upload_file == 1)
          {
            $rand_num = rand();
            $file_name = "ped_group_data_upload_".$rand_num.".txt";
	    $ret_val = move_uploaded_file($_FILES['ped_group_file']['tmp_name'],"../data/uploaded_data/$_SESSION[user_name]/$file_name");
	    if (!$ret_val)
	      {
                $date = date('Y-m-d H:i:s');
	        echo "Error #60: $date<br>\n";
	        echo "Error in call to link_pedigrees_to_pedigree_groups.php<br>\n";
	        echo "Unable to move uploaded file inorder to read it.<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";
	        $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
        else #file 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

	$pedigree_group_field_array = array('pedigree_group_id');
        $pedigree_group_value_array = array($ped_group_id);
        $exists_pedigree_group = existsValue('pedigree_group_table',$pedigree_group_field_array,$pedigree_group_value_array,$database_id);
        if (!preg_match('/\d+/',$exists_pedigree_group))
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #61: $date<br>\n";
	    echo "Error in call to existsValue from link_pedigrees_to_pedigree_groups.php<br>\n";
	    echo "$exists_pedigree_group<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 "</form>\n";
	    echo "</body></html>\n";
            #$ret_val    = unlink("../data/uploaded_data/$_SESSION[user_name]/$file_name");
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            fclose($uploaded_file_handle);
	    $ret_val = disconnect();
	    exit;
	  }#end if error in call to existsValue
        if ($exists_pedigree_group == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Pedigree Group ID $ped_group_id is not already a pedigree in the database.  It appears that this pedigree group was deleted from \n";
            echo "the database before you submited your request to populate it with pedigrees.<br>\n";
            echo "This message was generated at $date.\n";
            #$ret_val    = unlink("../data/uploaded_data/$_SESSION[user_name]/$file_name");
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            fclose($uploaded_file_handle);
            $ret_val = disconnect();
            exit;
	  }#end if pedigree group not already in the database
        else #pedigree group already in the database
          {
            $field_list = array('pedigree_group_id');
            $ped_group_rec_list = tableQuery('pedigree_group_table',$database_id,$field_list,$ped_group_id,'equal','all');
            if (!is_array($ped_group_rec_list))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #62: $date<br><br>\n";
                echo "Error in call to tableQuery from link_pedigrees_to_pedigree_groups.php<br>\n";
                echo "$ped_group_rec_list<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 "</form>\n";
		echo "</body></html>\n";
                #$ret_val    = unlink("../data/uploaded_data/$_SESSION[user_name]/$file_name");
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
                fclose($uploaded_file_handle);
		$ret_val = disconnect();
		exit;
              }#end error in call to tableQuery
            if (sizeof($ped_group_rec_list) == '0') 
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #63: $date<br>\n";
                echo "Error in data integrity.  existsValue says that pedigree group id $ped_group_id is a pedigree group in database id $database_id, \n";
                echo "but when query the pedigree group table with that id no records are returned.<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 "</form>\n";
		echo "</body></html>\n";
                #$ret_val    = unlink("../data/uploaded_data/$_SESSION[user_name]/$file_name");
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
                fclose($uploaded_file_handle);
		$ret_val = disconnect();
		exit;
              }#end error in data integrity
            elseif (sizeof($ped_group_rec_list) == '1') {$ped_group_rec = $ped_group_rec_list[0];}
            else
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #64: $date<br>\n";
                echo "Error in data integrity.  Error in data integrity.  existsValue says that pedigree group id $ped_group_id is a pedigree group \n";
                echo "in database id $database_id, but when query the pedigree table with that id multiple records are returned.<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 "</form>\n";
		echo "</body></html>\n";
                #$ret_val    = unlink("../data/uploaded_data/$_SESSION[user_name]/$file_name");
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
                fclose($uploaded_file_handle);
		$ret_val = disconnect();
		exit;
              }#end error in data integrity
          }#end else pedigree group already in the database
        $ped_group_name = $ped_group_rec['pedigree_group_name'];

	echo "Preparing to insert Pedigrees from the file into the Pedigree Group.  This may take a few minutes depending on how many \n";
	echo "pedigrees are in the uploaded file.<br><br><br>\n";

        $ret_val = chdir("../bin/database_query_scripts/");
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #65: $date<br>\n";
            echo "Error in call to chdir from link_pedigrees_to_pedigree_groups.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 #66: $date<br>\n";
                    echo "Error in call to exec to split file from link_pedigrees_to_pedigree_groups.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 '$file' '$database_id' 'Populate Pedigree Groups' '$_SESSION[user_name]'";
                    $command.= " '$background_flag' '$ped_group_id' '$ped_group_name' >> 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 #67: $date<br>\n";
	                echo "Error in system call to insert_data_into_database.php from link_pedigrees_to_pedigree_groups.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' 'Populate Pedigree Groups' '$_SESSION[user_name]'";
                $command.= " '$background_flag' '$ped_group_id' '$ped_group_name' >> 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 #68: $date<br>\n";
	            echo "Error in system call to insert_data_into_database.php from link_pedigrees_to_pedigree_groups.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' 'Populate Pedigree Groups' '$_SESSION[user_name]'";
            $command.= " '$background_flag' '$ped_group_id' '$ped_group_name'";

            passthru($command,$sys_ret_val);
            if ($sys_ret_val != '0')
	      {
                echo "<br><br>\n";
	        $date = date('Y-m-d H:i:s');
	        echo "Error #69: $date<br>\n";
	        echo "Error in system call to insert_data_into_database.php from link_pedigrees_to_pedigree_groups.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 #70: $date<br>\n";
            echo "Error in call to chdir from link_pedigrees_to_pedigree_groups.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 == '1') 

$ret_val = disconnect();		      
echo "</body></html>\n";
?>
Return current item: Generic Genetic Studies Database