<?php
class cls_tab_other_friendlink extends cls_base_table
{
	private $obj_db;
	private $perms;
	//构造函数
	function __construct()
	{
		parent::__construct();
		global $obj_db,$obj_tab_sys_config;
		$this->obj_db=$obj_db;
		$this->perms["type"]=array();
		if(is_array($obj_tab_sys_config->get_val("other","friendlink_type"))){
			$this->perms["type"]=$obj_tab_sys_config->get_val("other","friendlink_type");
		}
		$this->perms["state"]=array("通过"=>1,"待审"=>0,"无效"=>-1);
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function __set($name,$value){
		if(isset($this->perms["$name"])){
			$this->perms["$name"]=$value;
		}else{
		}
	}
	/*添加
	arr_fields:关健字数组
	*/
	function _insert($arr_fields)
	{
		$arr_return=array("err"=>"");
		if(!isset($arr_fields["link_name"]) || empty($arr_fields["link_name"])){
			$arr_return["err"]="链接网站名称不能为空！";
			return $arr_return;
		}
		if(!isset($arr_fields["link_url"]) || empty($arr_fields["link_url"])){
			$arr_return["err"]="链接网站地址不能为空！";
			return $arr_return;
		}
		global $obj_db;
		$obj_rs=$obj_db->get_one("select link_url,link_state from ".DB_PRE."other_friendlink where link_name='".$arr_fields["link_name"]."'");
		if(isset($obj_rs["link_url"])){
			if($obj_rs["link_state"]>0){
				$arr_return["err"]="申请的网站名称已经存在！";
				return $arr_return;
			}else if($obj_rs["link_url"]==$arr_fields["link_url"]){
				$arr_return["err"]="您的网站已经提交，请等待我们审核";
				return $arr_return;
			}
		}
		$arr_return=parent::on_insert(DB_PRE."other_friendlink",$arr_fields);
		return $arr_return;
	}
	function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"");
		$arr_return=parent::on_update(DB_PRE."other_friendlink",$arr_fields,$msg_where);
		return $arr_return;
	}
	function on_save($arr_fields=array())
	{
		$arr_return=array("err"=>"");
		(!isset($arr_fields["link_id"]))? $arr_fields["link_id"]=0:$arr_fields["link_id"]=intval($arr_fields["link_id"]);
		$lng_id=$arr_fields["link_id"];
		unset($arr_fields["link_id"]);//释放id
		$arr_fields["link_updatetime"]=TIME;
		if(isset($arr_fields["link_url"])){
			$arr_fields["link_url"]=strtolower($arr_fields["link_url"]);
			if(!strstr($arr_fields["link_url"],"http://")){
				$arr_fields["link_url"]="http://".$arr_fields["link_url"];
			}
		}
		if(isset($arr_fields["link_url"])) $arr_fields["link_url"]=get_html_url($arr_fields["link_url"]);
		if($lng_id<1){
			$arr_fields["link_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="link_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}

		return $arr_return;
	}
	function on_del($arr_id)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_return=parent::on_del(DB_PRE."other_friendlink","link_id in(".$str_id.")");
		return $arr_return;
	}
	//排序
	function on_sort_act($msgid,$msgact){
		$arr_return=array("err"=>"");
		$obj_result = $this->obj_db->query("SELECT link_sort FROM ".DB_PRE."other_friendlink where link_id='".$msgid."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort=$obj_rs["link_sort"];
		}else{
			$arr_return["err"]="排序记录不存在！[id=".$msgid."]";
			return $arr_return;
		}
		self::on_format_sort();
		if($msgact=="up"){
			$obj_result = $this->obj_db->query("SELECT link_id,link_sort FROM ".DB_PRE."other_friendlink where link_sort<'".$lng_sort."'  order by link_sort desc,link_id desc limit 0,1");
		}else{
			$obj_result = $this->obj_db->query("SELECT link_id,link_sort FROM ".DB_PRE."other_friendlink where link_sort>'".$lng_sort."' order by link_sort,link_id limit 0,1");
		}
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort_new=$obj_rs["link_sort"];
			$this->obj_db->execute("update ".DB_PRE."other_friendlink set link_sort=".$lng_sort." where link_id='".$obj_rs["link_id"]."'");
			$this->obj_db->execute("update ".DB_PRE."other_friendlink set link_sort=".$lng_sort_new." where link_id='".$msgid."'");
		}
	}
	//格式化排序
	function on_format_sort(){
		//取所有排序相同的记录
		$obj_result = $this->obj_db->query("SELECT link_sort,count(*) as num FROM ".DB_PRE."other_friendlink group by link_sort having num>1 order by link_sort desc");
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_num=$obj_rs["num"]-1;
			//把大于这个排序的记录后移lng_num位
			$this->obj_db->execute("update ".DB_PRE."other_friendlink set link_sort=link_sort+".$lng_num." where link_sort>'".$obj_rs["link_sort"]."'");
			$obj_result2 = $this->obj_db->query("SELECT link_id,link_sort FROM ".DB_PRE."other_friendlink where link_sort='".$obj_rs["link_sort"]."' order by link_sort");
			$lng_sort=0;
			while($obj_rs2 = $this->obj_db->fetch_array($obj_result2))
			{

				if($lng_sort>0){
					//第一个排序不变
					$this->obj_db->execute("update ".DB_PRE."other_friendlink set link_sort=link_sort+".$lng_sort." where link_id='".$obj_rs2["link_id"]."'");
				}
				$lng_sort++;
			}
		}
	}
	function on_type($arr_id,$msg_val){
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要设置的记录！";
			return $arr_return;
		}
		$msg_val=intval($msg_val);
		global $obj_db;
		$arr_return["err"]=$obj_db->execute("update ".DB_PRE."other_friendlink set link_type=".$msg_val." where link_id in(".$str_id.")");
		return $arr_return;
	}
	function on_state($arr_id,$msg_val){
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要设置的记录！";
			return $arr_return;
		}
		$msg_val=intval($msg_val);
		global $obj_db;
		$arr_return["err"]=$obj_db->execute("update ".DB_PRE."other_friendlink set link_state=".$msg_val." where link_id in(".$str_id.")");
		return $arr_return;
	}
}
?>
