<?php

/********** Func **********/

/*
	写入统计
	$appid		模块ID
	$id			记录ID
	$do			方法
*/
function analytic_ping($appid,$id,$do,$func=""){
	global $db,$mysql;
	global $Setting;
	
	//加载自定义配置
	include_once("setting.php");
	
	//如果统计已半闭
	if( $Setting["analytic"][$do] == "off"  ){
		return false;
	}	
	
	//如果参数错误或正在管理员身份登录
	// || $_SESSION["Manager"]["id"]
	if( !$appid || !$id || !$do ){
		return false;
	}
	
	//统计标识
	$appkey = $appid."-".$id;
	
	//数据类型
	$data = ($do=='click'?'clicks':'views');
	
	//日期
	$date = array('Y'=> date("Y") ,'M'=> date("Y-m") ,'D'=> date("Y-m-d") ,'H'=> date("H"));
	
	//查询语句
	$sql = array();
	$sql['UPDATE'] = "update ".VI_DBMODPRE."analytic set ".$data."=".$data."+1 where appkey='".$appkey."' and category ='{CATE}' and `date`='{DATE}'";
	$sql['INSERT'] = "insert into ".VI_DBMODPRE."analytic(appkey,category,date,".$data.") values('".$appkey."','{CATE}','{DATE}',1)";
	
	$cookie = $appid."-".$do."-".$id;
	
	//$_COOKIE[$cookie]
	
	if( !$_SESSION[$cookie] ){
		
		//mysql open
		reader_mysql();
		
		foreach( $date as $key => $val ){
			
			$update = str_replace(array('{CATE}','{DATE}'),array($key,$val),$sql['UPDATE']);
			$insert = str_replace(array('{CATE}','{DATE}'),array($key,$val),$sql['INSERT']);
			
			//更新数据
			$result = mysql_query($update,$mysql);
			
			$Project["queries"]++;
			
			//没有记录
			if( mysql_affected_rows($mysql) == 0 ){
				
				//创建数据
				mysql_query($insert,$mysql);
				
				$Project["queries"]++;
			}
			
		}
		
		//写入Cookie
		//$expire=time() + (3600*60*12);
		//setcookie($cookie, 'Y', $expire ,"" ,"");
		
		$_SESSION[$cookie] = "Y";
		
		if( $func && function_exists($func)){
			$func($mysql,$id,$do);
		}
		
		return '<!--ping:'.$do.' key:'.$appkey.' status:ok-->';
	  
		//mysql close
		reader_mysql(true);
		
	}else{
		return '<!--ping:'.$do.' key:'.$appkey.' status:no-->';	
	}
	
}

/*
	输出统计
	$appid		模块ID
	$id			记录ID
	$do			方法
*/
function analytic_tote($appid,$id,$do){
	
	global $db,$mysql;
	
	if( !$appid || !$id || !$do ){
		return false;
	}
	
	//统计标识
	$appkey = $appid."-".$id;
	
	//数据类型
	$data = ($do=='click'?'clicks':'views');
	
	//日期
	$date = array('Y'=> date("Y") ,'M'=> date("Y-m") ,'D'=> date("Y-m-d") ,'H'=> date("H"));
	
	//查询语句
	$sql = "select sum(".$data.") as count from ".VI_DBMODPRE."analytic where appkey='".$appkey."' and category = 'Y' ";
	
	//mysql open
	reader_mysql();

	//更新数据
	$result = mysql_query($sql,$mysql);
	
	$Project["queries"]++;
	
	if(mysql_num_rows($result)==1){
		$row=mysql_fetch_assoc($result);
		
		return $row["count"];
	}
  
	//mysql close
	reader_mysql(true);
	
}

/*
	清空统计
	$appid		模块ID
	$list		记录ID
*/
function clear($appid,$list){
	
	global $db,$mysql;
	
	if( !$appid || !$list ){
		return false;
	}
	
	//mysql open
	reader_mysql();
	
	$array = explode(",",$list);
	
	$Project["queries"]++;
	
	foreach( $array as $id ){
	
		//统计标识
		$appkey = $appid."-".$id;
		
		//查询语句
		$sql = "delete from ".VI_DBMODPRE."analytic where appkey='".$appkey."'";
		
		//更新数据
		$result = mysql_query($sql,$mysql);
	
	}
  
	//mysql close
	reader_mysql(true);
	
}


?>