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

<postgresconnect>
<build_timeframe>
<tab_avail_stubs>
<ltpage title="Supporter -- Availability">

<?
  if (!is_supporter() && !is_also_supporter()) {
    die ("Only supporters may read these page.");
  }

  $limit = '';
  reset ($ltdays);
  while (list($key,$day) = each ($ltdays)) {
    if ($GLOBALS["day_".$key] == 1) {
      $foo = explode (",", $day);
      if (strlen ($limit) == 0) {
        $limit = $foo[0];
     } else {
        $limit .= ", " . $foo[0];
      }
    }
  }

  if (strlen ($limit) > 0) {
    printf ("<p><b>Output limited to</b>: %s</p>", $limit);
  }

?>

<p>
<table cellpadding=0 cellspacing=1>
<?
  printf ($f_header_head, "<colh>");
  reset ($ltdays);
  $col = array();
  while (list($key,$day) = each ($ltdays)) {
    if ($limitation != 1 || $GLOBALS["day_".$key] == 1) {
      $foo = explode (",", $day);
      printf ($f_header_day, $foo[0]);
      $col[] = $key;
    }
  }
  echo ($f_header_tail);

  $person = array ();

  if ($limitation != 1) {
    $query = "SELECT id,name FROM person,junction "
	    ."WHERE person = id AND junction.project = 'Supporter' 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);
      $person[] = $row['name'];
      $available[$row['name']] = array ('id' => $row['id']);
    }
  }

  $query = "SELECT id,name,starttime,endtime FROM availability,person "
		   ."WHERE id = person "
		   ."ORDER BY upper(name)";

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

  $thisname = '';
  for ($nr = 0; $nr < pg_NumRows($sth); $nr++)
  {
    $row = pg_fetch_array($sth, $nr);

    if ($thisname != $row['name']) {
      if ($thisname != '') {
        $available[$thisname] = $avail;
      }
      if ($limitation == 1) {
        $person[] = $row['name'];
      }
      $avail = array ('id' => $row['id']);
      $thisname = $row['name'];
    }

    $foo = explode (" ", $row['starttime']);
    $time = build_timeframe ($row['starttime'], $row['endtime']);
    if (strlen ($avail[$foo[0]]) > 0) {
      $avail[$foo[0]] .= "&nbsp;" . $time;
    } else {
      $avail[$foo[0]] = $time;
    }
  }
  $available[$thisname] = $avail;

  $colindex = count ($col);
  $color = "<cold>";
  $empty = true;
  $line = '';
  reset ($person);
  while (list(,$name) = each ($person)) {
    reset ($col);
    while (list(,$day) = each ($col)) {
      if (strlen ($available[$name][$day]) > 0) {
	$empty = false;
        $line .= sprintf ($f_row_day, $available[$name][$day]);
      } else {
        $line .= sprintf ($f_row_day, "&nbsp;");
      }
    }

    if (isset ($limitation) && $empty) {
      $line = '';
    } else {
      $color = $color=="<cold>"?"<coln>":"<cold>";
      printf ($f_row_head, $color, "supporter", $available[$name]['id'], $name);
      echo ($line);
      echo ($f_row_tail);
      $empty = true;
      $line = '';
    }
  }
?>
</table>
</p>

<p>
<b>Limit Output</b>.  You can limit the output to certain days and
people working on certain tasks.</p>

<form method=get action=availability.php3>
<input type=hidden name=limitation value=1>

<center><table cellpadding=0 cellspacing=1>

<?
  $first = true;
  reset ($ltdays);
  while (list($key,$day) = each ($ltdays)) {
    if ($first) {
      $first = false;
?>
<row_checkbox	name="day_<? echo $key; ?>"
		title="<b>Days</b>"
		descr="<? echo $day; ?>"
		checked="<? echo (!isset ($limitation) || $GLOBALS["day_".$key] == 1)?"checked":''; ?>"
		>
<?
    } else {
?>
<row_checkbox	name="day_<? echo $key; ?>"
		title="&nbsp;"
		descr="<? echo $day; ?>"
		checked="<? echo (!isset ($limitation) || $GLOBALS["day_".$key] == 1)?"checked":''; ?>"
		>
<?
    }
  }
?>

<row_submit_plain submit="Redisplay">

</table></center>

</form>

</ltpage>

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

