<?php
include "../core.php";
saxue_checklogin();
if ( !isset( $_REQUEST['box'] ) || $_REQUEST['box'] != "outbox" ) {
		$GLOBALS['_REQUEST']['box'] = "inbox";
} 
if ( empty( $_REQUEST['page'] ) || !is_numeric( $_REQUEST['page'] ) ) {
		$GLOBALS['_REQUEST']['page'] = 1;
} 
include SAXUE_ROOT_PATH . "/model/member_message.php";
$message_handler = &saxuemembermessagehandler :: getinstance( "saxuemembermessagehandler" );
if ( empty( $_REQUEST['checkaction'] ) ) {
		$GLOBALS['_REQUEST']['checkaction'] = 0;
} 
if ( $_REQUEST['checkaction'] == 1 && is_array( $_REQUEST['checkid'] ) && 0 < count( $_REQUEST['checkid'] ) ) {
		$where = "";
		foreach ( $GLOBALS['_REQUEST']['checkid'] as $v ) {
				if ( is_numeric( $v ) ) {
						$v = intval( $v );
						if ( !empty( $where ) ) {
								$where .= " OR ";
						} 
						$where .= $message_handler -> autoid . "=" . $v;
				} 
		} 
		if ( !empty( $where ) ) {
				switch ( $_REQUEST['box'] ) {
						case "inbox" :
								$sql = "UPDATE " . saxue_dbprefix( "member_message" ) . " SET todel=1 WHERE toid=" . $_SESSION['saxueUserId'] . " AND fromdel=0 AND (" . $where . ")";
								$message_handler -> db -> query( $sql );
								$sql = "DELETE FROM " . saxue_dbprefix( "member_message" ) . " WHERE toid=" . $_SESSION['saxueUserId'] . " AND fromdel=1 AND (" . $where . ")";
								$message_handler -> db -> query( $sql );
								break;
						case "outbox" :
								$sql = "UPDATE " . saxue_dbprefix( "member_message" ) . " SET fromdel=1 WHERE fromid=" . $_SESSION['saxueUserId'] . " AND todel=0 AND (" . $where . ")";
								$message_handler -> db -> query( $sql );
								$sql = "DELETE FROM " . saxue_dbprefix( "member_message" ) . " WHERE fromid=" . $_SESSION['saxueUserId'] . " AND todel=1 AND (" . $where . ")";
								$message_handler -> db -> query( $sql );
				} 
		} 
		$GLOBALS['_REQUEST']['checkaction'] = 0;
} else if ( $_REQUEST['checkaction'] == 2 ) {
		switch ( $_REQUEST['box'] ) {
				case "inbox" :
						$sql = "UPDATE " . saxue_dbprefix( "member_message" ) . " SET todel=1 WHERE toid=" . $_SESSION['saxueUserId'] . " AND fromdel=0";
						$message_handler -> db -> query( $sql );
						$sql = "DELETE FROM " . saxue_dbprefix( "member_message" ) . " WHERE toid=" . $_SESSION['saxueUserId'] . " AND fromdel=1";
						$message_handler -> db -> query( $sql );
						break;
				case "outbox" :
						$sql = "UPDATE " . saxue_dbprefix( "member_message" ) . " SET fromdel=1 WHERE fromid=" . $_SESSION['saxueUserId'] . " AND todel=0";
						$message_handler -> db -> query( $sql );
						$sql = "DELETE FROM " . saxue_dbprefix( "member_message" ) . " WHERE fromid=" . $_SESSION['saxueUserId'] . " AND todel=1";
						$message_handler -> db -> query( $sql );
		} 
		$GLOBALS['_REQUEST']['checkaction'] = 0;
} 
if ( isset( $_GET['checkaction'] ) ) {
		unset( $_GET['checkaction'] );
} 
if ( isset( $_POST['checkaction'] ) ) {
		unset( $_POST['checkaction'] );
} 
saxue_loadlang( "message" );
saxue_getconfigs( "configs", "member" );
include SAXUE_WEB_PATH . "/member/header.php";
$saxueTpl -> assign( "box", $_REQUEST['box'] );
$saxueTpl -> assign( "url_action", 'message.php?menuid=' . $menuid . '&box=' . $_REQUEST['box'] );
$saxueTpl -> assign( "url_delete", 'message.php?menuid=' . $menuid . '&box=' . $_REQUEST['box'] . '&checkaction=1' );
$saxueTpl -> assign( "url_empty", 'message.php?menuid=' . $menuid . '&box=' . $_REQUEST['box'] . '&checkaction=2' );
$messagerows = array();
switch ( $_REQUEST['box'] ) {
		case "outbox" :
				$saxueTpl -> assign( "boxname", $saxueLang['message']['message_send_box'] );
				$saxueTpl -> assign( "usertitle", $saxueLang['message']['table_message_receiver'] );
				$criteria = new criteriacompo( new criteria( "fromid", $_SESSION['saxueUserId'] ) );
				$criteria -> add( new criteria( "fromdel", 0 ) );
				$criteria -> setsort( "messageid" );
				$criteria -> setorder( "DESC" );
				$criteria -> setlimit( $saxueConfigs['member']['messagepnum'] );
				$criteria -> setstart( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['member']['messagepnum'] );
				$message_handler -> queryobjects( $criteria );
				$k = 0;
				while ( $v = $message_handler -> getobject() ) {
						$messagerows[$k] = $v -> getvars( "n" );
						$messagerows[$k]['uid'] = $messagerows[$k]['toid'];
						$messagerows[$k]['uname'] = $messagerows[$k]['toname'];
						$messagerows[$k]['detail'] = 'messagedetail.php?menuid=' . $menuid . '&uid=' . $messagerows[$k]['toid'];
						++$k;
				} 
				$msgcount = $message_handler -> getcount( $criteria );
				$saxueTpl -> assign( "msgcount", $msgcount );
				$saxueTpl -> assign( "messagerows", $messagerows );
				include_once( SAXUE_ROOT_PATH . "/lib/util/page.php" );
				$jumppage = new saxuepage( $msgcount, $saxueConfigs['member']['messagepnum'], $_REQUEST['page'] );
				$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
				$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_THEME_PATH . "/member/message.html";
				break;
		case "inbox" :
				$saxueTpl -> assign( "boxname", $saxueLang['message']['message_receive_box'] );
				$saxueTpl -> assign( "usertitle", $saxueLang['message']['table_message_sender'] );
				$sql = "SELECT COUNT(*) AS msgcount FROM " . saxue_dbprefix( "member_message" ) . " WHERE (toid=0 OR toid=" . $_SESSION['saxueUserId'] . ") AND todel=0";
				$res = $message_handler -> db -> query( $sql );
				$row = $message_handler -> getrow( $res );
				$msgcount = ( integer )$row['msgcount'];
				if ( $msgcount > 0 ) {
						$sql = "SELECT * FROM " . saxue_dbprefix( "member_message" ) . " WHERE (toid=0 OR toid=" . $_SESSION['saxueUserId'] . ") AND todel=0 ORDER BY messageid DESC LIMIT " . ( ( $_REQUEST['page'] - 1 ) * $saxueConfigs['member']['messagepnum'] ) . ', ' . $saxueConfigs['member']['messagepnum'];
						$res = $message_handler -> db -> query( $sql );
						$k = 0;
						while ( $v = $message_handler -> getobject( $res ) ) {
								$messagerows[$k] = $v -> getvars( "n" );
								$messagerows[$k]['uid'] = $messagerows[$k]['fromid'];
								$messagerows[$k]['uname'] = $messagerows[$k]['fromname'];
								$messagerows[$k]['detail'] = 'messagedetail.php?menuid=' . $menuid . '&uid=' . $messagerows[$k]['fromid'];
								++$k;
						} 
				}
				$saxueTpl -> assign( "msgcount", $msgcount );
				$saxueTpl -> assign( "messagerows", $messagerows );
				include_once( SAXUE_ROOT_PATH . "/lib/util/page.php" );
				$jumppage = new saxuepage( $msgcount, $saxueConfigs['member']['messagepnum'], $_REQUEST['page'] );
				$saxueTpl -> assign( "url_jumppage", $jumppage -> whole_bar() );
				$saxueTpl -> assign( "totalpage", $jumppage -> total_page );
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_THEME_PATH . "/member/message.html";
				break;
} 
include SAXUE_WEB_PATH . "/member/footer.php";
