<?php
define('IN_OUN', true);
include_once( "includes/command.php");

if($Aconf['priveMessage'] != '')
{
   echo showMessage($Aconf['priveMessage']);
   exit;
}

$db_table = $pre."productcat";
//post ifhot
if( $_POST['action'] == 'add'  )
{
    $acids = '';
	$i = count($_POST["pro_acids"]);
    while( @list( $k, $v ) = @each( $_POST["pro_acids"]) ) 
	{
       $j = $k + 1;
       $acids .= ($i == $j )?$v:$v.',';
	}
	$Afields=array('fid'=>$_POST['fid'],'next_node'=>'','name'=>$_POST['name'],'descs'=>$_POST['descs'],'ifnav'=>$_POST['ifnav'],'ifhot'=>$_POST['ifhot'],'pro_interval'=>$_POST['pro_interval'],'acids'=>$acids,'domain_id'=>$_SESSION[$un_domain_id]);
    $tpcid = $oPub->install($db_table,$Afields);
    if($tpcid)
	{
		$_POST['fid'] = $_POST['fid'] + 0;

        $sql = "SELECT pcid FROM ".$db_table." where fid = ".$_POST['fid']." AND domain_id=".$_SESSION[$un_domain_id];
	    $row = $oPub->select($sql);
		$next_node  = '';
		while( @list( $key, $value ) = @each( $row) ) {
			$next_node .=  $value["pcid"].',';
		}
		if(!empty($next_node )){
			$next_node  = substr($next_node ,0,-1);
			$Afields=array('next_node'=>$next_node);
			$condition = "pcid = ".$_POST['fid']." AND domain_id=".$_SESSION[$un_domain_id];
			$oPub->update($db_table,$Afields,$condition);
		}

	    /* 导航条显示 */
	    if($_POST['ifnav'])
	    {
			$db_table = $pre."nav"; 
			$url = 'products.php?pcid='.$tpcid;			$Afields=array('name'=>$_POST['name'],'ifshow'=>0,'url'=>$url,'domain_id'=>$_SESSION[$un_domain_id]);
            $oPub->install($db_table,$Afields);
	    }
	}
	unset($Anorm);
}

if( $_POST['action'] == 'edit' && $_POST['pcid'] != $_POST['fid']){
	$db_table = $pre."productcat";
	$_POST['pcid'] = $_POST['pcid'] + 0;
	$_POST['fid'] = $_POST['fid'] + 0;

    $acids = '';
	$i = count($_POST["pro_acids"]);
    while( @list( $k, $v ) = @each( $_POST["pro_acids"]) ) 
	{
       $j = $k + 1;
       $acids .= ($i == $j )?$v:$v.',';
	}

	$sql = "SELECT fid FROM ".$db_table.' where pcid='.$_POST["pcid"].' AND domain_id='.$_SESSION[$un_domain_id];
	$old_fid = $oPub->getOne($sql); 
	$condition =' pcid='.$_POST["pcid"].' AND domain_id='.$_SESSION[$un_domain_id];
	$Afields=array('fid'=>$_POST['fid'],'name'=>$_POST['name'],'descs'=>$_POST['descs'],'ifnav'=>$_POST['ifnav'],'ifhot'=>$_POST['ifhot'],'pro_interval'=>$_POST['pro_interval'],'acids'=>$acids);
	$oPub->update($db_table,$Afields,$condition); 

	if($old_fid > 0){ 
        $sql = "SELECT pcid FROM ".$db_table." where fid = ".$old_fid." AND domain_id=".$_SESSION[$un_domain_id];
	    $row = $oPub->select($sql);
		$next_node  = '';
		while( @list( $key, $value ) = @each( $row) ) {
			$next_node .=  $value["pcid"].',';
		} 
		$next_node  = substr($next_node ,0,-1);
		$Afields=array('next_node'=>$next_node);
		$condition = "pcid = ".$old_fid." AND domain_id=".$_SESSION[$un_domain_id];
		$oPub->update($db_table,$Afields,$condition); 
	} 

	if($_POST['fid'] > 0){
        $sql = "SELECT pcid FROM ".$db_table." where fid = ".$_POST['fid']." AND domain_id=".$_SESSION[$un_domain_id];
	    $row = $oPub->select($sql);
		$next_node  = '';
		while( @list( $key, $value ) = @each( $row) ) {
			$next_node .=  $value["pcid"].',';
		} 
		$next_node  = substr($next_node ,0,-1);
		$Afields=array('next_node'=>$next_node);
		$condition = "pcid = ".$_POST['fid']." AND domain_id=".$_SESSION[$un_domain_id];
		$oPub->update($db_table,$Afields,$condition);

	}

	 /* 导航条显示 */
	 $db_table = $pre."nav"; 
	 if($Anorm['ifnav'])
	 {
		 if(!$_POST['ifnav'])
		 {
			 $url = "products.php?pcid=".$_POST['pcid'];
	         $condition = 'url="'.$url.'" AND domain_id='.$_SESSION[$un_domain_id];
             $oPub->delete($db_table,$condition);
	      }
	 }
	 else
	 {
		 if($_POST['ifnav'])
		 {
			$url = "products.php?pcid=".$_POST['pcid'];
			$Afields=array('name'=>$_POST['name'],'ifshow'=>0,'url'=>$url,'domain_id'=>$_SESSION[$un_domain_id]);
            $oPub->install($db_table,$Afields);
	      }
	 }

	 unset($Anorm);
	 unset($_GET);
}

//get
$db_table = $pre."productcat";
if( $_GET['action'] == 'edit'){
	$_GET['pcid'] = $_GET['pcid'] +0;
	$sql = "SELECT * FROM ".$db_table." where pcid = ".$_GET['pcid']." AND domain_id=".$_SESSION[$un_domain_id];
	$Anorm = $oPub->selectrow($sql);
}

if( $_GET['action'] == 'del'){
	/*还有子分类将不能删除*/
	$_GET['pcid'] = $_GET['pcid'] + 0;
	$_GET['fid'] = $_GET['fid'] + 0;
	$strwhere = " where pcid = ".$_GET['pcid']." AND domain_id=".$_SESSION[$un_domain_id];
	$sql = "SELECT next_node FROM ".$db_table.$strwhere;
	$Anorm = $oPub->selectrow($sql);
	if($Anorm[next_node] == '' && $_GET['fid'] != 0)
	{
	  /* 上级分类标识整理 */
	  $strwhere = " where pcid = ".$_GET['fid']." AND domain_id=".$_SESSION[$un_domain_id];
	  $sql = "SELECT * FROM ".$db_table.$strwhere;
	  $Anorm = $oPub->selectrow($sql);
	  $next_node = str_replace(','.$_GET['pcid'],'',$Anorm['next_node']);
	  $next_node = str_replace($_GET['pcid'],'',$next_node);
      $Afields=array('next_node'=>$next_node);
	  $condition = "pcid = ".$Anorm['pcid'];
	  $oPub->update($db_table,$Afields,$condition);
      unset($Anorm);

	  $condition = 'pcid='.$_GET['pcid']." AND domain_id=".$_SESSION[$un_domain_id];
      $oPub->delete($db_table,$condition);
	}
	elseif($_GET['fid'] == 0 && $Anorm[next_node] == '')
	{
	  $condition = 'pcid='.$_GET['pcid'].' AND domain_id='.$_SESSION[$un_domain_id];
      $oPub->delete($db_table,$condition);
	}
	else
	{
       $strMessage = '存在下级分类，不能删除。';
	}
}

if ($strMessage != '')
{
     echo  '<SCRIPT language="javascript"> alert( "'.$strMessage.'");</script>';
}

 

$ifnav_1 = ($Anorm[ifnav] == 1)? 'SELECTED':'';
$ifnav_0 = ($Anorm[ifnav] == 0)? 'SELECTED':'';

$ifhot_1 = ($Anorm[ifhot] == 1)? 'SELECTED':'';
$ifhot_0 = ($Anorm[ifhot] == 0)? 'SELECTED':''; 
 

$Strifnavopt = '<SELECT NAME="ifnav">';
$Strifnavopt .= '<OPTION VALUE="1" '.$ifnav_1.'>是</OPTION>';
$Strifnavopt .= '<OPTION VALUE="0" '.$ifnav_0.'>否</OPTION>';
$Strifnavopt .= '</SELECT>';

$Strifhotopt = '<SELECT NAME="ifhot">';
$Strifhotopt .= '<OPTION VALUE="1" '.$ifhot_1.'>是</OPTION>';
$Strifhotopt .= '<OPTION VALUE="0" '.$ifhot_0.'>否</OPTION>';
$Strifhotopt .= '</SELECT>';
/* 找到所有的分类到select start*/
$sql = "SELECT * FROM ".$db_table." where fid = 0 AND domain_id=".$_SESSION[$un_domain_id]." ORDER BY pcid ASC";
$AnormAll = $oPub->select($sql);

$Stropt = '<SELECT NAME="fid">';
$Stropt .= '<OPTION VALUE="0" >顶级分类</OPTION>';
$n = 0;
while( @list( $key, $value ) = @each( $AnormAll) ) {
	   $n ++;
       $selected = ($_GET['fid'] == $value["pcid"])? 'SELECTED':'';
       $Stropt .= '<OPTION VALUE="'.$value["pcid"].'" '.$selected.' >'.$n.'、'.$value["name"].'</OPTION>';
	   /* 查找儿子 */
       if($value["next_node"] != ''){          
           $Stropt .= get_next_node($value["next_node"],$_GET['fid'] );
	   }	   
}
$Stropt .= '</SELECT>';
/* 找到所有的分类到select end*/
/* 关联文章分类 start */
$db_table = $pre."articat";
$sql = "SELECT acid,name FROM ".$db_table." where fid = 0 AND ifshow = 1 AND domain_id=".$_SESSION[$un_domain_id]." ORDER BY acid ASC";
$AnormAll = $oPub->select($sql);
$strPro_acids_check = '';
$Aproacids = explode(",",$Anorm["acids"]);
while( @list( $k, $v ) = @each( $AnormAll) ) 
{
    $strChecked = (in_array($v[acid],$Aproacids))?'checked':'';
	$strPro_acids_check  .= ' <INPUT TYPE="checkbox"  name="pro_acids[]" value="'.$v[acid].'" '.$strChecked.'><span style="color:#cc0000;font-weight:bold">'.$v[name].'</span>';    
	$strAcid = next_node_all($v[acid],$db_table,'acid',true);
	if($strAcid)
	{
	   $Aacids = explode(",",$strAcid);
       while( @list( $key, $value ) = @each( $Aacids ) )
       {  
		   if($value)
		   { 
			         $strChecked = (in_array($value,$Aproacids))?'checked':'';
			         $sql = "SELECT fid,name FROM ".$db_table." 
			                 where acid = '".$value."'
					         AND domain_id=".$_SESSION[$un_domain_id]." limit 1";
                     $acidtmp = $oPub->getrow($sql); 
					 $fid = $acidtmp[fid];
					 $name = $acidtmp[name];
					 $fidname = '';
					 if($fid > 0)
					  {
			             $sql = "SELECT  name FROM ".$db_table." 
			                     where acid = '".$fid."'
					             AND domain_id=".$_SESSION[$un_domain_id]." limit 1";
                         $fidname = $oPub->getOne($sql);
						 $fidname = '<span style="color: #6E6E6E">'.$fidname.":</span>";
					  }              
		       $strPro_acids_check  .=' <INPUT TYPE="checkbox"  name="pro_acids[]" value="'.$value.'" '.$strChecked.'>'.$fidname.$name;
		   }

	   }
	}
	$strPro_acids_check .='<br/>';
}
/* 关联文章分类 end */

$db_table = $pre."productcat";
$sql = "SELECT * FROM ".$db_table." where fid = 0 AND domain_id=".$_SESSION[$un_domain_id]." ORDER BY pcid ASC";
$AnormAll = $oPub->select($sql);
$StrtypeAll = '';
$n = 0;
while( @list( $key, $value ) = @each( $AnormAll) ) {
	  $n ++;
	   $tmpstr = ($n % 2 == 0)?"even":"odd";
       $StrtypeAll .= '<TR class='.$tmpstr.'>';
       $StrtypeAll .= '<TD align=left>'.$n.'、'.$value["name"].'</TD>';
	   $StrtypeAll .= '<TD align=left>'.$value["descs"].'</TD>'; 
	   $tmp = ($value["ifnav"])?'是':'否';
	   $StrtypeAll .= '<TD align=left>'.$tmp.'</TD>';
	   $tmp = ($value["ifhot"])?'是':'否';
	   $StrtypeAll .= '<TD align=left>'.$tmp.'</TD>'; 
	   $StrtypeAll .= '<TD align=left>'.$value["pro_interval"].'</TD>';
       
	   $acidname = ' ';
	   if($value["acids"])
	   {
	       $db_table = $pre."articat";
           $sql = "SELECT name FROM ".$db_table." where ifshow = 1 AND domain_id=".$_SESSION[$un_domain_id]." AND acid in(".$value["acids"].") ORDER BY acid ASC";
           $row = $oPub->select($sql);
		   while( @list( $k, $v ) = @each( $row) ) 
		   {
               $acidname .= $v[name].',';
		   }

	   }

	   $StrtypeAll .= '<TD align=left>'.$acidname.'</TD>';
       $StrtypeAll .= '<TD align=left><a href="'.$_SERVER["PHP_SELF"].'?pcid='.$value["pcid"].'&fid=0&action=edit"><IMG SRC="images/b_edit.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="编辑"></a> _ ';
	   $StrtypeAll .= '<a href="'.$_SERVER["PHP_SELF"].'?pcid='.$value["pcid"].'&fid=0&action=del"><IMG SRC="images/b_drop.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="删除"></a></TD>';
       $StrtypeAll .= '</TR>';  
	   
	   /* 查找儿子 */
       if($value["next_node"] != ''){          
           $StrtypeAll .= tab_next_node($value["next_node"],$value["pcid"]);
	   }
	   /* 查找儿子 */	   
}

?>

<?php
   include_once( "header.php");
?>
<DIV class=content>
<TABLE width="100%" border=0>
  <TR>
  <form name="form1" method="post" action=""> 
    <TD width="13%" align="left" colspan="7">
        <span style="font-weight: bold">产品分类:</span>
     	<input name="name" type="text" value="<?php echo ($Anorm['pcid'])?$Anorm['name']:''?>" />

		 <span style="font-weight: bold">选择上级分类:</span><?php echo $Stropt;?> 
		<span style="font-weight: bold">导航条显示:</span>
		<?php echo $Strifnavopt;?>
		<br/>
		<span style="font-weight: bold">价格区间:</span>
		<input name="pro_interval" type="text" value="<?php echo ($Anorm['pcid'])?$Anorm['pro_interval']:''?>" size="50"/>[用于价格搜索,每项之间用","分隔  如:4000,5000,22000]
		<br/>
		<span style="font-weight: bold">关联文章分类:</span>[此分类自动关联的文章分类]
		<p style="margin-left: 60px;margin-top: 0px;margin-bottom: 0px">
        <?php echo $strPro_acids_check;?>
		</p>
		<span style="font-weight: bold">热们排行:</span>
		<?php echo $Strifhotopt;?>[将在产品首页显示该分类，评论最多的20件产品]
		<br/><span style="font-weight: bold">分类描述:</span>
     	<input name="descs" type="text" value="<?php echo ($Anorm['pcid'])?$Anorm['descs']:''?>" size="50"/>		
        <input type="hidden" name="action" value="<?php echo ($Anorm['pcid'])?'edit':'add'?>" />
        <input type="submit" name="Submit" value="<?php echo ($Anorm['pcid'])?'编辑':'增加' ?>" style="background-color: #FFCC66"/>
		<input type="hidden" name="pcid" value="<?php echo ($Anorm['pcid'])?$Anorm['pcid']:'0'?>" />  
    </TD>
    </form>
  </TR>	
  <TR class=bg5>
    <TD width="28%" align=left>产品分类</TD>
	<TD width="15%" align=left>描述(用于搜索)</TD> 
	<TD width="8%" align=left>导航条显示</TD>
	<TD width="8%" align=left>热门排行</TD>
	<TD width="18%" align=left>搜索价格区间</TD>
	<TD width="15%" align=left>关联文章分类</TD>
    <TD width="8%" align=left>操作</TD>
  </TR>
  <?php echo $StrtypeAll?>
  <TR class=bg5>
    <TD colspan="7" align=right><?php //echo $showpage = $page->ShowLink();?></TD>
  </TR>
</TABLE>
 
</DIV>
<?php
/* OPTION 递归 */
function get_next_node($next_node,$fid,$str = '　')
{
   global $oPub,$pre;
   $db_table = $pre.'productcat';
   $Agrad = explode(',',$next_node);
   $Stropt = '';
   if(count($Agrad) > 0 )
	{
	   $str .= '　';
	   $tn = 0;
	   while( @list( $k, $v ) = @each( $Agrad ) ) {
           if ($v == 0 && $v =='')
		   {
              break;
		   }		   
		   $sql = "SELECT * FROM ".$db_table." where pcid = $v";
           $Anorm = $oPub->selectrow($sql);
		   if( $Anorm["name"] != ''){
			   $tn ++;
			   $selected = ($fid == $v)? 'SELECTED':'';
		      $Stropt .=  '<OPTION VALUE="'.$v.'" '.$selected.'>'.$str.$tn.'）'.$Anorm["name"].'</OPTION>';
              $Stropt .= get_next_node($Anorm["next_node"],$fid,$str);
		   }
		   
	   }
	}
	return $Stropt;
}
/* tbale 递归 */
function tab_next_node($next_node,$fid,$str = '　')
{
   global $oPub,$pre,$un_domain_id;
   $db_table = $pre.'productcat';
   $Agrad = explode(',',$next_node);
   $Strtab = '';
   if(count($Agrad) > 0 )
	{
	   $str .= '　';
	   $tn = 0;
	   while( @list( $k, $v ) = @each( $Agrad ) ) {
           if ($v == 0 && $v =='')
		   {
              break;
		   }	
		    $db_table = $pre.'productcat';
		    $sql = "SELECT * FROM ".$db_table." where pcid = $v";
            $Anorm = $oPub->selectrow($sql);
			if( $Anorm["name"] != ''){
			  $tn ++ ;
	          $tmpstr = ($n % 2 == 0)?"even":"odd";
              $Strtab  .= '<TR class='.$tmpstr.'>';

              $Strtab  .= '<TD align=left>'.$str.$tn.'）'.$Anorm["name"].'</TD>';
			  $Strtab  .= '<TD align=left>'.$Anorm["descs"].'</TD>'; 
	          $tmp = ($Anorm["ifnav"])?'是':'否';
	          $Strtab .= '<TD align=left>'.$tmp.'</TD>';
	          $tmp = ($Anorm["ifhot"])?'是':'否';
	          $Strtab .= '<TD align=left>'.$tmp.'</TD>';
	          $Strtab .= '<TD align=left>'.$Anorm["pro_interval"].'</TD>';

	           $acidname = ' ';
	           if($Anorm["acids"])
	           {
	               $db_table = $pre."articat";
                   $sql = "SELECT name FROM ".$db_table." where ifshow = 1 AND domain_id=".$_SESSION[$un_domain_id]." AND acid in(".$Anorm["acids"].") ORDER BY acid ASC";
                   $row = $oPub->select($sql);
		           while( @list( $key, $value ) = @each( $row) ) 
		           {
                      $acidname .= $value[name].',';
		           }

	           }

	          $Strtab .= '<TD align=left>'.$acidname.'</TD>';
              $Strtab  .= '<TD align=left><a href="'.$_SERVER["PHP_SELF"].'?pcid='.$v.'&fid='.$fid.'&action=edit"><IMG SRC="images/b_edit.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="[编辑]"></a> _ ';
	          $Strtab  .= '<a href="'.$_SERVER["PHP_SELF"].'?pcid='.$v.'&fid='.$fid.'&action=del"><IMG SRC="images/b_drop.png" WIDTH="16" HEIGHT="16" BORDER="0" ALT="[删除]"></a></TD>';
              $Strtab  .= '</TR>';  
	          $n ++;
              $Strtab .= tab_next_node($Anorm["next_node"],$v,$str );
			}
	   }
	}
	return $Strtab;
}
?>	
<?php
include_once( "footer.php");
?>