#include <infodrom.style>
#include "lt.style"
#include "functions.inc"

<postgresconnect>
<ltpage title="Nili Management">

<?
  $t_head  = "<p><div align=\"center\"><table cellpadding=\"0\" cellspacing=\"0\" summary=\"\">";
  $t_title = "<tr bgcolor=\"%s\"><th><font face=\"Helvetica,Arial\">Name</font></th>"
	                       ."<th><font face=\"Helvetica,Arial\">Location</font></th>"
	                       ."<th><font face=\"Helvetica,Arial\"><name-event-date-1></font></th>"
	                       ."<th><font face=\"Helvetica,Arial\"><name-event-date_0></font></th>"
	                       ."<th><font face=\"Helvetica,Arial\"><name-event-date_1></font></th>"
	                       ."<th><font face=\"Helvetica,Arial\"><name-event-date_2></font></th>"
	                       ."<th><font face=\"Helvetica,Arial\"><name-event-date_3></font></th>"
	                       ."<th><font face=\"Helvetica,Arial\"><name-event-date_4></font></th></tr>";
  $t_row   = "<tr bgcolor=\"%s\"><td><font face=\"Helvetica,Arial\">%s</font></td>"
	                       ."<td><font face=\"Helvetica,Arial\">%s</font></td>"
	                       ."<td align=\"center\"><font face=\"Helvetica,Arial\">%s</font></td>"
	                       ."<td align=\"center\"><font face=\"Helvetica,Arial\">%s</font></td>"
	                       ."<td align=\"center\"><font face=\"Helvetica,Arial\">%s</font></td>"
	                       ."<td align=\"center\"><font face=\"Helvetica,Arial\">%s</font></td>"
	                       ."<td align=\"center\"><font face=\"Helvetica,Arial\">%s</font></td>"
	                       ."<td align=\"center\"><font face=\"Helvetica,Arial\">%s</font></td></tr>";

  $t_foot  = "</table></div><p>";

  $dbh = pg_pconnect ("<dbconnstring>")
	       or die("Unable to connect to SQL server");

  $query = "SELECT person.oid,person.nightplace,name,date(date),breakfast,dinner,sleeping,project "
          ."FROM person,nili "
	  ."WHERE person = id";

  if (strlen ($project) && is_member ($project)) {
    $query .= sprintf (" AND project = '%s'", addslashes ($project));
    printf ("<p><b>Limit to project</b>: %s</p>", $project);
  } else {
    $query .= sprintf (" AND project = '%s'", addslashes ($LINUXTAG_AUTH['project']));
    if (isset ($project)) {
      printf ("<p>Access to %s denied.</p>", $project);
      unset ($project);
    }
  }

  if (strlen ($nightplace)) {
    $query .= sprintf (" AND nightplace = '%s'", addslashes ($nightplace));
    printf ("<p><b>Limit to location</b>: %s</p>", $nightplace);
  }

  $query .= " AND date in ('<iso-event-date-1>', '<iso-event-date_0>', " .
	    "'<iso-event-date_1>', '<iso-event-date_2>', '<iso-event-date_3>', " .
            "'<iso-event-date_4>')";

  $query .= " ORDER BY upper(name)";

  $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");
  for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
    $row = pg_fetch_array ($sth, $nr);
    $nili[$row[2]][$row[3]] = $row;
  }

  if (pg_NumRows ($sth) > 0) {
    echo ($t_head);
    printf ($t_title, "<colh>");

    $color = "<coln>";
    $name='';
    $people = 0;
    $dinner = 0;
    $breakfast = 0;
    $days = array ('<iso-event-date-1>', '<iso-event-date_0>', '<iso-event-date_1>', '<iso-event-date_2>', '<iso-event-date_3>', '<iso-event-date_4>');
    reset ($nili);
    while (list($name,$row) = each($nili)) {
      $color=$color == "<cold>"?"<coln>" : "<cold>";
      $people++;

      $info = array ();
      $where = '';
      reset ($days);
      while (list(,$date) = each($days)) {
	$info[$date] = sprintf ("&nbsp;%s&nbsp;%s&nbsp;%s&nbsp;",
				$row[$date]['breakfast']==1?"B":"&nbsp;",
				$row[$date]['dinner']==1?"D":"&nbsp;",
				$row[$date]['sleeping']==1?"S":"&nbsp;");
	$sum[$date]['breakfast'] += $row[$date]['breakfast'];
	$sum[$date]['dinner'] += $row[$date]['dinner'];
	$sum[$date]['sleeping'] += $row[$date]['sleeping'];
	if (strlen ($row[$date]['nightplace'])) { $where = $row[$date]['nightplace']; }
	if (strlen ($row[$date]['oid'])) { $oid = $row[$date]['oid']; }
	if (strlen ($row[$date]['project'])) { $project = $row[$date]['project']; }
      }

      $link = $name;
#     Commented out for the moment, 2002-05-26, Joey
#     if ($project == 'Supporter') {
#	$link = sprintf ("<a href=\"../supporter/edit.php3?oid=%d\">%s</a>", $oid, $name);
#     } else {
#	$link = sprintf ("<a href=\"../projects/edit_person.php3?oid=%d\">%s</a>", $oid, $name);
#     }

      printf ($t_row, $color, $link, strlen ($where)?$where:"&nbsp;",
	      $info['<iso-event-date-1>'],
	      $info['<iso-event-date_0>'],
	      $info['<iso-event-date_1>'],
	      $info['<iso-event-date_2>'],
	      $info['<iso-event-date_3>'],
	      $info['<iso-event-date_4>']);
    }

    $color=$color == "<cold>"?"<coln>" : "<cold>";
    $link = "<b>Sum</b>";

    $info = array ();
    reset ($days);
    while (list(,$date) = each($days)) {
      $info[$date] = sprintf ("&nbsp;%d&nbsp;%d&nbsp;%d&nbsp;",
		     $sum[$date]['breakfast'], $sum[$date]['dinner'], $sum[$date]['sleeping']);

      $breakfast += $sum[$date]['breakfast'];
      $dinner += $sum[$date]['dinner'];
    }
    printf ($t_row, $color, $link, "&nbsp;",
	    $info['<iso-event-date-1>'],
	    $info['<iso-event-date_0>'],
	    $info['<iso-event-date_1>'],
	    $info['<iso-event-date_2>'],
	    $info['<iso-event-date_3>'],
	    $info['<iso-event-date_4>']);

    echo ($t_foot);
  }

  printf ("<p>People: %d", $people);

  if (is_supporter() && is_admin()) {
    printf ("<br>Breakfast: %d (%5.2f)", $breakfast, $breakfast * 3);
<when <not <string-eq "$(DINNER)" "" />>>
    printf ("<br>Dinner: %d (%5.2f)", $dinner, $dinner * 12);
</when>
  } else {
    printf ("<br>Breakfast: %d", $breakfast);
<when <not <string-eq "$(DINNER)" "" />>>
    printf ("<br>Dinner: %d", $dinner);
</when>
  }
?>

<p>
B = Breakfast<br>
D = Dinner<br>
S = Sleeping<br>


<? if ($people > 0) { ?>
<p>
<b>Change view</b>:
<form method=get action=nili.php3>
<input type=hidden name=project value="<? echo $project; ?>">

<?
  $query = "SELECT DISTINCT nightplace FROM person,nili WHERE person = id "
	  ."AND nightplace <> '' AND NOT nightplace IS NULL";

  if (isset ($project)) {
    $query .= sprintf (" AND project = '%s'", addslashes ($project));
  } else {
    $query .= sprintf (" AND project = '%s'", addslashes ($LINUXTAG_AUTH['project']));
  }

  $sth = pg_exec ($dbh, $query) or die("Datenbank-Abfrage!");

  if (pg_NumRows ($sth) > 1) {
?>

<br><b>Location</b>: <select name=nightplace><option value="">unlimited<?

    $nightplaces = array ();
    for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
      $row = pg_fetch_array ($sth, $nr);
      $nightplaces[] = $row['nightplace'];
    }

    sort ($nightplaces);
    for ($nr=0; $nr < count ($nightplaces); $nr++) {
      printf ("<option>%s", $nightplaces[$nr]);
    }
  echo ("</select>");
  }
?>

<br><input type=submit value="Redisplay">
</form>
<? } ?>

</ltpage>

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