<?php
if(!defined('IN_GATEWEI')) {	exit('Access Denied');}

if($ipost=='save'){
	
	$categorytitle=isset($categorytitle)?trim($categorytitle):'';
	if(empty($categorytitle) || strlen($categorytitle)>30){
		gadminmessage("分类标题不能为空且不能超过30个字符");
	}


	$db->query("INSERT INTO g_productcategory (pcid ,pid ,categorytitle ,ord , productcount , displayway , hlightway ,pagerecords,dateformat) VALUES (NULL , $pid, '$categorytitle', $timestamp, 1, 0 , 0 ,20, 'Y-n-j')");
	w2productcategory();
	writelog($userid."增加产品分类标题为".$categorytitle."成功");
	gadminmessage("添加成功","siteadmin.php?iframe=productcategory",1);
}

if($ipost=='update')
{
	$categorytitle=isset($categorytitle)?trim($categorytitle):'';
	$pagerecords=intval($pagerecords);
	if(empty($categorytitle) || strlen($categorytitle)>30){
		gadminmessage("分类标题不能为空且不能超过30个字符");
	}
	$db->query("UPDATE g_productcategory SET categorytitle = '$categorytitle', pid = $pid, displayway = $displayway, hlightway = $hlightway,pagerecords=$pagerecords ,dateformat='$dateformat' WHERE pcid =$pcid LIMIT 1");
	w2productcategory();
	require('./clientdata/cache/cache_navigation.php');
	foreach($nav as $key => $val){
	if($val['type']=='product' && $val['id']==$pcid){
			$nav[$key]['title']=$categorytitle;
			break;
		}
	}
	ksort($nav);
	w2navigation($nav,'update');
	writelog($userid."修改产品分类标题为".$categorytitle."成功");
	gadminmessage("修改成功","siteadmin.php?iframe=productcategory",1);
}

if($ipost=='delete')
{
	$pid=$_GET['pid'];
	$pcid=$_GET['cid'];
	$c=$db->result($db->query("SELECT count(*) FROM g_productcategory WHERE pid = $pcid"),0);
	if($c && !$pid){
		gadminmessage("该分类存在子分类，请先删除子分类");
	}
	$c=$db->result($db->query("SELECT count(*) FROM g_products WHERE categoryid = $pcid"),0);
	if($c>0){
		gadminmessage("有产品记录的分类将不能删除，请通过产品管理手动删除产品记录或合并到其他分类中，再删除该分类");
	}
	$db->query("DELETE FROM g_productcategory WHERE pcid = $pcid LIMIT 1");
	writelog($userid."删除产品分类(".$productcategory[$pcid]['categorytitle'].")成功");
	w2productcategory();
	gadminmessage("成功删除分类信息","siteadmin.php?iframe=productcategory",1);
}

if($ipost=='merge')
{
	list($spid,$spcid)=explode(',',$sourcecategory);
	list($dpid,$dpcid)=explode(',',$destinationcategory);

	if(($spid == 0 && $spcid==0) || ($dpid == 0 && $dpcid==0))
	{
		gadminmessage("不能合并根分类");
	}

	$db->query("UPDATE g_products SET pid = $dpid, categoryid = $dpcid WHERE pid = $spid and categoryid = $spcid");
	w2productcategory();
	writelog($userid."合并分类(源:".$spcid."目的:".$dpcid.")");
	gadminmessage("成功合并分类中的内容","siteadmin.php?iframe=productcategory",1);
}

if($ipost=='order'){
	foreach($productord as $key => $val){
	$val=intval($val);
	$db->query("UPDATE g_productcategory SET ord = $val WHERE pcid = $key");
	}
	w2productcategory();
	writelog($userid."排序产品分类成功");
	gadminmessage("设置成功","siteadmin.php?iframe=productcategory",1);
}

if($ipost=='rootsave'){
    $newproductname = cutstr(ghtmlspecialchars(trim($newproductname)), 8 ,'');
	$db->query("REPLACE INTO g_settings (var, val) VALUES ('productname', '$newproductname')");
	w2setting();
	foreach($nav as $key => $val){
	if($val['type']=='product' && $val['id']==0){
			$nav[$key]['title']=$newproductname;
			break;
		}
	}
	ksort($nav);//在写入的时候不会乱序
	w2navigation($nav,'update');
	writelog($userid."将产品系统根命名为".$newproductname);
	gadminmessage('设置成功',"siteadmin.php?iframe=productcategory",1);
}

if($ipost=='setidentification'){
	$i=0;
	$rident=array();
	foreach($newidentification as $key => $val){
		$val = cutstr(ghtmlspecialchars(trim($val)), 8 ,'');
		$rident[]=$i.','.$key.','.$val;
		$i++;
	}
	$s=implode(';',$rident);
	$s="{{".$s."}}";
	$db->query("REPLACE INTO g_settings (var, val) VALUES ('productidentification', '$s')");
	w2setting();
	writelog($userid."更新产品标示成功");
	gadminmessage('设置成功',"siteadmin.php?iframe=productcategory",1);
}

if($ipost=='modify')
{
	$pcid=isset($_GET['pcid'])?$_GET['pcid']:0;
	$q=$db->query("SELECT * FROM g_productcategory WHERE pcid = $pcid");
	$category=$db->fetch_array($q);
?>
 <table>
	<caption>产品分类修改</caption>
	<form method="post" action="siteadmin.php?iframe=productcategory&ipost=update">
		<tr>
			<th colspan="2"><span>分类修改</span><input type="hidden" name="pcid" value="<?php echo $pcid?>"></th>
		</tr>
  <tr>
    <td><span>产品分类名称</span></td>
    <td ><input type="text" name="categorytitle" class="formtext" value="<?php echo $category['categorytitle']?>"></td>
  </tr>
  <tr>
    <td><span>所属父分类</span></td>
    <td >
	  <select name="pid" class="formselect">
		<option value="0" <?php echo $category['pid']==0?'selected':''?>>产品根分类</option >
		<?php
		//ptitle是父标题
		$q=$db->query("SELECT * FROM g_productcategory WHERE pid = 0 ORDER BY ord");
		while($ptitle=$db->fetch_array($q))
		{ ?>
		<option value="<?php echo $ptitle['pcid'] ?>" <?php echo $ptitle['pcid']==$category['pid']?'selected':''?> ><?php echo $ptitle['categorytitle']?></option >
		<?php } ?>
      </select>
	</td>
  </tr>
    <tr>
    <td><span>产品显示方式</span></td>
    <td >
	 <select name="displayway" class="formselect">
		<option value="0" <?php echo $category['displayway']==0?'selected':''?>>直接显示最优先的产品内容</option >
		<option value="1" <?php echo $category['displayway']==1?'selected':''?>>以列表方式显示产品</option >
	 </select>
	</td>
  </tr>
   <tr>
    <td><span>列表每页显示记录数</span></td>
    <td >
	 <input type="text" name="pagerecords" class="formtext" value="<?php echo $category['pagerecords']?>">
	</td>
  </tr>
      <tr>
    <td><span>发布时间格式化</span></td>
    <td >
		 <select name="dateformat" class="formselect">

<?php foreach($dateformats as $val){
				$selected=$category['dateformat']==$val?'selected':'';
				echo '<option value="'.$val.'" '.$selected.'>'.$val.'</option >';
			}
?>
		 </select>
	</td>
  </tr>
    <tr>
    <td><span>高亮显示样式</span></td>
    <td >
	  <select name="hlightway" class="formselect">
	  <?php 
	   
       foreach($hlightways as $key => $val){
		   $hlightselected=$category['hlightway']==$key?'selected':'';
			echo '<option value="'.$key.'"  style="'.$val[1].'" '.$hlightselected.'>'.$val[0].'</option>';
	 }
	 ?>
	  </select>


	</td>
  </tr>
  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
			 
	      </td>
   </tr>
       <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
    </tr>
	</form>
	</table>
<?php  exit();	 } ?>





 <table>
	<caption>产品分类设置</caption>

			<form method="post" action="siteadmin.php?iframe=productcategory&ipost=rootsave">
		<tr>
			<th colspan="2"><span>设置根分类名称</span></th>
		</tr>
  <tr>
    <td width="40%"><span>产品根分类名称</span></td>
    <td ><input type="text" name="newproductname" class="smalltext" value="<?php echo $productname?>">&nbsp;&nbsp;<input type="submit" value="设&nbsp;&nbsp;置" ></td>
  </tr>
  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
	</form>
	<form method="post" action="siteadmin.php?iframe=productcategory&ipost=save">
		<tr>
			<th colspan="2"><span>添加新分类</span></th>
		</tr>
  <tr>
    <td width="40%"><span>产品分类名称</span></td>
    <td ><input type="text" name="categorytitle" class="smalltext"></td>
  </tr>
  <tr>
    <td><span>所属父分类</span></td>
    <td >
	  <select name="pid" class="smallselect">
		<option value="0" selected>产品根分类</option >
		<?php
		$q=$db->query("SELECT * FROM g_productcategory WHERE pid = 0");
		while($ptitle=$db->fetch_array($q))
		{ ?>
		<option value="<?php echo $ptitle['pcid'] ?>"><?php echo $ptitle['categorytitle']?></option >
		<?php } ?>
      </select>
	</td>
  </tr>
  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
			 
	      </td>
   </tr>
       <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
    </tr>
	</form>

	<!-- 排序表单 -->

<form id="orderproductcategory" method="post" action="siteadmin.php?iframe=productcategory&ipost=order">
 <tr>
    <th colspan="2"><span>产品分类管理</span></th>
  </tr>
  <tr>
    <td colspan="2">

	<ul>
<?php 

$q=$db->query("SELECT * FROM g_productcategory WHERE pid = 0 ORDER BY ord");
while($categorys=$db->fetch_array($q))
{
	$pid=$categorys['pcid'];
	$c=$db->result($db->query("SELECT count(*) FROM g_productcategory WHERE pid = $pid"),0);

	echo '<li>'.$categorys['categorytitle'].'  - 显示顺序:<input type="text" name="productord['.$categorys['pcid'].']" size="2"> '.'  [<a href="siteadmin.php?iframe=productcategory&ipost=modify&pcid='.$categorys['pcid'].' ">编辑</a>]  [<a href="###" onclick="delcategory(1,'.$categorys['pid'].','.$categorys['pcid'].','.$c.')">删除</a>]';
	if($c){
		echo '<ul>';
	}
	$subq=$db->query("SELECT * FROM g_productcategory WHERE pid = $pid ORDER BY ord");
	while($subcategorys=$db->fetch_array($subq)){
			echo '<li>'.$subcategorys['categorytitle'].'   - 显示顺序:<input type="text" name="productord['.$subcategorys['pcid'].']" size="2"> '.'  [<a href="siteadmin.php?iframe=productcategory&ipost=modify&pcid='.$subcategorys['pcid'].' ">编辑</a>] 
			[<a href="###" onclick="delcategory(1,'.$subcategorys['pid'].','.$subcategorys['pcid'].',0)">删除</a>] </li>';
		}
	if($c){
		echo '</ul>';
	}
	echo '</li>';
}

?>
	</ul>
  </td>
  </tr>
         		<tr>
          <td colspan="2">
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
 <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="g_product_category_order" type="submit" value="提&nbsp;&nbsp;交"></div></td>
    </tr>
   <form>

<!-- 合并表单 -->

   	<form id="mergeproductcategory" method="post">
   <tr>
    <th colspan="2"><span>分类合并</span></th>
  </tr>
  <tr>
    <td><span>源分类名称(源分类不会删除)</span></td>
    <td >
       <select name="sourcecategory" id="sourcecategory" class="smallselect">
	   <option value="0,0" selected >产品根分类</option >
	<?php
$qc=$db->query("SELECT * FROM g_productcategory WHERE pid = 0");
while($category=$db->fetch_array($qc)){ ?>
<option value="<?php echo $category['pid'].','.$category['pcid']?>"  style="background-color:#B1C3D9"><?php echo $category['categorytitle']?></option >
<?php 
$pid=$category['pcid'];
$subqc=$db->query("SELECT * FROM g_productcategory WHERE pid = $pid");
while($subcategory=$db->fetch_array($subqc)){
				echo '<option value="'.$subcategory['pid'].','.$subcategory['pcid'].'">'.$subcategory['categorytitle'].'</option>';
			}
		}
?>
      </select>
</td>
  </tr>
  <tr>
    <td><span>目标分类名称(源分类所有产品会归属到目标分类中)</span></td>
    <td >
	  <select name="destinationcategory" id="destinationcategory" class="smallselect">
	  <option value="0,0" selected >产品根分类</option >
<?php
	$qc=$db->query("SELECT * FROM g_productcategory WHERE pid = 0");
	while($category=$db->fetch_array($qc)){ ?>
	<option value="<?php echo $category['pid'].','.$category['pcid']?>"  style="background-color:#B1C3D9"><?php echo $category['categorytitle']?></option >
	<?php 
	$pid=$category['pcid'];
	$subqc=$db->query("SELECT * FROM g_productcategory WHERE pid = $pid");
	while($subcategory=$db->fetch_array($subqc)){
					echo '<option value="'.$subcategory['pid'].','.$subcategory['pcid'].'">'.$subcategory['categorytitle'].'</option>';
				}
			}
?>
      </select>
	</td>
  </tr>
  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
			 
	      </td>
   </tr>
       <tr>
    <td  colspan="2"> <div id="submitdiv"><input name="g_product_category_submit" type="button" value="合&nbsp;&nbsp;并" onclick="mergecategory('product')"></div></td>
    </tr>
</form>
		<!--文章标识设置-->
		<form method="post" action="siteadmin.php?iframe=productcategory&ipost=setidentification">
		<tr>
			<th colspan="2"><span>设置产品标识</span></th>
		</tr>
		<?php foreach($productidentification as $key => $val){?>
  <tr>
    <td ><span style="color:<?php echo $val[0]?>">标识字体颜色</span></td>
    <td ><input type="text" name="newidentification[<?php echo $val[0]?>]" class="smalltext" value="<?php echo $val[1]?>"></td>
  </tr>
  <?php }?>
  		<tr>
          <td colspan="2">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
    <tr>
    <td  colspan="2"> <div id="submitdiv"><input type="submit" value="提  交"></div></td>
    </tr>
	</form>
</table>
