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

<postgresconnect>
<row_select>
<build_timeframe>

<ltpage title="Project Management">

<?
  if (!(is_supporter() && is_admin())) {
    die("You have no permissions to add or edit jobs.");
  }

  if (isset ($id)) {
    echo "<p>Edit job information.</p>";
  } else {
    echo "<p>Add new job.</p>";
  }

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

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

    $query = sprintf("SELECT * FROM job WHERE id = %d", $id);
    $sth = pg_exec($dbh, $query) or die ("Datenbank-Fehler");
    if (pg_NumRows($sth) > 0) {
      $row = pg_fetch_array($sth, 0);
    } else {
      die ("Cannot fetch rows for this job.");
    }
  } else {
    $row = array();
  }
?>

<center><table cellpadding=0 cellspacing=1>
<form action=update.php3 method=post>

<input type=hidden name=type value=job>
<input type=hidden name=id value="<? echo $id; ?>">

<row_input	name=title
		title="<b>Title</b>"
		value="<? echo $row['title']; ?>"
		>

<row_input	name=location
		title="<b>Location</b>"
		value="<? echo $row['location']; ?>"
		>

<row_input	name=priority
		title="<b>Priority</b>"
		value="<? echo $row['priority']>0?$row['priority']:''; ?>"
		>

<row_input	name=comment
		title="<b>Comment</b>"
		value="<? echo $row['comment']; ?>"
		>

<?
  $sections = array();
  $query = sprintf("SELECT DISTINCT section FROM job ORDER BY section");
  $sth2 = pg_exec($dbh, $query) or die ("Cannot get sections");
  for ($x = 0; $x < pg_NumRows($sth2); $x++)
  {
    $row2 = pg_fetch_array($sth2, $x);
    if (strlen ($row2['section'])) {
      $sections[$row2['section']] = $row2['section'];
    }
  }

  row_select ("Section", "section", $sections, $row['section'], 1);
?>

<row_input	name=usersection
		title=""
		value=""
		>

<row_textarea	name=description
		title="<b>Description</b>"
		value="<? echo $row['description']; ?>"
		>

<row_delim	msg="<b>Job Fragments</b>"
		>

<?
  if (isset ($id) && $row['id'] > 0) {
    $oids = array();
    $query = sprintf ("SELECT oid,id,starttime,endtime,min,max,comment FROM fragment "
		     ."WHERE job = %d ORDER BY starttime", $row['id']);
    $sth = pg_exec($dbh, $query) or die ("Cannot get job fragments.");

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

	$append = sprintf ("_%d", $row['oid']);
	$oids[] = $row['oid'];

	# Bug: This can only handle fragments that don't pass day borders
	$foo = explode (" ", $row['starttime']);
	$bar = explode (" ", $row['endtime']);
	$fragment["date".$append] = $foo[0];

	$fragment["timeframe".$append] = build_timeframe ($row['starttime'], $row['endtime']);

	if ($row['max'] > $row['min']) {
	  $fragment["minmax".$append] = sprintf ("%d-%d", $row['min'], $row['max']);
	} else {
	  $fragment["minmax".$append] = sprintf ("%d", $row['min']);
	}
	$fragment["comment".$append] = $row['comment'];
      }

      $foo = '';
      reset ($oids);
      while (list(,$oid) = each ($oids)) {
	$append = sprintf ("_%d", $oid);
	$foo = $foo . " " . $oid;

	row_select ("Date", "date".$append, $ltdays, $fragment["date".$append], 0);
?>
<row_input	name="timeframe<? echo $append; ?>"
		title="<b>Timeframe</b>"
		value="<? echo $fragment["timeframe".$append]; ?>"
		>
<row_input	name="minmax<? echo $append; ?>"
		title="<b>Min-Max People</b>"
		value="<? echo $fragment["minmax".$append]; ?>"
		>
<row_input	name="comment<? echo $append; ?>"
		title="<b>Comment</b>"
		value="<? echo $fragment["comment".$append]; ?>"
		>
<?
      }
      printf ("<input type=\"hidden\" name=\"oids\" value=\"%s\">", $foo);

      # Ensure the entire list won't be too long
      $maxframes = 2;
    }
  }
?>

<?
  for ($nr=0; $nr < $maxframes; $nr++) {
    $append = sprintf ("_%d", $nr);
    row_select ("Date", "date".$append, $ltdays, "", 0);
?>

<row_input	name="timeframe<? echo $append; ?>"
		title="<b>Timeframe</b>"
		value=""
		>
<row_input	name="minmax<? echo $append; ?>"
		title="<b>Min-Max People</b>"
		value=""
		>
<row_input	name="comment<? echo $append; ?>"
		title="<b>Comment</b>"
		value=""
		>
<? } ?>

<? if ($id > 0) { ?>
<row_radiobox	name=delete
		title="<b>Delete</b>"
		checked_0="checked"
		checked_1=""
		>
<? } ?>
<row_submit>

</form>
</table></center>

<h3>Legend</h3>

<p><b>Location</b>: Where does the job need to be done?

<p><b>Priority</b>: optional, numerical (10 is more important than 90).

<p><b>Timeframe</b>: Something like 10-12 (for 10 o'clock - 12
o'clock), or just 11 (for 11 o'clock and a minimum job duration of 30
minutes).

<p><b>Min-Max</b>: Number of people required for this job, either 2
(for two people required) or 1-3 (for one to three people required).

</ltpage>

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