<?php
/*
主题表：pre_forum_thread
*/
if($start <= 1) {
	truncatetable('forum_thread');
	$sqlfilenum = 0;
	$extrastr = '';
}
$sqlfilenum ++;
$extrastr = '&sqlfilenum='.$sqlfilenum;
$maxtid = $xc->db['source']->result($xc->db['source']->query("SELECT max(tid) FROM {$source_tablepre}threads"), 0);
if($start < $maxtid){
	$converted = 1;
}
$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}threads WHERE tid >= $start AND tid <$start + $rpp") or dexit();

while ($pwthread = $xc->db['source']->fetch_array($query)) {
	$pwthread			=	daddslashes($pwthread);
	$is_group_thread		=	$xc->db['source']->result_first("SELECT COUNT(*) FROM {$source_tablepre}argument WHERE tid='{$pwthread[tid]}'");
	//if($is_group_thread) continue;
	//主题主表
	//pre_forum_thread
	$tid				=	$pwthread['tid'];
	$fid				=	$pwthread['fid'];
	$posttableid		=	0;
	$tmp_t_type = $xc->db['source']->result_first("SELECT t_type FROM {$source_tablepre}forums WHERE fid='$fid'");
	$tmp_t_type = explode("\t", $tmp_t_type);
	$classname			=	$tmp_t_type[$pwthread['type']];
	//$classname			=	$xc->db['source']->result($xc->db['source']->query("SELECT name FROM {$source_tablepre}topictype WHERE fid='{$fid}' AND id='{$pwthread['type']}' LIMIT 1"));
	$typeid				=	$xc->db['discuz']->result($xc->db['discuz']->query("SELECT typeid FROM {$discuz_tablepre}forum_threadclass WHERE fid='{$fid}' AND name='{$classname}' LIMIT 1"));
	$sortid				=	'';					//分类信息ID
	$readperm			=	0;					//阅读权限
	$price				=	0;					//出售价格
	$author				=	cutstr(htmlspecialchars(trim($pwthread['author'])), 15);
	$authorid			=	$pwthread['authorid'];
	$subject			=	formatstr(cutstr(trim(@strip_tags($pwthread['subject'])), 78));//formatstr去除字符串结尾的'\'
	$dateline			=	$pwthread['postdate'] > $timestamp ? $timestamp : $pwthread['postdate'];
	$lastpost			=	$pwthread['lastpost'] > $timestamp ? $timestamp : $pwthread['lastpost'];
	$lastposter			=	cutstr(htmlspecialchars(trim($pwthread['lastposter'])), 15);
	$views				=	$pwthread['hits'];
	$replies			=	$pwthread['replies'];
	$displayorder		=	in_array($pwthread['topped'],array(0,1,2,3)) ? $pwthread['topped'] : 0;
	$highlight			=	$pwthread['titlefont'] ? gethighlightid($pwthread['titlefont']) : 0;
	$digest				=	in_array($pwthread['digest'],array(0,1,2,3)) ? $pwthread['digest'] : 0;
	$rate				=	0;
	switch ($pwthread['special']) {
		case 0:			//普通
			$special = 0;
			break;
		case 1:			//投票
			$special = 1;
			break;
		case 22:			//活动-----
			$special = 4;
			break;
		case 3:			//悬赏
			$special = 3;
			break;
		case 4:			//商品
			$special = 2;
			break;
		case 5:			//辩论
			$special = 5;
			break;
	}
	$attachment			=	$pwthread['ifupload'] ? 1 : 0;
	$moderated			=	0;
	$closed				=	$pwthread['locked'] ? 1 : 0;
	$stickreply			=	0;
	$recommends			=	0;
	$recommend_add		=	0;
	$recommend_sub		=	0;
	$heats				=	0;
	$status				=	0;
	$isgroup			=	0;

	if($special == 1) {
	} elseif($special == 2) {
	} elseif($special == 3) {
		$query_3 = $xc->db['source']->query("SELECT cbtype FROM {$source_tablepre}reward WHERE tid='$tid'");
		if($cbtypeprice = $xc->db['source']->result($query_3, 0)) {
			$price = $cbtypeprice;
		} else {
			$price = 0;
		}

	} elseif($special == 4) {
	} elseif($special == 5) {
	}

	$field1				=	array('tid','fid','posttableid','typeid','sortid','readperm','price','author','authorid','subject','dateline','lastpost','lastposter','views','replies','displayorder','highlight','digest','rate','special','attachment','moderated','closed','stickreply','recommends','recommend_add','recommend_sub','heats','status','isgroup');
	$query1				=	getinsertsql("{$discuz_tablepre}forum_thread", $field1);

	if($xc->config['info']['convert_type'] == 'file') {
		writesql('forum_thread', $sqlfilenum, $query1);
		$convertedrows ++;
	} else {
		if ($xc->db['discuz']->query($query1)) {
			$convertedrows ++;
		} else {
			reportlog("无法转换主题 tid = $tid subject = '$subject'，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$sql."</textarea>");
		}
	}
	$converted = 1;
	$totalrows ++;
}
?>
