#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_admin() && $new == 1) {
     if ($LINUXTAG_AUTH['project'] == 'Supporter') { ?>
<p>Add new supporter.
<?   } else { ?>
<p>Add new user.
<?   } ?>
<? } else { ?>
<p>Edit user information.
<? } ?>

<?

<when <not <string-eq "$(NILI)" "" />>>
function count_nili ($dbh, $nightplace)
{
  $query = "SELECT name,nightplace FROM person,nili WHERE person = id ";
  if (strlen ($nightplace)) {
    $query .= sprintf ("AND nightplace='%s' ", $nightplace);
  }
  $query .= "GROUP BY name,nightplace";

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

  # This is just a rough approximation...
  return pg_NumRows ($sth);
}

function uses_nili ($dbh, $nightplace, $person)
{
  $query = "SELECT name,nightplace FROM person,nili WHERE person = id ";
  if (strlen ($nightplace)) {
    $query .= sprintf ("AND nightplace='%s' ", $nightplace);
  }
  $query .= sprintf ("AND id=%s ", $person);

  $sth = pg_exec($dbh, $query) or die("Datenbank-Abfrage!");
  return pg_NumRows ($sth)>0?true:false;
}
</when>

if (empty ($id) && $new != 1) {
    $id = $LINUXTAG_AUTH['id'];
}

if ((!empty($id)) or ($new == 1))
{
    // A new user?

    if ($new == 1)
    {
        // Only a admin can create a new user
	if (!(is_admin()))
	{
	    die ("Only the project admin may create a new user.");
	}
	$row = array();
	# Default to let people log in
	$row['login'] = 1;
	# Default: opt-out infomails
	$row['infomail'] = 1;
	$row['project'] = $LINUXTAG_AUTH['project'];
    }
    else
    {

        // First we need to check if the current user is allowed to edit this user

        if (!allowed_to_edit($id))
        {
	    die("You are not allowed to edit this user.");
        }

        if (preg_match("/^[0-9]+$/", $id)) {
          $query = sprintf("SELECT * from person WHERE id = %d", $id);
	} else {
	  die ("ID must be numeric");
	}
        $sth = pg_exec($dbh, $query) or die("Datenbank-Abfrage!");

        if (pg_NumRows ($sth) > 0) {
        $row = pg_fetch_array ($sth, 0);
        } else {
	    die ("The user has disappeard.");
        }
    }

<when <not <string-eq "$(NILI)" "" />>>
    if (is_also_supporter()) {
      $edit_nili = true;
    } else {
      $edit_nili = false;
      if (count_nili ($dbh, "<nili_name>") > <nili_max>) {
	if (strlen ($row['name']) > 0) {
	  if (uses_nili ($dbh, '', $row['id'])) {
	    $edit_nili = true;
	  }
	}
      } else {
	$edit_nili = true;
      }
    }
</when>
?>

<form method=post action=update.php3>
<? if ($new <> 1) { ?>
<input type=hidden name=type value=person>
<input type=hidden name=id value="<? echo $row['id']; ?>">
<? } else { ?>
<input type=hidden name=type value=person_new>
<? } ?>

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

<? if (!is_supporter()) { ?>
<row_info	name=project
		title="<b>Project</b>"
		value="<? echo strlen ($row['project'])?$row['project']:$LINUXTAG_AUTH['project']; ?>"
		>
<? } ?>

<? if ($new <> 1) { ?>
<row_info	name=name
		title="<b>Name</b>"
		value="<? echo $row['name']; ?>"
		>
<? } else { ?>
<row_input	name=name
		title="<b>Name</b>"
		value=""
		>
<? } ?>

<? if (is_admin() && ($LINUXTAG_AUTH['project'] == $row['project'])) { ?>
# The Mail-Address can only be changed by an admin
<row_input	name=email
		title="<b>E-Mail</b>"
		value="<? echo $row['email']; ?>"
		>
<? } else { ?>
<row_info	name=email
		title="<b>E-Mail</b>"
		value="<? echo $row['email']; ?>"
		>
<? } ?>

<row_input	name=mobile
		title="<b>Mobile</b>"
		value="<? echo $row['mobile']; ?>"
		>
<row_input	name=phone
		title="<b>Phone</b>"
		value="<? echo $row['phone']; ?>"
		>

<when <not <string-eq "$(NILI)" "" />>>
<? if ($row['list'] == 0 && $edit_nili) { ?>
<row_input	name=nightplace
		title="<b>Nightplace</b>"
		value="<? echo $row['nightplace']; ?>"
		>
<? } ?>
</when>

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

<row_radiobox	name=infomail
		title="<b>Infomail</b>"
		checked_0="<? if ($row['infomail'] == 0) { echo 'checked'; } ?>"
		checked_1="<? if ($row['infomail'] == 1) { echo 'checked'; } ?>"
		>

<when <not <string-eq "$(SOCIALEVENT)" "" />>>
<?
   #
   # The social event box should only be available if
   #  - person is not a supporter since they are granted free entrance
   #  - less than <sev_limit> people have signed up for it
   #  - or the person has it marked already
   #
   $edit_sev = 0;
   if (! is_also_supporter()) {
     if ($row['socialevent'] == 1) {
       $edit_sev = 1;
     } else {
       $query = "SELECT count(*) FROM person WHERE socialevent = 1";
       $sth2 = pg_exec ($dbh, $query) or die ("Cannot execute social event lookup query");
       if (pg_NumRows($sth2) > 0) {
         $row2 = pg_fetch_array ($sth2, 0);
	 if ($row2[0] < <sev_limit>) {
	   $edit_sev = 1;
	 }
       } else {
         # looks like an internal error occurred, so better open registration
         $edit_sev = 1;
       }
     }
   }
?>

<? if ($edit_sev) { ?>
<row_checkbox	name=socialevent
		title="<b>Social Event</b>"
		descr="Price &euro; <sev_price>"
		checked="<? if ($row['socialevent'] == 1) { echo 'checked'; } ?>"
		>
<? } ?>
</when>

<when <not <string-eq "$(INSURANCE)" "" />>>
<row_radiobox	name=insurance
		title="<b>Insurance</b>"
		checked_0="<? if ($row['insurance'] == 0) { echo 'checked'; } ?>"
		checked_1="<? if ($row['insurance'] == 1) { echo 'checked'; } ?>"
		>
</when>

<? if (is_admin() && ($LINUXTAG_AUTH['project'] == $row['project'])
                  && !($LINUXTAG_AUTH['id'] == $row['id'])) { ?>
<row_checkbox	name=list
		title="<b>List</b>"
		descr="Address is a mailing list"
		checked="<? if ($row['list'] == 1) { echo 'checked'; } ?>"
		>
<? } ?>

<? if (is_admin() && ($LINUXTAG_AUTH['project'] == $row['project'])) { ?>
<row_checkbox	name=admin
		title="<b>Admin</b>"
		descr="Manages this project"
		checked="<? if ($row['admin'] == 1) { echo 'checked'; } ?>"
		>
<? } elseif (is_admin()) { ?>
# If he is not an admin, he can only edit a non-admin-user
<row_info	name=admin
		title="<b>Admin</b>"
		value=No
		>
<? } ?>

<? if (is_admin() && ($LINUXTAG_AUTH['project'] == $row['project'])) { ?>
<row_checkbox	name=login
		title="<b>Login</b>"
		descr="Permission to log in"
		checked="<? if ($row['login'] == 1) { echo 'checked'; } ?>"
		>
<? } elseif (is_admin()) { ?>
<row_info	name=login
		title="<b>Login</b>"
		value="<? if ($row['login'] == 1) { echo 'Yes'; } else { echo 'No'; } ?>"
		>
<? } ?>

<? if (is_admin()) { ?>
<row_checkbox	name=password
		title="<b>Password</b>"
		descr="Randomize password"
		checked="<? if ($new == 1) { echo 'checked'; } ?>"
		>
<? } ?>

# Now we care about nili
<when <not <string-eq "$(NILI)" "" />>>
<? if ($row['list'] == 0 && $edit_nili) {

$nili = array();

if ($new <> 1)
{
  $query = sprintf ("SELECT oid,breakfast,dinner,sleeping,comment,date(date) FROM nili WHERE person = %d" .
                  " 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>')", $row['id']);

  $sth2 = pg_exec ($dbh, $query) or die ("Cannot execute nili-query");

// This query is allowed to return 0 rows

  for ($x = 0; $x < pg_NumRows($sth2); $x++)
  {
	$row2 = pg_fetch_array ($sth2, $x);
	$nili[$row2[5]] = $row2;
  }

}
?>

<when <not <string-eq "$(DINNER)" "" />>>
<row_checkbox	name=dinner-<iso-event-date-1>
		title="<b><name-event-date-1></b>"
		descr="Dinner"
		checked="<? if($nili['<iso-event-date-1>']['dinner'] == 1) { echo 'checked'; } ?>"
		>
<row_checkbox	name=sleeping-<iso-event-date-1>
		title="<iso-event-date-1>"
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date-1>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<when <string-eq "$(DINNER)" "" />>
<row_checkbox	name=sleeping-<iso-event-date-1>
		title="<b><name-event-date-1></b>"
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date-1>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<row_checkbox	name=breakfast-<iso-event-date_0>
		title="<b><name-event-date_0></b>"
		descr="Breakfast"
		checked="<? if($nili['<iso-event-date_0>']['breakfast'] == 1) { echo 'checked'; } ?>"
		>

<when <not <string-eq "$(DINNER)" "" />>>
<row_checkbox	name=dinner-<iso-event-date_0>
		title="<iso-event-date_0>"
		descr="Dinner"
		checked="<? if($nili['<iso-event-date_0>']['dinner'] == 1) { echo 'checked'; } ?>"
		>
<row_checkbox	name=sleeping-<iso-event-date_0>
		title=""
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_0>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<when <string-eq "$(DINNER)" "" />>
<row_checkbox	name=sleeping-<iso-event-date_0>
		title="<iso-event-date_0>"
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_0>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<row_checkbox	name=breakfast-<iso-event-date_1>
		title="<b><name-event-date_1></b>"
		descr="Breakfast"
		checked="<? if($nili['<iso-event-date_1>']['breakfast'] == 1) { echo 'checked'; } ?>"
		>

<when <not <string-eq "$(DINNER)" "" />>>
<row_checkbox	name=dinner-<iso-event-date_1>
		title="<iso-event-date_1>"
		descr="Dinner"
		checked="<? if($nili['<iso-event-date_1>']['dinner'] == 1) { echo 'checked'; } ?>"
		>
<row_checkbox	name=sleeping-<iso-event-date_1>
		title=""
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_1>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<when <string-eq "$(DINNER)" "" />>
<row_checkbox	name=sleeping-<iso-event-date_1>
		title="<iso-event-date_1>"
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_1>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<row_checkbox	name="breakfast-<iso-event-date_2>"
		title="<b><name-event-date_2></b>"
		descr="Breakfast"
		checked="<? if($nili['<iso-event-date_2>']['breakfast'] == 1) { echo 'checked'; } ?>"
		>

<when <not <string-eq "$(DINNER)" "" />>>
<row_checkbox	name="dinner-<iso-event-date_2>"
		title="<iso-event-date_2>"
		descr="Dinner"
		checked="<? if($nili['<iso-event-date_2>']['dinner'] == 1) { echo 'checked'; } ?>"
		>
<row_checkbox	name="sleeping-<iso-event-date_2>"
		title=""
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_2>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<when <string-eq "$(DINNER)" "" />>
<row_checkbox	name="sleeping-<iso-event-date_2>"
		title="<iso-event-date_2>"
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_2>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<row_checkbox	name="breakfast-<iso-event-date_3>"
		title="<b><name-event-date_3></b>"
		descr="Breakfast"
		checked="<? if($nili['<iso-event-date_3>']['breakfast'] == 1) { echo 'checked'; } ?>"
		>

<when <not <string-eq "$(DINNER)" "" />>>
<row_checkbox	name="dinner-<iso-event-date_3>"
		title="<iso-event-date_3>"
		descr="Dinner"
		checked="<? if($nili['<iso-event-date_3>']['dinner'] == 1) { echo 'checked'; } ?>"
		>
<row_checkbox	name="sleeping-<iso-event-date_3>"
		title=""
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_3>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<when <string-eq "$(DINNER)" "" />>
<row_checkbox	name="sleeping-<iso-event-date_3>"
		title="<iso-event-date_3>"
		descr="Sleeping"
		checked="<? if($nili['<iso-event-date_3>']['sleeping'] == 1) { echo 'checked'; } ?>"
		>
</when>

<row_checkbox	name="breakfast-<iso-event-date_4>"
		title="<b><name-event-date_4></b>"
		descr="Breakfast"
		checked="<? if($nili['<iso-event-date_4>']['breakfast'] == 1) { echo 'checked'; } ?>"
		>
<? } ?>
</when>

# When should availability be displayed:
#  a) user is a supporter and edits his own data
#  b) admin creates new supporter
#  c) admin edits supporter data
#  d) admin edits is own data

#  a) !isset($new) && !isset($user) && !is_admin() && is_also_supporter()
#  b) isset($new) && is_admin() && is_supporter()
#  c) !isset($new) && isset ($user) && is_admin() && is_supporter()
#  d) !isset($new) && !isset($user) && is_admin() && is_supporter()

# [ isset ($user) ==>  $user != $LINUXTAG_AUTH['email']

<? if ( (!isset($new) && ($id == $LINUXTAG_AUTH['id']) && !is_admin() && (is_supporter() || is_also_supporter())) ||
        ( isset($new) && is_admin() && is_supporter()) ||
	(!isset($new) && ($id != $LINUXTAG_AUTH['id']) && is_admin() && is_supporter()) ||
	(!isset($new) && ($id == $LINUXTAG_AUTH['id']) && is_admin() && is_supporter()) ) { ?>

<row_delim	msg="<b>Availability</b>"
		>

<?
  if (isset ($new)) {
    reset ($ltdays);
    $counter=0;
    while (list($key,$day) = each ($ltdays)) {
      if ($day == $ltdays[0]) { continue; }
      $append = sprintf ("_%d", $counter);

      row_select ("Date", "date".$append, $ltdays, $key, 0);
?>

<row_input	name="shift<? echo $append; ?>"
		title="<b>Shift</b>"
		value=""
		>
<row_input	name="comment<? echo $append; ?>"
		title="<b>Comment</b>"
		value=""
		>

<?
      $counter++;
    }
    $empties = 0;
  } else {
    $thisday = '';
    $counter = 0;
    $query = sprintf ("SELECT starttime,endtime,comment,oid FROM availability "
		     ."WHERE person = %d ORDER BY starttime",
		     addslashes ($row['id']));
    $sth = pg_exec($dbh, $query) or die ("Database Query");
    for ($nr=0; $nr < pg_NumRows ($sth); $nr++) {
      $append = sprintf ("_%d", $counter);
      $row = pg_fetch_array($sth, $nr);

      printf ("<input type=\"hidden\" name=\"oid%s\" value=\"%d\">", $append, $row['oid']);
      $foo = explode (" ", $row['starttime']);
      if ($thisday != $foo[0]) {
        $thisday = $foo[0];
	row_select ("Date", "date".$append, $ltdays, $thisday, 0);
      }
      $shift = build_timeframe ($row['starttime'], $row['endtime']);      
?>
<row_input	name="shift<? echo $append; ?>"
		title="<b>Shift</b>"
		value="<? echo $shift; ?>"
		>
<row_input	name="comment<? echo $append; ?>"
		title="<b>Comment</b>"
		value="<? echo $row['comment']; ?>"
		>
<?
      $counter++;
    }

    
    if (strlen ($row2['section'])) {
      $sections[$row2['section']] = $row2['section'];
    }
    if (pg_NumRows ($sth) > 0) {
      $empties = 2;
    } else {
      $empties = 5;
    }
  }

  # Warning: Continues to use the counter variable from above
  for ($nr=0; $nr < $empties; $nr++) {
    $append = sprintf ("_%d", $counter);
    row_select ("Date", "date".$append, $ltdays, "", 1);
?>
<row_input	name="shift<? echo $append; ?>"
		title="<b>Shift</b>"
		value=""
		>
<row_input	name="comment<? echo $append; ?>"
		title="<b>Comment</b>"
		value=""
		>
<?
    $counter++;
  }
  printf ("<input type=\"hidden\" name=\"maxcounter\" value=\"%d\">", $counter);
?>

<? } ?>

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

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

<?   if (is_also_supporter()) { ?>

<p>Shift is something like "10-12" or "10:15-12:30" and denotes the
timeframe in which you have time and interest to help out with the
general event.  Please note that you should always have enough time to
attend talks and meetings as well.  You should hence define the shifts
so that there are holes when you attend a talk etc.</p>

<?   } ?>

<?   if (!is_admin()) { ?>

<p>If you want us to know your mobile phone number (which may be
useful if something goes wrong or we need to contact you during the
exhibition), you may want to add it in the format "+49 170 3292539" to
the proper phone field.

<?   } else { ?>

<p>Please note your mobile phone number in the form above if you can
be reached via a mobile during LinuxTag and a few days before.  This
may be helpful if we need to get in touch with you accidently when
something unplanned happens during the time when we are already in
Karlsruhe preparing the event and are not good with email anymore.
The format for phone numbers is "+49 170 3292539".</p>

<?   } ?>
<? } ?>

<p>The list of Breakfast/Dinner/Sleeping is only relevant for the <a
href="http://www.infodrom.org/Debian/events/LinuxTag2005/faq.html#sleep">\
facility</a> the local user group provides for the community as crash
space.  Details should be in the FAQ.</p>

</ltpage>
