<?php
/*===================================================+
|| # Woodpecker Housekeeping
|+====================================================+
|| # Copyright © 2008 SuperFun. All rights reserved.
|| # http://www.superfunn.nl/
|+===================================================+
|| # Notice: This is free software.
|+===================================================+
|| # Versions.
|| # Housekeeping version: V3.5
|| # Version of this file: V1.4 (rooms.php)
|+===================================================+
||
|| # Usage of this page:
|| # Room management.
||
|+===================================================*/

include('inc/header.php');
if($_GET['do'] == "findid"){
	if(min_fuse('housekeeping_findroomid', $my_id)){
		if($_POST['submit']){
			if(empty($_POST['search'])){
				echo 'Vul iets in om op te zoeken';
			}else{
				if($_POST['type'] == "roomname"){
					$sql = mysql_query("SELECT * FROM rooms WHERE roomname LIKE '%".beveilig($_POST['search'])."%' AND ccts IS NULL ORDER BY id");
				}else{
					$user = gebruikerid($_POST['search']);
					$sql = mysql_query("SELECT * FROM rooms WHERE ownerid = '".beveilig($user)."' AND ccts IS NULL ORDER BY id");
				}
				if(mysql_num_rows($sql) < 1){
					echo mysql_error();
					if($_POST['type'] == "roomname"){
						echo 'No room found';
					}else{
						echo 'No room found with that owner: '.htmlspecialchars($_POST['search']);
					}
				}else{
					echo '<table border="1">';
					echo '<tr>';
					echo '<td><b>Room ID</b></td>';
					echo '<td><b>Room Name</b></td>';
					echo '<td><b>Owner</b></td>';
					echo '</tr>';
					while($b = mysql_fetch_assoc($sql)){
						echo '<tr>';
						echo '<td>'.$b['id'].'</td>';
						echo '<td>'.htmlspecialchars($b['roomname']).'</td>';
						if($_POST['type'] == "roomname"){
							echo '<td>'.gebruiker($b['ownerid']).'</td>';
						}else{
							echo '<td>'.htmlspecialchars($_POST['search']).'</td>';
						}
						echo '</tr>';
					}
					echo '</table>';
				}
			}
		}else{
			echo '<form method="post" action="./rooms.php?do=findid">';
			echo 'Search Users: <input type="text" name="search">';
			echo '<SELECT NAME="type"><OPTION value="roomname" SELECTED>Room Name</OPTION><OPTION value="username">Username</OPTION></SELECT>';
			echo '<input type="submit" name="submit" value="Search">';
			echo '</form>';	
		}
	}else{
		echo $acc_denied;
	}
}elseif($_GET['do'] == "edit"){
	if(min_fuse('housekeeping_roommanagement', $my_id)){
		if($_GET['editid']){
			if($_POST['submit']){
				$a = mysql_query("SELECT * FROM rooms WHERE id = '".beveilig($_GET['editid'])."' AND ccts IS NULL LIMIT 1");
				$b = mysql_num_rows($a);
				if($b < 1){
					echo 'This room does not exist';
				}else{
					if(empty($_GET['editid'])){
						echo 'Please enter a room ID';
					}elseif(!is_numeric($_GET['editid'])){
						echo 'A room ID is a number';
					}elseif(empty($_POST['roomname'])){
						echo 'Enter a Room Name';
					}elseif(empty($_POST['visitors_max'])){
						echo 'Enter Max Visitors to the room';
					}elseif(!is_numeric($_POST['visitors_max'])){
						echo 'De maximale aantal bezoekers in een nummer.';
					}else{
						mysql_query("UPDATE rooms SET roomname = '".beveilig(fix($_POST['roomname']))."' WHERE id = '".beveilig($_GET['editid'])."'");
						mysql_query("UPDATE rooms SET description = '".beveilig(fix($_POST['description']))."' WHERE id = '".beveilig($_GET['editid'])."'");
						mysql_query("UPDATE rooms SET showname = '".beveilig(fix($_POST['showname']))."' WHERE id = '".beveilig($_GET['editid'])."'");
						mysql_query("UPDATE rooms SET superusers = '".beveilig(fix($_POST['superusers']))."' WHERE id = '".beveilig($_GET['editid'])."'");
						mysql_query("UPDATE rooms SET accesstype = '".beveilig(fix($_POST['accesstype']))."' WHERE id = '".beveilig($_GET['editid'])."'");
						mysql_query("UPDATE rooms SET password = '".beveilig(fix($_POST['password']))."' WHERE id = '".beveilig($_GET['editid'])."'");
						mysql_query("UPDATE rooms SET visitors_max = '".beveilig(fix($_POST['visitors_max']))."' WHERE id = '".beveilig($_GET['editid'])."'");
						echo 'Room Updated.';
					}
				}
			}elseif($_POST['deleteroom']){
				$a = mysql_query("SELECT * FROM rooms WHERE id = '".beveilig($_GET['editid'])."' AND ccts IS NULL LIMIT 1");
				$b = mysql_num_rows($a);
				if($b < 1){
					echo 'The room does not exist';
				}else{
					if($_POST['sure'] == "0"){
						echo '<form method="post" action="rooms.php?do=edit&editid='.$_GET['editid'].'">';
						echo '<input type="hidden" name="sure" value="1">';
						echo 'Are you sure you want to delete the room?<br>';
						echo '<input type="submit" name="deleteroom" value="Verwijder kamer">';
						echo '</form>';
					}elseif($_POST['sure'] == "1"){
						mysql_query("DELETE FROM rooms WHERE id = '".beveilig($_GET['editid'])."' AND ccts IS NULL LIMIT 1");
						echo 'Room deleted.';
					}else{
						echo $acc_denied;
					}
				}
			}else{
				$a = mysql_query("SELECT * FROM rooms WHERE id = '".beveilig($_GET['editid'])."' AND ccts IS NULL LIMIT 1");
				$b = mysql_num_rows($a);
				if($b < 1){
					echo 'Deze kamer bestaat niet.';
				}else{
					$cc = mysql_fetch_assoc($a);
					echo '<form method="post" action="rooms.php?do=edit&editid='.$_GET['editid'].'">';
					echo 'ID: <input type="text" value="'.$cc['id'].'" READONLY><br>';
					echo 'OwnerID <input type="text" value="'.gebruiker($cc['ownerid']).'" READONLY><br>';
					echo 'Category: <input type="text" value="'.category($cc['category']).'" READONLY><br>';
					echo 'Room Name: <input type="text" name="roomname" value="'.$cc['roomname'].'"><br>';
					echo 'Description: <input type="text" name="description" value="'.$cc['description'].'"><br>';
					echo 'Model type: <input type="text" value="'.$cc['modeltype'].'" READONLY><br>';
					echo 'Show Username: <SELECT NAME="showname"><OPTION value="false"'; if($cc['showname'] == "false"){ echo ' SELECTED'; } echo '>Nee</OPTION><OPTION value="true"'; if($cc['showname'] == "true"){ echo ' SELECTED'; } echo '>Ja</OPTION></SELECT><br>';
					echo 'Super Users: <SELECT NAME="superusers"><OPTION value="false"'; if($cc['superusers'] == "false"){ echo ' SELECTED'; } echo '>Nee</OPTION><OPTION value="true"'; if($cc['superusers'] == "true"){ echo ' SELECTED'; } echo 'Ja</OPTION></SELECT><br>';
					echo 'Access type: <SELECT NAME="accesstype"><OPTION value="0"'; if($cc['accesstype'] == "0"){ echo ' SELECTED'; } echo '>Open</OPTION><OPTION value="1"'; if($cc['accesstype'] == "1"){ echo ' SELECTED'; } echo '>Deurbel</OPTION><OPTION value="2"'; if($cc['accesstype'] == "2"){ echo ' SELECTED'; } echo '>Wachtwoord</OPTION></SELECT><br>';
					echo 'Room password: <input type="text" name="password" value="'.$cc['password'].'"><br>';
					echo 'Maximum Visitors: <input type="text" name="visitors_max" value="'.$cc['visitors_max'].'"> (max 100)<br>';
					echo '<input type="hidden" name="sure" value="0">';
					echo '<input type="submit" name="submit" value="Save room"> <input type="submit" name="deleteroom" value="Delete Room">';
					echo '</form>';
				}
			}
		}else{
			if($_POST['submit']){
				if(empty($_POST['search'])){
					echo 'Enter a query';
				}else{
				if($_POST['type'] == "roomname"){
					$sql = mysql_query("SELECT * FROM rooms WHERE roomname LIKE '%".beveilig($_POST['search'])."%' AND ccts IS NULL ORDER BY id");
				}else{
					$user = gebruikerid($_POST['search']);
					$sql = mysql_query("SELECT * FROM rooms WHERE ownerid = '".beveilig($user)."' AND ccts IS NULL ORDER BY id");
				}
					if(mysql_num_rows($sql) < 1){
						echo mysql_error();
						if($_POST['type'] == "roomname"){
							echo 'Er is geen kamer gevonden met een naam als deze!';
						}else{
							echo 'No rooms with that owner: '.htmlspecialchars($_POST['search']);
						}
					}else{
						echo '<table border="1">';
						echo '<tr>';
						echo '<td><b>Room ID</b></td>';
						echo '<td><b>Room Naam</b></td>';
						echo '<td><b>Owner</b></td>';
						echo '<td><b>Edit</b></td>';
						echo '</tr>';
						while($b = mysql_fetch_assoc($sql)){
							echo '<tr>';
							echo '<td>'.$b['id'].'</td>';
							echo '<td>'.htmlspecialchars($b['roomname']).'</td>';
							if($_POST['type'] == "roomname"){
								echo '<td>'.gebruiker($b['ownerid']).'</td>';
							}else{
								echo '<td>'.htmlspecialchars($_POST['search']).'</td>';
							}
							echo '<td><a href="./rooms.php?do=edit&editid='.$b['id'].'" target="_self"><img src="images/edit.gif" border="0"></a></td>';
							echo '</tr>';
						}
						echo '</table>';
					}
				}
			}else{
				echo '<form method="post" action="./rooms.php?do=edit">';
				echo 'Zoek Kamers: <input type="text" name="search">';
				echo '<SELECT NAME="type"><OPTION value="roomname" SELECTED>Room Naam</OPTION><OPTION value="username">Username</OPTION></SELECT>';
				echo '<input type="submit" name="submit" value="Search">';
				echo '</form>';	
			}
		}
	}else{
		echo $acc_denied;
	}
}else{
	echo $acc_denied;
}
include('inc/footer.php');
?>