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

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

<h3>Component Detail</h3>

<?
<dbconnect>

  if ($_GET[id]) {
    $fields = array ('component.id',
		     'component.name',
		     'serno',
		     'price',
		     'hwtype',
		     'types.name as type',
		     'owner as orgid',
		     'organisation.name as owner',
		     'starttime', 'endtime',
		     'status',
		     'component.comment'
		     );
    $query = sprintf ("SELECT %s "
		     ."FROM component,types,organisation "
		     ."WHERE hwtype = types.id "
		     ."AND organisation.id = component.owner "
		     ."AND component.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 ("<br>Name: <a href=\"edit_component.php?id=%d\">%s</a>", $row['id'], $row['name']);
      printf ("<br>Type: <a href=\"components.php?type=%d\">%s</a>", $row['hwtype'], $row['type']);
      printf ("<br>S/N: %s", $row['serno']);
      if ($row['price'] > 0) {
        printf ("<br>Price: %s", $row['price']);
      } else {
        printf ("<br>Price: n/a");
      }
      printf ("<br>Owner: <a href=\"organisation.php?id=%d\">%s</a>", $row['orgid'], $row['owner']);
      $start = explode (" ", $row['starttime']);
      printf ("<br>Begin: %s", $start[0]);
      if ($row['endtime']) {
	$end = explode (" ", $row['endtime']);
        printf ("<br>End: %s", $end[0]);
      }
      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 starttime,endtime,name,compound,usage "
		     ."FROM screw,compound "
		     ."WHERE component = %d AND compound = id "
		     ."ORDER BY starttime", $_GET[id]);
    $sth = pg_exec ($dbh, $query) or die_query("Cannot issue query!", $query);

    if (pg_NumRows ($sth) > 0) {
      echo "<h3>Use History</h3><p>";

      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']);
	} else {
	  $end = array ('today');
	}
	printf ("<br>%s - %s: <a href=\"compound.php?id=%d\">%s</a>",
		$start[0], $end[0], $row['compound'], $row['name']);
	if ($row['usage']) {
	  printf (" (%s)", $row['usage']);
	}
      }
      echo ("</p>");
    }
  }
?>

</page>

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