Index: trunk/pstamp/web/request.php
===================================================================
--- trunk/pstamp/web/request.php	(revision 25322)
+++ trunk/pstamp/web/request.php	(revision 29086)
@@ -16,19 +16,6 @@
 // XXX This is just a prototype for testing purposes. 
 
-
-// XXX: change to include pstampconfig.php
-
-// BEGIN Local configuration
-
-$WORKDIR = "/data/ipp049.0/pstamp/work";
-$dsroot = "/data/ipp049.0/datastore/dsroot";
-$dbname = "ippRequestServer";
-$dbserver = "ipp049";
-
-$PSCONFDIR = "/data/ipp053.0/home/bills/psconfig";
-$PSCONFIG  = "debug";
-$PSBINDIR  = "$PSCONFDIR/$PSCONFIG.lin64/bin";
-
-// END Local configuration 
+require "pstamp.php";
+require "submitted.php";
 
 # this script sets up the environment to run IPP commands with current directory
@@ -55,4 +42,5 @@
 $diff_checked = "";
 $list_checked = "";
+$getstatus_checked = "";
 $pstamp_checked = "";
 $get_checked = "";
@@ -102,5 +90,5 @@
 if ($rvar_project == "gpc1") {
     $gpc1_selected = "selected";
-    $require_class_id = 1;
+//    $require_class_id = 1;
 } else if ($rvar_project == "megacam-mops") {
     $mops_selected = "selected";
@@ -122,6 +110,6 @@
     $diff_checked = "CHECKED";
 } else {
-    // nothing checked default to By Exposure
-    $exp_checked = "CHECKED";
+    // nothing checked default to By ID
+    $file_checked = "CHECKED";
 }
 
@@ -140,10 +128,10 @@
 
 // is the center is specified in Pixels or sky coordinates
-if ($rvar_center_type == "Sky") {
+if ($rvar_center_type == "Pixels") {
+    $sky_checked="";
+    $pix_checked="checked";
+} else {
     $sky_checked="checked";
     $pix_checked="";
-} else {
-    $sky_checked="";
-    $pix_checked="checked";
 }
 
@@ -168,16 +156,17 @@
 }
 
+$getstatus_checked = "";
+$pstamp_checked = "";
+$list_checked = "";
+$get_checked = "";
 if ($rvar_cmd_mode == "Make Stamps") {
     $pstamp_checked = "checked";
-    $list_checked = "";
-    $get_checked = "";
 } else if ($rvar_cmd_mode == "Get Images") {
     $get_checked = "checked";
-    $list_checked = "";
-    $pstamp_checked = "";
+} else if ($rvar_cmd_mode == "Get Status") {
+    $getstatus_checked = "checked";
 } else {
-    $list_checked = "checked";
-    $get_checked = "";
-    $pstamp_checked = "";
+    // default
+    $pstamp_checked = "checked";
 }
 
@@ -188,11 +177,10 @@
 
 // How do we know whether or not this is the intial page load or not?
-// Well, in that case rvar_img_type is not set so key off of that
+// Well, the first time rvar_img_type is not set. So we key off of that.
 // TODO: find a better way to decide whether or not to run commands
 
 if ($rvar_img_type) {
-
     $jobFinished = 0;
-    if ($request_id == 0) {
+    if (! $getstatus_checked) {
         try {
             $command_line = build_request_cmd();
@@ -201,19 +189,19 @@
             if (! $list_checked) {
                 // The only output from a successful run is the request_id
+                $request_name = trim(Array_pop($output_array));
                 $request_id = Array_pop($output_array);
                 $last_request_id = $request_id;
-                setcookie("our_request_id", $request_id);
-                // echo "The request id is $request_id\n";
-                if (count($output_array) != 0) {
-                    throw new Exception("unexpected output returned by pstampwebrequest.");
+                if ($request_id && $request_name) {
+                    addRequest($request_id, $request_name);
+                    // setcookie("our_request_id", $request_id);
+                    // echo "The request id is $request_id\n";
+                    $getstatus_checked = "checked";
+                    $pstamp_checked = "";
+                } else {
+                    // XXX: TODO print out the error
+                    if (count($output_array) != 0) {
+                        throw new Exception("unexpected output returned by pstampwebrequest.");
+                    }
                 }
-
-                $jobRunning = getRequestStatus();
-                if (!$jobRunning) {
-                  //   echo "1  getRequestStatus reuturned 0\n";
-                    $jobFinished = 1;
-                    $request_id = 0;
-                }
-
             } else {
                 $last_request_id = 0;
@@ -222,26 +210,8 @@
             $error_line = $e->getMessage();
         }
-    } else {
-        try {
-            // get the list of jobs for the request
-            // echo "calling getRequestStatus\n";
-            $jobRunning = getRequestStatus();
-
-            if (!$jobRunning) {
-               //  echo "2  getRequestStatus reuturned 0\n";
-                $jobFinished = 1;
-                $request_id = 0;
-            }
-
-        } catch (Exception $e) {
-            echo "Got Exception $request_id $e\n";
-            $error_line = $e->getMessage();
-        }
-    }
-    if ($last_request_id) {
-        // This doesn't work for get_image
-        listJobs($last_request_id, $jobFinished);
-    }
-}
+    }
+}
+
+// This is the end of the Logic
 
 function build_request_cmd()
@@ -296,10 +266,10 @@
                 throw new Exception('RA and DEC must be specified.');
             }
-            $cmd .= " -skycenter $rvar_RA $rvar_DEC";
+            $cmd .= " --ra $rvar_RA --dec $rvar_DEC";
         } else {
             if (! $rvar_X || ! $rvar_Y) {
                 throw new Exception('X and Y must be specified.');
             }
-            $cmd .= " -pixcenter $rvar_X $rvar_Y";
+            $cmd .= " -pixcenter --x $rvar_X --y $rvar_Y";
         }
 
@@ -308,10 +278,10 @@
                 throw new Exception('dRA and dDEC must be specified.');
             }
-            $cmd .= " -arcrange $rvar_dRA $rvar_dDEC";
+            $cmd .= " --arcseconds --width $rvar_dRA --height $rvar_dDEC";
         } else {
             if (! $rvar_W || ! $rvar_H) {
                 throw new Exception('width and height must be specified.');
             }
-            $cmd .= " -pixrange $rvar_W $rvar_H";
+            $cmd .= " --width $rvar_W --height $rvar_H";
         }
     }
@@ -324,35 +294,37 @@
 
     if ($exp_checked) {
+        if ($rvar_img_type == "stack") {
+            throw new Exception('Lookup by exposure name not supported for stack images.');
+        }
         if (! $rvar_id ) {
             throw new Exception('Must set ID to the Exposure ID.');
         }
-        $cmd .= " -byexp $rvar_img_type $rvar_id";
+        $cmd .= " --req_type byexp --stage $rvar_img_type --id $rvar_id";
     } else if ($file_checked) {
         if (! $rvar_id ) {
             throw new Exception('Must set ID to the exposure name.');
         }
-        $cmd .= " -byid $rvar_img_type $rvar_id";
+        $cmd .= " --req_type byid --stage $rvar_img_type --id $rvar_id";
     } else if ($coord_checked) {
-        // $cmd .= " -bycoord $rvar_img_type";
-        $coord_checked = "";
-        $exp_checked = "checked";
-        throw new Exception("Image selection by coordinate not implemented yet.");
+        $cmd .= " --req_type bycoord --stage $rvar_img_type";
+        $coord_checked = "checked";
+//        throw new Exception("Image selection by coordinate not implemented yet.");
     } else if ($diff_checked) {
         if (! $rvar_id ) {
             throw new Exception('Must set ID to Diff Image ID.');
         }
-        $cmd .= " -bydiff $rvar_img_type $rvar_id";
-    }
-
-    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
-        if ($require_class_id && ! $rvar_class_id ) {
-            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
-        }
-        if ((!$rvar_class_id) || ($rvar_class_id == "all")) {
-            $cmd .= " null";
-        } else {
-            $cmd .= " $rvar_class_id";
-        }
-    }
+        $cmd .= " --req_type bydiff --stage $rvar_img_type --id $rvar_id";
+    }
+
+// XXX: don't need to require class_id anymore
+//    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
+//        if (!$sky_checked && ($require_class_id && ! $rvar_class_id )) {
+//            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
+//        }
+        // leave off compoennt if we're looking up by coordinates. It breaks it
+        if (!$coord_checked && (($rvar_class_id) && ($rvar_class_id != "all"))) {
+            $cmd .= " --component $rvar_class_id";
+        }
+//    }
 
     return escapeshellcmd($cmd);
@@ -390,4 +362,5 @@
 }
 
+// This is no longer used
 function printURL($line)
 {
@@ -442,119 +415,25 @@
     echo "</td></tr>";
 }
-
-function countRunningJobs()
-{
-    global $output_array;
-
-    $runningJobs = 0;
-    $size = sizeof($output_array);
-    for ($i = 0; $i < $size; $i++) {
-        $elements = explode(" ", $output_array[$i]);
-        if (count($elements) == 3) {
-            $state    = $elements[1];
-            if ($state != "stop") {
-                $runningJobs++;
-            }
-        } else {
-            throw new Exception ("incorrect data in job status: $output_array[$i]");
-        }
-    }
-    return $runningJobs;
-}
-
-function listJobs($request_id, $jobFinished)
-{
-    global $WORKDIR;
-    global $SCRIPT;
-
-    $command_line = "$SCRIPT pstamp_listjobs.pl $request_id";
-    global $dbname;
-    global $dbserver;
-    if ($dbname) {
-        $command_line .= " --dbname $dbname --dbserver $dbserver";
-    }
-
-    run_command($command_line);
-    if ($jobFinished) {
-        global $outFileset;
-        global $dsroot;
-        $parse_error = "$WORKDIR/$request_id/parse_error.txt";
-        #echo "reading $parse_error\n";
-        // readfile( $parse_error );
-        if (file_exists($parse_error)) {
-            $fhandle = fopen($parse_error, "r");
-            if ($fhandle) {
-                $contents = fread($fhandle, 1024);
-                if ($contents) {
-                    global $last_request_id;
-                    global $error_line;
-                    $error_line = "Request $last_request_id: $contents\n";
-                }
-            }
-        }
-    }
-}
-
-function getRequestStatus()
-{
-    global $request_id;
-    global $command_status;
-    global $output_array;
-    global $outFileset;
-    global $dbname;
-    global $dbserver;
-    global $SCRIPT;
-
-    $command_line = "$SCRIPT pstamptool -listreq -req_id $request_id -simple";
-    if ($dbname) {
-        $command_line .= " -dbname $dbname -dbserver $dbserver";
-    }
-    // echo "Running $command_line\n";
-
-    run_command($command_line);
-    if ($command_status == 0) {
-        $size = sizeof($output_array);
-        $runningReq = 0;
-        for ($i = 0; $i < $size; $i++) {
-            $elements = explode(" ", $output_array[$i]);
-            if (count($elements) >= 4) {
-                $state = $elements[2];
-                $outFileset = $elements[4];
-                if ($state != "stop") {
-                    $runningReq++;
-                }
-            } else {
-                throw new Exception ("incorrect data in job status: $output_array[$i]");
-            }
-        }
-        return $runningReq;
-    } else {
-        return 0;
-    }
-}
-
 ?>
 
-<!-- Beginning of the HTML --------------------------------------------- -->
+<!----------------------Beginning of the HTML --------------------------------------------- -->
+
 <html>
- <head>
-  <title>Postage Stamp Request Form</title>
-    <?php
-        if ($request_id != 0) {
-            // This doesn't do what I want. It does a get not a post
-
-            // echo '<META HTTP-EQUIV="refresh" CONTENT="5">';
-
-        }
-    ?>
- </head>
+<head>
+  <title>
+    Postage Stamp Request Form (prototype)
+  </title>
+</head>
+<body>
+
+<H1 align=center>
+Postage Stamp Request Form
+</h1>
+
+<?php
+    welcomeHeader($auth_user, "pstamp_links.php", "Postage Stamp Home");
+?>
+
 <form method="post">
-
-<body>
-
-<H1 align=center>
-Postage Stamp Request
-</h1>
-
 <!-- Whole page is a single column table -->
 
@@ -586,8 +465,9 @@
 <td>
 &nbsp;<b>Select Images By:</b>&nbsp;&nbsp;&nbsp;
+<input type=radio name="select_by" value="db_id" <?php echo $file_checked; ?> >Database ID
+&nbsp;
 <input type=radio name="select_by" value="exposure_id" <?php echo $exp_checked; ?> >Exposure Name
 &nbsp;
-<input type=radio name="select_by" value="db_id" <?php echo $file_checked; ?> >Database ID
-&nbsp;
+
 <input type=radio name="select_by" value="coord" <?php echo $coord_checked; ?> >Coordinates
 &nbsp;
@@ -625,5 +505,5 @@
         echo "Chip ID:";
       } else {
-        echo "Class ID:";
+        echo "Component:";
       }
 ?>
@@ -685,4 +565,5 @@
             &nbsp;
             <input type="text" name="dRA" size=10  value= <?php echo $rvar_dRA; ?> >
+            &nbsp; "
         </td>
         <td>
@@ -691,4 +572,5 @@
             &nbsp;
             <input type="text" name="dDEC" size=10 value="<?php echo $rvar_dDEC;?>" >
+            &nbsp; "
         </td>
     </tr>
@@ -742,19 +624,16 @@
   <tr>
 
-<?php
-  if ($request_id == 0): ?>
     <td><input type=submit value="Submit"></td>
     <td><b>Mode:</b>&nbsp;&nbsp;
+    <input type=radio name="cmd_mode" value="Get Status"<?php echo $getstatus_checked; ?> >Get Status
     <input type=radio name="cmd_mode" value="Make Stamps"<?php echo $pstamp_checked; ?> >Make Stamps
-    <input type=radio name="cmd_mode" value="Get Images" <?php echo $get_checked; ?> >Get Images
+    <input type=radio name="cmd_mode" value="Get Images" <?php echo $get_checked; ?> >Get Bundles
+<!--
     <input type=radio name="cmd_mode" value="List Images" <?php echo $list_checked; ?> >List Images
+-->
     </td>
-<?php 
-  else: ?>
-    <td><input type=submit value="Check Status"></td>
-<!---    <td><input type=submit value="Cancel"></td> --->
 <?php
-  echo "<td><b>Request Id: $request_id";
-  endif; ?>
+  // echo "<td><b>Request Id: $request_id";
+?>
 
   </tr>
@@ -766,6 +645,14 @@
 
 <tr>
+<!--- Don't show the command
     <td>
     <b>Command:</b>&nbsp;&nbsp; <?php echo "$command_line\n";?>
+    </td>
+--->
+
+</tr>
+<tr>
+    <td>
+    <b>Last Command</b>
     </td>
 </tr>
@@ -794,8 +681,11 @@
 <td>
 <table align=center width=100% rules=none>
-<caption height=10 valign=center><b>Results</b></caption>
-
+<caption height=10 valign=center><b>Request Results</b></caption>
 
 <?php 
+if (0) {
+    // This is the old way of listing the status of the current request.
+    // now we save the submitted requests in the session see listRequests() below
+
     $size = sizeof($output_array);
     // echo "<pre>size of output array is $size\n</pre>";
@@ -818,4 +708,5 @@
         }
     }
+} // end if if(0)
 ?>
 </table>
@@ -845,4 +736,8 @@
 </table>
 
+<?php
+    listRequests("http://datastore.ipp.ifa.hawaii.edu/pstampresults", "pstamp_results_fileset");
+?>
+
 <!-- The end -->
 
@@ -861,6 +756,5 @@
 ?>
 
+</form>
 </body>
-</form>
-
 </html>
