<?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_articlecategory (acid ,pid ,categorytitle ,ord ,articlecount,displayway , hlightway ,pagerecords,dateformat) VALUES (NULL , $pid, '$categorytitle', 0 , 0 , 1 , 0 , 20 , 'Y-n-j')");
	w2articlecategory();
	writelog($userid."增加文章分类标题为".$categorytitle."成功");
	gadminmessage("添加成功","siteadmin.php?iframe=articlecategory",1);

}

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

}

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

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

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

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


if($ipost=='order'){
	foreach($articleord as $key => $val){
	$val=intval($val);
	$db->query("UPDATE g_articlecategory SET ord = $val WHERE acid = $key");
	}
	w2articlecategory();
	writelog($userid."排序文章分类成功");
	gadminmessage("设置成功","siteadmin.php?iframe=articlecategory",1);
}

if($ipost=='rootsave'){
    $newarticlename = cutstr(ghtmlspecialchars(trim($newarticlename)), 8 ,'');
	$db->query("REPLACE INTO g_settings (var, val) VALUES ('articlename', '$newarticlename')");
	w2setting();
	foreach($nav as $key => $val){
	if($val['type']=='article' && $val['id']==0){
			$nav[$key]['title']=$newarticlename;
			break;
		}
	}
	ksort($nav);//在写入的时候不会乱序
	w2navigation($nav,'update');
	writelog($userid."将文章系统根命名为".$newarticlename);
	gadminmessage('设置成功',"siteadmin.php?iframe=articlecategory",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 ('articleidentification', '$s')");
	w2setting();
	writelog($userid."文章标示更新成功");
	gadminmessage('设置成功',"siteadmin.php?iframe=articlecategory",1);
}

if($ipost=='modify')
{
	$acid=isset($_GET['acid'])?$_GET['acid']:0;
	$q=$db->query("SELECT * FROM g_articlecategory WHERE acid = $acid");
	$category=$db->fetch_array($q);
?>
 <table>
	<caption>文章分类修改</caption>
	<form method="post" action="siteadmin.php?iframe=articlecategory&ipost=update">
		<tr>
			<th colspan="2"><span>分类修改</span><input type="hidden" name="acid" value="<?php echo $acid?>"></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
		$q=$db->query("SELECT * FROM g_articlecategory WHERE pid = 0 ORDER BY ord");
		while($ptitle=$db->fetch_array($q))
		{ ?>
		<option value="<?php echo $ptitle['acid'] ?>" <?php echo $ptitle['acid']==$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=articlecategory&ipost=rootsave">
		<tr>
			<th colspan="2"><span>设置根分类名称</span></th>
		</tr>
  <tr>
    <td width="40%"><span>文章根分类名称</span></td>
    <td ><input type="text" name="newarticlename" class="smalltext" value="<?php echo $articlename?>">&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=articlecategory&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_articlecategory WHERE pid = 0");
		while($ptitle=$db->fetch_array($q))
		{ ?>
		<option value="<?php echo $ptitle['acid'] ?>"><?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="orderarticlecategory" method="post" action="siteadmin.php?iframe=articlecategory&ipost=order">
 <tr>
    <th colspan="2"><span>文章分类管理</span></th>
  </tr>
  <tr>
    <td colspan="2">
	<ul>
<?php 

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

	echo '<li>'.$categorys['categorytitle'].' - 显示顺序:<input type="text" name="articleord['.$categorys['acid'].']" size="2"> '.'  [<a href="siteadmin.php?iframe=articlecategory&ipost=modify&acid='.$categorys['acid'].' ">编辑</a>]  [<a href="###" onclick="delcategory(0,'.$categorys['pid'].','.$categorys['acid'].','.$c.')">删除</a>]';
	if($c){
		echo '<ul>';
	}
	$subq=$db->query("SELECT * FROM g_articlecategory WHERE pid = $pid ORDER BY ord");
	while($subcategorys=$db->fetch_array($subq)){
			echo '<li>'.$subcategorys['categorytitle'].' - 显示顺序:<input type="text" name="articleord['.$subcategorys['acid'].']" size="2"> '.'  [<a href="siteadmin.php?iframe=articlecategory&ipost=modify&acid='.$subcategorys['acid'].' ">编辑</a>] 
			[<a href="###" onclick="delcategory(0,'.$subcategorys['pid'].','.$subcategorys['acid'].',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_article_category_order" type="submit" value="提&nbsp;&nbsp;交"></div></td>
    </tr>

  </form>


  <!-- 合并表单 -->


   	<form id="mergearticlecategory" 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_articlecategory WHERE pid = 0");
while($category=$db->fetch_array($qc)){ ?>
<option value="<?php echo $category['pid'].','.$category['acid']?>"  style="background-color:#B1C3D9"><?php echo $category['categorytitle']?></option >
<?php 
$pid=$category['acid'];
$subqc=$db->query("SELECT * FROM g_articlecategory WHERE pid = $pid");
while($subcategory=$db->fetch_array($subqc)){
				echo '<option value="'.$subcategory['pid'].','.$subcategory['acid'].'">'.$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_articlecategory WHERE pid = 0");
	while($category=$db->fetch_array($qc)){ ?>
	<option value="<?php echo $category['pid'].','.$category['acid']?>"  style="background-color:#B1C3D9"><?php echo $category['categorytitle']?></option >
	<?php 
	$pid=$category['acid'];
	$subqc=$db->query("SELECT * FROM g_articlecategory WHERE pid = $pid");
	while($subcategory=$db->fetch_array($subqc)){
					echo '<option value="'.$subcategory['pid'].','.$subcategory['acid'].'">'.$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_article_category_submit" type="button" value="合  并" onclick="mergecategory('article')"></div></td>
    </tr>
</form>
		<!--文章标识设置-->
		<form method="post" action="siteadmin.php?iframe=articlecategory&ipost=setidentification">
		<tr>
			<th colspan="2"><span>设置文章标识</span></th>
		</tr>
		<?php foreach($articleidentification 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>
