<?php
require 'inc.php';
$obj_tab_friendlink=new cls_tab_other_friendlink();
$tab_friendlink=DB_PRE."other_friendlink";
$arr_type=$obj_tab_friendlink->type;
$arr_state=$obj_tab_friendlink->state;
if(!isset($int_pagesize)) $int_pagesize=10;
if(!isset($get_act)) $get_act="";
(!isset($get_page))? $get_page=0:$get_page=intval($get_page);
(!isset($get_id))? $get_id=0:$get_id=intval($get_id);

if(!isset($get_s_key)) $get_s_key="";
if(!isset($get_s_type)) $get_s_type="";
if(!isset($get_s_state)) $get_s_state=-999;

if($get_act=="del"){
	$arr_return=$obj_tab_friendlink->on_del($get_id);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="del_sel"&&isset($get_friendlinkid)){
	$arr_return=$obj_tab_friendlink->on_del($get_friendlinkid);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="sort_up"){
	$arr_return=$obj_tab_friendlink->on_sort_act($get_id,"up");
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="sort_down"){
	$arr_return=$obj_tab_friendlink->on_sort_act($get_id,"down");
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="sort"&&isset($get_friendlinkid)){
	foreach($get_friendlinkid as $item){
		$var="get_sort_".$item;
		if(isset($$var)){
			$str_msg=$obj_db->execute("update ".DB_PRE."other_friendlink set link_sort='".$$var."' where link_id='".$item."'");
			if($str_msg!="") $obj_msg->alert($str_msg);
		}
	}
}
if($get_act=="type"){
	$arr_msg=$obj_tab_friendlink->on_type($get_friendlinkid,$get_type_val);
	if($arr_msg["err"]!="") $obj_msg->alert($arr_msg["err"]);
}
if($get_act=="state"){
	$arr_msg=$obj_tab_friendlink->on_state($get_friendlinkid,$get_state_val);
	if($arr_msg["err"]!="") $obj_msg->alert($arr_msg["err"]);
}

switch($get_act)
{	
	case 'save':
		$arr_fields=array();
		$arr_fields["link_id"]=$get_id;
		$arr_fields["link_name"]=$get_link_name;
		$arr_fields["link_url"]=$get_link_url;
		$arr_fields["link_pic "]=$get_link_pic;
		$arr_fields["link_sort"]=$get_link_sort;
		$arr_fields["link_type"]=$get_link_type;
		$arr_msg=$obj_tab_friendlink->on_save($arr_fields);
		include 'tpl/_msg.tpl.php';
	    break;
	case 'edit':
		$arr_friendlink=array();
		$obj_result = $obj_db->query("SELECT * FROM ".$tab_friendlink." where link_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_friendlink = $obj_rs;
		}
		include 'tpl/friendlink.edit.tpl.php';
		break;
	case 'add':
		$get_id=0;
		$arr_friendlink=array();
		include 'tpl/friendlink.add.tpl.php';
		break;
	default:
		$get_issearch=0;
		$arr_friendlink = array();
		$str_where=" where 1";
		if($get_s_key!=""){
			$str_where.=" and (link_name like '%".$get_s_key."%' or link_url like '%".$get_s_key."%')";
			$get_issearch=1;
		}
		if($get_s_state!=-999){
			$str_where.=" and link_state='".$get_s_state."'";
			$get_issearch=1;
		}
		if($get_s_type!=""){
			$str_where.=" and link_type='".$get_s_type."'";
			$get_issearch=1;
		}
		$obj_rs  = $obj_db->get_one("SELECT count(*) as num FROM ".$tab_friendlink.$str_where);
		$int_total=$obj_rs["num"];
		$int_pages= ceil($int_total/$int_pagesize);
		$get_page = min($int_pages, $get_page);
		$get_page = max($get_page, 1);
		$page_split_page=get_pages($int_total, $get_page, $int_pagesize);
		$int_offset = $int_pagesize*($get_page-1);
		$str_limit = " limit ".$int_offset.",".$int_pagesize;
		$str_sql="SELECT * FROM ".$tab_friendlink.$str_where." order by link_sort,link_id desc".$str_limit;
		$obj_result = $obj_db->query($str_sql);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["pic"]="";
			if($obj_rs["link_pic"]!=""){
				$obj_rs["pic"]=get_html_url($obj_rs["link_pic"]);
			}
			$obj_rs["addtime"]=date("Y-m-d H:i",$obj_rs["link_addtime"]);
			$obj_rs["type"]=array_search($obj_rs["link_type"],$arr_type);
			$obj_rs["state"]=array_search($obj_rs["link_state"],$arr_state);
			if($obj_rs["link_state"]<1) $obj_rs["state"]="<span style='color:#ff0000'>".$obj_rs["state"]."</span>";
			$arr_friendlink[] = $obj_rs;
		}

		include 'tpl/friendlink.tpl.php';
}

?>