<?php

function admin_profile($profile){
	if(!empty($profile)){
		global $user;
		$edit=true;
		if($edit) echo "<form action=\"index.php?view=user&name={$profile["username"]}\" method=\"post\">\n";
		echo "<table>\n";
		echo "<tr><td>";
		display_avatar(false,$profile,true);
		echo "</td><th>{$profile["username"]}</th></tr>\n";
		echo "<tr><td>Rank</td><td>";
		$ranks = get_ranks();
		echo "<select name=\"rank\" style=\"width: 100px; float: left; \" >\n";
		for($rank_id=0; $rank_id<$user["permissions"]; $rank_id++){
			echo "<option value=\"{$rank_id}\"";
			if ($profile["permissions"] == $rank_id) echo " selected=\"selected\"";
			echo ">{$ranks[$rank_id]}</option>\n";
		}
		echo "</select>";
		echo "</td></tr>\n";
		
		display_text_row($profile,"name",$edit);
		display_text_row($profile,"email",$edit);
		if($edit||(!empty($profile["birthday_day"])&&!empty($profile["birthday_month"])&&!empty($profile["birthday_year"]))||!empty($profile["gender"])){
			echo "<tr><th>Personal</th><td></td></tr>\n";
			if($edit||(!empty($profile["birthday_day"])&&!empty($profile["birthday_month"])&&!empty($profile["birthday_year"]))){
				echo "<tr><td>Birthday:</td><td>\n";
				if ($edit){
					echo "<select name=\"birthday_day\" style=\"width: 50px; float: left; \" >\n";
					echo "<option value=\"0\">--</option>\n";
					for($day=1; $day<=31; $day++){
						echo "<option value=\"{$day}\"";
						if ($profile["birthday_day"] == $day) echo " selected=\"selected\"";
						echo ">{$day}</option>\n";
					}
					echo "</select>";
				} else echo $profile["birthday_day"] . "/";
							
				if ($edit){
					echo "<select name=\"birthday_month\" style=\"margin-left: 5px; width: 100px; float: left; \">\n";
					echo "<option value=\"0\">--</option>\n";
					$months = array("January","February","March","April","May","June","July","August","September","October","November","December");
					for ($month=1; $month<=12; $month++){
						echo "<option value=\"{$month}\"";
						if ($profile["birthday_month"]==$month) echo " selected=\"selected\"";
						echo ">{$months[$month-1]}</option>\n";
					}
					echo "</select>\n";
				} else echo $profile["birthday_month"] . "/";
				
				if ($edit){
					echo "<select name=\"birthday_year\" style=\"margin-left: 5px; width: 70px; float: left; \" >\n";
					echo "<option value=\"0\">--</option>\n";
					for ($year=2005;$year>=1925;$year--){
						echo "<option value=\"{$year}\"";
						if ($profile["birthday_year"] == $year) echo " selected=\"selected\"";
						echo ">{$year}</option>\n";
					}
					echo "</select>\n";
				} else echo $profile["birthday_year"];
				echo "</td></tr>\n";
			}
		}
		if(!empty($profile["phone"]) || !empty($profile["msn"]) || !empty($profile["skype"])) {
			echo "<tr><th>Contacts</th><td></td></tr>\n";
			display_text_row($profile,"phone",$edit);
			display_text_row($profile,"msn",$edit);
			display_text_row($profile,"skype",$edit);
		}
		echo "<tr><th>Address</th><td></td></tr>\n";
		display_text_row($profile,"address",$edit);
		display_text_row($profile,"city",false);
		display_text_row($profile,"state",false);
		display_text_row($profile,"zip",$edit);
		
		if($edit){
			echo "<tr><td></td><td>Administrate</td><tr>\n";
			echo "<tr><td style=\"color: #FFFF00;\">Clear Avatar</td><td><input type=\"checkbox\" name=\"clear_avatar\" value=\"Clear\" /> </td><tr>\n";
			echo "<tr><td style=\"color: #FF3333;\">Block User</td><td><input type=\"checkbox\" name=\"block\" value=\"Block\" " . ($profile["blocked"] ? "checked=\"checked\"" : "") . "/> </td><tr>\n";
			if($user["permissions"]>=2)
				echo "<tr><td style=\"color: #FF0000;\">Delete User</td><td><input type=\"checkbox\" name=\"delete\" value=\"Delete\" /> </td><tr>\n";
		}
		echo "<tr><td></td><td><input type=\"submit\" name=\"profile-submit\" value=\"Save Changes\"></td></tr>\n";
		echo "</table>";
		if($edit)
			echo "</form>\n";
		
	} else {
		echo "Invalid user";
	}
}

function delete_confirm($profilename){
	if(isset($_POST["delete-submit"])){
		// check if admin has permissions
		global $user;
		$profile_to_delete = get_user_by_name($profilename);
		if($profile_to_delete === null){
			echo "Invalid user.";
			return;
		}
		if($user["permissions"]>$profile_to_delete["permissions"]){
			delete_user($profilename);
			echo "User deleted successfully.<br>\n";
			echo "<a href=\"index.php\">back to userlist</a>";
		}
	} else {
		echo "<form action=\"index.php?view=user&name={$profilename}\" method=\"post\">\n";
		echo "<font color=\"#FF0000\">Are you sure you want to delete user <b>{$profilename}</b> and all events created by this user?</font><br>\n";
		echo "<input type=\"hidden\" name=\"delete\" value=\"Delete\">";
		echo "<input type=\"submit\" name=\"delete-submit\" value=\"Delete permanently\"> <a href=\"index.php?view=user&name={$profilename}\">Cancel</a>\n";
		echo "</form>";
	}
}

function edit_admin_profile($username){
	if(empty($username)) return false;
	global $user;
	$profile = get_user_by_name($username);
	if($user["username"]==$username || $user["permissions"]<=$profile["permissions"]) return false;
	$username = str_replace("'","''",$username);
	
	global $DB;
	
	
	if(isset($_POST["name"])) {
		$name = str_replace("'","''",ucfirst(strtolower($_POST["name"])));
		$DB->qry("UPDATE users SET name='{$name}' WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["birthday_day"]) && isset($_POST["birthday_month"]) && isset($_POST["birthday_year"])) {
		if(!empty($_POST["birthday_day"])&&!empty($_POST["birthday_month"])&&!empty($_POST["birthday_year"])){
			$day = intval($_POST["birthday_day"]);
			$month = intval($_POST["birthday_month"]);
			$year = intval($_POST["birthday_year"]);
		} else {
			$day = 0;
			$month = 0;
			$year = 0;
		}
		$DB->qry("UPDATE users SET birthday_day={$day}, birthday_month={$month}, birthday_year={$year} WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["address"])) {
		$address = str_replace("'","''",$_POST["address"]);
		$DB->qry("UPDATE users SET address='{$address}' WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["zip"])) {
		$zip = intval($_POST["zip"]);
		$DB->qry("UPDATE users SET zip='{$zip}' WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if(isset($_POST["block"])) {
		$DB->qry("UPDATE users SET blocked=1 WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	} else {
		$DB->qry("UPDATE users SET blocked=0 WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	
	// administrative
	if(isset($_POST["rank"])) {
		$rank = intval($_POST["rank"]);
		$DB->qry("SELECT * FROM ranks WHERE id={$rank};");
		if($DB->numrows()===0) return false;
		$DB->qry("UPDATE users SET permissions={$rank} WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	if($_POST["clear_avatar"] == "Clear") {
		$DB->qry("UPDATE users SET avatar=0 WHERE username='{$username}';");
		if($DB->numrows()===0) return false;
	}
	return true;
}

function reload_user(){
	global $DB, $user;
	if(isset($user)){
		$user = $DB->qry("SELECT u.*,z.city,z.state_prefix AS state,z.lat,z.lon FROM users u LEFT JOIN zip_code z ON u.zip=z.zip_code WHERE username='{$user["username"]}';",4);
		if($DB->numrows()==0) {
			$user = null;
		}
	}
}

function display_text_row($array,$key,$edit=false){
	if(!empty($array[$key])||$edit){
		echo "<tr><td>" . ucfirst(strtolower($key)) . ": </td>";
		if($edit) echo "<td><input type=\"text\" name=\"{$key}\" value=\"{$array[$key]}\"></td></tr>\n";
		else echo "<td>{$array[$key]}</td></tr>\n";
	}
}

function display_users($result,$pages = false){
	echo "<table class=\"userlist\"><tr><th>Username</th><th>Rank</th><th>Name</th><th>Zip</th><th>Events</th><th>Subs</th><th>Registered</th></tr>";
	while($row = mysql_fetch_assoc($result)){
		echo "<tr><td style=\"text-align:left;\"><a href=\"index.php?view=user&name={$row["username"]}\">{$row["username"]}</a></td><td>{$row["rank"]}</td><td>{$row["name"]}</td><td>{$row["zip"]}</td><td style=\"text-align:right;\">{$row["cnt_events"]}</td><td style=\"text-align:right;\">{$row["cnt_subs"]}</td><td>" . gmdate("m.d.y g:ia", $row["registered"]) . "</tr>";
	}
	echo "</table>";
	if($pages) display_pages($pages);
}

function delete_user($profilename){
	global $DB;
	$DB->qry("DELETE FROM users WHERE username='{$profilename}';");
	$DB->qry("DELETE FROM events WHERE username='{$profilename}';");
	$DB->qry("DELETE FROM comments WHERE username='{$profilename}';");
	$DB->qry("DELETE FROM subscriptions WHERE host='{$profilename}' OR subscriber='{$profilename}';");
}

?>