Location: PHPKode > projects > Web Task Manager with Email/SMS Voting > Calendar/cal_message.php
<?php

include ('cal_header.inc.php');
include ("cal_utils.php") ;


calHeaderFunc();

function viewdetail($msg,$sent){
echo "<br /><hleft>Viewing msg number ".$msg."</hleft><br />";


 $query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 $userid=$row->user_id;
 
if ($sent==0)
{
$query = "UPDATE message set IsRead = TRUE where messageid = ".$msg." and ToUserId = ".$userid;
 $result = mysql_query($query);
}
 
if ($sent)
{
$query = "select MessageID, FromUserID, ToUserID, subject, body, MessageType, username, created from message left join calendar_users on calendar_users.user_id = message.ToUserID where FromUserID = ".$userid. " and MessageID = ".$msg;
}
else if (!$sent)
{
$query = "select MessageID, FromUserID, ToUserID, subject, body, MessageType, username, created from message left join calendar_users on calendar_users.user_id = message.FromUserID where ToUserID = ".$userid. " and MessageID = ".$msg;
}
  $result = mysql_query($query);
  $rows = mysql_num_rows($result);

  if($rows==0)
	{
		echo "No matching message details available.<br>";
	}
	while ($row = mysql_fetch_object($result))
	{
		if($sent)
			echo "<br>To: ".$row->username;
		else if (!$sent)
			echo "<br>From: ".$row->username;
		echo "<br>Subject: ".$row->subject;
		echo "<br>Body: ".$row->body;
		echo "<br><br>MessageType: ".$row->MessageType;
		echo "<br>Time Sent:".$row->created."<br><br>";
	}	
}

//viewing messages for user
function viewbrief(){
echo "<br /><div class=headerc>Message Inbox for ".$_SESSION["login"]."</div><br />";
 $query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 $userid=$row->user_id;
 
$query = "
SELECT MessageID, FromUserID, ToUserID, subject, body, MessageType, IsRead, COALESCE( username, groups.groupname ) AS dispusername, groupname, message.created, message.groupid
FROM (
message
left JOIN calendar_users ON calendar_users.user_id = message.FromUserID
)
left JOIN groups ON message.GroupID = groups.GroupID
WHERE ToUserID =".$userid." and (FromUserID is not null or message.groupid is not null) and (messagetype = 0 or messagetype = 2)" ;
  $result = mysql_query($query);
  $rows = mysql_num_rows($result);
	if($rows==0)
	{
		echo "No Messages.<br>";
	}
    $rows=0;
	echo "<table width=100% class=\"mes\"><tr><td width = 10% class=\"mes\">From:</td><td width = 60% class=\"mes\">Subject</td><td width = 20% class=\"mes\">Time Sent</td><td class=\"mes\">Read?</td></tr>";
	while ($row = mysql_fetch_object($result))
	{
        $rows++;
        $rows=$rows%2;
		echo "<tr><td class=\"mes".$rows."\">".$row->dispusername."</td>";
		echo "<td class=\"mes".$rows."\"><a href=cal_message.php?op=viewdetail&amp;msg=".$row->MessageID.">".$row->subject."</a></td>";
		echo "<td class=\"mes".$rows."\">".$row->created."</td>";
		echo "<td class=\"mes".$rows."\">";
		if($row->IsRead)
			echo "Y";
		else
			echo "N";
		echo "</td></tr>";
	}	
	echo "</table><br>";
}

function sentmsg(){
echo "<br /><div class=headerc>Message Outbox of ".$_SESSION["login"]."</div><br>";
 $query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 $userid=$row->user_id;
 

$query = "select MessageID, FromUserID, ToUserID, subject, body, MessageType, username, IsSent, created from message left join calendar_users on calendar_users.user_id = message.FromUserID where FromUserID = ".$userid." order by Created Desc" ;
  $result = mysql_query($query);
  $rows = mysql_num_rows($result);
	if($rows==0)
	{
		echo "No Messages.<br>";
	}
	else
	{
    $rows=0;
		echo "<table width=100% class=\"mes\"><tr><td width = 10% class=\"mes\">From:</td><td width = 50% class=\"mes\">Subject</td><td width = 15% class=\"mes\">Time Sent</td><td width = 35% class=\"mes\">Message Type</td></tr>";
	while ($row = mysql_fetch_object($result))
	{
          $rows++;
        $rows=$rows%2;
		echo "<tr><td class=\"mes".$rows."\">".$row->username."</td>";
		echo "<td class=\"mes".$rows."\"><a href=cal_message.php?op=viewdetail&amp;msg=".$row->MessageID."&amp;sent=1>".$row->subject."</a></td>";
		echo "<td class=\"mes".$rows."\">".$row->created."</td>";
		echo "<td class=\"mes".$rows."\">";
		if($row->MessageType == 0)
		{
			echo "Internal Only";
		}		
		else if($row->MessageType == 1)
		{
			echo "Email - ";
			if($row->IsSent)
				echo "sent";
			else
				echo "unsent";
				
		}
		else if($row->MessageType == 2)
		{
		echo "Msg,";
		echo "Email - ";
			if($row->IsSent)
				echo "sent";
			else
				echo "unsent";
		}
		
		echo "</td></tr>";
	}	
    echo "</table><br />";
    
	}

}

//send interface
function send(){

if (isset($_GET['event']))
	$event = $_GET['event'];
else
	$event = 0;	


echo "<div class=titlefont>Send Message</div><br/><form name=evfrm action=cal_message.php?op=addmessage";

if($event != 0)
{echo "&event=".$event;}

echo " method=post><div class=normalfont>
Message Subject<br/>
<input type=text name=Subject size=20 value='' ><br/>
Message Body<br/>
<textarea name=body cols=70 rows=7></textarea><br/>";

if($event == 0)
{

echo"
To User:
<select name=touser>
";


$query = "select user_id, username from calendar_users" ;
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 
 while ($row = mysql_fetch_object($result)){echo "<option value=".stripslashes($row->user_id).">".stripslashes($row->username);}

echo "</select><br><br>";
echo "<input type=\"radio\" name=\"messagetype\" value=\"0\"> Internal Message Only<br>
<input type=\"radio\" name=\"messagetype\" value=\"1\" checked> Send Email Only<br>
<input type=\"radio\" name=\"messagetype\" value=\"2\" checked> Send Email and Internal Message<br>
";
}
echo "
<br/><input type=submit value=\"Send Message\">
&nbsp;&nbsp;<input type=button value=\"Cancel\" onclick='Javascript:window.history.go(-1);'>
<br/>
";
}


//query to send message
function addmessage(){
if (isset($_GET['event']))
{	
$event = $_GET['event'];

$subject = $_POST['Subject'];
 if($subject=='') $subject ='none';
 $body = $_POST['body'];
 echo "<br>Details of message Posted:<br>Subject: ".$subject."<br>Body: ".$body."<br>To Event: ".$event."<br>";

 
 $query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 
 
 $query = "INSERT INTO message (FromUserID, ToUserID, Subject, Body, IsRead, MessageType, IsSent, GroupID, ReminderID, EventID, Created) VALUES ('".$row->user_id."', 0, '".$subject."', '".$body."', 0, 0, 0, 0, 0, ".$event.", NOW())";

mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');


}
else
{
	
 $subject = $_POST['Subject'];
 if($subject=='') $subject ='none';
 $body = $_POST['body'];
 $touser = $_POST['touser'];
 $messagetype = $_POST['messagetype'];
 echo "<br>Details Posted:<br>Subject: ".$subject."<br>Body: ".$body."<br>To User: ".$touser."<br>";

 
 $query = "select user_id from calendar_users where username = '".$_SESSION["login"]."'";
 $result = mysql_query($query);
 $rows = mysql_num_rows($result);
 $row = mysql_fetch_object($result);
 
 $query = "INSERT INTO message (FromUserID, ToUserID, Subject, Body, IsRead, MessageType, IsSent, GroupID, ReminderID, EventID, Created) VALUES ('".$row->user_id."', '".$touser."', '".$subject."', '".$body."', 0, ".$messagetype.", 0, 0, 0, 0, NOW())";

mysql_query($query) or die('Error, insert query failed');

$query = "FLUSH PRIVILEGES";
mysql_query($query) or die('Error, insert query failed');
}

}
// message functions
switch ($op){
    
    case "viewmsg":{
	 viewbrief();
    break;
    }

	case "viewdetail":{

	if (!isset($_GET['sent']))
		$sent = 0;
	else
		$sent = $_GET['sent'];
	 
	 $msg = $_GET["msg"];
	 if ($msg) viewdetail($msg,$sent);
	 else viewbrief();
    break;
    }
	
	case "sendmsg":{
	 send();
    break;
    }
	
	case "addmessage":{
	 addmessage();
	 break;
	 }
	 
	 case "sent":{
	 sentmsg();
	 break;
	 }
	
    // default: 
    default:{
	viewbrief();
      break;
	}
}

include ('cal_functions.inc.php');
include ('cal_footer.inc.php');

?>
Return current item: Web Task Manager with Email/SMS Voting