<?php
{
    function CreateQuery($_GET)
    {
        $query0  = "SELECT ";
        //datasets (0-3)
        $query0 .= " DataSets.fDataSetNumber, fWobble, fComment, ";//0,1,2
        $query0 .= " fSourceName, ";//3
        //steps (4-7)
        $query0 .= " fDataSetInserted, fStarFilesAvail, ";//4,5
        $query0 .= " fGanymed, fFillGanymed, ";//6,7
        //ganymed (8-13)
        $query0 .= " fExcessEvents, fBackgroundEvents, fSignalEvents, ";//8,9,10
        $query0 .= " fSignificance, fScaleFactor, fEffOnTime ";//11,12,13

        $query0 .= " FROM DataSets ";

        $query0 .= " LEFT JOIN Source ON DataSets.fSourceKEY=Source.fSourceKEY ";
        $query0 .= " LEFT JOIN DataSetProcessStatus ON DataSets.fDataSetNumber=DataSetProcessStatus.fDataSetNumber ";
        $query0 .= " LEFT JOIN Ganymed ON DataSets.fDataSetNumber=Ganymed.fDataSetNumber ";

        $query0 .= " WHERE DataSets.fDataSetNumber between " . $_GET["fRunMin"] . " and " . $_GET["fRunMax"] ;
        if (!empty($_GET["fDataSetNo"]))
            $query0 .= " AND DataSets.fDataSetNumber='" . $_GET["fDataSetNo"] . "'";
        $query0 .= " AND fSourceName REGEXP \"^" . $_GET["fSourceN"] . "\" ";
        $query0 .= " ORDER BY DataSets.fDataSetNumber ";

        return $query0;
    }

    function InitGet($_GET)
    {
        // Find out whether it is the first call to the php script
        $first = empty($_GET["fRunMin"]) && empty($_GET["fRunMax"]);

        if (empty($_GET["fNumResults"]))
            $_GET["fNumResults"]="20";

        if (empty($_GET["fAll"]))
            $_GET["fAll"]="Off";

    }

    function PrintForm($_GET, $db)
    {
        printf("<center>\n");
        printf("<form action=\"ganymed.php\" METHOD=\"GET\">\n");

        printf("DataSet#&nbsp;<input name=\"fDataSetNo\" type=\"text\" size=\"6\" maxlength=\"6\" value=\"");
        if (!empty($_GET["fDataSetNo"]))
            printf("%s", $_GET["fDataSetNo"]);
        printf("\">&nbsp;&nbsp;&nbsp;\n");

        if (empty($_GET["fRunMin"]))
            $min = "100";
//            $min = GetMin("fDataSetNumber", "DataSets", $db);
        else
            $min = $_GET["fRunMin"];

        if (empty($_GET["fRunMax"]))
            $max = GetMax("fDataSetNumber", "DataSets", $db);
        else
            $max = $_GET["fRunMax"];

        printf("DataSets&nbsp;from&nbsp;<input name=\"fRunMin\" type=\"text\" size=\"6\" maxlength=\"6\" value=\"%s\">\n", $min);
        printf("to&nbsp;<input name=\"fRunMax\" type=\"text\" size=\"6\" maxlength=\"6\" value=\"%s\">&nbsp;&nbsp;&nbsp;\n", $max);

        if ($_GET["fAll"]=="On")
            $checked = "checked";
        else
            $checked = "";
        printf("<input type=\"checkbox\" name=\"fAll\" value=\"On\" %s>plots\n", $checked);

        printf(" <P>\n");

        printf("Source&nbsp;(<A HREF=\"regexp.html\">regexp</A>)&nbsp;<input name=\"fSourceN\" type=\"text\" size=\"15\" maxlength=\"15\" value=\"");
        if (!empty($_GET["fSourceN"]))
            printf("%s", $_GET["fSourceN"]);
        printf("\">&nbsp;&nbsp;&nbsp;\n");


        ini_set("mysql.trace_mode", "Off");
        ini_set("display_errors", "Off");

        printf("<input class='Width' type='submit' value='Query Table'>&nbsp;&nbsp;&nbsp;\n");
        printf("<input class='Width' type='button' value='Reset' onClick='self.location.href=\"ganymed.php\"'>&nbsp;&nbsp;&nbsp;\n");
        if (strchr($_SERVER["REQUEST_URI"], '?')!=FALSE)
            printf("<input class='Width' type='button' value='Print' onClick='self.location.href=\"%s&fPrintTable=1\"'>&nbsp;&nbsp;&nbsp;\n", $_SERVER["REQUEST_URI"]);
        printf("</form>\n");
        printf("</center>\n");
        printf("</td>\n");
        printf("</tr>\n");
        printf("<tr class='Block'>\n");
        printf("<td>\n");
    }

    function GetStatus($step)
    {
        if (is_null($step))
            return "<font color='#FF0000'>not done</font>";
        else
        {
            if (strpos($step, "1970-01-01 00:00:00")==FALSE)
                return "done";
            else
                return "not to be done";
        }
        return "there is an error -> tell Dani";
    }
/*
    function GetSequences($datasetfile)
//    function QueryRuns($type, $sequ, $db_id, $db)
    {
        printf(" <td align='left'>");
        printf(" to be done");
        printf(" </td>\n");
        printf(" <td> to be done ");
        printf(" </td>\n");
     }

    function QueryMissingFiles($sequ, $db_id, $db)
    {
        $query2  = "SELECT fSequenceFirst FROM SequencesSequenceProcessStatus ";
        $query2 .= " WHERE fSequenceFirst=" . $sequ . " AND IsNull(fStar)";

        mysql_select_db($db);
        $files = mysql_query($query2, $db_id);
        $numfiles = mysql_num_rows($files);
        printf(" <td align='left'>");
        for ($i=0 ; $i<$numfiles ; $i++)
        {
            $file=mysql_result($files, $i, 0);
            if ($i!=0)
                printf(", ");
            printf(" %s ", $file);
        }
        printf(" </td>\n");
     }
*/

    function PrintDataSetInfo($result0, $db_id, $db)
    {
        $numres = mysql_num_rows($result0);

        printf("<center>");
        printf("<table BORDER=\"0\">\n");
        printf("<tr><td>");
        printf("# of DataSets: %d ", $numres);
        printf("</td></tr>");

        for ($i=0 ; $i<$numres ; $i++)
        {
            $datasetno=mysql_result($result0, $i, 0);
            $wobble=mysql_result($result0, $i, 1);
            $comment=mysql_result($result0, $i, 2);
            $source=mysql_result($result0, $i, 3);
            $inserted=mysql_result($result0, $i, 4);
            $filesavail=mysql_result($result0, $i, 5);
            $ganymed=mysql_result($result0, $i, 6);
            $fillganymed=mysql_result($result0, $i, 7);
            $excess=mysql_result($result0, $i, 8);
            $bg=mysql_result($result0, $i, 9);
            $signal=mysql_result($result0, $i, 10);
            $signif=mysql_result($result0, $i, 11);
            $scale=mysql_result($result0, $i, 12);
            $effontime=mysql_result($result0, $i, 13);

            printf("<tr><td>");

            printf(" <table BORDER=\"1\">");
            printf(" <tr BGCOLOR='#C0C0C0'>\n");
            printf(" <th colspan=\"6\"><u>DataSet#:</u> %s - %s </th>", $datasetno, $source);
            printf(" </tr><tr BGCOLOR='#E0E0E0' ALIGN='left'>\n");
            printf(" <th colspan=\"6\"> %s ", $comment);
            printf(" </th></tr><tr>\n");

            printf(" <td colspan=\"6\"> Wobble: %s", $wobble);
            if (!is_null($ganymed))
                printf(", <font color='green'> ganymed done</font> ");

            printf(" <tr>");

            if (!is_null($fillganymed))
            {
                printf("<td colspan=\"6\"></td></tr>\n");
                printf(" <tr BGCOLOR='#C0C0C0'><th colspan=\"6\">results - ");
                $num=sprintf("%08d",$datasetno);
                printf(" <a href=\"http://www.astro.uni-wuerzburg.de/datacenter/ganymed/%s/%s/\">plots</a>",
                       substr($num,0,5), $num);
                printf(" </th></tr><tr BGCOLOR='#E0E0E0'>\n");
                printf(" <td>excess events</td>\n");
                printf(" <td>background events</td>\n");
                printf(" <td>signal events</td>\n");
                printf(" <td>significance</td>\n");
                printf(" <td>scale factor</td>\n");
                printf(" <td>effective ontime</td>\n");
                printf(" </tr><tr BGCOLOR='#D0D0D0' align='right'>");
                printf(" <td>%s</td>\n", $excess);
                printf(" <td>%s</td>\n", $bg);
                printf(" <td>%s</td>\n", $signal);
                printf(" <td>%s</td>\n", $signif);
                printf(" <td>%s</td>\n", $scale);
                printf(" <td>%s</td>\n", $effontime);
            }

            if ($_GET["fAll"]=="On" && !is_null($ganymed))
            {
                printf("<tr><td colspan=\"6\">\n");
                printf(" plots (to be added) </td></tr>");
            }

            printf(" </table>");
            printf("</td></tr><tr><td><br></td></tr>");
        }
        printf("</table>\n");

        printf("</center>\n");
        printf("</tr><tr class='Block'><td>\n");
    }

    function PrintPage($html, $db)
    {
        $db_id = mysql_connect("hercules.astro.uni-wuerzburg.de", "MAGIC", "d99swMT!");
        if ($db_id==FALSE)
        {
            printf("mysql_connect returned the following error: %s\n", mysql_error());
            die("");
        }
        mysql_select_db($db);

        $query0 = CreateQuery($_GET);

        $result0 = mysql_query($query0, $db_id);

        if ($result0)
        {
            if ($html=="1" || $html=="2")
                PrintDataSetInfo($result0, $db_id, $db);
            else
                PrintText($result0);

            mysql_free_result($result0);
        }
        mysql_close($db_id);

        if ($html=="1")
            printf("<U><B>submitted query:</B></U><BR>%s<BR>", htmlspecialchars($query0));
    }

    include ("include.php");
    include ("db.php");

    ini_set("display_errors", "On");
    ini_set("mysql.trace_mode", "On");

    if (!empty($_GET["fSendTxt"]))
    {
        header("Content-type: application/octet");
        header("Content-Disposition: attachment; filename=query-result.txt");

        PrintPage("0", $db);
    }
    else
    {
        echo (file_get_contents("index-header.html"));

        $environment = sizeof($_GET);

        InitGet($_GET);
        if (empty($_GET["fPrintTable"]))
            PrintForm($_GET, $db);

        if ($environment==0)
            printf("No query submitted yet.<BR>");
        else
        {
            if (empty($_GET["fPrintTable"]))
                PrintPage("1", $db);
            else
                PrintPage("2", $db);
        }

        echo (file_get_contents("index-footer.html"));
    }

    ini_set("display_errors", "Off");
    ini_set("mysql.trace_mode", "Off");
}
?>
