<?php
require_once(dirname(__FILE__)."/admin_inc.php");
/*******************************************************************************************
' Software name: DJCMS Content Management System
' Version:3.0
' Web: http://www.djcms.net
' Author: 心情(qq:1130731)
' Copyright (C) 2009-2010 DJCMS官方 版权所有
' 法律申明：DJCMS程序所有代码100%原创、未引入任何网上代码,对一切抄袭行为、坚决严肃追究法律责任
'*******************************************************************************************/
viewHead("友情链接管理"."-" .$menuList[5][0]);
$action = getForm("action", "get");
$errorInfo="信息填写不完整，请检查"; $errorSort="排序必须为数字";
$keyword = replaceStr(getForm("keyword", "both"),"'","");
switch ($action) {
	case "last" : moveToLast();break;
	case "next" : moveToNext();break;
	case "del" : delLink();break;
	case "delall" : delAll();break;
	case "editall" : editAllLink();break;
	case "edit" : main();edit();break;
	case "save" : saveEdit();break;
	case "add" : addLink();break;
	case "hide" : hideLink();break;
	default : main();add();break;
}
viewFoot();

function main() {
	global $keyword,$menuList;
	$id = getForm("id","get");
	$keyword = unescape($keyword);
	$number = 30;
	$whereStr = '';
	if ($keyword<>"") $whereStr = "WHERE `m_name` like '%$keyword%' or `m_des` like '%$keyword%' or `m_url` like '%$keyword%'";
    $sqlStr = "SELECT `m_id`,`m_type`,`m_name`,`m_pic`,`m_url`,`m_des`,`m_sort` FROM ".DB::table('link')." $whereStr ORDER BY m_sort ASC";
	$allRecordset = db::getCount($sqlStr);
?>
<div id="append_parent"></div>
<div class="container" id="cpcontainer">
<!--当前导航-->
<script type="text/JavaScript">if(parent.$('admincpnav')) parent.$('admincpnav').innerHTML='后台首页&nbsp;&raquo;&nbsp;<?=$menuList[5][0]?>&nbsp;&raquo;&nbsp;友情链接管理';</script>
<?php
   if ($allRecordset > 0) {
?>
<form action="" method="post" name="linkform">
<table class="tb">
<tr class="thead"><th colspan="7"><em style="font-weight:bold">友情链接管理</em>&nbsp;&nbsp;<em style="color:#000;">关键字：<input name="keyword" type="text" id="keyword" size="20" value="<?php echo $keyword;?>"> <input type="submit" name="selectBtn" value="查 询..." class="btn" onclick="location.href='?keyword='+escape($('keyword').value);" /></em></th></tr>
    <TR align="center">
	  <TD>&nbsp;</TD>
      <TD width="6%">ID</TD>
      <TD>名称</TD>
      <TD>网址</TD>
      <TD>类型</TD>
      <TD>排序</TD>
      <TD>操作</TD>
    </TR>
<?php
   $query = DB::query($sqlStr);
   while($row = DB::fetch($query)) {
       $m_id=$row['m_id'];
?>
     <TR align="center"<?php if($id==$m_id) echo " class=\"editlast\"";?>>
	  <TD>&nbsp;</TD>
      <TD align="left"><input type="checkbox" value="<?=$m_id?>" name="m_id[]"  class="checkbox" /><?=$m_id?></TD>
      <TD><input type="text" name="m_name<?=$m_id?>" value="<?=$row['m_name']?>" size="20"/></TD>
      <TD><input name="m_url<?=$m_id?>" type="text" value="<?=$row['m_url']?>"  size="30"/></TD>
      <td><?php if ($row['m_type']=="font"){;?>文字链接<?php }elseif ($row['m_type']=="pic"){;?>图片链接<?php }elseif (strpos($row['m_type'],"hide")>0){;?>已被隐藏<?php };?></td>
      <TD><input name="m_sort<?=$m_id?>" type="text" value="<?=$row['m_sort']?>"  size="10"/></TD>
      <TD><a  href="?action=last&id=<?=$m_id?>">上移</a> <a href="?action=next&id=<?=$m_id?>">下移</a> <a href="?action=edit&id=<?=$m_id?>">编辑</a> <a  onclick="if(confirm('确定要删除吗')){return true;}else{return false;}" href="?action=del&id=<?=$m_id?>">删除</a>&nbsp;<a href="?action=hide&id=<?=$m_id?>&m_type=<?=$row['m_type']?>"><?php if (strpos($row['m_type'],"hide")>0){;?>解除隐藏<?php }else{;?>隐藏链接<?php };?></TD>
    </TR>
<?php
}
?>
    <tr><td  colspan="6"><label>全选<input type="checkbox" name="chkall" id="chkall" class="checkbox" onclick="checkAll(this.checked,'input','m_id[]')" /></label><label>反选<input type="checkbox" name="chkothers" id="chkothers" class="checkbox" onclick="checkOthers('input','m_id[]')" /></label><input type="submit" value="批量删除" onclick="if(confirm('确定要删除吗')){linkform.action='?action=delall';}else{return false}" class="btn"  />&nbsp;&nbsp;<input type="submit" value="批量修改选中友情链接" name="Submit"  class="btn"  onclick="linkform.action='?action=editall';"/></td></tr>
</TABLE>
</form>
<?php
   } else {
     echo "<table lass='tb'  ><tr><td><font color='red'>没有任何记录</font></td></tr></table>";
  }
}

function add(){
?>
<form action="?action=add" method="post" >
<table class="tb mt20">
<tr class="thead"><th colspan="2">添加友情链接</th></tr>
    <TR>
      <TD vAlign=center width="20%" >网站名称：</TD>
      <TD ><input type="text" size="50" name="m_name" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >网站地址：</TD>
      <TD ><input type="text" size="50" name="m_url" /><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >链接类型：</TD>
      <TD ><select name="m_type"  style=" width:100px"  onChange="selectPicLink(this,'pic');">
        <option value="font">文字链接</option>
        <option value="pic">图片链接</option>
      </select><font color="red">＊</font></TD>
    </TR>
    <TR id="tr_m_pic" style="display:none">
      <TD >LOGO地址：</TD>
      <TD ><input type="text" size="50" name="m_pic" /></TD>
    </TR>
    <TR>
      <TD >排序：</TD>
      <TD ><input type="text" size="20" name="m_sort"/></TD>
    </TR>
    <TR>
      <TD >网站介绍：</TD>
      <TD ><textarea name="m_des" cols="50" rows="4" ></textarea></TD>
    </TR>
    <TR  >
      <TD></TD> <TD><input type="submit" value="添加友情链接" class="btn" />
      &nbsp;<input type="button" value="返   回"  class="btn" onClick="javascript:history.go(-1)" /></TD>
    </TR>
</td></tr></table>
</form>
<?php
}

function edit() {
	$id = getForm("id","get");
	$row = DB::fetch_first("SELECT * FROM ".DB::table('link')." WHERE m_id='$id'");
?>
<form action="?action=save&id=<?=$id?>" method="post" name="editlink" >
<table class="tb mt20">
<tr class="thead"><th colspan="2">编辑友情链接</th></tr>
    <TR>
      <TD vAlign=center width="20%" >网站名称：</TD>
      <TD ><input type="text" size="50" name="m_name"  value="<?=$row['m_name']?>"/><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >网站地址：</TD>
      <TD ><input type="text" size="50" name="m_url" value="<?=$row['m_url']?>"/><font color="red">＊</font></TD>
    </TR>
    <TR>
      <TD >链接类型：</TD>
      <TD ><select name="m_type"  style=" width:100px"  onChange="selectPicLink(this,'pic');">
        <option value="font" <?php if($row['m_type'] == "font") echo " selected";?>>文字链接</option>
        <option value="pic" <?php if($row['m_type'] == "pic") echo " selected";?>>图片链接</option>
      </select><font color="red">＊</font></TD>
    </TR>
    <TR id="tr_m_pic" <?php if($row['m_type'] == "pic"){;?>style="display:block"<?php }else{;?>style="display:none"<?php };?>>
      <TD >LOGO地址：</TD>
      <TD ><input type="text" size="50" name="m_pic" value="<?=$row['m_pic']?>"/></TD>
    </TR>
    <TR>
      <TD >排序：</TD>
      <TD ><input type="text" size="20" name="m_sort" value="<?=$row['m_sort']?>"/></TD>
    </TR>
    <TR>
      <TD >网站介绍：</TD>
      <TD ><textarea name="m_des" cols="50" rows="4" ><?=$row['m_des']?></textarea></TD>
    </TR>
    <TR >
      <td></td><TD><input type="submit" value="修改友情链接" class="btn" name="m_eidtlinksubmit" />
      &nbsp;<input type="button" value="返   回"  class="btn" onClick="javascript:history.go(-1)" /></TD>
    </TR>
</td></tr></table>
</form>
<script>editlink.m_eidtlinksubmit.focus()</script>
<?php
}

function moveToLast() {
	$id = getForm("id","get");
	if (empty($id)) alertMsg("","admin_link.php");
	$cur = DB::result_first("SELECT m_sort FROM ".DB::table('link')." WHERE m_id=".$id);
	$cou = DB::result_first("SELECT count(*) FROM ".DB::table('link')." WHERE m_sort<'$cur'");
	if ($cou > 0) {
	    $flag = DB::result_first("SELECT m_sort FROM ".DB::table('link')." WHERE m_sort<'$cur' ORDER BY m_sort DESC");
		DB::update('link', array('m_sort' => $flag),array('m_id' => $id));
	} else {
		DB::query("UPDATE ".DB::table('link')." SET m_sort=m_sort-1 WHERE m_id='$id'");
	}
	alertMsg("", "admin_link.php?id=$id");
}


function moveToNext() {
	$id = getForm("id","get");
	if (empty($id)) alertMsg("","admin_link.php");
	$cur = DB::result_first("SELECT m_sort FROM ".DB::table('link')." WHERE m_id=".$id);
	$cou = DB::result_first("SELECT count(*) FROM ".DB::table('link')." WHERE m_sort>'$cur'");
	if ($cou > 0) {
		$flag = DB::result_first("SELECT m_sort FROM ".DB::table('link')." WHERE m_sort>'$cur' ORDER BY m_sort DESC");
		DB::update('link', array('m_sort' => $flag),array('m_id' => $id));
	} else {
		DB::query("UPDATE ".DB::table('link')." SET m_sort=m_sort+1 WHERE m_id='$id'");
	}
	alertMsg("", "admin_link.php?id=$id");
}

function delAll() {
	$id = getForm("m_id","post");
	DB::delete('link', 'm_id IN ('.dimplode($id).')');
	alertMsg("", "admin_link.php");
}

function delLink() {
	$id = getForm("id","get");
	DB::delete('link', array('m_id' => $id));
	alertMsg("", "admin_link.php?id=$id");
}

function hideLink() {
	$id = getForm("id","get");$m_t = getForm("m_type","get");
	if ($m_t == "") {
		$m_t = "font-hide";
    } else if (strpos($m_t,"-hide") > 0) {
		$m_t = replaceStr($m_t,"-hide","");
    } else {
		$m_t = $m_t."-hide";
    }
	DB::update('link', array('m_type' => $m_t),array('m_id' => $id));
	alertMsg("", "admin_link.php?id=$id");
}

function editAllLink() {
	global $errorInfo,$errorSort;
	$ids = getForm("m_id","post");
	if (empty($ids)) { alertMsg("", getRefer());exit();}
	foreach ($ids as $id) {
		$m_name = getForm("m_name".$id,"post");
		if (empty($m_name)) die($errorInfo);
		$m_sort = getForm("m_sort".$id,"post");
		if (empty($m_sort)) {
		    $m_sort = DB::result_first("SELECT max(m_sort)+1 FROM ".DB::table('link')."");
	    }
		$m_url = getForm("m_url".$id,"post");
		if (empty($m_url)) die($errorInfo);
		if (!is_numeric($m_sort)) die($errorSort);
		$linkarr = array(
	    'm_name' => $m_name,
	    'm_url' => $m_url,
	    'm_sort' => $m_sort,
	    );
	    DB::update('link', $linkarr, array('m_id' => $id));
	}
	alertMsg("", "admin_link.php");
}

function saveEdit() {
	global $errorInfo,$errorSort;
	$id = getForm("id","get");$m_name = getForm("m_name","post");$m_url = getForm("m_url","post");$m_type = getForm("m_type","post");$m_des=getForm("m_des","post");$m_pic=getForm("m_pic","post");
	if (empty($m_name) || empty($m_url)) die($errorInfo);
	$m_sort = getForm("m_sort","post");
	if (empty($m_sort)) {
		$m_sort = DB::result_first("SELECT max(m_sort)+1 FROM ".DB::table('link')."");
	}
	if (!is_numeric($m_sort)) die($errorSort);
	$linkarr = array(
	'm_type' => $m_type,
	'm_name' => $m_name,
	'm_pic' => $m_pic,
	'm_url' => $m_url,
	'm_des' => $m_des,
	'm_sort' => $m_sort,
	);
	DB::update('link', $linkarr, array('m_id' => $id));
	alertMsg("", "admin_link.php");
}

function addLink() {
	global $errorInfo;
	$m_name = getForm("m_name","post");$m_url = getForm("m_url","post");$m_type = getForm("m_type","post");$m_des = getForm("m_des","post");$m_pic = getForm("m_pic","post");
	if (empty($m_name) || empty($m_url)) die($errorInfo);
	$m_sort = getForm("m_sort","post");
	if (empty($m_sort)) {
		$m_sort = DB::result_first("SELECT max(m_sort)+1 FROM ".DB::table('link')."");
	}
	if (!is_numeric($m_sort)) $m_sort = 1;
	$linkarr = array(
	'm_type' => $m_type,
	'm_name' => $m_name,
	'm_pic' => $m_pic,
	'm_url' => $m_url,
	'm_des' => $m_des,
	'm_sort' => $m_sort,
	);
	DB::insert('link', $linkarr);
	alertMsg("","admin_link.php");
}
?>
