<?php
require 'inc.php';
$obj_tab_click=new cls_tab_other_click();
$tab_click=DB_PRE."other_click";
$arr_state=$obj_tab_click->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_record_click_title)) $get_record_click_title="";
if(!isset($get_objid)) $get_objid="";
if(!isset($get_dialog_id)) $get_dialog_id="";

(isset($get_s_state))? $get_s_state=intval($get_s_state):$get_s_state=-101;
if(!isset($get_s_key)) $get_s_key="";

if($get_act=="del_sel"){
	$arr_msg=$obj_tab_click->on_del($get_clickid);
	if($arr_msg["err"]!=""){
		$obj_msg->alert($arr_msg["err"]);
	}
}
if($get_act=="del"){
	$arr_msg=$obj_tab_click->on_del($get_id);
	if($arr_msg["err"]!=""){
		$obj_msg->alert($arr_msg["err"]);
	}
}
if($get_act=="clear"){
	$arr_msg=$obj_tab_click->on_clear($get_id);
	if($arr_msg["err"]!=""){
		$obj_msg->alert($arr_msg["err"]);
	}
}

if($get_act=="state" && isset($get_state_val)){
	$arr_msg=$obj_tab_click->on_state($get_clickid,$get_state_val);
	if($arr_msg["err"]!=""){
		$obj_msg->alert($arr_msg["err"]);
	}
}
$page_footer_info=<<<HTML
<input type="hidden" name="record_click_title" value="{$get_record_click_title}">
<input type="hidden" name="objid" value="{$get_objid}">
<input type="hidden" name="dialog_id" value="{$get_dialog_id}">
HTML;
switch($get_act)
{	
	case 'save':
		$arr_fields=array();
		$arr_fields["click_id"]=$get_id;
		$arr_fields["click_title"]=$get_click_title;
		$arr_fields["click_state"]=$get_click_state;
		$arr_fields["click_url"]=$get_click_url;
		if($get_click_starttime!="" && is_date($get_click_starttime)) $arr_fields["click_starttime"]=strtotime($get_click_starttime);
		if($get_click_endtime!="" && is_date($get_click_endtime)) $arr_fields["click_endtime"]=strtotime($get_click_endtime);
		$arr_msg=$obj_tab_click->on_save($arr_fields);
		if(isset($arr_msg["id"])) $get_id=$arr_msg["id"];
		include 'tpl/_msg.tpl.php';
	    break;
	case 'edit':
		$arr_click=array();
		$obj_result = $obj_db->query("SELECT * FROM ".$tab_click." where click_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			(!empty($obj_rs["click_starttime"]))? $obj_rs["starttime"]=format_date($obj_rs["click_starttime"],"Y-m-d H:i:s") : $obj_rs["starttime"]="";
			(!empty($obj_rs["click_endtime"]))? $obj_rs["endtime"]=format_date($obj_rs["click_endtime"],"Y-m-d H:i:s") : $obj_rs["endtime"]="";
			$arr_click = $obj_rs;
		}
		include 'tpl/click.edit.tpl.php';
		break;
	case "record":
		require_once(SYS_ROOT."/cls/cls.ip.php");
		$obj_ip=new cls_ip;
		$arr_record = array();
		$get_issearch=0;
		$str_where=" where record_click_id=".$get_id;
		if($get_s_key!=""){
			$str_where.=" and record_ip like '%".$get_s_key."%'";
			$get_issearch="1";
		}
		if(isset($get_s_starttime) && is_date($get_s_starttime)){
			$str_where.=" and record_addtime>='".strtotime($get_s_starttime)."'";
			$get_issearch="1";
		}else{
			$get_s_starttime="";
		}
		if(isset($get_s_endtime) && is_date($get_s_endtime)){
			$str_time=$get_s_endtime;
			if(strlen($str_time)<11) $str_time.=" 23:59:59";
			$str_where.=" and record_addtime<='".strtotime($str_time)."'";
			$get_issearch="1";
		}else{
			$get_s_endtime="";
		}
		$obj_rs  = $obj_db->get_one("SELECT count(*) as num FROM ".DB_PRE."other_click_record".$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 ".DB_PRE."other_click_record".$str_where." order by record_id desc".$str_limit;
		$obj_result = $obj_db->query($str_sql);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["addtime"]=date("Y-m-d H:i:s",$obj_rs["record_addtime"]);
			$str_address=$obj_ip->get_area($obj_rs["record_ip"]);
			$obj_rs["ip"]=$obj_rs["record_ip"];
			if(!empty($str_address)){
				$obj_rs["ip"]=$str_address."(".$obj_rs["ip"].")";
			}
			$arr_record[] = $obj_rs;
		}
		include 'tpl/click.record.tpl.php';
		break;
	case 'add':
		$get_id=0;
		$arr_click = $obj_db->get_fields(DB_PRE."other_click",true);
		$arr_click["starttime"]="";
		$arr_click["endtime"]="";
		$arr_click["click_state"]="1";
		include 'tpl/click.edit.tpl.php';
		break;
	default:
		$arr_click = array();
		$str_where=" where 1";
		$get_issearch=0;
		if($get_s_key!=""){
			$str_where.=" and (click_title like '%".$get_s_key."%' or click_url like '%".$get_s_key."%')";
			$get_issearch="1";
		}
		if($get_s_state!=-101){
			$str_where.=" and click_state='".$get_s_state."'";
			$get_issearch="1";
		}
		$obj_rs  = $obj_db->get_one("SELECT count(*) as num FROM ".$tab_click.$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_click.$str_where." order by click_id desc".$str_limit;
		$obj_result = $obj_db->query($str_sql);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$obj_rs["state"]=array_search($obj_rs["click_state"],$arr_state);
			$arr_click[] = $obj_rs;
		}

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

?>
