<?php
//by chinayubo QQ：415204 2011-07-03
require("../class/connect.php");
require("../class/db_sql.php");
require("../class/functions.php");
require("../class/classfun.php");
require("../data/dbcache/class.php");

$link=db_connect();
$empire=new mysqlquery();
$lur=is_login();
$logininid=$lur['userid'];
$loginin=$lur['username'];
$loginrnd=$lur['rnd'];
$loginlevel=$lur['groupid'];
is_login($logininid,$loginin,$loginrnd);
$editor=0;
CheckLevel($logininid,$loginin,$classid,"group");

$action = $_POST[action];
if($action){
$oldclassid = (integer)$_POST['oldclassid'];
$content = trim($_POST['content']);

if(!$oldclassid or !$content){
	printerror("请检查你的输入是否正确","history.go(-1)",0,0,1);
}

//先提取要复制的数据源
$r = $empire->fetch1( "select * from {$dbtbpre}enewsclass where classid={$oldclassid} limit 1" );

if(!$r){
	printerror("你要复制的栏目不存在,请检查你的输入是否正确","history.go(-1)",0,0,1);
}

//构造SQL语句
$query = $empire->query( "select * from {$dbtbpre}enewsclass" );
$fields_num = mysql_num_fields( $query );
for ( $i=0; $i<$fields_num; $i++ ){

	$f = mysql_field_name( $query, $i );
	if($f=="classid") continue;
	$t = mysql_field_type( $query, $i );
	$dot = (($i+1)==$fields_num) ? "" : ", ";
	$sr = "";
	if($t!="smallint" and $t!="tinyint" and $t!="int"){
		$sr = "'";
	}
	$sqls .= "{$f}={$sr}{$r[$f]}{$sr}{$dot}";
}

foreach(explode("\r",trim($content)) as $value){
	$ret = $sqls;
	if(strstr($value,"|")){
		$cp = explode("|",trim($value));
		$classname = trim($cp[1]);
		$classpath = trim($cp[0]);
	}else{
		$classname = trim($value);
		//$cname=iconv("UTF-8","GBK",$classname);
		//require("pp.php");
		//$PingYing = new GetPingYing();
		//$classpath=$PingYing->getFirstPY($cname);
		$classpath = ReturnPinyinFun($classname);
	}
	$bname = $classname;
	$ret = str_replace( "classname='".$r[classname]."'", "classname='".$classname."'", $ret );
	$ret = str_replace( "bname='".$r[bname]."'", "bname='".$bname."'", $ret );
	
	if(strstr($r[classpath],"/")){
		$path = substr(strrchr($r[classpath],'/'),1);
		$path = str_replace( "/".$path."'", "/".$classpath."'", "classpath='".$r[classpath]."'" );
		$ret = str_replace( "classpath='".$r[classpath]."'", $path, $ret );
	}else{
		$ret = str_replace( "classpath='".$r[classpath]."'", "classpath='".$classpath."'", $ret );
	}
	$sql= $empire->query("insert into {$dbtbpre}enewsclass set ".$ret."");
	$cid=$empire->lastid();
	$fsql=$empire->query("insert into {$dbtbpre}enewsclassadd (classid) values($cid);");
	$fsql=$empire->query("insert into {$dbtbpre}enewsclass_stats (classid) values($cid);");
	//修改父栏目的子栏目
	$add[bclassid]=$class_r[$oldclassid][bclassid];
		if($add[bclassid])
		{
			$b_r=$empire->fetch1("select sonclass,featherclass from {$dbtbpre}enewsclass where classid='$add[bclassid]'");
			if(empty($b_r[sonclass]))
			{
				$b_r[sonclass]="|";
			}
			$new_sonclass=$b_r[sonclass].$cid."|";
			$update=$empire->query("update {$dbtbpre}enewsclass set sonclass='$new_sonclass' where classid='$add[bclassid]'");
			//更改父类别的父栏目的子栏目
			$where=ReturnClass($b_r[featherclass]);
			if(empty($where)){
				$where="classid=0";
			}
			$bsql=$empire->query("select sonclass,classid from {$dbtbpre}enewsclass where ".$where);
			while($br=$empire->fetch($bsql))
			{
				if(empty($br[sonclass]))
				{
					$br[sonclass]="|";
				}
				$new_sonclass=$br[sonclass].$cid."|";
				$update=$empire->query("update {$dbtbpre}enewsclass set sonclass='$new_sonclass' where classid='$br[classid]'");
            }
	    }
	unset($sql);
	
}

if($fsql){
	DelFcListClass();
	//DelListEnews();
	GetClass();
	printerror("所有栏目添加成功,请恢复栏目录！","copyclass.php",0,0,1);
}

}else{
if(!file_exists($fcfile))
{$class=ShowClass_AddClass("",0,0,"|-",0,0);}
?>
<html>
<head>
<title>批量复制栏目</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="adminstyle/1/adminstyle.css" type="text/css">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
  <tr> 
	<td>位置：<a href=ListClass.php>栏目管理</a>&nbsp;->&nbsp;批量复制栏目 </td>
  </tr>
</table>
<br>

<form name="form1" method="post" action="" onSubmit="return confirm('确认要添加？');">
  <table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
    <tr class="header"> 
      <td height="25" colspan="2">批量复制栏目</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td width="19%" height="25">源栏目</td>
      <td width="81%" height="25">

        <select name="oldclassid">
          <option value=0>选择要复制的栏目</option>
         <?=$class?>
        </select>
        
		</td>
    </tr>
    <tr bgcolor="#FFFFFF"> 
      <td>新栏目<br /><font color="red">新栏目一行一个,新栏目将与源栏目同级别存在</font></td>
      <td height="200"><textarea name="content" style="width:90%; height:200px;" id="content"></textarea></td>
    </tr>

    <tr bgcolor="#FFFFFF"> 
      <td height="25">&nbsp;</td>
      <td height="25"><input type="submit" name="action" value="提交"> <input type="reset" name="Submit2" value="重置"></td>
    </tr>
  </table>
</form>
</body>
</html>
<?
}
db_close();
$empire=null;
?>