<?php

/***************************************************************************
 *
 *      Update to 2.0.15 by Alf (07.05.2005) 22:02
 *
 *	Title: xphpBBi - phpBB Complete Integration project 
 *	Description: Project to turn the phpBB forums into a fully functioning
 *		Xoops2 Module. Based on the phpBB 2.0.11 port from BBpixel.com - 
 *		(PBBoard v1.22).
 *	Credits: Xoops2 Module Development Team - Koudanshi - phpBB Group
 *	Thanks: To Koudanshi for a great start in his port, and phpBB Group for
 *		such a great forum software.
 *	License: GNU/GPL version 2 or later.
 ****************************************************************************/

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'common.php');
global $xoopsUser, $xoopsDB;

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_PRIVMSGS);
init_userprefs($userdata);
//
// End session management
//

//
// Var definitions
//
if ( !empty($HTTP_POST_VARS['mode']) || !empty($HTTP_GET_VARS['mode']) )
{
	$mode = ( !empty($HTTP_POST_VARS['mode']) ) ? $HTTP_POST_VARS['mode'] : $HTTP_GET_VARS['mode'];
	$mode = htmlspecialchars($mode);
}
else
{
	$mode = '';
}

//
// Update unread status
//
$sql = "UPDATE " . USERS_TABLE_EXT . "
	SET user_unread_privmsg = user_unread_privmsg + user_new_privmsg, user_new_privmsg = 0, user_last_privmsg = " . $userdata['session_start'] . "
	WHERE uid = ".$userdata['uid'];
if ( !$db->sql_query($sql) )
{
	message_die(GENERAL_ERROR, 'Could not update private message new/read status for user', '', __LINE__, __FILE__, $sql);
}

// ----------
// Start main
//
if ( $mode == 'newpm' )
{
	$gen_simple_header = TRUE;

	$page_title = $lang['Private_Messaging'];
	include($phpbb_root_path . 'includes/nowrap_header.php');

	$template->set_filenames(array(
		'body' => 'privmsgs_popup.tpl')
	);

	if (is_object($xoopsUser)) {
	    $pm_handler =& xoops_gethandler('privmessage');
	    $criteria = new CriteriaCompo(new Criteria('read_msg', 0));
	    $criteria->add(new Criteria('to_userid', $xoopsUser->getVar('uid')));
	    $new_pms = $pm_handler->getCount($criteria);
//	    $new_pms = $xoopsDB->fetchRow($xoopsDB->query("SELECT COUNT(*) FROM ".$xoopsDB->prefix("priv_msgs")." WHERE to_userid = '".$xoopsUser->getVar("uid")."' AND read_msg = 0"));
		if ($new_pms > 0) $l_new_message = ($new_pms == 1) ? $lang['You_new_pm'] : $lang['You_new_pms'];
		else$l_new_message = $lang['You_no_new_pm'];
    	    $l_new_message .= '<br /><br />' . sprintf($lang['Click_view_privmsg'], '<a href="/viewpmsg.php" target="_new" onclick="jump_to_inbox();return false;">', '</a>');
//onclick="jump_to_inbox();return false;"
	} else {
	    $l_new_message = $lang['Login_check_pm'];
	}

	$template->assign_vars(array(
		'L_CLOSE_WINDOW' => $lang['Close_window'], 
		'L_MESSAGE' => $l_new_message)
	);

	$template->pparse('body');

//	include($phpbb_root_path . 'includes/page_tail.php');
	
} else if ( $mode == 'read' ) {
redirect("../../viewpmsg.php", true);
} else if ( $mode == 'newpm' ) {
redirect("../../viewpmsg.php", true);
}
?>