<?php
include('header.php');
require_once 'inc/swift_mailer/swift_required.php';
	if (empty($_GET['do'])) {echo $do = '';} else {$do = $_GET['do'];}
	switch ($do) {
	case 'new';
	echo '<div class="page_title">
	<h3>New Message</h3>
	<a href="messages.php">Back To Messages</a>
	</div>';
	if (isset($_POST['submit'])) {
	$reply_title = htmlentities($_POST['reply_title'], ENT_QUOTES);
	$reply_message = htmlentities($_POST['reply_message'], ENT_QUOTES);
	$to = $_POST['to'];
	if ($general_setting['send_mail_method'] == 0) {
	$send = send_php_mail($general_setting['sender_mail'],$to,$reply_title,$reply_message);
	} else {
	$transport = Swift_SmtpTransport::newInstance(trim($general_setting['smtp_host']), trim($general_setting['smtp_port'])) // 587
	->setUsername(trim($general_setting['smtp_username']))
	->setPassword(trim($general_setting['smtp_password']));
	$mailer = Swift_Mailer::newInstance($transport);
	$activemessage = Swift_Message::newInstance($reply_title)
	->setFrom(array($general_setting['smtp_username'] => $general_setting['site_title']))
	->setTo(array("$to"))
	->setBody($reply_message,'text/html');
	$send = $mailer->send($activemessage);
	}
	if ($send) {
	$messagen = $general->notification('success','Message Sent Successfully.');
	} else {
	$messagen = $general->notification('error','Error Happened');
	}
	}
	if (isset($messagen)) {echo $messagen;}
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="10" cellspacing="0">
	<tr>
	<td class="divider_td" colspan="2">Create New Message</td>
	</tr>
	<tr>
	<td class="label_td">To</td>
	<td class="input_td"><input type="text" name="to" size="40" /></td>
	</tr>
	<tr>
	<td class="label_td">Title</td>
	<td class="input_td"><input type="text" name="reply_title" size="60" /></td>
	</tr>
	<tr>
	<td colspan="2" class="input_td"><textarea name="reply_message" rows="5" style="width:98%;"></textarea></td>
	</tr>
	<tr>
	<td colspan="2"><input type="submit" name="submit" value="Send Message" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	break;
	break;
	case 'show';
	$id = abs(intval($_GET['id']));
	$message = $general->get_message($id);
	echo '<div class="page_title">
	<h3>'.$message['message_title'].'</h3>
	<a href="messages.php">Back To Messages</a>
	</div>';
	if (isset($_POST['submit'])) {
	$reply_title = htmlentities($_POST['reply_title'], ENT_QUOTES);
	$reply_message = htmlentities($_POST['reply_message'], ENT_QUOTES);
	$to = $_POST['to'];
	if ($general_setting['send_mail_method'] == 0) {
	$send = send_php_mail($general_setting['sender_mail'],$to,$reply_title,$reply_message);
	if ($send) {
	$messagen = $general->notification('success','Message Sent Successfully.');
	} else {
	$messagen = $general->notification('error','Error Happened');
	}
	} else {
	$transport = Swift_SmtpTransport::newInstance(trim($general_setting['smtp_host']), trim($general_setting['smtp_port'])) // 587
	->setUsername(trim($general_setting['smtp_username']))
	->setPassword(trim($general_setting['smtp_password']));
	$mailer = Swift_Mailer::newInstance($transport);
	$activemessage = Swift_Message::newInstance($reply_title)
	->setFrom(array($general_setting['smtp_username'] => $general_setting['site_title']))
	->setTo(array("$to"))
	->setBody($reply_message,'text/html');
	$send = $mailer->send($activemessage);
	if ($send) {
	$messagen = $general->notification('success','Message Sent Successfully.');
	} else {
	$messagen = $general->notification('error','Error Happened');
	}
	}
	}
	if (isset($messagen)) {echo $messagen;}
	
	echo '<table width="100%" cellpadding="10" cellspacing="0">
	<tr>
	<td class="message_details">'.$message['message_sender_name'].'</td>
	<td class="message_details">'.$message['message_sender_email'].'</td>
	<td class="message_details">'.date('Y-n-j h:i a',$message['message_datetime']).'</td>
	</tr>
	<tr>
	<td colspan="3" class="message_content">'.$message['message_content'].'</td>
	</tr>
	</table>';
	mysql_query("UPDATE messages SET message_readed='1' WHERE message_id='$id'");
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="10" cellspacing="0">
	<tr>
	<td class="divider_td" colspan="2">Reply</td>
	</tr>
	<tr>
	<td class="label_td">Title</td>
	<td class="input_td"><input type="text" name="reply_title" value="Re : '.$message['message_title'].'" size="50" /></td>
	</tr>
	<tr>
	<td colspan="2" class="input_td"><textarea name="reply_message" rows="5" style="width:98%;"></textarea></td>
	</tr>
	<tr>
	<td colspan="2">
	<input type="hidden" name="to" value="'.$message['message_sender_email'].'" />
	<input type="submit" name="submit" value="Reply" class="inputbutton grey" />
	</td>
	</tr>
	</table>
	</form>';
	break;
	
	case 'delete';
	$id = abs(intval($_GET['id']));
	echo '<div class="page_title">
	<h3>Messages - Delete Message</h3>
	<a href="messages.php">Back To Messages</a>
	</div>';
	if (isset($_POST['delete'])) {
	$delete = mysql_query("DELETE FROM messages WHERE message_id='$id'");
	if ($delete) {
	$message = $general->notification('success','Message Is Deleted Successfully. You Will Be Redirected To Messages Page In 3 Seconds.');
	echo "<meta http-equiv='refresh' content='3;URL=messages.php'>";
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	$mess = $general->get_message($id);
	if (isset($message)) {echo $message;}
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="5" cellspacing="0" style="margin-bottom:30px;">
	<tr>
	<td class="label_td">Are You Sure To Delete Message : <b>'.$mess['message_title'].'</b> ?</td>
	</tr>
	<tr>
	<td><input type="submit" name="delete" value="Delete" class="inputbutton grey" /></td>
	</tr>
	</table>
	</form>';
	break;
	
	default;
	if (isset($_POST['delete']) AND isset($_POST['id'])) {
	$ids = $_POST['id'];
	$count= count($ids);
	for($i=0;$i<$count;$i++){
	$del_id = $ids[$i];
	$sql = "DELETE FROM messages WHERE message_id='$del_id'";
	$res = mysql_query($sql);
	if ($res) {
	$message = $general->notification('success','The Selected Messages Has Been Deleted Successfully.');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	if (isset($_POST['mark_read']) AND isset($_POST['id'])) {
	$ids = $_POST['id'];
	$count= count($ids);
	for($i=0;$i<$count;$i++){
	$del_id = $ids[$i];
	$sql = "UPDATE messages SET message_readed='1' WHERE message_id='$del_id'";
	$res = mysql_query($sql);
	if ($res) {
	$message = $general->notification('success','The Selected Messages Has Been Marked As Readed.');
	} else {
	$message = $general->notification('error','Error Happened');
	}
	}
	}
	echo '<div class="page_title">
	<h3>Messages</h3>
	<a href="?do=new">New Message</a>
	</div>';
	$messages_number = mysql_num_rows(mysql_query("SELECT message_id FROM messages"));
	if ($messages_number == 0) {
	echo $general->notification('warning','There is no New Messages.');
	} else {
	$page = 1;
	$size = 10;
	if (isset($_GET['page'])){ $page = abs(intval($_GET['page'])); }
	$pagination = new Pagination();
	$pagination->setLink("?page=%s");
	$pagination->setPage($page);
	$pagination->setSize($size);
	$pagination->setTotalRecords($messages_number);
	$sql = "SELECT * FROM messages ORDER BY message_id DESC ". $pagination->getLimitSql();
	$query = mysql_query($sql);
	echo '<form method="POST" action="">
	<table width="100%" cellpadding="10" cellspacing="0">';
	echo '<tr>
	<td class="divider_td" width="10"><input type="checkbox" class="parentCheckBox" /></td>
	<td class="divider_td">Messages</td>
	<td class="divider_td"></td>
	</tr>';
	while ($row = mysql_fetch_array($query)) {
	echo '<tr>
		<td class="label_td"><input type="checkbox" name="id[]" class="childCheckBox" value="'.$row['message_id'].'" /></td>
		<td class="label_td">
		';
		if ($row['message_readed'] == 0) { 
		echo '<a href="messages.php?do=show&id='.$row['message_id'].'" class="unreaded">'.$row['message_title'].'</a>'; 
		} else {
		echo '<a href="messages.php?do=show&id='.$row['message_id'].'" class="readed">'.$row['message_title'].'</a>'; 
		}
		echo '
		<span>'.$row['message_sender_email'].' ('.$row['message_sender_name'].') at '.date('Y-n-j h:i a',$row['message_datetime']).'</span>
		</td>
		<td class="label_td" width="150" align="right">
		<a href="?do=delete&id='.$row['message_id'].'" class="delete">Delete</a>
		</td>
	</tr>';
	}
	echo '</table>
	<table width="100%" cellpadding="10" cellspacing="0">
	<tr>
	<td align="left">
	<input type="submit" name="mark_read" value="Mark As Read" class="inputbutton blue" />
	<input type="submit" name="delete" value="Delete" class="inputbutton grey" />
	</td>
	<td align="right">'.$pagination->create_links().'</td>
	</tr>
	</table>
	</form>';
	}
	}
include('footer.php');
?>