<?php
/*
===========================用户控制器
^^^^^^^^^^^^^^^^^^成员
------添加(on_insert)
------修改(on_update)
------删除(on_del)

*/
class cls_tab_other_click extends cls_base_table
{
	private $tab_click;
	private $obj_db,$perms;
	//构造函数
	function __construct()
	{
		global $obj_db;
		parent::__construct();
		$this->obj_db=$obj_db;
		$this->tab_click=DB_PRE."other_click";
		$this->perms["state"]=array("启动"=>1,"关闭"=>0);
	}
	function __get($name){
		if(isset($this->perms["$name"])){
			return $this->perms["$name"];
		}
	}
	function on_jc($msg_id){
		$arr_return=array("err"=>"");
		$lng_id=(int)$msg_id;
		if($lng_id<1){
			$arr_return["err"]="监测id不存在";
			return $arr_return;
		}
		global $obj_db,$obj_ctl_user;
		$obj_result=$obj_db->query("select click_id,click_url,click_state,click_starttime,click_endtime from ".DB_PRE."other_click where click_id=".$lng_id);
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_return["url"]=$obj_rs["click_url"];
			if($obj_rs["click_state"]>0 && ($obj_rs["click_starttime"]<1 || TIME>$obj_rs["click_starttime"]) && ($obj_rs["click_endtime"]<1 || TIME<$obj_rs["click_endtime"])){
				//总数增加
				$obj_db->execute("update ".DB_PRE."other_click set click_num=click_num+1 where click_id=".$lng_id);
				$str_request_url="";
				$str_ieinfo="";
				if(isset($_SERVER["HTTP_REFERER"])) $str_request_url=$_SERVER["HTTP_REFERER"];
				if(isset($_SERVER["HTTP_USER_AGENT"])) $str_ieinfo=$_SERVER["HTTP_USER_AGENT"];
				$arr_fields=array();
				$arr_fields["record_click_id"]=$lng_id;
				$arr_fields["record_ip"]=get_user_ip();
				$arr_fields["record_addtime"]=TIME;
				$arr_fields["record_user_id"]=$obj_ctl_user->id;
				$arr_fields["record_request_url"]=$str_request_url;
				$arr_fields["record_ieinfo"]=$str_ieinfo;
				$arr_re=parent::on_insert(DB_PRE."other_click_record",$arr_fields);
				if($arr_re["err"]!="") $arr_return["err"]=$arr_re["err"];
			}
		}else{
			$arr_return["err"]="监测id不存在";
			return $arr_return;
		}
		return $arr_return;
	}
	/*添加
	arr_fields:关健字数组
	*/
	function on_save($arr_fields,$str_where="")
	{
		global $obj_ctl_user;
		$arr_return=array("err"=>"","id"=>0);
		$arr_search=array();
		(!isset($arr_fields["click_id"]))? $arr_fields["click_id"]=0:$arr_fields["click_id"]=intval($arr_fields["click_id"]);
		//格式化数据
		if(isset($arr_fields["click_title"])&&$arr_fields["click_title"]==""){
			$arr_return["err"]="监测名称不能为空！";
			return $arr_return;
		}
		$lng_id=$arr_fields["click_id"];
		unset($arr_fields["click_id"]);//释放id
		$arr_fields["click_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["click_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="click_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		return $arr_return;
	}
	function _insert($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		if(!isset($arr_fields["click_title"])){
			$arr_return["err"]="监测名称不能为空！";
			return $arr_return;
		}
		$arr_return=parent::on_insert($this->tab_click,$arr_fields);
		return $arr_return;
	}
	function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_return=parent::on_update($this->tab_click,$arr_fields,$msg_where);
		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($this->tab_click,"click_id in(".$str_id.")");
		//清除相关的明细记录
		parent::on_del(DB_PRE."other_click_record","record_click_id in(".$str_id.")");
		return $arr_return;
	}
	function on_clear($arr_id){
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要清空的记录！";
			return $arr_return;
		}
		global $obj_db;
		$str_msg=$obj_db->execute("update ".DB_PRE."other_click set click_num=0 where click_id in(".$str_id.")");
		//清除相关的明细记录
		if($str_msg==""){
			parent::on_del(DB_PRE."other_click_record","record_click_id in(".$str_id.")");
		}
		return $arr_return;
	}
	function on_state($arr_id,$msg_state=0)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要设置的记录！";
			return $arr_return;
		}
		$str_txt=array_search($msg_state,$this->perms["state"]);
		if(!is_numeric($msg_state) || empty($str_txt)){
			$arr_return["err"]="设置的状态不存在！";
			return $arr_return;
		}

		global $obj_db;
		$str_msg=$obj_db->execute("update ".$this->tab_click." set click_state='".$msg_state."' where click_id in(".$str_id.")");
		if($str_msg!=""){
			$arr_return["err"]="设置状态时出错！";
		}
		return $arr_return;
	}
}
?>
