<?php
class cls_tab_other_ads extends cls_base_table
{
	private $tab_ads;
	private $obj_db;
	private $perms;
	//构造函数
	function __construct()
	{
		global $obj_db,$obj_tab_sys_config;
		$arr_cfg_other=$obj_tab_sys_config->get_model("other");
		parent::__construct();
		$this->perms=array();
		if(isset($arr_cfg_other["ads_position"])){
			$this->perms["position"]=$arr_cfg_other["ads_position"];
		}
		$this->obj_db=$obj_db;
		$this->tab_ads=DB_PRE."other_ads";
	}
	function on_save($arr_fields,$str_where="")
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_job=array();
		(!isset($arr_fields["ads_id"]))? $arr_fields["ads_id"]=0:$arr_fields["ads_id"]=intval($arr_fields["ads_id"]);
		//保存操作
		$lng_id=$arr_fields["ads_id"];
		unset($arr_fields["ads_id"]);//释放id
		$arr_fields["ads_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["ads_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="ads_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		return $arr_return;
	}
	/*添加
	arr_fields:关健字数组
	*/
	function _insert($msg_fields)
	{
		$arr_fields=self::_exe_ads($msg_fields);
		$arr_return=parent::on_insert($this->tab_ads,$arr_fields);
		return $arr_return;
	}
	function _update($msg_fields,$msg_where)
	{
		$arr_fields=self::_exe_ads($msg_fields);
		$arr_return=parent::on_update($this->tab_ads,$arr_fields,$msg_where);
		return $arr_return;
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function _exe_ads($arr_fields=array())
	{
		$lng_width=0;
		$lng_height=0;
		$str_style=$arr_fields["ads_param"];
		if(is_array($arr_fields["ads_param"]))
		{
			$str_stylle="";
			if(trim($arr_fields["ads_param"][0])!=""){
				$str_style.=" width='".$arr_fields["ads_param"][0]."'";
				$lng_width=$arr_fields["ads_param"][0];
			}
			if(trim($arr_fields["ads_param"][1])!=""){
				$str_style.=" height='".$arr_fields["ads_param"][1]."'";
				$lng_height=$arr_fields["ads_param"][1];
			}
			$arr_fields["ads_param"] = implode(',', $arr_fields["ads_param"]);
		}
		if($arr_fields["ads_type"]=="pic")
		{
			$str_content="<a href=\"".$arr_fields["ads_linkurl"]."\" target=\"_blank\"><img src=\"".$arr_fields["ads_val"]."\" style=\"border:0px;\"".$str_style."></a>";
			$arr_fields["ads_content"]=$str_content;
		}else if($arr_fields["ads_type"]=="swf"){
			$str_content="<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0'".$str_style."><param name=movie value='".$arr_fields["ads_val"]."'><param name=quality value=high><param name='wmode' value='transparent'><embed src='".$arr_fields["ads_val"]."' wmode='transparent' quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash'".$str_style."></embed></object>";
			//$arr_fields["ads_content"]=addslashes($str_content);
			if(!empty($arr_fields["ads_linkurl"])){
				$str_content="<div style='float:left;width:".$lng_width."px;height:".$lng_height."px'>".$str_content."<div style='position:relative;top:-".$lng_height."px;left:0px;z-index:3'><a href='".$arr_fields["ads_linkurl"]."' target='_blank'><img src='".WEB_URL."/images/css/blank.gif' width='".$lng_width."px' height='".$lng_height."px'></a></div></div>";
			}
			$arr_fields["ads_content"]=$str_content;
		}else{
			$arr_fields["ads_content"]=$arr_fields["ads_val"];
		}
		return $arr_fields;
	}
	function on_state($arr_id,$msg_val)
	{
		$arr_return=array("err"=>"");
		$msg_val=intval($msg_val);
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要设置的记录！";
			return $arr_return;
		}
		//-3表示企业状态无效，所以这里的状态不能控制-3
		$obj_result["err"] = $this->obj_db->execute("update ".$this->tab_ads." set ads_state=".$msg_val." where ads_id in(".$str_id.")");
		return $arr_return;
	}
	function on_position($arr_id,$msg_val)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要设置的记录！";
			return $arr_return;
		}
		//-3表示企业状态无效，所以这里的状态不能控制-3
		$obj_result["err"] = $this->obj_db->execute("update ".$this->tab_ads." set ads_position='".$msg_val."' where ads_id in(".$str_id.")");
		return $arr_return;
	}

	function on_del($msg_where)
	{
		$arr_return=parent::on_del($this->tab_ads,$msg_where);
		return $arr_return;
	}
	//排序
	function on_sort_act($msgid,$msgact){
		$arr_return=array("err"=>"");
		$obj_result = $this->obj_db->query("SELECT ads_position,ads_sort FROM ".DB_PRE."other_ads where ads_id='".$msgid."'");
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort=$obj_rs["ads_sort"];
			$str_ads_position=$obj_rs["ads_position"];
		}else{
			$arr_return["err"]="排序记录不存在！[id=".$msgid."]";
			return $arr_return;
		}
		self::on_format_sort($str_ads_position);
		if($msgact=="up"){
			$obj_result = $this->obj_db->query("SELECT ads_id,ads_sort FROM ".DB_PRE."other_ads where ads_sort<'".$lng_sort."' and ads_position='".$str_ads_position."' order by ads_sort desc,ads_id desc limit 0,1");
		}else{
			$obj_result = $this->obj_db->query("SELECT ads_id,ads_sort FROM ".DB_PRE."other_ads where ads_sort>'".$lng_sort."' and ads_position='".$str_ads_position."' order by ads_sort,ads_id limit 0,1");
		}
		if($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$lng_sort_new=$obj_rs["ads_sort"];
			$this->obj_db->execute("update ".DB_PRE."other_ads set ads_sort=".$lng_sort." where ads_id='".$obj_rs["ads_id"]."'");
			$this->obj_db->execute("update ".DB_PRE."other_ads set ads_sort=".$lng_sort_new." where ads_id='".$msgid."'");
		}
	}
	//格式化排序
	function on_format_sort($msg_ads_position){
		$str_ads_position=$msg_ads_position;
		//取所有排序相同的记录
		$obj_result = $this->obj_db->query("SELECT ads_sort,count(*) as num FROM ".DB_PRE."other_ads where ads_position='".$str_ads_position."' group by ads_sort,ads_position having num>1 order by ads_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_ads set ads_sort=ads_sort+".$lng_num." where ads_sort>'".$obj_rs["ads_sort"]."' and ads_position='".$str_ads_position."'");
			$obj_result2 = $this->obj_db->query("SELECT ads_id,ads_sort FROM ".DB_PRE."other_ads where ads_sort='".$obj_rs["ads_sort"]."' and ads_position='".$str_ads_position."' order by ads_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_ads set ads_sort=ads_sort+".$lng_sort." where ads_id='".$obj_rs2["ads_id"]."'");
				}
				$lng_sort++;
			}
		}
	}
}
?>
