<?php
/*
收藏夹表：pre_home_favorite
*/
if($start <= 1) {
	truncatetable('home_share');
	truncatetable('home_favorite');
}

$maxsid = $xc->db['source']->result($xc->db['source']->query("SELECT max(id) FROM {$source_tablepre}collection"), 0);
if($start < $maxsid) $converted = 1;

$query = $xc->db['source']->query("SELECT * FROM {$source_tablepre}collection WHERE id >= $start AND id < $start + $rpp") or dexit();
while ($share = $xc->db['source']->fetch_array($query)) {
	$content		=	unserialize($share['content']);
	$share			=	daddslashes($share);

	$sid			=	$share['id'];
	switch($share['type']) {
		case in_array($share['type'], array('postfavor', 'diary')):
			//pre_home_favorite
			//$favid	=	'';
			$id	=	$share['typeid'];
			$idtype	=	$share['type'] == 'diary' ? 'blogid' : 'tid';
			$spaceuid	=	0;
			$title	=	$content[$share['type']]['subject'];
			//$description	=	'';
			$dateline	=	$share['postdate'];
			break;
		case 'group':
			$maxfid	=	$xc->db['source']->result($xc->db['source']->query("SELECT MAX(fid) FROM {$source_tablepre}forums"));
			$maxgid	=	$xc->db['source']->result($xc->db['source']->query("SELECT MAX(id) FROM {$source_tablepre}cnstyles"));
			$id	=	$share['typeid'] + $maxfid + $maxgid;
			$idtype	=	'gid';
			$spaceuid	=	0;
			$title	=	$content['group']['name'];
			$dateline	=	$share['postdate'];
			break;
		case 'web' :
			$title_template	=	'分享了一个网址';
			$body_template	=	'{link}';
			$arr			=	$content;
			foreach($arr as $key => $value){
				${$key} = $value;
			}
			$data			=	$link;
			$link			=	"<a href=\"".$data."\" target=\"_blank\">".$data."</a>";
			$body_data		=	serialize(array("link" => $link, "data" => $data));
			$body_general	=	$descrip;
			$image			=	'';
			$image_link		=	'';
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'link';
			break;
		case 'weibo':
			if($content['type'] == 'group_photos') {
				$title_template	=	'分享了一个图片';
				$body_template	=	'相册: <b>{albumname}</b><br>{username}<br>{title}';
				$type = 'pic';
				$extra = unserialize($content['extra']);
				$body_data = serialize(array("albumname" => $extra['aname'], "username" => $extra['photos'][0]['uploader'], "title" => ""));
				$body_general	=	$descrip;
				$image = 'attachment/'.$extra['photos'][0]['path'];
				$image_link = "space.php?uid=".$share['uid']."&do=album&picid=".$extra['photos'][0]['pid'];
			}
			break;
		case 'photo' :
			$title_template	=	'分享了一个图片';
			$body_template	=	'相册: <b>{albumname}</b><br>{username}<br>{title}';

			$arr			=	$content;
			foreach($arr as $key => $value){
				if(is_array($value)){
					foreach($value as $subkey => $subvalue){
						${$subkey} = $subvalue;
					}
				} else {
					${$key}		=	$value;
				}
			}
			$query_photo = $xc->db['source']->query("select a.aname, p.aid, p.pid from {$source_tablepre}cnphoto p, {$source_tablepre}cnalbum a where p.aid = a.aid and p.path = '$image'");
			$detail			=	$xc->db['source']->fetch_array($query_photo);
			$albumname		=	$detail['aname'];
			$albumid		=	$detail['aid'];
			$picid			=	$detail['pid'];

			$albumname		=	"<a href=\"space.php?uid=".$uid."&do=album&id=".$share['typeid']."\">".$albumname."</a>";
			$username		=	"<a href=\"space.php?uid=".$uid."\">".$username."</a>";
			$body_data		=	serialize(array("albumname" => $albumname, "username" => $username, "title" => ""));
			$body_general	=	$descrip;
			$image			=	$image;
			$image_link		=	"space.php?uid=".$uid."&do=album&picid=".$share['typeid'];
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'pic';
			break;

		case 'album' :
			$title_template	=	'分享了一个相册';
			$body_template	=	'<b>{albumname}</b><br>{username}';
			$arr			=	$content;
			foreach($arr as $key => $value){
				if(is_array($value)){
					foreach($value as $subkey => $subvalue){
						${$subkey} = $subvalue;
					}
				}else{
					${$key}		=	$value;
				}
			}
			$query_album = $xc->db['source']->query("select p.aid, a.aname from {$source_tablepre}cnphoto p, {$source_tablepre}cnalbum a where p.aid=a.aid and p.path = '$image'");
			$detail			=	$xc->db['source']->fetch_array($query_album);
			$aid			=	$detail['aid'];
			$name			=	$detail['aname'];

			$albumname		=	"<a href=\"space.php?uid=".$uid."&do=album&id=".$aid."\">".$name."</a>";
			$username		=	"<a href=\"space.php?uid=".$uid."\">".$username."</a>";

			$body_data		=	serialize(array("albumname" => $albumname, "username" => $username));
			$body_general	=	$descrip;
			$image			=	$image;
			$image_link		=	"space.php?uid=".$uid."&do=album&id=".$aid;
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'album';
			break;
		case 'user' :
			$title_template	=	'分享了一个用户';
			$body_template	=	'<b>{username}</b><br>{reside}<br>{spacenote}';
			$arr			=	$content;
			foreach($arr as $key => $value){
				if(is_array($value)){
					foreach($value as $subkey => $subvalue){
						${$subkey} = $subvalue;
					}
				}else{
					${$key}		=	$value;
				}
			}
			$uid = $xc->db['source']->result($xc->db['source']->query("select uid from {$source_tablepre}members where username = '$username'"), 0);

			$username		=	"<a href=\"space.php?uid=".$uid."\">".$username."</a>";

			$body_data		=	serialize(array("username" => $username, "reside" => "", "spacenote" => ""));
			$body_general	=	$descrip;
			$image			=	'';
			$image_link		=	'';
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			$type			=	'space';
			break;

		case 'multimedia':
			$title_template	=	'分享一首音乐';
			$body_template	=	' 	{link}';
			$arr			=	$content;
			foreach($arr as $key => $value){
				${$key} = $value;
			}
			$data			=	$link;
			$link			=	"<a href=\"".$data."\" target=\"_blank\">".$data."</a>";
			$body_data		=	serialize(array("link" => $link, "data" => $data, "musicvar"=>$data));
			$body_general	=	$descrip;
			$image			=	'';
			$image_link		=	'';
			$hash_data		=	md5($title_template."\t".$body_template."\t".$body_data);
			break;
	}
	$uid			=	$share['uid'];
	$username		=	$share['username'];
	$dateline		=	$share['postdate'];
	$hot			=	'';
	$hotuser		=	'';

	$field1 = array('sid','type','uid','username','dateline','title_template','body_template','body_data','body_general','image','image_link','hot','hotuser');
	$query1 = getinsertsql("{$discuz_tablepre}home_share", $field1);
	if(in_array($share['type'], array('postfavor', 'group', 'diary'))) {
		$field1 = array('uid', 'id', 'idtype', 'spaceuid', 'title', 'dateline');
		$query1 = getinsertsql("{$discuz_tablepre}home_favorite", $field1);
		$uid = $id = $idtype = $spaceuid = $title = $dateline = '';
	}

	if ($xc->db['discuz']->query($query1)) {
		$convertedrows ++;
	} else {
		reportlog("无法转换分享 id = $sid ，SQL 语句如下 ：<br><textarea rows=\"3\" style=\"width: 100%;\">".$query1."</textarea>");
	}
	$converted = 1;
	$totalrows ++;
}

?>
