<?php
require(dirname(__FILE__) .'/check.php');
$skin->CheckIframe();


/* **** 版权所有 ****

 程序开发：网钛科技
 网钛CMS官网：http://otcms.com
 网钛IDC官网：http://idccms.com
 ＱＱ：877873666
 微信：QQ877873666
 邮箱：877873666@qq.com

 **** 版权所有 **** */


//用户检测
$MB->Open('','login',2);


switch ($mudi){
	case 'del':
		$menuFileID = 142;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		del();
		break;

	case 'moreDel':
		$menuFileID = 142;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		MoreDel();
		break;

	case 'revContent':
		$menuFileID = 141;
		$MB->IsSecMenuRight('alertClose',$menuFileID,$dataType);
		RevContent();
		break;

	case 'reply':
		$menuFileID = 141;
		$MB->IsSecMenuRight('alertClose',$menuFileID,$dataType);
		reply();
		break;

	case 'addReply':
		$menuFileID = 141;
		$MB->IsSecMenuRight('alertClose',$menuFileID,$dataType);
		AddReply();
		break;

	case 'setState':
		$menuFileID = 151;
		$MB->IsSecMenuRight('alertClose',$menuFileID,$dataType);
		SetState();
		break;

	case 'moreSet':
		$menuFileID = 151;
		$MB->IsSecMenuRight('alertBack',$menuFileID,$dataType);
		MoreSetDeal();
		break;

	case 'updateReplyNum':
		$menuFileID = 151;
		$MB->IsSecMenuRight('alert',$menuFileID,$dataType);
		UpdateReplyNum();
		break;

	default:
		die('err');
}

$MB->Close();
$DB->Close();





// 删除
function del(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$dataType	= OT::GetStr('dataType');
	$dataTypeCN	= OT::GetStr('dataTypeCN');
	$theme		= OT::GetStr('theme');
	$dataID		= OT::GetInt2( OT::ParaArr('id') );

	if ($dataID<=0){
		JS::AlertEnd('指定ID错误！');
	}

	$judRes = $DB->query('delete from '. OT_dbPref .'infoMessage where IM_ID='. $dataID);
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'title'	=> '昵称',
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】删除'. $alertRes .'！',
		));
	
	JS::ParentHiddenEnd('data'. $dataID);
}



// 批量删除
function MoreDel(){
	global $DB,$mudi,$menuFileID,$menuTreeID;

	$dataType	= OT::PostStr('dataType');
	$dataTypeCN	= OT::PostStr('dataTypeCN');
	$backURL	= OT::PostStr('backURL');
	$selDataID	= OT::Post('selDataID');

	$selCount = OT::NewCount($selDataID);
	if ($selCount <= 0){
		JS::AlertBackEnd('请先选择要删除的记录.');
	}

	$whereStr = '';
	for ($i=0; $i<$selCount; $i++){
		$whereStr .= ','. intval($selDataID[$i]);
	}
	if ($whereStr == ''){
		JS::AlertBackEnd('请先选择要删除的记录.');
	}

	$judRes = $DB->query('delete from '. OT_dbPref .'infoMessage where IM_ID in (0'. $whereStr .')');
		if ($judRes){
			$alertRes='成功';
		}else{
			$alertRes='失败';
		}

	Adm::AddLog(array(
		'note'	=> '【'. $dataTypeCN .'】批量删除'. $alertRes .'！',
		));

	JS::AlertHrefEnd('批量删除'. $alertRes .'.',$backURL);
}



// 修改内容
function RevContent(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataID			= OT::PostInt('dataID');
	$isGetScore		= OT::PostInt('isGetScore');
	$getScore1		= OT::PostInt('getScore1');
	$getScore2		= OT::PostInt('getScore2');
	$getScore3		= OT::PostInt('getScore3');
	$vote1			= OT::PostInt('vote1');
	$vote2			= OT::PostInt('vote2');
	$newContent		= Str::MoreReplace(OT::PostRStr('newContent'),'html');

	$record = array();
	$record['IM_isGetScore']	= $isGetScore;
	$record['IM_getScore1']		= $getScore1;
	$record['IM_getScore2']		= $getScore2;
	$record['IM_getScore3']		= $getScore3;
	$record['IM_vote1']			= $vote1;
	$record['IM_vote2']			= $vote2;
	$record['IM_content']		= $newContent;
	$judRes = $DB->UpdateParam('infoMessage', $record, 'IM_ID='. $dataID);
		if ($judRes){
			$alertRes='成功';
		}else{
			$alertRes='失败';
		}

	Adm::AddLog(array(
		'note'	=> '【'. $dataTypeCN .'】修改内容'. $alertRes .'！',
		));

	JS::AlertHref('修改成功',str_replace('&judRevContent=true','',$backURL));
}



// 保存回复
function reply(){
	global $DB,$mudi,$menuFileID,$menuTreeID;

	$backURL		= OT::PostStr('backURL');
	$theme			= OT::PostStr('theme');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$dataID			= OT::PostInt('dataID');
	$replyContent	= OT::PostRStr('replyContent');
	$isReply		= 0;
		if (strlen($replyContent)>0){ $isReply=1; }

	$record = array();
	$record['IM_replyTime']	= TimeDate::Get();
	$record['IM_isReply']	= $isReply;
	$record['IM_reply']		= $replyContent;

	$judRes = $DB->UpdateParam('infoMessage',$record,'IM_ID='. $dataID);
		if ($judRes){
			$alertRes='成功';
		}else{
			$alertRes='失败';
		}

	Adm::AddLog(array(
		'title'	=> '昵称',
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】回复保存'. $alertRes .'！',
		));

	JS::AlertClose('保存'. $alertRes .'.');
}



// 保存追加回复
function AddReply(){
	global $DB,$mudi,$menuFileID,$menuTreeID;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$theme			= OT::PostStr('theme');
	$dataID			= OT::PostInt('dataID');
	$addReplyContent= OT::PostRStr('addReplyContent');

	$revrec=$DB->query('select IM_reply from '. OT_dbPref .'infoMessage where IM_ID='. $dataID);
		if (! $row = $revrec->fetch()){ JS::AlertBackEnd('找不到该记录.'); }

	$judRes = $DB->UpdateParam('infoMessage',array('IM_reply'=>$row['IM_reply'] .'<br /><div style="color:red;">管理员回复：<br />'. Str::MoreReplace($addReplyContent,'html') .'</div>'),'IM_ID='. $dataID);
	unset($revrec);
		if ($judRes){
			$alertRes='成功';
		}else{
			$alertRes='失败';
		}

	Adm::AddLog(array(
		'title'	=> '昵称',
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】回复保存'. $alertRes .'！',
		));

	echo('
	<script language="javascript" type="text/javascript">
	alert("保存'. $alertRes .'");parent.document.location.reload();
	</script>
	');
}



// 设置状态
function SetState(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$dataType,$dataTypeCN;

	$backURL		= OT::GetStr('backURL');
	$dataType		= OT::GetStr('dataType');
	$dataTypeCN		= OT::GetStr('dataTypeCN');
	$theme			= OT::PostStr('theme');
	$dataID			= OT::GetInt2( OT::ParaArr('id') );
	$newState		= OT::GetInt('newState');

	$row = $DB->GetRow('select IM_userID,IM_infoID,IM_content,IM_isGetScore,IM_state from '. OT_dbPref .'infoMessage where IM_ID='. $dataID);

	$judRes = $DB->query('update '. OT_dbPref .'infoMessage set IM_state='. $newState .' where IM_ID='. $dataID);
		if ($judRes){
			$alertRes = '成功';
			if ($row['IM_userID'] > 0 && $row['IM_state'] == 0 && $row['IM_isGetScore'] == 0 && $newState == 1){
				$usArr = Users::AddScore('infoMessage', $row['IM_userID'], $dataID, $row['IM_infoID'], '', $row['IM_content']);
				if (strlen($usArr['note']) > 0){
					$alertRes .= '。'. $usArr['note'];
				}
			}
		}else{
			$alertRes = '失败';
		}

	if ($newState == 1){ $stateCN = '审核通过'; }else{ $stateCN = '待审核'; }

	Adm::AddLog(array(
		'title'	=> '昵称',
		'theme'	=> $theme,
		'note'	=> '【'. $dataTypeCN .'】设置状态（'. $stateCN .'）'. $alertRes .'！',
		));

	JS::AlertHref('状态修改'. $alertRes,$backURL);

}



// 批量设置
function MoreSetDeal(){
	global $DB,$mudi,$menuFileID,$menuTreeID,$systemArr;

	$backURL		= OT::PostStr('backURL');
	$dataType		= OT::PostStr('dataType');
	$dataTypeCN		= OT::PostStr('dataTypeCN');
	$moreSetTo		= OT::PostRegExpStr('moreSetTo','sql');
	$moreSetToCN	= OT::PostRegExpStr('moreSetToCN','sql');
	$selDataID		= OT::Post('selDataID');

	$selCount = OT::NewCount($selDataID);
	if ($selCount <= 0){
		JS::AlertBackEnd('请先选择要设置的记录.');
	}
	if ($moreSetTo==''){
		JS::AlertBackEnd('请选择批量设置的操作.');
	}

	$whereStr = '';
	for ($i=0; $i<$selCount; $i++){
		$whereStr .= ','. intval($selDataID[$i]);
	}
	if ($whereStr == ''){
		JS::AlertBackEnd('请先选择要设置的记录.');
	}

	switch ($moreSetTo){
		case 'state1':		$dealStr = 'IM_state=1';		break;
		case 'state0':		$dealStr = 'IM_state=0';		break;
		default :
			JS::AlertBackEnd('目的不明确.');
			break;
	}

	$judRes = $DB->query('update '. OT_dbPref .'infoMessage set '. $dealStr .' where IM_ID in (0'. $whereStr .')');
		if ($judRes){
			$alertRes = '成功';
		}else{
			$alertRes = '失败';
		}

	Adm::AddLog(array(
		'note'		=> '【'. $dataTypeCN .'】批量设置成【'. $moreSetToCN .'】'. $alertRes .'！',
		));

	JS::AlertHref('批量设置成【'. $moreSetToCN .'】'. $alertRes .'.',$backURL);
}


function UpdateReplyNum(){
	global $DB,$mudi,$menuFileID,$menuTreeID;

	$pageNum	= OT::GetInt('pageNum');
		if ($pageNum < 100){ $pageNum = 100; }
	$page		= OT::GetInt('page');
		if ($page < 1){ $page = 1; }

	$total = $DB->GetOne('select count(IF_ID) from '. OT_dbPref .'info');

	$pageCount = ceil($total / $pageNum);
		if ($page > $pageCount){ $page = $pageCount; }

	$infoexe = $DB->query('select IF_ID from '. OT_dbPref .'info limit '. ($page-1) .','. $pageNum);
	while ($row = $infoexe->fetch()){
		$replyNum = $DB->GetOne('select count(IM_ID) from '. OT_dbPref .'infoMessage where IM_infoID='. $row['IF_ID']);
		$DB->query('update '. OT_dbPref .'info set IF_replyNum='. $replyNum .' where IF_ID='. $row['IF_ID']);
	}
	unset($infoexe);

	if ($page >= $pageCount){
		JS::DiyEnd('
		parent.document.getElementById("replyRes").innerHTML="（进度完成：'. $page .' / '. $pageCount .'，每次'. $pageNum .'）";
		');
	}else{
		// echo('（进度：'. $page .' / '. $pageCount .'）');
		JS::DiyEnd('
		parent.document.getElementById("replyRes").innerHTML="（进度：'. $page .' / '. $pageCount .'，每次'. $pageNum .'）";
		document.location.href="?mudi=updateReplyNum&pageNum='. $pageNum .'&page='. ($page+1) .'";
		');
	}
}
?>