<?php
#------------------------------------------------------#
#               本程序为zChain.net提供                 #
#                zChain论坛(ZBB)v2.00                  #
#------------------------------------------------------#
# INPUT: $action,$bid,$tid,$f_tobid,$f_move

require "bin/config.php";
require "bin/common.php";

require_once('core/class_forum_base.php');
CForumBase::setFileBase("$filepath");
require_once('core/base_memo_file.php');
require_once('core/class_board.php');
require_once('core/class_topic.php');
require_once('core/base_record_file.php');
require_once('core/class_topic_list.php');
require_once('core/base_index_file.php');
require_once('core/class_topic_index.php');

#--- log the event to trace attack ---
require_once('core/class_log.php');
CLOG::writeLogEntry("TopicMgr[action=$action,bid=$bid,tid=$tid] is called by user [$sv_user]");

$bid=str_replace(".", "", $bid);
$tid=str_replace(".", "", $tid);

#--- Validate user ---
$oBrd=new CBoard;
if(!$oBrd->load($bid)){errorview('指定论坛错误！');}
if(!$oBrd->isModerator($sv_user)){errorview("对不起，您没有此项操作的权限！");}

$oTpc=new CTopic;
if(!$oTpc->load($bid,$tid)){errorview("topic error!");}

$oTpcLst=new CTopicList;
$oTpcIdx=new CTopicIndex;

switch($action){
 case 'lock':
  $oTpc->locked=1; $oTpc->store($bid,$tid);
  _updateList($bid,&$oTpc);
  okview("主题已经关闭，不再接受新的回复！");
  exit;
 case 'unlock':
  $oTpc->locked=0; $oTpc->store($bid,$tid);
  _updateList($bid,&$oTpc);
  okview("主题已经开放，可以自由回复！");
  exit;
 case 'collect':
  $oTpc->setFlag('COLLECTED'); $oTpc->store($bid,$tid);
  _updateList($bid,&$oTpc);

  if($oTpcIdx->open($bid)){
   $oTpcIdx->selectIndex(2); $oTpcIdx->activateTopic($oTpc->refid);
   $oTpcIdx->close();
  }
  okview("主题已经添加到精华区！");
  exit;
 case 'uncollect':
  $oTpc->setFlag('COLLECTED',false); $oTpc->store($bid,$tid);
  _updateList($bid,&$oTpc);

  if($oTpcIdx->open($bid)){
   $oTpcIdx->selectIndex(2); $oTpcIdx->deleteTopic($oTpc->refid);
   $oTpcIdx->close();
  }
  okview("主题已经已经成功移出精华区！");
  exit;
 case 'settop':
  $oTpc->setFlag('TOPPED',true); $oTpc->store($bid,$tid);
  _updateList($bid,&$oTpc);

  if($oTpcIdx->open($bid)){
   $oTpcIdx->selectIndex(0); $oTpcIdx->deleteTopic($oTpc->refid);
   $oTpcIdx->selectIndex(1); $oTpcIdx->activateTopic($oTpc->refid);
   $oTpcIdx->close();
  }
  okview("主题已经成功放置顶端！");
  exit;
 case 'unsettop':
  $oTpc->setFlag('TOPPED',false); $oTpc->store($bid,$tid);
  _updateList($bid,&$oTpc);

  if($oTpcIdx->open($bid)){
   $oTpcIdx->selectIndex(1); $oTpcIdx->deleteTopic($oTpc->refid);
   $oTpcIdx->selectIndex(0); $oTpcIdx->activateTopic($oTpc->refid);
   $oTpcIdx->close();
  }
  okview("该主题已经取消置顶！");
  exit;
 case 'activate':
  $idx=($oTpc->getFlag('TOPPED')) ? 1 : 0;
  if($oTpcIdx->open($bid)){
   $oTpcIdx->selectIndex($idx); $oTpcIdx->activateTopic($oTpc->refid);
   $oTpcIdx->close();
  }
  okview("主题已经成功拉前！");
  exit;
 case 'move':
  move_form($bid,$tid);
  exit;
 case 'movecommit':
  move_commit($tid,$bid,$f_tobid,$f_move);
  jumpto("viewboard.php?bid=$bid","已经成功地将主题移动到您选择的论坛了，请稍候，系统将自动返回......");
  exit;
 default:
  errorview("unknow/not implemented action!");
  exit;
}

function _updateList($bid,$oTpc)
{
  $oTpcLst=new CTopicList;
  if($oTpcLst->open($bid)){
   $oTpcLst->updateTopic($oTpc->refid,$oTpc->id,$oTpc->getBrief());
   $oTpcLst->close();
  }
}

function move_form($bid, $tid)
{
 global $imgurl,$cgiurl,$ver;
 global $oBrd,$oTpc;

 require_once('core/base_array_file.php');
 require_once('core/class_board_list.php');
 $oBrdLst=new CBoardList;

 $flag=$oBrdLst->load();
 $moveSelection='<SELECT name=f_tobid><OPTION value="">将主题移动到</OPTION>';
 while($flag){
  $moveSelection.="<OPTION value=$oBrdLst->id>『{$oBrdLst->name}』";
  $flag=$oBrdLst->moveNext();
 }
 $moveSelection.='</SELECT>';

 page_header('移动帖子',"$imgurl/bg.gif");
 print "<CENTER>";
 $posText=getRootText()." >> 移动帖子";
 menu_bar($posText,$hpurl);

 print <<<EOT
<SCRIPT>
var i=0;
function formCheck()
{
 i++; if (i>1) {alert('正在移动中，请耐心等待！'); return false;}
 return true;
}
</SCRIPT>
<br>
<form name=form method="POST" onSubmit="return formCheck()">
<INPUT TYPE="HIDDEN" NAME="action" VALUE=movecommit>
<INPUT TYPE="HIDDEN" NAME="bid" VALUE=$bid>
<INPUT TYPE="HIDDEN" NAME="tid" VALUE=$tid>
<table width="80%" border="0" cellspacing="2" cellpadding="2" align="center">
<tr>
 <td width="34%" align="left">论坛：{$oBrd->name}</td>
 <td width="66%">主题：{$oTpc->title}</td>
</tr>
</table>
<table width="80%" border="0" cellspacing="1" cellpadding="2" align="center" class="forumLine">
<tr>
 <td colspan="3" align="center" class="rowCaption"> 移动帖子功能（注意：只有社区长或版主才拥有此权限)</td>
</tr>
<tr>
 <td class="row2" width="5%" valign="top"> 
  <input type="radio" checked></td>
 <td class="row2" width="67%"> 主题转移到论坛： <br>
  注意: 您有两个选项. 转移之后删除主题在当前的论坛或保持主题在当前的论坛. 
  <br><br>
  　<input type="radio" name=f_move> 转移之后保持主题在当前的论坛.<br>
  　<input type="radio" name=f_move checked value=1> 转移之后删除主题从当前的论坛.</td>
 <td class="row2" width="28%" valign="top" align="center"><br>
  $moveSelection<input type=submit value=" 确定 "></td>
</tr>
</table>
</form>
<div align="center"><a href=javascript:history.back()>
<IMG src=$imgurl/back.gif border=0></a>
EOT;

 page_footer();

}

function move_commit($tid, $bidfrom, $bidto, $move)
{
 global $oBrd,$oTpc;

 require_once('core/base_array_file.php');
 require_once('core/class_reply_set.php');
 $oRpl=new CReplySet;

 if(!$bidto){errorview("请选择您要将主题移动到哪个论坛！");}
 if($bidfrom == $bidto){return;}

 $oTpcLst=new CTopicList;
 $oTpcIdx=new CTopicIndex;
 $oTpcTest=new CTopic;

 #--- Check destination ---
 if(!$oTpcLst->open($bidto)){errorview('目标论坛错误！');}
 if($oTpcTest->load($bidto,$tid)){errorview('该主题在目标论坛已存在！');}

 #--- Copy topic to destination board ---
 $refid=$oTpc->refid;

 $oTpc->setFlag('TOPPED',false); $oTpc->setFlag('COLLECTED',false);
 if(!$oTpcLst->insertTopic($tid,$oTpc->getBrief())){errorview('列表生成错误！');}

 $oTpc->refid=$oTpcLst->getAbsolutePosition();
 if(!$oTpc->store($bidto,$tid)){errorview('主题文件复制错误！');}
 if($oRpl->load($bidfrom,$oTpc->replyid)){
  if(!$oRpl->store($bidto,$oTpc->replyid)){errorview('回复文件复制错误！');}
 }

 $oTpcLst->close();

 if($oTpcIdx->open($bidto)){
  $oTpcIdx->selectIndex(0); $oTpcIdx->activateTopic($oTpc->refid);
  $oTpcIdx->close();
 }

 #--- Process source board ---
 if($move == 1) {
  $oTpcLst->open($bidfrom);
  $oTpcLst->deleteTopic($refid,$tid);
  $oTpcLst->close();
  if($oTpcIdx->open($bidfrom)){
   $oTpcIdx->selectIndex(0); $oTpcIdx->deleteTopic($refid);
   $oTpcIdx->selectIndex(1); $oTpcIdx->deleteTopic($refid);
   $oTpcIdx->selectIndex(2); $oTpcIdx->deleteTopic($refid);
   $oTpcIdx->close();
  }
  #--- Delete topic ---
  $oRpl->destroy($bidfrom,$oTpc->replyid);
  $oTpc->destroy($bidfrom,$tid);
 }

}

?>