<h2>Users</h2>
<?php
include_once($UTIL_DIR . "/user.php");
$users = new Users($DATA_DIR. "/users.xml");
?>


<div class="small_header">Add user</div>
<?php
if($task == "adduser") {
	if($password1 == $password2) {
		if(!$users->findUser($newuserid)) {
			$user = new User($newuserid,
											 sha1(md5($newpassword1)),
											 $newmodule_users,
											 $newmodule_news,
											 $newmodule_events,
											 $newmodule_guestbook,
											 $newmodule_gallery,
											 $newmodule_user,
											 $newmodule_newsletter);
			$users->add($user);
			$users->write();
			echo "<p>User <em>" . $newuserid . "</em> added successfully.</p>\n";
		} else {
			echo "<p>ERROR: User <em>" . $newuserid . "</em> already exists.</p>\n";
		}
	} else {
		echo "<p>ERROR: Passwords do not match.</p>\n";
	}
}
?>
<form method="post" action="?page=admin&amp;module=users&amp;task=adduser">
  <p>
  UserID: <input name="newuserid" value="<?php echo $newuserid; ?>"/>
  </p>
  <p>
    <input name="newmodule_users"
           type="checkbox" <?php if($newmodule_users == "on") echo "checked"; ?>/>
    Add/edit.delete users:<br/>
    <input name="newmodule_user"
           type="checkbox" <?php if($newmodule_user == "on") echo "checked"; ?>/>
    Change password:<br/>
    <input name="newmodule_news"
           type="checkbox" <?php if($newmodule_news == "on") echo "checked"; ?>/>
    Add news:<br/>
    <input name="newmodule_events"
           type="checkbox" <?php if($newmodule_events == "on") echo "checked"; ?>/>
    Add events:<br/>
    <input name="newmodule_gallery"
           type="checkbox" <?php if($newmodule_gallery == "on") echo "checked"; ?>/>
    Add images in gallery:<br/>
    <input name="newmodule_newsletter"
           type="checkbox" <?php if($newmodule_newsletter == "on") echo "checked"; ?>/>
    Send newsletter:<br/>
    <input name="newmodule_guestbook"
           type="checkbox" <?php if($newmodule_guestbook == "on") echo "checked"; ?>/>
    Modify guestbook
  </p>
  <p>
    New password:
    <input type="password" name="newpassword1" style="width: 100px;" value="<?php echo $newpassword1; ?>"/>
  </p>
  <p>
    Confirm password:
    <input type="password" name="newpassword2" style="width: 100px;" value="<?php echo $newpassword2; ?>"/>
  </p>
  <p>
  <button type="submit">Add user</button>
  </p>
</form>

<div class="small_header">Edit user</div>
<?php
if($task == "updateuser") {
	$user = $users->findUser($edituserid);
	$user->users = $module_users;
	$user->user = $module_user;
	$user->news = $module_news;
	$user->events = $module_events;
	$user->gallery = $module_gallery;
	$user->newsletter = $module_newsletter;
	$user->guestbook = $module_guestbook;
	if($password1 != "") {
		if($password1 == $password2) {
			$user->password = sha1(md5($password1));
			$users->add($user);
			$users->write();
			echo "<p>User <em>" . $edituserid . "</em> successfully updated.</p>\n";
		} else {
			echo "<p>ERROR: Passwords do not match</p>\n";
			$task = "edituser";
		}
	} else {
		$users->add($user);
		$users->write();
		echo "<p>User <em>" . $edituserid . "</em> successfully updated. Password left untouched.</p>\n";
	}
}
?>
<?php
if($task == "edituser") {
	$user = $users->findUser($edituserid);
?>
<form method="post" action="?page=admin&amp;module=users&amp;task=updateuser">
  <p>
  UserID: <input name="edituserid" readonly value="<?php echo $user->userid; ?>"/>
  </p>
  <p>
    <input name="module_users"
           type="checkbox" <?php if($user->users == "on") echo "checked"; ?>/>
    Add/edit.delete users:<br/>
    <input name="module_user"
           type="checkbox" <?php if($user->user == "on") echo "checked"; ?>/>
    Change password:<br/>
    <input name="module_news"
           type="checkbox" <?php if($user->news == "on") echo "checked"; ?>/>
    Add news:<br/>
    <input name="module_events"
           type="checkbox" <?php if($user->events == "on") echo "checked"; ?>/>
    Add events:<br/>
    <input name="module_gallery"
           type="checkbox" <?php if($user->gallery == "on") echo "checked"; ?>/>
    Add images in gallery:<br/>
    <input name="module_newsletter"
           type="checkbox" <?php if($user->newsletter == "on") echo "checked"; ?>/>
    Send newsletter:<br/>
    <input name="module_guestbook"
           type="checkbox" <?php if($user->guestbook == "on") echo "checked"; ?>/>
    Modify guestbook
  </p>
  <p>
    New password:
    <input type="password" name="password1" style="width: 100px;" value=""/>
  </p>
  <p>
    Confirm password:
    <input type="password" name="password2" style="width: 100px;" value=""/>
  </p>
  <p>
  <button type="submit">Submit changes</button>
  </p>
</form>
<?php
}
?>
<form method="post" action="?page=admin&amp;module=users&amp;task=edituser">
	<p>
    Select the user you want to edit:<br/>
    <select name="edituserid">
<?php
$uids = $users->useridList();
foreach($uids as $uid) {
?>
    <option value="<?php echo $uid;?>"><?php echo $uid;?></option>
<?php
}
?>
    </select>
    <button type="submit">Edit</button>
  </p>
</form>

<div class="small_header">Delete user</div>
<?php
if($task == "deleteconfirm") {
?>
Are you sure you want to delete the <em><?php echo $deleteuserid; ?></em> user?<br/>
<form method="post" action="?page=admin&amp;module=users&amp;task=delete">
	<input name="deleteuserid" type="hidden" value="<?php echo $deleteuserid ?>"/>
  <button type="submit">Yes</button>
</form>
<form method="post" action="?page=admin&amp;module=users">
	<input name="deleteuserid" type="hidden" value="<?php echo $deleteuderid; ?>"/>
  <button type="submit">No</button>
</form>
<?php
}
?>

<?php
if($task == "delete") {
	if($users->deleteUser($deleteuserid)) {
		echo "<p>User <em>".$deleteuserid."</em> deleted successfully.</p>\n";
	}
}
?>
<form method="post" action="?page=admin&amp;module=users&amp;task=deleteconfirm">
	<p>
    Select the user you want to delete:<br/>
    <select name="deleteuserid">
<?php
$uids = $users->useridList();
foreach($uids as $uid) {
?>
    <option value="<?php echo $uid;?>"><?php echo $uid;?></option>
<?php
}
?>
    </select>
    <button type="submit">Delete</button>
  </p>
</form>