<?php
/**
*修改者：孙新乐
*2013.09.27
*修复类别迁移，子类不跟着变
*/
	session_start(); 
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	
	$opendb=new mydb();
	require_once('inc/char_set.php');
	$origincate=intval($_POST['origincate']);
	$destinationcate=intval($_POST['destcate']);
	
	//print_r($_POST);exit;
	
	if($_POST['type']==1){

	//操作种类区分
	$origincachesql="select * from ls_products where ID={$origincate}";
	$origincachequery=$opendb->query($origincachesql);
	$origincache=mysql_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=mysql_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 = mysql_num_rows($query);
		if($n>0){
			while($row = mysql_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=mysql_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=mysql_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders']."/".$origincache['midcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$destinationcache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysql_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=mysql_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 = mysql_num_rows($query);
		if($n>0){
			while($row = mysql_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=mysql_fetch_assoc($directorybuildquery);
		$originfolder="/".$directorybuild['midcateFolders']."/".$origincache['subcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$directorybuild['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysql_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=mysql_fetch_assoc($directorybuildquery);
		$originfolder="/".$directorybuild['midcateFolders']."/".$origincache['subcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$directorybuild['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysql_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=mysql_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=mysql_fetch_assoc($directorybuildquery);
		$originfolder="/".$directorybuild['midcateFolders']."/".$origincache['subcateFolders'];
		$directorybuildsql="select * from ls_products where ID={$directorybuild['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysql_fetch_assoc($directorybuildquery);
		$originfolder="../".$directorybuild['cateFolders'].$originfolder;
		$directorybuildsql="select * from ls_products where ID={$destinationcache['midcateID']}";
		$directorybuildquery=$opendb->query($directorybuildsql);
		$directorybuild=mysql_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($_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']);	
			$opendb->query($sql);
			
		}
		caterest($origincate);
		caterest($destinationcate);
		
	}

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 = mysql_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>
	</head>

 <body>
	<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
			<tr>
				<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong>产品类别迁移结果</strong></font></div>
				</td>
			</tr>
			<tr>
				<td height="213" valign="center" align="center" bgcolor="#dee7ff">
				<?
					echo "文件及目录已转移：&nbsp;&nbsp;{$originfolder}&nbsp;&nbsp;&rArr;&nbsp;&nbsp;{$destinationfolder}。<br />";
				?>
				请检查各类别和产品信息是否正常，确认正常后请重新生成整个站点。<br /><br /><a href="catetransfer.php">点此返回。</a>
				</td>
			</tr>
	</table>
			
 </body>
</html>