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

<postgresconnect>
<build_timeframe>
<tab_frag_stubs>
<fragment_ok>
<ltpage title="Supporter -- Open Jobs">

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

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

  pg_exec ($dbh, "SET DateStyle = 'ISO'") or die("Datenbank-Abfrage!");

  $limit_days = '';
  $count = 0;
  reset ($ltdays);
  while (list($key,$day) = each ($ltdays)) {
    if ($GLOBALS["day_".$key] == 1) {
      $count++;
      $foo = explode (",", $day);
      if (strlen ($limit_days) == 0) {
        $limit_days = $foo[0];
     } else {
        $limit_days .= ", " . $foo[0];
      }
    }
  }
  # Limitation to all days is no limitation
  if ($count == count($ltdays)) {
    $limit_days = '';
  }

  $limit_sections = '';
  $where_sections = '';
  $count = 0;
  $query = "SELECT DISTINCT section FROM job WHERE section <> '' ORDER BY section";
  $sth = pg_exec ($dbh, $query) or die ("Datenbank-Abfrage!");

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

    $sect = "section_".strip_space($row['section']);

    if ($GLOBALS[$sect] == 1) {
      $count++;
      if (strlen ($limit_sections) == 0) {
        $limit_sections = $row['section'];
	$where_sections = sprintf ("( section = '%s' ", $row['section']);
     } else {
        $limit_sections .= ", " . $row['section'];
	$where_sections .= sprintf ("OR section = '%s' ", $row['section']);
      }
    }
  }
  # Limitation to all sections is no limitation
  if ($count == pg_NumRows($sth)) {
    $limit_sections = '';
    $where_sections = '';
  } else {
    if (strlen ($where_sections) > 0) {
      $where_sections .= ") ";
    }
  }

  if (strlen ($limit_days) > 0 || strlen ($limit_sections) > 0) {
    printf ("<p><b>Output limited.</b>  <a href=\"jobs.php3\">unlimited list</a>");

    if (strlen ($limit_days) > 0) {
      printf ("<br>&nbsp;&nbsp;&nbsp;Days: %s", $limit_days);
    }
    if (strlen ($limit_sections) > 0) {
      printf ("<br>&nbsp;&nbsp;&nbsp;Sections: %s", $limit_sections);
    }
    echo ("</p>\n");
  }

?>

<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);

  $query = "SELECT job.id,title,starttime,endtime,fragment.id as fragment FROM job,fragment "
		   ."WHERE job.id = fragment.job ";
  if (strlen($where_sections) > 0) {
    $query .= "AND " . $where_sections;
  }
  $query .= "ORDER BY title,starttime";

  $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['title']) {
      if ($thisname != '') {
        $available[$thisname] = $avail;
      }
      $avail = array ('id' => $row['id']);
      $thisname = $row['title'];
    }

    $miss = missing_supporters ($dbh, $row['fragment']);
    if ($miss > 0) {
      # FIXME: Link setzen
      $foo = explode (" ", $row['starttime']);
      $time = build_timeframe ($row['starttime'], $row['endtime']);
      if (strlen ($avail[$foo[0]]) > 0) {
	$avail[$foo[0]] .= "&nbsp;" . $time;
	if ($miss > 1) {
	  $avail[$foo[0]] .= "(" . $miss . ")";
	}
      } else {
	$avail[$foo[0]] = $time;
	if ($miss > 1) {
	  $avail[$foo[0]] .= "(" . $miss . ")";
	}
      }
    }
  }
  $available[$thisname] = $avail;

  $colindex = count ($col);
  $color = "<cold>";
  $empty = true;
  $line = '';
  reset ($available);
  while (list($name,) = each ($available)) {

    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 ($empty) {
      $line = '';
    } else {
      $color = $color=="<cold>"?"<coln>":"<cold>";
      printf ($f_row_head, $color, "job", $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=open.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":''; ?>"
		>
<?
    }
  }

  $first = true;

  $query = "SELECT DISTINCT section FROM job WHERE section <> '' ORDER BY section";
  $sth = pg_exec ($dbh, $query) or die ("Datenbank-Abfrage!");

  for ($nr = 0; $nr < pg_NumRows($sth); $nr++)
  {
    $row = pg_fetch_array($sth, $nr);
    $sect = "section_".strip_space($row['section']);

    if ($first) {
      $first = false;

?>
<row_checkbox	name="<? echo $sect; ?>"
		title="<b>Sections</b>"
		descr="<? echo $row['section']; ?>"
		checked="<? echo (!isset ($limitation) || $GLOBALS[$sect] == 1)?"checked":''; ?>"
		>
<?  } else { ?>
<row_checkbox	name="<? echo $sect; ?>"
		title="&nbsp;"
		descr="<? echo $row['section']; ?>"
		checked="<? echo (!isset ($limitation) || $GLOBALS[$sect] == 1)?"checked":''; ?>"
		>
<?
    }
  }
?>

<row_submit_plain submit="Redisplay">

</table></center>

</form>

</ltpage>

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

