<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //

$xoopsOption['pagetype'] = "pmsg";
include_once("mainfile.php");
include_once(XOOPS_ROOT_PATH."/class/xoopspm.php");

if ( !$xoopsUser ) {
	$errormessage = _PM_SORRY."<br />"._PM_PLZREG."";
    	redirect_header("user.php",2,$errormessage);
} else {
	if ( isset($HTTP_POST_VARS['delete_messages']) ) {
		$size = sizeof($HTTP_POST_VARS['msg_id']);
		$msg = $HTTP_POST_VARS['msg_id'];
        	for ( $i = 0; $i < $size; $i++ ) {
			$pm = new XoopsPM($msg[$i]);
			$pm->delete();
		}
		redirect_header("viewpmsg.php",1,_PM_DELETED);
		exit();
	}
	include("header.php");
	$pm_arr = XoopsPM::getAllPM(array("to_userid='".$xoopsUser->getVar("uid")."'"), true);
	OpenTable(); 
	echo "<h4 style='text-align:center;'>". _PM_PRIVATEMESSAGE ."</h4><br /><a href='userinfo.php?uid=". $xoopsUser->getVar("uid")."'>". _PM_PROFILE ."</a>&nbsp;<span style='font-weight:bold;'>&raquo;&raquo;</span>&nbsp;". _PM_INBOX ."<br /><br /><table border='0' cellspacing='1' cellpadding='0' align='center' valign='top' width='100%'><tr><td><table border='0' cellspacing='1' cellpadding='1' width='100%'>\n";
	echo "<form name='prvmsg' method='post' action='viewpmsg.php'>"; 
	echo "<tr class='bg2' align='center' valign='middle'><td class='bg2'><input name='allbox' id='allbox' onclick='xoopsCheckAll(\"prvmsg\", \"allbox\");' type='checkbox' value='Check All' /></td><td class='bg2'><img src='images/download.gif' alt='' border='0' /></td><td class='bg2'>&nbsp;</td><td><b>". _PM_FROM ."</b></td><td><b>". _PM_SUBJECT ."</b></td><td align='center'><b>". _PM_DATE ."</b></td></tr>\n";
	$total_messages = count($pm_arr);
	if ( $total_messages == 0 ) {
		echo "<td class='bg3' colspan='6' align='center'>"._PM_YOUDONTHAVE."</td></tr> ";
		$display= 0;
	} else {
		
		$display = 1;
	}
	$count = 0;
    	foreach ( $pm_arr as $pm_ele ) {
		echo "<tr align='left'><td class='bg1' valign='top' width='2%' align='center'><input type='checkbox' id='msg_id[]' name='msg_id[]' value='".$pm_ele->getVar("msg_id")."' /></td>\n";
		if ( $pm_ele->getVar("read_msg") == "1" ) {
			echo "<td valign='top' width='5%' align='center' class='bg1'>&nbsp;</td>\n";
		} else {
			echo "<td valign='top' width='5%' align='center' class='bg1'><img src='images/read.gif' alt='"._PM_NOTREAD."' border='0' /></td>\n";
		}
		echo "<td class='bg3' valign='top' width='5%' align='center'><img src='images/subject/".$pm_ele->getVar("msg_image", "E")."' alt='' border='0' /></td>\n";
		$postername = XoopsUser::getUnameFromId($pm_ele->getVar("from_userid"));
		echo "<td class='bg1' valign='middle' width='10%'>";
		// no need to show deleted users
		if ( $postername ) {
			echo "<a href='userinfo.php?uid=".$pm_ele->getVar("from_userid")."'>".$postername."</a>";
		} else {
			echo $xoopsConfig['anonymous'];
		}
		echo "</td>\n";
		echo "<td class='bg3' valign='middle'><a href='readpmsg.php?start=$count&amp;total_messages=$total_messages'>".$pm_ele->getVar("subject")."</a></td>";
		echo "<td class='bg1' valign='middle' align='center' width='20%'>".formatTimestamp($pm_ele->getVar("msg_time"))."</td></tr>";
		$count++;
	}

	if ( $display == 1 ) {
		echo "<tr class='bg2' align='left'><td colspan='6' align='left'><input type='button' onclick='javascript:openWithSelfMain(\"".XOOPS_URL."/pmlite.php?send=1\",\"pmlite\",350,280);' value='"._PM_SEND."' />&nbsp;<input type='submit' name='delete_messages' value='"._PM_DELETE."' /></td></tr></form>";
	} else {
		echo "<tr class='bg2' align='left'><td colspan='6' align='left'><input type='button' onclick='javascript:openWithSelfMain(\"".XOOPS_URL."/pmlite.php?send=1\",\"pmlite\",360,290);' value='"._PM_SEND."' /></td></tr></form>";
	}

	echo "</table></td></tr></table>";
	CloseTable();
	include("footer.php");
}
?>