<?php
/**
 * @version        Somao_Search X1.0 9:23 2012-1-1 Somao $
 * @package        Somao_Search.Site
 * @copyright      Copyright (c) 2009 - 2012, Somao, Inc.
 * @license        http://www.somao123.com/
 * @link           http://www.somao.cc/
**/
require "global.php";
CheckPurview('keyadmin',$authoritys,$rank);
$action=HtmlReplace(trim($_GET["action"]));
$kw=HtmlReplace(trim($_REQUEST["kw"]));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<meta http-equiv="x-ua-compatible" content="ie=7" />
<link href="css/admincp.css" rel="stylesheet" type="text/css" />
<script language="javascript">
function CheckAll(form,name)
  {
  for (var i=0;i<form.elements.length;i++)
    {
    var e = form.elements[i];
    if (e.name == name)
       e.checked = form.chkall.checked;
    }
  }
function checklistform()
{
var do_action;
var obj = document.getElementsByName("do_action"); //这个是以标签的name来取控件 
                 for(i=0; i<obj.length;i++)    { 
                  if(obj[i].checked){ 
                          do_action=obj[i].value; 
                   }
              } 
  //if(do_action=="undefined"){alert("请选择");return false;};
  if(do_action=="del")
  {
       if(!confirm("确定删除吗?"))
	   {
	       return false;
	   }
  }
}
</script>
<script language="javascript">
   function getCheckedvalue(e){
    var str='';
    for(var i=0;i<e.elements.length-1;i++)
	{  
	  if(e.elements[i].checked==true)
	  { 
	    str=str+e.elements[i].value+",";
	  }  
	}
	location.href="?action=del&kid="+str;
  }
</script>
</head>
<body>
<div class="container">
  <div class="floattop">
    <div class="itemtitle"><h3>关键字管理</h3>
      <ul class="tab1">
        <li class="current"><a href="keyword.php"><span>关键字列表</span></a></li>
        <li><a href="?action=addform"><span>添加关键字</span></a></li>
      </ul>
     </div>
   </div>
 <div class="floattopempty"></div>
<table class="tb tb2 " id="tips">
<tr><th  class="partition">提示</th></tr>
<tr><td class="tipsblock" s="1"><ul id="tipslis"><li>网页提交网站管理。</li></ul></td></tr>
</table>

<?php
switch ($action)
{
    case "saveform":
    saveform();
    break;

    case "addform":
	addform($action);
	break;
	
	case "modify":
	addform($action);
	break;
	
	case "del":
		 $array=explode(',',$_REQUEST['kid']);
		 foreach($array as $value){	 
           $sql="delete from ve123_keywords where kid='".$value."'";
		   $db->query($sql);
		 }
		 jsalert("删除成功");
	break;
}	
if($action=="" or $kw!=""){?>
<form id="search_form" name="search_form" method="post" action="?action=search">
<table class="tb tb2 ">
  <tr>
    <td class="title">关键词:<input type="text" name="kw" value="<?php echo $kw;?>"/>
      <input type="submit" name="Submit2" value="查找" /></td>
  </tr>
</table>
</form>
<br />
<form id="kid[]" name="kid[]" method="post" action="?action=dolistform" onsubmit="return checkform();">
<table class="tb tb2 ">
  <tr class="header">
    <th style="text-align: center">ID</th>
    <th>选择</th>
    <th>关键词</th>
    <th></th>
    <th>总搜索</th>
    <th>月搜索</th>
    <th>周搜索</th>
    <th>日搜索</th>
    <th>操作</th>
  </tr>
  <?php
  if($action=="search")
  { 
        $where=" where";
		if(!empty($kw))
		{
			$where.=" keyword like '%".$kw."%'";
		}
		else
		{
		    $where="";
		}
  }
  $sql="select * from ve123_keywords".$where;//echo $sql;
      $result=$db->query($sql);
      $total=$db->num_rows($result);//记录总数
      $pagesize=50;//每页显示数
      $totalpage=ceil($total/$pagesize);
      $page=intval($_GET["page"]);
      if($page<=0){$page=1;}
      $offset=($page-1)*$pagesize;
  $result=$db->query($sql." order by kid desc limit $offset,$pagesize");
  while ($rs=$db->fetch_array($result))
  {
  ?>
  <tr>
    <td height="30" align="center"><?php echo $rs["kid"]?></td>
    <td height="30"><input type="checkbox" name="kid[]" value="<?php echo $rs["kid"]?>" /></td>
    <td height="30"><?php echo "<a target=\"_blank\" href=\"../s/?wd=".urlencode($rs["keyword"])."\" >".$rs["keyword"];?></td>
    <td height="30"><?php echo $rs["ktype"]?></td>
    <td height="30"><?php echo $rs["hits"];?></td>
    <td height="30"><?php echo $rs["click_month"];?></td>
    <td height="30"><?php echo $rs["click_week"];?></td>
    <td height="30"><?php echo $rs["click_day"];?></td>
    <td ><a href="?action=modify&amp;kid=<?php echo $rs["kid"]?>">修改</a>
	<a href="?action=del&kid=<?php echo $rs["kid"];?>" onclick="if(!confirm('确认删除吗?')) return false;">删除</a></td>
  </tr>
  <?php
  }
  ?>
   <tr>
    <td height="36" colspan="10" align="center" bgcolor="#F9FCEF">
   
                全选<input name="chkall" type="checkbox" id="chkall" onclick="CheckAll(this.form,'kid[]')" value="checkbox">&nbsp;&nbsp;
                <strong>操作</strong><input name="do_action" type="radio" value="del" checked="checked">
                删除&nbsp;&nbsp; <input type="button" name="Submit" value=" 执 行 " onclick="getCheckedvalue(this.form)">
     
    <?php  echo pageshow($page,$totalpage,$total,"?action=".$action."&kw=".$kw."&");?>
    </td>
  </tr>
  </table>
</form>
<?php
}

function addform($do_action)
{
global $db;
  if ($do_action=="modify")
  {
      $kid=$_GET["kid"];
	  $sql="select * from ve123_keywords where kid='$kid'";
	  $rs=$db->get_one($sql);
	  $keyword=$rs["keyword"];
	  $ip=$rs["ip"];
	  $btn_txt="确定修改";
  }
  else
  {
     $btn_txt="确定提交";
  }
?>
<table class="tb tb2 ">
<form id="form1" name="form1" method="post" action="?action=saveform">
  <tr>
    <td width="100">关键词:</td>
    <td><textarea name="keyword" cols="50" rows="10"><?php echo $keyword?></textarea></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>
	<input type="hidden" name="kid" value="<?php echo $kid?>">
	<input type="hidden" name="do_action" value="<?php echo $do_action?>">
	<input type="submit" name="Submit" value="<?php echo $btn_txt?>" />	</td>
  </tr>
  </form>
</table>
<?php
}

function saveform()
{
global $db;
   $keyword=trim($_POST["keyword"]);
   $kid=$_POST["kid"];
   $do_action=$_POST["do_action"];
   $keyarr = array_unique(explode("\n",$keyword));
       foreach ($keyarr as $key){
		  $key=trim($key);
		  $accRows=$db->get_one("select * from ve123_keywords where keyword='$key'");
		  if(!$accRows) {
			   if($do_action=="modify"){
  			      $array=array('keyword'=>$key);
				  $db->update("ve123_keywords",$array,"kid='$kid'");
				  
  			   }else{
   			     $array=array('keyword'=>$key);
			 	 $db->insert("ve123_keywords",$array);
 			  }
		  }
	   }  
   jsalert("提交成功");
   
}
?>

</div>
</body>
</html>