#include <infocon.style>
#include "hwdb.inc"

<page func=InfoCon title="Hardware Database">

<?
<dbconnect>

  function display_components ($headline, $sth)
  {
    if (pg_NumRows ($sth) > 0) {
      printf ("<h3>%s</h3>\n", $headline);
    }

    for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
      $row = pg_fetch_array ($sth, $nr);
      $start = explode (" ", $row['starttime']);
      if ($row['endtime']) {
        $end = explode (" ", $row['endtime']);
	printf ("%s <a href=\"component.php?id=%d\">%s</a> %s &mdash; %s",
		$row['type'], $row['component'], $row['name'], $start[0], $end[0]);
      } else {
	printf ("%s <a href=\"component.php?id=%d\">%s</a> since %s",
		$row['type'], $row['component'], $row['name'], $start[0]);
      }
      if ($row['usage']) {
        printf (" (%s)", $row['usage']);
      }
      echo "<br />";
    }
  }

  if ($_GET[id]) {
    $fields = array ('compound.name',
		     'compound.hostname',
		     'compound.ip',
		     'compound.ident',
		     'compound.location',
		     'compound.status',
		     'organisation.name as organisation');
    $query = sprintf ("SELECT %s "
		     ."FROM compound,organisation "
		     ."WHERE location = organisation.id AND compound.id = %d",
		     implode (",", $fields), $_GET[id]);
    $sth = pg_exec ($dbh, $query) or die_query("Cannot issue query!", $query);

    if (pg_NumRows ($sth) > 0) {
      $row = pg_fetch_array ($sth, 0);

      printf ("<h3>Compound %s</h3>", $row['name']);
      if ($row['hostname']) { printf ("<br>Hostname: %s", $row['hostname']); }
      if ($row['ip']) { printf ("<br>IP Addr: %s", $row['ip']); }
      if ($row['ident']) { printf ("<br>Ident: %s", $row['ident']); }
      printf ("<br>Location: <a href=\"organisation.php?id=%d\">%s</a>",
	      $row['location'], $row['organisation']);
      printf ("<br>Status: %s", $row['status']);
      if ($row['comment']) {
<protect>
      printf ("\n<blockquote>\n%s\n</blockquote>\n", ereg_replace ("\n\n", "<p>", $row['comment']));
</protect>
      }
    } else {
      echo "<h3>No id given!</h3>";
    }

    $query = sprintf ("SELECT component.name,component,screw.starttime,screw.endtime,types.name as type,usage "
		     ."FROM screw,component,types "
		     ."WHERE compound = %d AND component = component.id "
		     ."AND screw.endtime IS NULL "
		     ."AND types.id = hwtype "
		     ."ORDER BY types.name,component.name,screw.starttime", $_GET[id]);
    $sth = pg_exec ($dbh, $query) or die_query("Cannot issue query!", $query);
    display_components ("Components", $sth);

    $query = sprintf ("SELECT component.name,component,screw.starttime,screw.endtime,types.name as type,usage "
		     ."FROM screw,component,types "
		     ."WHERE compound = %d AND component = component.id "
		     ."AND screw.endtime IS NOT NULL "
		     ."AND types.id = hwtype "
		     ."ORDER BY types.name,component.name,screw.starttime", $_GET[id]);
    $sth = pg_exec ($dbh, $query) or die_query("Cannot issue query!", $query);
    display_components ("Outdated Components", $sth);
  }
?>

</page>

# Local variables:
# mode: text
# mode: auto-fill
# end:
