<?php
require_once(dirname(__FILE__)."/config.php");
if(empty($ID)) $ID = 0;
if(empty($listtype)) $listtype="";
if(empty($dopost)) $dopost = "";
if(empty($channelid)) $channelid = 1;
$ID = ereg_replace("[^0-9]","",$ID);

if($ID==0){ CheckPurview('t_New'); }
else{
	CheckPurview('t_AccNew');
	CheckCatalog($ID,"你无权在本栏目下创建子类！");
}

$dsql = new DedeSql(false);


//保存栏目
/*------------------------
function __SaveCatalog()
--------------------------*/
if($dopost=="save")
{
   if(empty($reID)) $reID = 0;
   if(empty($upinyin)) $upinyin = 0;
   $description = Html2Text($description);
   $keywords = Html2Text($keywords);

   //栏目的参照目录
   if($reID==0 && $moresite==1) $nextdir = "/";
   else{
     if($referpath=="cmspath") $nextdir = "{cmspath}";
     else if($referpath=="basepath") $nextdir = "";
     else $nextdir = $nextdir;
   }
   //用拼音命名
   if( $upinyin==1 ||
     ( $typedir=="" && $sitepath=="" ) ||
     ( $typedir=="" && $moresite==1 && $reID>0 ) )
   {
     	 $typedir = GetPinyin($typename);
   }

   //子分类
   $smalltypes = "";
   if(is_array($sonlists) && isset($needson)){
   	 $n = count($sonlists);
   	 for($i=0;$i<$n;$i++){
   	 	 if($i==($n-1)) $smalltypes .= $sonlists[$i];
   	 	 else $smalltypes .= $sonlists[$i].",";
   	 }
   }

   $typedir = $nextdir."/".$typedir;

   $typedir = ereg_replace("/{1,}","/",$typedir);

   if($referpath=="basepath" && $siteurl!="") $typedir = "";

   //检测二级网址
   if($siteurl!=""){
      $siteurl = ereg_replace("/$","",$siteurl);
      if(!eregi("http://",$siteurl)){
      	$dsql->Close();
   	    ShowMsg("你绑定的二级域名无效，请用(http://域名)的形式！","-1");
   	    exit();
      }
      if(eregi($cfg_basehost,$siteurl)){
      	$dsql->Close();
   	    ShowMsg("你绑定的二级域名与当前站点是同一个域名，不需要绑定！","-1");
   	    exit();
      }
   }

   //创建目录
   $true_typedir = str_replace("{cmspath}",$cfg_cmspath,$typedir);
   $true_typedir = ereg_replace("/{1,}","/",$true_typedir);
   if(!CreateDir($true_typedir,$siterefer,$sitepath))
   {
   	  $dsql->Close();
   	  ShowMsg("创建目录 {$true_typedir} 失败，请检查你的路径是否存在问题！","-1");
   	  exit();
   }

   if($channeltype == '-2'){
   		$isdefault = '-1';


	}


   $in_query = "insert into #@__arctype(
    reID,sortrank,typename,typedir,isdefault,defaultname,issend,channeltype,
    tempindex,templist,temparticle,tempone,modname,namerule,namerule2,
    ispart,corank,description,keywords,moresite,siterefer,sitepath,siteurl,ishidden,smalltypes)Values(
    '$reID','$sortrank','$typename','$typedir','$isdefault','$defaultname','$issend','$channeltype',
    '$tempindex','$templist','$temparticle','$tempone','default','$namerule','$namerule2',
    '$ispart','$corank','$description','$keywords','$moresite','$siterefer','$sitepath','$siteurl','$ishidden','$smalltypes')";


   $dsql->SetQuery($in_query);
   if(!$dsql->ExecuteNoneQuery($in_query))
   {
   	 $dsql->Close();
   	 ShowMsg("保存目录数据时失败，请检查你的输入资料是否存在问题！","-1");
   	 exit();
   }
   $dsql->Close();
   $rndtime = time();
   $rflwft = "<script language='javascript'>
   if(window.navigator.userAgent.indexOf('MSIE')>=1){
     if(top.document.frames.menu.location.href.indexOf('catalog_menu.php')>=1)
     { top.document.frames.menu.location = 'catalog_menu.php?$rndtime'; }
   }else{
  	 if(top.document.getElementById('menu').src.indexOf('catalog_menu.php')>=1)
     { top.document.getElementById('menu').src = 'catalog_menu.php?$rndtime'; }
   }
   </script>";
   echo $rflwft;
   ShowMsg("成功创建一个分类！","catalog_main.php");
   exit();

}
//End dopost==save
//结束保存栏目事件

//--------------------------
//读取父类参数
//----------------------------
if(empty($myrow)) $myrow = "";
$issend = 1;
$corank = 0;

if($ID>0)
{
  $myrow = $dsql->GetOne("Select #@__arctype.*,#@__channeltype.typename as ctypename From #@__arctype left join #@__channeltype on #@__channeltype.ID=#@__arctype.channeltype where #@__arctype.ID=$ID");
	$issennd = $myrow['issend'];
	$corank = $myrow['corank'];
	$topID = $myrow['topID'];
	$issend = $myrow['issend'];
	$corank = $myrow['corank'];
	if($topID>0)
	{
	  	$toprow = $dsql->GetOne("Select moresite,siterefer,sitepath,siteurl From #@__arctype where ID=$topID");
	  	foreach($toprow as $k=>$v){
	  		if(!ereg("[0-9]",$k)) $myrow[$k] = $v;
	  	}
	}
	$typedir = $myrow['typedir'];
}
//读取频道模型信息
if(is_array($myrow)) $channelid = $myrow['channeltype'];
else $channelid = 1;
$row = $dsql->GetOne("select * from #@__channeltype where ID='$channelid'");
$nid = $row['nid'];
//读取所有模型资料
$dsql->SetQuery("select * from #@__channeltype where ID<>-1 And isshow=1 order by ID");
$dsql->Execute();
while($row=$dsql->GetObject())
{
  $channelArray[$row->ID]['typename'] = $row->typename;
  $channelArray[$row->ID]['nid'] = $row->nid;
}
//父栏目是否为二级站点
if(!empty($myrow['moresite'])){
	 $moresite = $myrow['moresite'];
}else{
	 $moresite = 0;
}

require_once(dirname(__FILE__)."/templets/catalog_add.htm");

$dsql->Close();

?>