<?php
/**
*修改者：孙新乐
*2013.09.27
*修复类别迁移，子类不跟着变
*/
 
	require_once('_approot.php');	

	$origincate=intval($_POST['origincate']); 
	$destinationcate=intval($_POST['destcate']);
	if(isset($_POST['keepold']))$keepold=true;	
	//print_r($_POST);exit; 
	
	if($_POST['type']==1){

	//操作种类区分
	$origincachesql="select * from ls_products where ID={$origincate}";
	$origincachequery=$opendb->query($origincachesql);
	$origincache=mysqli_fetch_assoc($origincachequery);
		
	if(!is_null($origincache['cateName']))
	{
		$operationsign="G";
	}
	else if(!is_null($origincache['midcateName']))
	{
		$operationsign="M";
	}
	else if(!is_null($origincache['subcateName']))
	{
		$operationsign="S";
	}
	else
	{
		$errormsg="What the hell you pass me?  ";
	}
	
	if($destinationcate!=-1)
	{
		$destinationcachesql="select * from ls_products where ID={$destinationcate}";
		$destinationcachequery=$opendb->query($destinationcachesql);
		$destinationcache=mysqli_fetch_assoc($destinationcachequery);
		
		if(!is_null($destinationcache['cateName']))
		{
			$operationsign.="M";
		}
		else if(!is_null($destinationcache['midcateName']))
		{
			$operationsign.="S";
		}
		else if(!is_null($destinationcache['subcateName']))
		{
			$operationsign.="C";
		}
		else
		{
			$errormsg.="What the hell you pass me again?   ";
		}
	}
	else
	{
		$operationsign.="G";
	}
	
	//大类降级为中类，操作标记：GM  cateBrief
	if($operationsign=="GM")
	{
		$originfolder="../".$origincache['cateFolders'];
		$destinationfolder="../".$destinationcache['cateFolders']."/".$origincache['cateFolders'];
		$updatesql="update ls_products set cateName=null,cateFolders=null,cateBrief=null,cateDetail=null,midcateName='{$origincache['cateName']}',midcateFolders='{$origincache['cateFolders']}',midcateBrief='{$origincache['cateBrief']}',midcateDetail='{$origincache['cateDetail']}',midcateID={$destinationcate} where ID={$origincate}";
		$update=$opendb->query($updatesql);	
		$sql = "select * from ls_products where midcateID=$origincate";
		$query = $opendb->query($sql);
		$n = mysqli_num_rows($query);
		if($n>0){
			while($row = mysqli_fetch_assoc($query)){
				$updatesql="update ls_products set midcateName=null,midcateFolders=null,midcateBrief=null,midcateDetail=null,midcateID=null,subcateName='{$row['midcateName']}',subcateFolders='{$row['midcateFolders']}',subcateBrief='{$row['midcateBrief']}',subcateDetail='{$row['midcateDetail']}',subcateID={$origincate} where midcateID={$origincate}";
				$update=$opendb->query($updatesql);
			}
		}
	}
	
	//大类降级为小类，操作标记：GS
	if($operationsign=="GS")
	{
		$originfolder="../".$origincache['cateFolders'];
		$destinationfolder="../";
		$directorybuildsql="select * from ls_products where ID={$destinationcache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$destinationfolder.=$directorybuild['cateFolders']."/".$destinationcache['midcateFolders']."/".$origincache['cateFolders'];
		$updatesql="update ls_products set cateName=null,cateFolders=null,cateBrief=null,cateDetail=null,subcateName='{$origincache['cateName']}',subcateFolders='{$origincache['cateFolders']}',subcateBrief='{$origincache['cateBrief']}',subcateDetail='{$origincache['cateDetail']}',subcateID={$destinationcate} where ID={$origincate}";
		$update=$opendb->query($updatesql);
	}
	
	//中类降级为小类，操作标记：MS
	if($operationsign=="MS")
	{
		$directorybuildsql="select * from ls_products where ID={$origincache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders']."/".$origincache['midcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$destinationcache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$destinationfolder="../".$directorybuild['cateFolders']."/".$destinationcache['midcateFolders']."/".$origincache['midcateFolders'];
		$updatesql="update ls_products set midcateName=null,midcateFolders=null,midcateBrief=null,midcateDetail=null,midcateID=null,subcateName='{$origincache['midcateName']}',subcateFolders='{$origincache['midcateFolders']}',subcateBrief='{$origincache['midcateBrief']}',subcateDetail='{$origincache['midcateDetail']}',subcateID={$destinationcate} where ID={$origincate}";
		$update=$opendb->query($updatesql);
	}
		
	//中类升级为大类，操作标记：MG
	if($operationsign=="MG")
	{
		$directorybuildsql="select * from ls_products where ID={$origincache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders']."/".$origincache['midcateFolders'];
		$destinationfolder="../".$origincache['midcateFolders'];
		$updatesql="update ls_products set midcateName=null,midcateFolders=null,midcateBrief=null,midcateDetail=null,midcateID=null,cateName='{$origincache['midcateName']}',cateFolders='{$origincache['midcateFolders']}',cateBrief='{$origincache['midcateBrief']}',cateDetail='{$origincache['midcateDetail']}' where ID={$origincate}";
		$update=$opendb->query($updatesql);
		$sql = "select * from ls_products where subcateID={$origincate}";
		$query = $opendb->query($sql);
		$n = mysqli_num_rows($query);
		if($n>0){
			while($row = mysqli_fetch_assoc($query)){
				$updatesql="update ls_products set subcateName=null,subcateFolders=null,subcateBrief=null,subcateDetail=null,subcateID=null,midcateName='{$row['subcateName']}',midcateFolders='{$row['subcateFolders']}',midcateBrief='{$row['subcateBrief']}',midcateDetail='{$row['subcateDetail']}',midcateID={$origincate} where subcateID={$origincate}";
				$update=$opendb->query($updatesql);
			}
		}
		
	}
		
	//小类升级为大类，操作标记：SG
	if($operationsign=="SG")
	{
		$directorybuildsql="select * from ls_products where ID={$origincache['subcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="/".$directorybuild['midcateFolders']."/".$origincache['subcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$directorybuild['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders'].$originfolder;
		$destinationfolder="../".$origincache['subcateFolders'];
		$updatesql="update ls_products set subcateName=null,subcateFolders=null,subcateBrief=null,subcateDetail=null,subcateID=null,cateName='{$origincache['subcateName']}',cateFolders='{$origincache['subcateFolders']}',cateBrief='{$origincache['subcateBrief']}',cateDetail='{$origincache['subcateDetail']}' where ID={$origincate}";
		$update=$opendb->query($updatesql);
	}
		
	//小类升级为中类，操作标记：SM
	if($operationsign=="SM")
	{
		$directorybuildsql="select * from ls_products where ID={$origincache['subcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="/".$directorybuild['midcateFolders']."/".$origincache['subcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$directorybuild['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders'].$originfolder;
		$destinationfolder="../".$destinationcache['cateFolders']."/".$origincache['subcateFolders'];
		$updatesql="update ls_products set subcateName=null,subcateFolders=null,subcateBrief=null,subcateDetail=null,subcateID=null,midcateName='{$origincache['subcateName']}',midcateFolders='{$origincache['subcateFolders']}',midcateBrief='{$origincache['subcateBrief']}',midcateDetail='{$origincache['subcateDetail']}',midcateID={$destinationcate} where ID={$origincate}";
		$update=$opendb->query($updatesql);
	}
	
	//中类平级迁移，操作标记：MM
	if($operationsign=="MM")
	{
		$directorybuildsql="select * from ls_products where ID={$origincache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders']."/".$origincache['midcateFolders'];
		$destinationfolder="../".$destinationcache['cateFolders']."/".$origincache['midcateFolders'];
		$updatesql="update ls_products set midcateID={$destinationcate} where ID={$origincate}";
		$update=$opendb->query($updatesql);
	}
	
	//小类平级迁移，操作标记：SS
	if($operationsign=="SS")
	{
		$directorybuildsql="select * from ls_products where ID={$origincache['subcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="/".$directorybuild['midcateFolders']."/".$origincache['subcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$directorybuild['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders'].$originfolder;
		$directorybuildsql="select * from ls_products where ID={$destinationcache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysqli_fetch_assoc($directorybuildquery);
		$destinationfolder="../".$directorybuild['cateFolders']."/".$destinationcache['midcateFolders']."/".$origincache['subcateFolders'];
		$updatesql="update ls_products set subcateID={$destinationcate} where ID={$origincate}";
		$update=$opendb->query($updatesql);
	}	
	//dump($operationsign,$origincache,$destinationcache,$originfolder,$destinationfolder,$updatesql);
	
	rename($originfolder,$destinationfolder);
	if($keepold)copy($destinationfolder,$originfolder);//保留旧的以便优化
	
	}
	if($_POST['type']==2){
	
		$sql = "select * from ls_products where productID=".$origincate;
		$or = $opendb->allRows($sql);
		
		$sql = "select * from ls_products where ID=".$destinationcate;
		$des = $opendb->OneRow($sql);
		$desdir = getcatefolder($des['ID']);
		

		foreach($or as $v){
			$sql = "update ls_products set productID=".$destinationcate." where ID=".$v['ID'];	
			
			$oldpic = getpicfolder($v['ID']);
			$newpic = $desdir."/images/";
			$oldthpic = getpicfolder($v['ID'],2);
            $newthpic = $desdir."/thumbnails/";

            if(!file_exists($newpic)){
                mkdir($newpic,0777,true);
            }
            if(!file_exists($newthpic)){
                mkdir($newthpic,0777,true);
            }
			
			rename($oldthpic,$newthpic.$v['productPic']);
			rename($oldpic,$newpic.$v['productPic']);
			if($keepold)copy($newthpic.$v['productPic'],$oldthpic); //保留旧的以便优化
			if($keepold)copy($newpic.$v['productPic'],$oldpic);
			
			$opendb->query($sql);
			
		}
		caterest($origincate);
		caterest($destinationcate);
		
	}
	
	writelog($_SERVER['PHP_SELF'],"cate transfer: old:".$originfolder .", new:".$destinationfolder,"ID:".$origincate." ->". $destinationcate);	
	if($keepold){ //拷贝留备份
		$filename="../ls_admin/catetranscp.log";
		$fp = fopen($filename,"r");
		$logstr = fread($fp,filesize ($filename));
		$logstr = date('Y-m-d H:m:s')." ".$originfolder ." -> ".$destinationfolder."\r\n".$logstr;
		writegeneralfile($filename,$logstr);
	}


function getcatefolder($id){
	global $opendb;
	if($id==''){
		return false;	
	}
	$sql = "select * from ls_products where ID=".$id;
	$row = $opendb->OneRow($sql);
	if($row['cateFolders']!=null){
		return "../".$row['cateFolders'];	
	}else if($row['midcateFolders']!=null){
		$sql = "select * from ls_products where ID=".$row['midcateID'];	
		$big = $opendb->OneRow($sql);
		return "../".$big['cateFolders']."/".$row['midcateFolders'];
	}else if($row['subcateFolders']!=null){
		$sql = "select * from ls_products where ID=".$row['subcateID'];
		$mid = $opendb->OneRow($sql);
		$sql = "select * from ls_products where ID=".$mid['midcateID'];
		$big = $opendb->OneRow($sql);
		return "../".$big['cateFolders']."/".$mid['midcateFolders'].'/'.$row['subcateFolders'];
	}else{
		return false;
		}
	
	
	}
	
	
function getpicfolder($id,$type=1){
	global $opendb;
	if($id==''){
		return false;	
	}
	
	$sql = "select * from ls_products where ID=".$id;
	$pro = $opendb->OneRow($sql);
	
	$catefolder = getcatefolder($pro['productID']);
	if($type==1){
		$path = "images";
	}else{
		$path = "thumbnails";	
	}
	return $catefolder."/".$path."/".$pro['productPic'];
}

//类别排序方法
function caterest($id){
	global $opendb;
	$productID_sql="select * from ls_products where productID=".$id." order by Num asc";
	$productID_query=$opendb->query($productID_sql);
	$i=1;
	while($row = mysqli_fetch_assoc($productID_query)){
		$sql = "update ls_products set Num=$i where ID=".$row['ID'];
		$opendb->query($sql);
		$i++;
	}	
}

	
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<script language="JavaScript" src="js/ls.js"></script>
		<title>无标题文档</title>
            <link rel="stylesheet" href="css/style2.css" />
        <style>body{ font-family:"微软雅黑";}</style>
	</head>

 <body>
	<table width="100%" border="0" align="center" cellpadding="1" cellspacing="1">

            <tr>
				<td height="18" style="border-bottom:2px solid #00c0ef; padding-bottom:5px;"><div align="center"><font color="#3c8dbc" style="font-size:16px;">产品类别迁移结果</font></div>
				</td>
			</tr>
			<tr>
				<td height="213" valign="center" align="center">
				<?php 					echo "文件及目录已转移：&nbsp;&nbsp;{$originfolder}&nbsp;&nbsp;&rArr;&nbsp;&nbsp;{$destinationfolder}。<br />";
				?>
				请检查各类别和产品信息是否正常，确认正常后请重新生成整个站点。<br /><br /><a href="catetransfer.php">点此返回。</a>
				</td>
			</tr>
	</table>
			
 </body>
</html>