<?php
	session_start(); 
	ini_set("display_errors","Off");
	require_once('inc/conn.php');
	require_once('inc/constant.php');
	require_once('inc/ls_syscode.php');
	require_once('inc/check.php');
	$opendb=new mydb();
	require_once('inc/char_set.php');
	
	$ls=$_GET['ls'];
	$id=$_GET['id'];
	$modiid=$_POST['modiid'];
	$linkMemo=trim($_POST['linkMemo']);
	$linkTitle=$_POST['linkTitle'];
	$linkMemo=$_POST['linkMemo'];	
	$linkNum=$_POST['linkNum'];
	$linkURL=strtolower(trim($_POST['linkURL']));
	$cateName=$_POST['cateName'];
	if($cateName==""){$cateName=' ';}

	$nowlinknum=$_GET['linkNum'];
	$selectcate=$_GET['selectcate'];
	if($_POST['cateName']!=""){$selectcate=$_POST['cateName'];}
	$ls_msg="友情链接管理";
	
	
	
	
	switch ($ls)
	{
		case "up":
			$down=$nowlinknum-1;
			$down_sql="update ls_links set linkNum=".$nowlinknum." where linkNum=".$down." ";
			$down_sql2="update ls_links set linkNum=linkNum-1 where id=".$id." ";
			//echo $down_sql;
			$down_query=$opendb->query($down_sql);
			$down_query=$opendb->query($down_sql2);
			$ls_msg="上移操作成功！";
			break;
		case "down":
			$up=$nowlinknum+1;
			$up_sql="update ls_links set linkNum=".$nowlinknum." where linkNum=".$up."  ";
			$up_sql2="update ls_links set linkNum=linkNum+1 where id=".$id." ";
			
			//echo $up_sql;
			$up_query=$opendb->query($up_sql);
			$up_query=$opendb->query($up_sql2);

			$ls_msg="下移操作成功！";

			break;
		case "insert":
			$select_sql2="select * from ls_links where linkTitle='".$linkTitle."' ";
			$select_query2=$opendb->query($select_sql2);
			$select_rows2=mysql_num_rows($select_query2); //总数
			if($select_rows2>0 || trim($linkTitle)=="" )
			{ $ls_msg="友情链接已经存在 或链接文字为空，请重新选择";}
			else{

				$select_sql2="select * from ls_links ";
				$select_query2=$opendb->query($select_sql2);
				$select_rows2=mysql_num_rows($select_query2); //总数
				$image=$_FILES['linkIconPic']['name'];  
				//if($image!="")
				//{
					$arr=array(" ",".");   
				//产生文件名   
					$linkIconPicName=str_replace($arr ,"",time()).".".substr($image,-3);
					$linkIconPicFile=$pathuploadfiles.$linkIconPicName;
				
					move_uploaded_file($_FILES['linkIconPic']['tmp_name'], $linkIconPicFile);

					$toW=$linkIconPicWidth;			//宽度
					$toH=$linkIconPicHeight;			//高度
					$toFile= $linkIconPicFile;		//目标地址
					$srcFile=$linkIconPicFile;	//目标地址

					ImageResize($srcFile,$toW,$toH,$toFile);	//resize the big pic width & height to stand size
				//}
				$insert_sql="insert into ls_links(cateName,linkTitle,linkIconPic,linkURL,linkMemo,linkNum,enable) values ('".$cateName."','".$linkTitle."','".$linkIconPicName."','".$linkURL."','".$linkMemo."',".($select_rows2+1).",1)";
				//echo $insert_sql;
				$insert_query=$opendb->query($insert_sql);

				
				$ls_msg="添加操作成功！";
			}
			break;
		case "modi":
			$modi_sql="update ls_links set linkMemo='".$linkMemo."', cateName='".$cateName."', linkTitle='".$linkTitle."',   linkURL='".$linkURL."', linkMemo='".$linkMemo."'  where id=".$modiid." ";
			//echo $modi_sql;
			$modi_query=$opendb->query($modi_sql);
			
			$modi_sql2="select linkIconPic from ls_links where   id=".$modiid." ";
			$modi_query2=$opendb->query($modi_sql2);
			$linkIconPic_row2=mysql_fetch_assoc($modi_query2);
			$linkIconPicName=$linkIconPic_row2['linkIconPic'];
			$image=$_FILES['changeLinkIconPic']['name'];
			if($image!="")
			{
				$linkIconPicFile=$pathuploadfiles.substr($linkIconPicName,0,(strlen($linkIconPicName)-3));
				$linkIconPicFile.=substr($image,-3);
				//echo $linkIconPicFile."--".$image;
				move_uploaded_file($_FILES['changeLinkIconPic']['tmp_name'], $linkIconPicFile);
			}
			$ls_msg="修改操作成功！ <a href='#' onclick='document.execCommand(\"Refresh\");'>请刷新本页</a>";
				
			break;
		case "del":
			$link_sql2="select * from ls_links where id=".$id." ";
			$link_query2=$opendb->query($link_sql2);
			$link_rows2=mysql_fetch_assoc($link_query2);
			$num_sql2="update ls_links set linkNum=linkNum-1 where linkNum>".$link_rows2['linkNum']." ";//all order number-1
			$down_query=$opendb->query($num_sql2);
		
			$del_sql="delete from ls_links where id=".$id." ";
			//echo $del_sql;
			$del_query=$opendb->query($del_sql);
			$ls_msg="删除操作成功！";
			break;
		case "unenable":
			$unenable_sql="update ls_links set enable=0 where id=".$id." ";
			//echo $unenable_sql;
			$unenable_query=$opendb->query($unenable_sql);
			$ls_msg="禁止操作成功！";
			break;
		case "enable":
			$enable_sql="update ls_links set enable=1 where id=".$id." ";
			//echo $enable_sql;
			$enable_query=$opendb->query($enable_sql);
			$ls_msg="允许操作成功！";
			break;
	}
	if($selectcate == null){
		$cateName_sql="select * from ls_links";
		$cateName_query=$opendb->query($cateName_sql);
		$cateName_assoc=mysql_fetch_assoc($cateName_query);
		$selectcate = $cateName_assoc['cateName'];
	}
	$link_sql="select * from ls_links where  cateName='".$selectcate."' order by cateName asc, linkNum asc";
	$link_query=$opendb->query($link_sql);
	$link_num=mysql_num_rows($link_query);	

	$link_cate_sql="SELECT distinct(cateName) FROM ls_links  where  cateName!='' order by cateName asc";//count the error by searchtype
	$link_cate_query=$opendb->query($link_cate_sql);

?>
<!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=utf-8">
		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<script language="JavaScript" src="js/ls.js"></script>
		<title>无标题文档</title>
	</head>
	<body>
		<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#6396d6">
			<tr>
				<td height="18" background="images/admin_bg.gif"><div align="center"><font color="#ffffff"><strong>友情链接管理</strong></font></div>
				</td>
			</tr>
			
			<tr>
				<td height="213" valign="top" bgcolor="#dee7ff"><br>
					<table border="0" width="96%" align="center">
<tr>
							<td><?
                          while($news_rows2=mysql_fetch_assoc($link_cate_query))
		  					{
								if($selectcate==$news_rows2['cateName'])
								{echo "<a href='?selectcate=".$news_rows2['cateName']."'><span style=\"color:#930\" >".$news_rows2['cateName']."</span></a> | ";}
								else
								{echo "<a href='?selectcate=".$news_rows2['cateName']."'>".$news_rows2['cateName']."</a> | ";}
								
							}
						   /*if($selectcate=="")
								{echo "<a href='?selectcate=".$selectcate."'><span style=\"color:#930\" >无分类</span></a> | ";}
								else
								{echo "<a href='?selectcate='><span style=\"\" >无分类</span></a> | ";}
								*/
						  ?>
							  <br />
		      提示：<font color="#FF0000"><?php echo $ls_msg; ?></font></td>
						</tr>
					</table>
					
					<table width="96%" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#FFFFFF">
					 
					 <tr bgcolor="#B9DAFF"> 
						<td align="center" width="35" height="28">序号</td>
						<td align="center" width="196">类别</td>
						<td align="center" width="194" height="28">锚文本</td>
					   <!--<td align="center" width="268">链接图片</td>-->
					   <td width="168" align="center">链接</td>
					   <td width="168" align="center">说明</td>
					   <td colspan="2" align="center">排序</td>
						<td width="24" align="center" >操作</td>
						<td width="97" align="center" >删除</td>
						<td  width="97" align="center">允许</td>
					  </tr>
					 <?php 
					 $i=1;
					  while ($link_rows=mysql_fetch_assoc($link_query))
					  { ?>
						<tbody id='ZH_<?=$link_rows['ID']?>' >	
						<tr bgcolor='#f4f4f4'
						onMouseOver=
						 	this.style.backgroundColor='#CCFFCC' 
						onMouseOut=
						 	this.style.backgroundColor='#f4f4f4' 
						>
						<td align='left' style='padding-left:10px;' height='25' >
					  	<?=$i?>
						</td>
						<td align='left' style='padding-left:10px;' ><?=$link_rows['cateName']?></td>
						<td align='left' style='padding-left:10px;' height='25' >
					  	<?=$link_rows['linkTitle']?>
						</td>
						<!--<td align='left' style='padding-left:10px;' height='25' >
						<?
                        $imagefile="uploadfiles/".$link_rows['linkIconPic'];
						
                        if(file_exists($imagefile))
						{?>
					  		<img src="uploadfiles/<?=$link_rows['linkIconPic']?>" name="pic<?=$link_rows['ID']?>" width="240" height="50" border="1" style="float:left;">
						<? }
						else
						{ ?>
							<img src="images/noimagelong.png" name="pic<?=$link_rows['ID']?>" width="240" height="50" border="1" style="float:left;">
						<? }?>
							</td>-->
							<td align='left' style='padding-left:10px;' height='25'>
					  	<?=$link_rows['linkURL']?>
							</td>
							<td align='left' style='padding-left:10px;' height='25'>
					  	<?=trim($link_rows['linkMemo'])?>
							</td>
							<td width="21" align='center'> 
                        <?
						if($i!=1)// not start up
						{?>					
								<a href='?page=<?=$page?>&id=<?=$link_rows['ID']?>&linkNum=<?=$link_rows['linkNum']?>&ls=up&selectcate=<?=$selectcate?>' title='向上移动'><img src='images/up.gif' border='0'></a>
						<? } ?>
						  </td>
							<td width="18" align='center'> 
                        <? 
						if($i!=$link_num)// not end down
						{ ?>
								 <a href='?page=<?=$page?>&id=<?=$link_rows['ID']?>&linkNum=<?=$link_rows['linkNum']?>&ls=down&selectcate=<?=$selectcate?>' title='向下移动'><img src='images/down.gif' border='0'></a>
						<? } ?>
						  </td>
							<td align='center'> 
							<a href='javascript:secBoard(<?=$link_rows['ID']?>)' title='编辑'><img src='images/b_edit.png' border='0'></a>
						  </td>
							<td align='center'> 
                        <? 
						if($link_rows['linkAdmin']==1)
						{ ?>
								<img src='images/b_undrop.png' border='0'>
						<? }
						if($link_rows['linkAdmin']==0)
						{ ?>
									<a href='?page=<?=$page?>&id=<?=$link_rows['ID']?>&ls=del&selectcate=<?=$selectcate?>' onclick='return ConfirmDel();' title='删除'><img src='images/b_drop.png' border='0'></a>
						<? } ?>
						  </td>
							<td align='center'>
						  
						<?   if($link_rows['enable']==1)
							{ ?>
								<a href='?page=<?=$page?>&id=<?=$link_rows['ID']?>&ls=unenable&selectcate=<?=$selectcate?>' title='显示'><img src='images/enable.gif' border='0'></a>
						<? }
						  if($link_rows['enable']==0)
						  	{ ?>
								<a href='?page=<?=$page?>&id=<?=$link_rows['ID']?>&ls=enable&selectcate=<?=$selectcate?>' title='不显示'><img src='images/unenable.gif' border='0'></a>
						<?	} ?>
													
							</td>
						</tr>
						</tbody>
						
						<tbody id='Z_<?=$link_rows['ID']?>' style='display:none;'>
						 <form name='fm_link' action='?ls=modi' enctype='multipart/form-data' method='post'> 
							<tr bgcolor='#CCCCCC'>
							<td align='left' style='padding-left:10px;' height='146' >
					  	<?=$link_rows['ID']?>
							</td>
							<td align='left' style='padding-left:10px;' ><input name='cateName' type='text' class='inp' value='<?=$link_rows['cateName']?>' size="16" />
							  <select name="select" id="select"  onchange="this.form.cateName.value=this.value;">
							    <option value="0">请选择分类：</option>
							    <?
			$news_sql="SELECT distinct(cateName) FROM ls_links  where enable=1 and cateName!='' order by linkNum";//count the error by searchtype
			$news_query=$opendb->query($news_sql);
			while($news_rows2=mysql_fetch_assoc($news_query))
		  	{
				echo "<option value=".$news_rows2['cateName'].">".$news_rows2['cateName']."</option>";

			}

			?>
						      </select></td>
							<td align='left' style='padding-left:10px;' height='146' >
					  				<input name='linkTitle' type='text' class='inp' value='<?=$link_rows['linkTitle']?>' size="30"> 
							</td>
								<!--<td style='padding-left:5px;'>
                                  <p>
                                    <?
						$imagefile="uploadfiles/".$link_rows['linkIconPic'];
						if(file_exists($imagefile))
						{ ?>
                                    <img src="uploadfiles/<?=$link_rows['linkIconPic']?>" name="changepic" width="240" height="50" border="1" style="float:left;">
                                    <? }
						else
						{ ?>
                                    <img src="images/noimagelong.png" name="changepic" width="240" height="50" border="1" style="float:left;">
                                    <? }?>
                                 <br/>
                                    <input name="changeLinkIconPic" type="file"  onBlur="document.changepic.src=this.value;" size="30">
                                </p></td>-->
							<td align='left' style='padding-left:5px;' height='146'>
					  				<input name='linkURL' type='text' class='inp' value='<?=$link_rows['linkURL']?>' size="30"> 
							</td>
						  <td align='left' style='padding-left:5px;' height='146'><input name='linkMemo2' type='text' class='inp' value='<?=$link_rows['linkMemo']?>' size="30" /></td>
								<td colspan='5' align='center'>
							    <input value='<?=$link_rows['ID']?>' name='modiid' type='hidden'>
									<input type='submit' value='确定'>
									<input type='button' onclick='javascript:secBoard2(<?=$link_rows['ID']?>)' value='放弃'>
						 		</td>
							</tr>
						 </form>
						<td height="2"></tbody>
					<?	$i++;
					  }
					 ?> 
					</table>
					<br />
					
					
<form name="fmaddlink" action="?ls=insert" enctype="multipart/form-data" method="post" onSubmit="return Checkaddlink();">
				   <table width="96%" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#FFFFFF">
					 
						 <tr bgcolor="#B9DAFF">
						   <td width="12%" style='padding-left:20px;'>锚文本 ：</td>
						   <td width="44%" height="28" style='padding-left:20px;'><input name="linkTitle" type="text" size="70">
						   *</td>
						   <td width="44%" style='padding-left:40px;'><input type="text" name="cateName"  id="cateName" value="<?=$selectcate==""?"":$selectcate?>" style="width:150px;"  />
                             <select name="select2" id="select2"  onchange="this.form.cateName.value=this.value;">
                               <option value="0">请选择分类：</option>
                               <?
			$news_sql="SELECT distinct(cateName) FROM ls_links  where enable=1 and cateName!='' order by linkNum asc";//count the error by searchtype
			$news_query=$opendb->query($news_sql);
			while($news_rows2=mysql_fetch_assoc($news_query))
		  	{
				echo "<option value=".$news_rows2['cateName'].">".$news_rows2['cateName']."</option>";
			}

			?>
                </select>
                           可直接添分类</td>
					   </tr>
						 <!--<tr bgcolor="#B9DAFF">
						   <td style='padding-left:20px;'>链接图片 ：</td>
						   <td height="28" valign="bottom" style='padding-left:20px;'>
						     <input name="linkIconPic" type="file"  onblur="document.pic.src=fmaddlink.linkIconPic.value;" size="56" />
						     *
						   </td>
						   <td width="44%" style='padding-left:40px;' >&nbsp;</td>
			         </tr>-->
						 <tr bgcolor="#B9DAFF">
						   <td style='padding-left:20px;'>链接 ：</td>
						   <td height="28" style='padding-left:20px;'><input name="linkURL" type="text"  value="http://" size="70"></td>
						   <td>&nbsp;</td>
					   </tr>
						 <tr bgcolor="#B9DAFF">
						   <td style='padding-left:20px;'>说明： </td> 
							<td height="28" style='padding-left:20px;'><input name="linkMemo" type="text" size="70" /></td>
							<td align="center" style='padding-left:20px;'><input type="submit" value="添加" />
                              <label>
                                <input type="reset" name="button" id="button" value="重置" />
                            </label></td>
				       </tr>
					  </table>
				  </form>
					
					
				<br /></td>
			</tr>
		</table>
	</body>
</html>