<?php 

include 'ipp.php';
delete_old_tmp_files();

$ID = checkID ();

// require an explicit project
if (! $ID['proj']) { projectform ($ID); }

$db = dbconnect($ID['proj']);

if ($ID['menu']) {
  $myMenu = $ID['menu'];
} else {
  $myMenu = "$MENU";
}

menu($myMenu, '$TITLE', '$STYLE', $ID['link'], $ID['proj']);

echo "<p> $TITLE </p>";

// set up the form
echo "<form action=\"$FILE\" method=\"POST\">\n";

$restricted = 0;

// define restrictiosn to the queries
// ** TABLE RESTRICTIONS **

if ($restricted == 0) {//Don't select anything
  if ($WHERE == "") {
    $WHERE = "WHERE (0 > 1)";
  } else {
    $WHERE = "WHERE (0 > 1)";
  }
}

// get the result table count
$sql = "SELECT count(*) FROM $TABLE $WHERE";

$qry = $db->query($sql);
if (dberror($qry)) {
  echo "<b>error reading $TABLE table count</b><br>\n";
  echo "<br><small><b> count query : $sql </b></small><br>\n";
  menu_end();
}
if (!$qry->fetchInto($row)) {
  echo "<b>error reading $TABLE table count</b><br>\n";
  echo "<br><small><b> count query : $sql </b></small><br>\n";
  menu_end();
}
$nValues = $row[0];
echo "<b>$nValues match selection</b><br>\n";

$sql = "SELECT $TOPLOT FROM $TABLE $WHERE";
$qry = $db->query($sql);

if (dberror($qry)) {
  echo "<b>error reading $TABLE table</b><br>\n";
  echo "<br><small><b> table query : $sql </b></small><br>\n";
  menu_end();
}

// ** HEAD CODE **

// ** BUTTON RESTRICTIONS **
navigate_buttons ($rowStart, $rowLast, $dTABLE, $rowTotal, $buttonLink, $ID, '$FILE');

echo "&nbsp; : &nbsp; or enter start row: <input type=\"text\" name=\"from\" size=\"5\" value=\"$rowStart\">\n";

// set up the table
echo "<table class=list>\n";

// echo "<tr><td></td>\n"; // first field is a label set below for the query rows only
// ** TABLE HEADER **
// echo "</tr>\n";

// query restriction form
// echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// ** TABLE QUERY **
// echo "</tr>\n";

// query restriction form
// echo "<tr>\n";
// echo "<td class=list> <input type=\"text\" name=\"expID\"></td>\n";
// TABLE QUERY 
// echo "</tr>\n";

// close the table and form
echo "</table>\n";
echo "<input type=\"submit\" name=\"constraints\" value=\"refine search\">\n";
$pass = $ID['pass'];
$proj = $ID['proj'];
$menu = $ID['menu'];
echo "<input type=\"hidden\" name=\"pass\" value=\"$pass\">\n";
echo "<input type=\"hidden\" name=\"proj\" value=\"$proj\">\n";
echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
echo "</form>\n";

if ($row[0] == 0) {
  menu_end();
}

$datFilename = tempnam("/tmp", "dvodat.");
$pngFilename = $datFilename.".png";

$datfile = fopen($datFilename, "w");
chmod($datFilename, 0777);

// output the results to tmpfile
while ($qry->fetchInto($row)) {
  $line = "";
  foreach ($row as $i => $value) {
    switch ($value) {
    case 'g.00000':
      $filter = 1;
      break;
    case 'r.00000':
      $filter = 2;
      break;
    case 'i.00000':
      $filter = 3;
      break;
    case 'z.00000':
      $filter = 4;
      break;
    case 'y.00000':
      $filter = 5;
      break;
    case 'w.00000':
      $filter = 6;
      break;
    case 'OPEN':
      $filter = 0;
      break;
    case 'g':
      $filter = 1;
      break;
    case 'r':
      $filter = 2;
      break;
    case 'i':
      $filter = 3;
      break;
    case 'z':
      $filter = 4;
      break;
    case 'y':
      $filter = 5;
      break;
    case 'w':
      $filter = 6;
      break;
    default:
      $filter = $value;
      break;
    }
    $line .= "$filter ";
  }
  if (preg_match('/^[ \t]*$/', $line) == 0) {//Don't write empty lines
    $line = preg_replace('/[ \t]*$/', '', $line);
    fwrite ($datfile, $line."\n");
  }
}
fclose($datfile);

echo "<img src=\"$PLOTTER?input=$datFilename&output=$pngFilename&title='$PLOTTITLE'\"><br>\n";

// ** TAIL CODE **

echo "<small> WHERE: $WHERE<br><br></small>\n";
echo "<small> SQL: $sql<br><br></small>\n";

echo "Temporary files are [$datFilename] and [$pngFilename]\n";

menu_end();

?>
