<?php
/*
转换论坛版块
pre_forum_forum
pre_forum_forumfield
*/

truncatetable('forum_forum');
truncatetable('forum_forumfield');

$query = $xc->db['source']->query("SELECT f.*, fd.* FROM {$source_tablepre}forums f	LEFT JOIN {$source_tablepre}forumdata fd ON f.fid=fd.fid ORDER BY f.fid ASC") or dexit();
while($pwforum = $xc->db['source']->fetch_array($query)) {
	$pwforum = daddslashes($pwforum);

	//查询具体版块的信息
	if(!empty($pwforum['fup'])){
		$t_query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}forumsextra WHERE fid=$pwforum[fid]");
		if($t_extra = $xc->db['source']->fetch_array($t_query)) {
			$pwforum['set']	=	unserialize($t_extra['forumset']);
		}
	}

	//pre_forum_forum
	$fid				=	$pwforum['fid'];

	switch(strtolower($pwforum['type'])) {
		case 'category':
			$pwforum['type']	=	'group';
			break;
		case 'forum':
			$pwforum['type']	=	'forum';
			break;
		case 'sub':
			$pwforum['type']	=	'sub';
			break;
		default:
			$pwforum['type']	=	'forum';
			break;
	}
	if($pwforum['type'] == 'sub' && $xc->db['source']->result($xc->db['source']->query("SELECT type FROM {$source_tablepre}forums WHERE fid='$pwforum[fup]'")) == 'sub') {
		$pwforum['type'] = 'forum';
		$pwforum['fup'] = 0;
	}
	$type			=	$pwforum['type'];
	$fup			=	$type == 'category' ? 0 : $pwforum['fup'];		//上级论坛id
	$name			=	cutstr(htmlspecialchars(trim(@strip_tags($pwforum['name']))), 50);

	$status			=	strtolower($pwforum['f_type']) == 'hidden' ? 0 : 1;	//显示状态(0:隐藏 1:正常 2:只显示子版块 3:群组)
	$displayorder		=	$pwforum['vieworder'];
	$styleid		=	0;
	$threads		=	$type != 'category' ? $pwforum['topic'] : 0;			//主题数量
	$posts			=	$type != 'category' ? $pwforum['article'] : 0;		//帖子数量
	$todayposts		=	0;			//今日发帖数量

	list($l_subject,$l_username,$l_timestamp,$l_url) = explode("\t", $pwforum['lastpost']);
	$l_tid			=	str_replace(array('read.php?tid=', '&page=e#a'), array('', ''), $l_url);
	$lastpost		=	$l_tid."\t".$l_subject."\t".$l_timestamp."\t".$l_username;//$pwforum['lastpost'];
	$allowsmilies		=	$type != 'category' ? 1 : 0;
	$allowhtml		=	0;
	$allowbbcode		=	$allowsmilies;
	$allowimgcode		=	$allowsmilies;
	$allowmediacode		=	$allowsmilies;
	$allowanonymous		=	0;
	$allowpostspecial	=	0;			//允许发表特殊主题


	//特殊主体开启与否的转换
	$tmp_allownormal 		=	($pwforum['allowtype'] & 1) ? 1 : 0;
	$tmp_allowpoll			=	($pwforum['allowtype'] & 2) ? 1 : 0;
	$tmp_allowactivity 		=	($pwforum['actmids'] || ($pwforum['allowtype'] & 4) ) ? 1 : 0;
	$tmp_allowreward		=	($pwforum['allowtype'] & 8) ? 1 : 0;
	$tmp_allowtrade			=	($pwforum['allowtype'] & 16) ? 1 : 0;
	$tmp_allowdebate		=	($pwforum['allowtype'] & 32) ? 1 : 0;
	$allowpostspecial		=	bindec(intval($tmp_allowdebate).intval($tmp_allowtrade).intval($tmp_allowreward).intval($tmp_allowactivity).intval($tmp_allowpoll).intval(1));


	$allowspecialonly		=	!$tmp_allownormal;
	$alloweditrules			=	0;
	$allowfeed			=	1;
	$allowside			=	1;
	$recyclebin			=	0;
	$modnewposts			=	0;
	$jammer				=	0;
	$disablewatermark		=	0;
	$inheritedmod			=	0;
	$autoclose			=	0;
	$forumcolumns			=	0;
	$threadcaches			=	0;
	$alloweditpost			=	1;
	$simple				=	$pwforum['childid'] ? $pwforum['viewsub'] : 0;
	$modworks			=	0;
	$allowglobalstick		=	1;
	$level				=	0;
	$commoncredits			=	0;
	$archive			=	0;
	$recommend			=	0;

	//pre_forum_forumfield
	//$fid				=	'';
	$description			=	$pwforum['descrip'];
	$password			=	'';
	$icon				=	$pwforum['logo'];
	$redirect			=	$pwforum['set']['link'];
	$attachextensions		=	'';
	$creditspolicy			=	'';
	$formulaperm			=	'';
	$moderators			=	addmoderators(explode(',', $pwforum['forumadmin']), $fid);	//版主
	$rules				=	'';
	$threadtypes			=	'';
	$threadsorts			=	'';
	$viewperm			=	'';
	$postperm			=	'';
	$replyperm			=	'';
	$getattachperm			=	'';
	$postattachperm			=	'';
	$postimageperm			=	'';
	$keywords			=	$pwforum['keywords'];
	$supe_pushsetting		=	'';
	$modrecommend			=	'';
	$threadplugin			=	'';
	$extra				=	'';
	$jointype			=	'';
	$gviewperm			=	'';
	$membernum			=	'';
	$dateline			=	'';
	$lastupdate			=	'';
	$activity			=	'';
	$founderuid			=	'';
	$foundername			=	'';
	$banner				=	'';
	$groupnum			=	'';
	$commentitem			=	'';

	$field1		=	array('fid','fup','type','name','status','displayorder','styleid','threads','posts','todayposts','lastpost','allowsmilies','allowhtml','allowbbcode','allowimgcode','allowmediacode','allowanonymous','allowpostspecial','allowspecialonly','alloweditrules','allowfeed','recyclebin','modnewposts','jammer','disablewatermark','inheritedmod','autoclose','forumcolumns','threadcaches','alloweditpost','simple','modworks','allowglobalstick','level','commoncredits','archive');
	$query1		=	getinsertsql("{$discuz_tablepre}forum_forum", $field1);

	$field2		=	array('fid','description','password','icon','redirect','attachextensions','creditspolicy','formulaperm','moderators','rules','threadtypes','threadsorts','viewperm','postperm','replyperm','getattachperm','postattachperm','postimageperm','keywords','supe_pushsetting','modrecommend','threadplugin','extra','jointype','gviewperm','membernum','dateline','lastupdate','activity','founderuid','foundername','banner','groupnum');
	$query2		=	getinsertsql("{$discuz_tablepre}forum_forumfield", $field2);

	if ($xc->db['discuz']->query($query1)) {
		if ($xc->db['discuz']->query($query2)) {
			$convertedrows ++;
		} else {
			$xc->db['discuz']->query("DELETE FROM {$discuz_tablepre}forumfields WHERE fid='$fid' LIMIT 1;");
			reportlog("插入版区扩展信息数据出错 fid = $fid, name = $name");
		}
	} else {
		reportlog("插入版区基本数据出错 fid = $fid, name = $name");
	}
	$totalrows ++;
}

?>
