require_once ("common_db.php");
function getDescription($targetid, &$localId, &$globalId, &$orgFullName)
{
     $query = "SELECT organism.name,gi, rosie_id FROM target, gene, organism ".
		"WHERE target.id=".$targetid." AND ".
		"base_gene_id=gene.id AND organism_id=organism.id";
	// echo $query. " ";
     $result = mysql_query ($query) or die ("Query failed in target description");
     while($line = mysql_fetch_array($result))
     {
	$orgName = $line[0];
	$orgFullName = $orgName;
	$secWord = trim(strstr($orgName, ' '));
	$geneGi = $line[1];
	$globalId = $geneGi;
	$localId = $line[2];
	return substr($orgName,0,1).substr($secWord,0,1)."-".$geneGi;
//	return $orgName."-".$geneGi;
     }
}
function dispTargetDescription($targetId)
{
    /*
	$desp = getDescription($targetId, $rosieId, $Gi, $orgName);
	echo "
Target Status for ".$rosieId. " ";
	echo "GI = ".$Gi. ", From: " . $orgName. " \n";
    */
$targetID = $targetId+0;
$geneID = targetBaseID($targetID);
$gi = geneGI($geneID);
$organism = organismName(geneOrganism($geneID));
$rosie = targetRosie($targetID);
echo ("You are updating target $rosie. ");
echo ("This target is gi number $gi  from $organism .\n");
 echo ("Click ");
 openTargetInfoLink($targetID);
 echo ("here for more info on $rosie.
\n
 \n");
}
function &getCurrentStatus($targetId) # return the whole array
{
     $query = "SELECT * FROM target_status WHERE targetid = ".$targetId;
     $result = mysql_query ($query) or die ("Fail in getCurrentStatus");
     while($line = mysql_fetch_array($result))
     {
        return $line;
     }
}
// Check if $updateId is in the detal_cancel table, if is, return TRUE
// otherwise return 0;
function isCanceled($updateId)
{
     $bRet = 0;		// false;
     $query = "SELECT result FROM detail_cancel WHERE result = ".$updateId;
     $result = mysql_query ($query) or die ("Fail in query detail_cancel");
     while($line = mysql_fetch_array($result))
     {
	if ($line[0]>0)
	{
        	$bRet = 1;
		break;
	}
     }
     return $bRet;
}
// return the latest uncanceled ID of table target_update2.
function getLatestUpdateId($TID, $AID)
{
     $iRet = 0;		// did not find;
     $query = "SELECT id FROM target_update2 WHERE target_id = $TID";
     $query .= " AND action_id = $AID ORDER BY edit_datetime DESC";
     $result = mysql_query ($query) or die ("Fail in get Id of target_update2.");
     while($line = mysql_fetch_array($result))
     {
	if ($line[0]>0 && !isCanceled($line[0]))
	{
        	$iRet = $line[0];
		break;
	}
     }
     return $iRet;
}
function getActionId($actName, &$detailTable)
{
        $table = "action2";
        $query = "SELECT id, table_name FROM $table WHERE name='$actName'";
        $result = mysql_query ($query) or die ("Query failed in $table.\n");
        $iActId =0;
        $detailTable = '';
        while ($object = mysql_fetch_object($result))
        {
                $iActId =$object->id;
                $detailTable = $object->table_name;
                break;
        }
        return $iActId;
}
function getActionName($AID, &$detailTable, &$dispName)
{
        $table = "action2";
        $query = "SELECT name, table_name, display_name FROM $table WHERE id=$AID";
        $result = mysql_query ($query) or die ("Query failed in $table.\n");
        $actName ='';
        $detailTable = "";
        $dispName ="";
        while ($object = mysql_fetch_object($result))
        {
                $actName =$object->name;
                $detailTable = $object->table_name;
                $dispName= $object->display_name;
                break;
        }
        return $actName;
}
// return 2 array of action name and action display name: from selected to workstopped
function getActionDispNames(&$aName, &$aDispName)
{
        $table = "action2";
        $query = "SELECT name, display_name FROM $table WHERE id>=2 AND id <=18";
        $result = mysql_query ($query) or die ("Query failed in $table.\n");
        $aName = array();
        $aDispName =array();
        $iRet =0;
        while ($object = mysql_fetch_object($result))
        {
                array_push($aName, $object->name);
                array_push($aDispName, $object->display_name);
                $iRet++;
        }
        return $iRet;
}
function getStatusCount($actName, $option =1)
{       // $option 0: all targets; 1:length(name)>=1; 2:S 3:I, etc.
        $table = "target_status";
        if (0== $option)
                $stmWhere = "";
        else if (2 == $option)
                $stmWhere = "WHERE soluble = 'S'";
        else if (3 == $option)
                $stmWhere = "WHERE soluble = 'I'";
        else
                $stmWhere ="WHERE LENGTH($actName)>0"; // the default
        $query = "SELECT count(*) FROM $table $stmWhere";
	
//	echo $query;
        $result = mysql_query ($query) or die ("Query failed in function getStatusCount and table: $table.\n");
        $iRet =0;
        while ($line = mysql_fetch_array($result))
        {
                $iRet = $line[0];
                break;
        }
        return $iRet;
}
function getStatusSum(&$aDispName, &$aCount)
{
        $iNumStatus = getActionDispNames($aName, $aDisp);
        $aDispName = array();
        $aCount = array();
        for ($i =0; $i<$iNumStatus; $i++)
        {
                $actName = $aName[$i];
                $dispName = $aDisp[$i];
                array_push($aDispName, $dispName);
                if (0 == strcasecmp($actName, "Soluble")) {
		    array_push($aCount, getStatusCount($actName, 1)); // total
		    array_push($aDispName, "  ".$dispName." (Soluble)");
		    array_push($aCount, getStatusCount($actName, 2));
		    $inSol = "  ".$dispName." (Insoluble)";
		    array_push($aDispName, $inSol);
		    array_push($aCount, getStatusCount($actName, 3));
                }
		else if (0 == strcasecmp($actName, "Selected"))
		    array_push($aCount, getStatusCount($actName, 0));
                else
		    array_push($aCount, getStatusCount($actName,1));
        }
        return $iNumStatus+2;
}
function getTableHeader($tableName, $link)
{
        $fields = mysql_list_fields("bsgc", $tableName, $link);
        $iNumColumns = mysql_num_fields($fields);
        $aFldName = array();
        for ($i = 0; $i < $iNumColumns; $i++)
        {
                array_push($aFldName, mysql_field_name($fields, $i));
        }
        return $aFldName;
}
function getSelectionDate($TID)
{
        $query = "SELECT selected_date FROM target WHERE id=$TID";
        $result = mysql_query ($query) or die ("Query fail in target.\n");
        $dtRet ='0000-00-00';
        while ($object = mysql_fetch_object($result))
        {
                $dtRet = $object->selected_date;
                break;
        }
        return $dtRet;
}
function getUidFromName($loginName)
{
        $query = "SELECT id FROM user WHERE name='$loginName'";
        $result = mysql_query ($query) or die ("Query fail in User.\n");
        $iRet =0;
        while ($object = mysql_fetch_object($result))
        {
                $iRet = $object->id;
                break;
        }
        return $iRet;
}
function insertSelction2Update($TID, $upTable)
{
        $dateSelect = getSelectionDate($TID);
        $UID = getUidFromName("jmc");         //4; // jmc only  may need further work
        $detTmp = '';
        $AID = getActionId("selected", $detTmp);
        $upInsert = "INSERT INTO $upTable SET target_id =$TID, user_id=$UID,exp_date='$dateSelect',";
        $upInsert.= "edit_datetime='$dateSelect', action_id=$AID";
     //   echo $upInsert;
        $result = mysql_query ($upInsert) or die ("Insert selection failed in $upTable.\n");
}
function getDetailRes($JID, $dtTable)
{
        $query = "SELECT result FROM $dtTable WHERE tupdate_id=$JID";
        $result = mysql_query ($query) or die ("Query fail Function: getDetailRes, Table: $dtTable\n");
        $Ret ="";
        while ($line = mysql_fetch_array($result))
        {
                $Ret = "$line[0]";
                break;
        }
        return $Ret;
}
function getTargetResult($journalTable, $TID, &$aUserName, &$aAction, &$aValue, &$aExpDate, &$aEditDate, &$aDispName)
{
        $iRet =0;
        $aUserName = array();
        $aAction   = array();
        $aValue    = array();
        $aExpDate  = array();
        $aEditDate = array();
        $aDispName = array();
        $query = "SELECT id,user_id,action_id,exp_date,DATE_FORMAT(edit_datetime,'%Y-%m-%d')";
        $query.= " FROM $journalTable WHERE target_id= $TID ORDER BY action_id,edit_datetime DESC";
        $result2 = mysql_query ($query) or die ("Query failed get results from $journalTable.\n");
        $prevAID=0;
        $JID = $AID =$UID = 0;
        $expDate='';
        $editDate='';
        while ($line = mysql_fetch_array($result2))
        {
                $JID = $line[0];
                $AID = $line[2];
                if (isCanceled($JID) || 1 == $AID || $prevAID == $AID)  // cancel record or a cancelled record
                        continue;
                $UID = $line[1];
                $res = mysql_query ("SELECT name FROM user WHERE id=$UID") or die ("Query failed in user.\n");
                if ($line2 = mysql_fetch_array($res))
                        $userName = $line2[0];
                $expDate =$line[3];
                $editDate = $line[4];
                $actName = getActionName($AID, $dtTable, $dispName);
                $tmpValue = "";
                if ($AID>=2)
                {
                        if (strlen($dtTable)>0)
                                $tmpValue = getDetailRes($JID, $dtTable);
                        else
                                $tmpValue ="X";
                }
                array_push($aUserName, $userName);
                array_push($aAction, $actName);
                array_push($aValue, $tmpValue);
                array_push($aExpDate, $expDate);
                array_push($aEditDate,$editDate);
                array_push($aDispName,$dispName);
                $iRet++;
                $prevAID = $AID;
        } // end of parse this TID
        return $iRet;
}
function dispTargetResult($TID)
{
        $iNumRes = getTargetResult("target_update2", $TID, $aUserName, $aAction, $aValue, $aExpDate, $aEditDate, $aDispName);
        if ($iNumRes<1)
                return;
        $tda = "";
        $tdb = " ";
	echo ("\n");
        echo "Experiment Result User Experiment Date  ";
        for ($i =0; $i<$iNumRes; $i++)
        {
                $dispVal = $aValue[$i];
                if (0== strcasecmp("X", $aValue[$i]))
                        $dispVal = "Done";
                echo "$tdb$aDispName[$i]$tda$dispVal$tda$aUserName[$i]$tda$aExpDate[$i] ";
        }
        echo "
";
}
// return the latest actionID in action2 and its values.
function getLatestActId($TID, &$value)
{
        $journalTable = "target_update2";
        $iRet =0;
        $value = "";
        $query = "SELECT id,user_id,action_id,exp_date,DATE_FORMAT(edit_datetime,'%Y-%m-%d')";
        $query.= " FROM $journalTable WHERE target_id= $TID ORDER BY action_id DESC,edit_datetime DESC";
        $result2 = mysql_query ($query) or die ("Query failed to get results from $journalTable.\n");
        $JID = $AID = 0;
        while ($line = mysql_fetch_array($result2))
        {
                $JID = $line[0];
                $AID = $line[2];
                if (isCanceled($JID) || 1 == $AID)  // cancel record or a cancelled record
                        continue;
                $actName = getActionName($AID, $dtTable, $dispName);
                $tmpValue = "";
                if ($AID>=2)
                {
                        if (strlen($dtTable)>0)
                                $tmpValue = getDetailRes($JID, $dtTable);
                        else
                                $tmpValue ="X";
                }
                $value = $tmpValue;
                $iRet= $AID;
                break;
        } // end of parse this TID
        return $iRet;
}
?>
Target Info