<?php 
if(!defined('IN_GATEWEI')) {
	exit('access denied');
}
require_once GATEWEI_ROOT.'./engine/attachment.func.php';
require_once GATEWEI_ROOT.'./engine/post.func.php';
$pagerecords=10;
$curpage=isset($page)?$page:1;
$startrecord=($curpage-1)*$pagerecords;
$totalrecord = $db->result($db->query("select count(*) from g_articles where top = 0"),0);
$q = $db->query("select * from g_articles where top =0 order by ord desc limit $startrecord , $pagerecords");
$qurl="siteadmin.php?iframe=articlemanagement";
$topq = $db->query("select * from g_articles where top =1 order by ord desc limit 0 , 5");
$ctopq = $db->query("select count(*) from g_articles where top =1");

	if($ipost=='query'){
		if($action=='tag'){
			$totalrecord = $db->result($db->query("select count(*) from g_articles where tag=$qtxt"),0);
			$q = $db->query("select * from g_articles where tag=$qtxt order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=articlemanagement&ipost=query&action=tag&qtxt=".$qtxt;
		}elseif($action=='cid'){
			$textarray=explode(',',$qtxt);
			if($textarray[0]==0 && $textarray[1]==0){
				gadminmessage("请选择文章分类");
			}
			$totalrecord = $db->result($db->query("select count(*) from g_articles where pid =$textarray[0] and categoryid = $textarray[1]"),0);
			$q = $db->query("select * from g_articles where pid =$textarray[0] and categoryid = $textarray[1] order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=articlemanagement&ipost=query&action=cid&qtxt=".$qtxt;
		}elseif($action=="articletitle"){
			if(empty($qtxt)){
				gadminmessage("查询关键字不能为空");
			}
			$totalrecord = $db->result($db->query("select count(*) from g_articles where articletitle like '%$qtxt%'"),0);
		    $q = $db->query("select * from g_articles where articletitle like '%$qtxt%' order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=articlemanagement&ipost=query&action=articletitle&qtxt=".$qtxt;
		}elseif($action=="timeslice"){
			$date=explode(';',$qtxt);
			list($sy,$sm,$sd)=explode(',',$date[0]);
			list($ey,$em,$ed)=explode(',',$date[1]);
			$stime=mktime(0,0,0,$sm,$sd,$sy);
			$etime=mktime(23,59,59,$em,$ed,$ey);
			$totalrecord = $db->result($db->query("select count(*) from g_articles where subtime > $stime and subtime < $etime"),0);
		    $q = $db->query("select * from g_articles where subtime > $stime and subtime < $etime order by ord desc limit $startrecord , $pagerecords");
			$qurl="siteadmin.php?iframe=articlemanagement&ipost=query&action=timeslice&qtxt=".$qtxt;
		}
	}

	$multipage=multi($totalrecord, $pagerecords, $curpage, $qurl);

if($ipost=='setarticle'){
	if(!$pervalue{17}){
		showmsg("该用户组无此权限");
	}
	if($action=='unsettop'){
		articlesetting($action,$top_article_check);
	}elseif($action=='delete'){
		$article_check = isset($article_check)?$article_check:array();
		$top_article_check = isset($top_article_check)?$top_article_check:array();
		$merge_check=array_merge($article_check,$top_article_check);
		articlesetting($action,$merge_check);
	}else{
		articlesetting($action,$article_check);
	}
}

if($ipost=="update"){
		uparticle2db($articletitle,$g_editor_doc,$tag,$aid);
	}

if($ipost=="modify"){
	if($action=='delete'){
		modifydelattach($mid,$fid,1);
	}
	$q = $db->query("select * from g_articles a,g_article_content b where a.aid=b.aid and a.aid= $mid limit 1");
	$aq = $db->query("select * from g_attachments where bid = $mid");
	$modarticles=$db->fetch_array($q);
?>
<form enctype="multipart/form-data" method="post" action="siteadmin.php?iframe=articlemanagement&ipost=update">
<table>
<caption>文章信息修改</caption>
   <tr>
      <td width="10%"><span>文章标题<input type="hidden" name="aid" value="<?php echo $modarticles['aid']?>"></span></td>
      <td width="90%"><input type="text" id="articletitle" name="articletitle" class="formtext" value="<?php echo $modarticles['articletitle']?>">&nbsp;
	  <select name="tag" class="smallselect">
	  <?php 
	  foreach($articleidentification as $key => $val){
		  $tagselect=$modarticles['tag']==$key ? 'selected':'';
		  echo '<option value="'.$key.'" style="color:'.$val[0].'" '.$tagselect.'>'.$val[1].'</option >';
	  }
	  ?>
      </select></td>
   </tr>
  <tr>
    <td><span>所属分类</span></td>
    <td >
	  <select id="cid" name="cid" class="formselect">
		<option value="0,0" selected >文章根分类</option >
<?php
$q=$db->query("select * from g_articlecategory where pid = 0");
while($category=$db->fetch_array($q)){ 
	$categoryselect=($modarticles['pid'] == $category['pid'] && $modarticles['categoryid'] == $category['acid'])?'selected':'';
	?>
<option value="<?php echo $category['pid'].','.$category['acid']?>"  style="background-color:#b1c3d9" <?php echo $categoryselect?>><?php echo $category['categorytitle']?></option >
<?php 
$pid=$category['acid'];
$subq=$db->query("select * from g_articlecategory where pid = $pid");
while($subcategory=$db->fetch_array($subq)){
	$subcategoryselect=($modarticles['pid'] == $subcategory['pid'] && $modarticles['categoryid'] == $subcategory['acid'])?'selected':'';
				echo '<option value="'.$subcategory['pid'].','.$subcategory['acid'].'" '.$subcategoryselect.'>'.$subcategory['categorytitle'].'</option>';
			}
		}
?>
      </select>
	</td>
  </tr>
<tr>
          <td colspan="2" valign="top">
<?php
require_once("./admin/g_admin_editor.func.php");
geditor($modarticles['content']);
?>
</td>
   </tr>

   </table>
<?php
if($db->result($db->query("select count(*) from g_attachments where bid= $mid limit 1"),0)){
?>

   	  <table>
	       	<tr>
          <td colspan="4">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
			<tr>
				<th width="60%">文章附件</th>
				<th width="12%">附件大小</th>
				<th width="20%">附件类型</th>
				<th width="8%">下载次数</th>
			</tr>
   <?php

	while($modarticleattach=$db->fetch_array($aq)){
	$fid=$modarticleattach['fid'];
	?>
	<tr>
		  <td>
		  <div id="attach<?php echo $fid?>">		 
				[<a href="###" onclick="deleteattach('article',<?php echo $aid?>,<?php echo $fid?>,false)">删除</a>] 
				[<a href="###" onclick="modifyattach('<?php echo $fid?>')">修改</a>] 
				[<a href="###" onclick="insertAttachtext(<?php echo $fid?>);return false;">插入</a>]
				<?php echo '('.$fid.') '.$modarticleattach['name']?>
				</div>
				<span id="modifyattach<?php echo $fid?>"></span>
			</td>
			<td><?php echo sizecount($modarticleattach['size'])?></td>
			<td><?php echo $modarticleattach['type']?></td>
			<td><?php echo $modarticleattach['downtimes']?></td>
			</td>
   </tr>
<?php
		}
	}
?>
     	<tr>
          <td colspan="4">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
   <tr>
    <td  colspan="4"> <div id="submitdiv"><input name="g_article_submit" type="submit" value="提&nbsp;&nbsp;交" ></div></td>
    </tr>
</table>

</form>

<?php
	gexit();
	}
?>
<script language=javascript src="./engine/js/calendar.js"></script>
<table>
<caption>文章信息管理</caption>

	<form id="articlequery" method="post">
		<input type="hidden" id="qtxt" name="qtxt">
	</form>

<tr>
      <td width="20%"><span>文章标题关键字</span></td>
      <td width="80%"><input type="text" id="articletitle" name="articletitle" class="formtext">  <input type="button" value="查询" onclick="aquery('articletitle')"></td>   </tr>
<tr>
      <td width="20%"><span>信息标示</span></td>
      <td width="80%">
	  <select id="tag" name="tag" class="formselect">
	  <?php 
	  foreach($articleidentification as $key => $val){
		  echo '<option value="'.$key.'" style="color:'.$val[0].'">'.$val[1].'</option >';
	  }
	  ?>
      </select>    <input type="button" value="查询" onclick="aquery('tag')">
	  </td>   
	  </tr>
 <tr>
    <td><span>所属分类</span></td>
    <td >
	  <select id="cid" name="cid" class="formselect">
		<option value="0,0" selected >文章根分类</option >
<?php echo $echoselectform;?>
      </select>
	    <input type="button" value="查询" onclick="aquery('cid')">
	</td>
  </tr>
      <tr>
    <td><span>文章发布时间段</span></td>
    <td >
	<div id="calendar_start" class="calendar"></div>
	<div class="calendarspace">至</div>
	<div id="calendar_end" class="calendar"></div>
	<div class="calendarquery"><input type="button" value="查询" onclick="aquery('timeslice')"></div>
	</td>
  </tr>
    	   <tr>
          <td colspan="5">
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
	      </td>
   </tr>
</table>


<table>
<form id="form_articlelist" method="post">
<?php if($db->result($ctopq,0)){?>
 <tr>
    <th colspan="4">置顶文章<img src="images/siteadmin/g_admin_top.gif"></th>
  </tr>
<tr>
<tr>
     <td width="5%" ><input type="checkbox" name="top_article_check_all" id="top_article_check_all" onclick="checkall_onclick('top_article')"></td>
	 	<td  width="5%" >修改</td>
	    <td width="75%" >文章标题</td>
		<td width="15%" >发布时间</td>
  </tr>
  <?php
   $i=0;
	while($articles=$db->fetch_array($topq)){	?>
    <tr>
    <td ><input type="checkbox" id="top_article_check_<?php echo $i?>" name="top_article_check[]" value="<?php echo $articles['aid']?>"></td>
    <td ><a href="siteadmin.php?iframe=articlemanagement&ipost=modify&mid=<?php echo $articles['aid']?>"><img src="./images/siteadmin/g_admin_edit.gif"></a></td>
	<td ><?php echo '['.$articlecategory[$articles['categoryid']]['categorytitle'].'] <font color="'.$articleidentification[$articles['tag']][0].'">['.$articleidentification[$articles['tag']][1].']</font>  '.$articles['articletitle'] ?></td>
	<td ><?php echo gmdate($articlecategory[$articles['categoryid']]['dateformat'],$articles['subtime']+3600*8) ?></td>
  </tr>
  <?php	
		$i++;
	  } ?>
<?php }?>
 <tr >
    <th colspan="4">文章列表</th>
  </tr>
<tr>
     <td width="5%" ><input type="checkbox" name="article_check_all" id="article_check_all" onclick="checkall_onclick('article')"></td>
	 	<td  width="5%" >修改</td>
	    <td width="75%" >文章标题</td>
		<td width="15%" >发布时间</td>
  </tr>
  <?php
   $i=0;
	while($article=$db->fetch_array($q)){?>
    <tr>
    <td >
		<input type="checkbox" id="article_check_<?php echo $i?>" name="article_check[]" value="<?php echo $article['aid']?>">
	</td>
    <td >
<a href="siteadmin.php?iframe=articlemanagement&ipost=modify&mid=<?php echo $article['aid']?>">
<img src="./images/siteadmin/g_admin_edit.gif">
</a>
</td>
<td ><?php echo $articlecategory[$article['categoryid']]['categorytitle']?> | 

				<a class="ls_purple" href="###"><font color="<?php echo $articleidentification[$article['tag']][0]?>"><?php echo $articleidentification[$article['tag']][1]?></font></a>
				| <a href="viewarticle.php?aid=<?php echo $article['aid']?>" target="_Blank">
				<?php echo height_light($article['articletitle'],$article['hlight'],$articlecategory[$article['categoryid']]['hlightway'])?></a>
</td>
	<td ><?php echo gmdate($articlecategory[$article['categoryid']]['dateformat'],$article['subtime']+3600*8) ?></td>

  </tr>
  <?php	
		$i++;
	  } ?>

 <tr >
    <td colspan="4"> 
	<?php	
				if($multipage){
				echo '<div class="pages_btns font_14">'.$multipage.'</div>';
				}
			?></td>
  </tr>	  

   <tr>
          <td colspan="5">&nbsp;
	         <div id="hr_u"> </div>
	         <div id="hr_d"> </div>
 <div id="moveform" style="display:none;">
			<select id="move_select_cid" name="move_select_cid">
				<option value="0,0" selected >文章根分类</option >
                    <?php echo $echoselectform;?>
            </select>
	        <button onclick="asetting('siteadmin.php?iframe=articlemanagement&','move',0,0)"> 开始转移 </button> 
	</div>


	      </td>
   </tr>
   </form>
      <tr>
        <td colspan="5"> 
		<div id="toolbox">
	<button onclick="asetting('siteadmin.php?iframe=articlemanagement&','settop',0,0)">文章置顶</button>
   <button onclick="asetting('siteadmin.php?iframe=articlemanagement&','unsettop',0,0)"> 取消置顶</button>
	<button onclick="asetting('siteadmin.php?iframe=articlemanagement&','priority',0,0)"> 文章优先</button>
	<button onclick="asetting('siteadmin.php?iframe=articlemanagement&','unpriority',0,0)"> 取消优先</button>
	<button onclick="asetting('siteadmin.php?iframe=articlemanagement&','sethlight',0,0)"> 高亮显示</button>
	<button onclick="asetting('siteadmin.php?iframe=articlemanagement&','unsethlight',0,0)"> 取消高亮</button>
	<button onclick="asetting('siteadmin.php?iframe=articlemanagement&','delete',0,0)"> 删除文章</button> 
	<button onclick='showmoveform()'> 转移文章</button> 
	</div>
	      </td>
   </tr>
  
</table>

<script language="javascript">

	loadcalendar("_start");
	loadcalendar("_end");

</script>
