<?php
/*
pre_forum_threadclass
*/

if($start <= 1) {
	truncatetable('forum_threadclass');
	validid('boardid', 'board');
}
$query = $xc->db['source']->query("SELECT max(boardid) FROM {$source_tablepre}board");
$maxid = $xc->db['source']->fetch_array($query);

$query = $xc->db['source']->query("SELECT boardid, board_setting FROM {$source_tablepre}board WHERE boardid BETWEEN $start AND $end");
while($dvforum = $xc->db['source']->fetch_assoc($query)) {
	$dvforum	=	daddslashes($dvforum);

	$fid		=	$dvforum['boardid'];
	$ftemp		=	explode(',', $dvforum['board_setting']);
	$ftmp		=	$ftemp[48];
	$tmp		=	explode('$$', $ftmp);
	$typearray	=	$types	=	array();
	foreach($tmp AS $k => $v) {
		if(!empty($v)) {
			$typearray[$k + 1] = $v;
		}
	}

	if(count($typearray)) {
		foreach($typearray AS $typeid => $typename) {
			$typename = trim($typename);
			if($typename) {
				$threadtypeid = $xc->db['discuz']->result($xc->db['discuz']->query("SELECT typeid FROM {$discuz_tablepre}forum_threadclass WHERE name='$typename' LIMIT 1"), 0);
				if(!$threadtypeid) {
					//pre_forum_threadclass
					$typearray['types'][]	=	$typename;
					$typearray['icon'][]	=	'';
					//$typeid	=	'';
					//$fid	=	'';
					$name	=	$typename;
					$displayorder =	0;
					$icon	=	'';

					$field1	=	array('fid','name','displayorder','icon');
					$query1	=	getinsertsql("{$discuz_tablepre}forum_threadclass", $field1);
					if($xc->db['discuz']->query($query1)){
						$threadtypeid = $xc->db['discuz']->insert_id();
						$convertedrows ++;
					} else {
						reportlog("添加主题分类出错 name = $name ,SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
					}
					$totalrows ++;	
				}
				$types[$threadtypeid] = $typename;
				$query2	=	"UPDATE {$discuz_tablepre}forum_thread SET typeid='$threadtypeid' WHERE fid='$fid' AND typeid='$typeid'";
				if(!$xc->db['discuz']->query($query2)){
					reportlog("添加主题出错 typeid = $threadtypeid ,SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query2."</textarea>");
				}
			}
		}
		
		$typearray['required'] 	=	1;
		$typearray['listable']	=	1;
		$typearray['prefix'] 	=	1;
		$typearray['flat'] 		=	$typearray['types'];
		$threadtypes 	=	serialize($typearray);
		
		$query3	=	"UPDATE {$discuz_tablepre}forum_forumfield SET threadtypes='$threadtypes' WHERE fid='$fid'";
		if(!$xc->db['discuz']->query($query3)) {
			reportlog("添加版块主题分类出错 fid = $fid ,SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query3."</textarea>");
		}
	}
	$converted = 1;
	$url = 'index.php?mod=dealdata&step='.$step.'&start='.($dvforum['boardid']+1).'&stay='.$stay.'&totalrows='.$totalrows.'&convertedrows='.$convertedrows.'&sqlfilenum='.$sqlfilenum;
	file_put_contents(XCONVERT_ROOT.'./cache/lock/progress_'.$step.".txt", $url);
}
if( $end < $maxid ) {
	$converted = 1;
}
?>