<?php
define("PAGE_USES_AUTOCOMPLETE",true);

require_once("includes/SQLConnect.php");
require_once("includes/user_data.php");


if(isset($_GET['to_user_ID']) && (int)$_GET['to_user_ID']>0)
	$to_user_ID[] = $_GET['to_user_ID'];


if(isset($_POST['subject'])){
	$limit = (int)$_POST['limit'];

	for($i=0;$i<$limit+1;$i++){
		if(isset($_POST['to_user_ID'.$i])){
			$to_user_ID = (int)$_POST['to_user_ID'.$i];
						
			if($to_user_ID>0){
				$from_user_ID = (int)$user_data['ID'];
				$subject = htmlentities($_POST['subject']);
				$body = htmlentities($_POST['body']);
				
				$first_name = mysql_result(mysql_query("SELECT first_name FROM Users WHERE ID = ".$to_user_ID),0);
				
				if(mysql_query("INSERT INTO User_Messages(from_user_ID,to_user_ID,subject,body) 
				VALUES(".$from_user_ID.",".$to_user_ID.",'".mysql_real_escape_string(substr($subject,0,255))."','".mysql_real_escape_string($body)."')")){
						
					//Primitive version of notifying people that they have a message via email
					$mail_send_name = $user_data['first_name']." ".$user_data['last_name']; //senders name
					$mail_email = "no-reply@districtbydistrict.summerhilldesign.com"; //senders e-mail adress
					$mail_recipient = mysql_result(mysql_query("SELECT email FROM Users WHERE ID = ".$to_user_ID." LIMIT 1"),0); //recipient
					$mail_body = "You've received a message on your District-by-District Organizing Tool account. It is shown below.\r\n-----------\r\n\r\n\r\n".$body; //mail body
					$mail_subject = "You've received a message from a District-by-District Organizing Tool member"; //subject
					$mail_header = "From: ". $mail_send_name. " <" . $mail_email . ">\r\n"; //optional headerfields
					
					ini_set('sendmail_from', $mail_email); //Suggested by "Some Guy"
										
					mail($mail_recipient, $mail_subject, $mail_body, $mail_header);
					
					if($limit==0) //Just one recipient (it's zero because it wasn't set and was then cast to integer)
						header("Location:message_inbox.php?success=".urlencode('"').htmlentities($subject).urlencode('"')." sent to ".$first_name.".");
					

				}
				else
					$error = "Message not sent to ".$first_name.". Please try again.";
				
				if(!isset($error)){
					if($limit>0) //Make sure singular/plural is set properly
						$target = "people";
					else
						$target = "person";
					header("Location:message_inbox.php?success=".urlencode('"').htmlentities($subject).urlencode('"')." sent to ".$limit." ".$target.".");
				}
					
			}
		}
	}
	
	$error = "You did not specify any recipients or your unchecked all of them.";
}
elseif(isset($_POST['action']) && $_POST['action'] == "reply" && (int)$_POST['message_ID']>0){

	//Replying to a message.
	$message_ID = (int)$_POST['message_ID'];
	$user_ID = (int)$user_data['ID']; //Stop people from reading other people's mail by "replying" to messages
	
	$message = mysql_fetch_assoc(mysql_query("SELECT * FROM User_Messages WHERE ID = ".$message_ID." AND to_user_ID=".$user_ID." LIMIT 1"));
		
	$body = "\r\n\r\n\r\nSent on ".$message['TS']." \r\n-----------------------\r\n\r\n ".$message['body'];
	$subject = "Re: ".$message['subject'];
	$to_user_ID[] = $message['from_user_ID'];
	
//user_message_ID
}

include_once("includes/header.php");

?>



<h2>Send Message</h2>
<form action="message_compose.php" method="POST">

	<label>To:</label>
	<input type="text" name="autocomplete" value="" id="autocomplete">
	<input type="hidden" class="hidden" name="user_ID" value="<?=$user_data['ID'];?>">
	
	<script type="text/javascript">

	</script>
	
	<label>Recipients:</label>	
	<ul id="recipients">
	<?php

	//If this page is posting to itself (if there was an error and the POST fields need to be decoded and printed for the user)
	if(isset($_POST['limit'])){
		$limit = (int)$_POST['limit'];
	
		for($i=0;$i<$limit+1;$i++)
			if(isset($_POST['to_user_ID'.$i]))
				$to_user_ID[] = (int)$_POST['to_user_ID'.$i];
	}

	//If this is a "reply" or rebuilding the list of recipients, this code will insert them manually (before the autocompleter gets to it)
	$counter = 0;	
	if(isset($to_user_ID) && is_array($to_user_ID)){
		foreach($to_user_ID as $user_ID){
			$counter++;
			$user_name = mysql_result(mysql_query("SELECT CONCAT(first_name,last_name) FROM Users WHERE ID = ".(int)$user_ID),0); //Get user's name
		?>
			<li class='to' id='<?=$user_ID;?>'><input type='checkbox' name='to_user_ID<?=$counter;?>' class='checkbox' value='<?=$user_ID;?>' checked/><?=$user_name;?></li>
		<?php
		}
	}
	
	?>
	</ul>
	<input type="hidden" class='hidden' name="limit" value="<?=($counter+1);?>">
	
	
	<label>Subject:</label>
	<input type="text" name="subject" value="<?php if(isset($subject)){echo $subject;} ?>" class="large">
		
	<label>Body:</label>
	<textarea name="body" rows="13"><?php if(isset($body)){echo $body;} ?></textarea>
	
	<input type=submit value="Send Message" class="submit">

</form>

<?php
include_once("includes/footer.php");
?>