<?
include_once('./_common.php');
@include_once(G5_LIB_PATH.'/json.lib.php');
include_once(G5_LIB_PATH.'/visit.lib.php');

if($_POST[mode]=='visit'){
	if($_POST[types]=='hour'){
		$to_date=date('Y-m-d', G5_SERVER_TIME);
		$sql="select SUBSTRING(vi_time,1,2) as vi_hour, count(vi_id) as cnt
					from {$g5['visit_table']}
					where vi_date like '%{$to_date}%'
					group by vi_hour
					order by vi_hour";
		$result=sql_query($sql);
		
		$arr=array();
		$to_max=0;
		$to_count=0;
		for($i=0; $row=sql_fetch_array($result); $i++){
			$arr[$row['vi_hour']]=$row['cnt'];

			if($row['cnt']>$to_max){
				$to_max=$row['cnt'];
			}

			$to_count+=$row['cnt'];
		}

		$to_ary=array();
		for($i=0;$i<24;$i++){
			$hour=sprintf("%02d",$i);
			$count=(int)$arr[$hour];

			$to_ary['kind'][]=$hour;
			$to_ary['count'][]=$count;
		}

		$ary[to_data]=$to_ary;
				
		$fr_date=date('Y-m-d', G5_SERVER_TIME-86400);
		$sql="select SUBSTRING(vi_time,1,2) as vi_hour, count(vi_id) as cnt
					from {$g5['visit_table']}
					where vi_date like '%{$fr_date}%'
					group by vi_hour
					order by vi_hour";
		$result=sql_query($sql);
		
		$arr=array();
		$fr_max=0;
		$fr_count=0;
		for($i=0; $row=sql_fetch_array($result); $i++){
			$arr[$row['vi_hour']]=$row['cnt'];

			if($row['cnt']>$fr_max){
				$fr_max=$row['cnt'];
			}

			$fr_count+=$row['cnt'];
		}
		
		$fr_ary=array();
		for($i=0;$i<24;$i++){
			$hour=sprintf("%02d",$i);
			$count=(int)$arr[$hour];

			$fr_ary['kind'][]=$hour;
			$fr_ary['count'][]=$count;
		}

		$ary[fr_data]=$fr_ary;

		echo json_encode($ary);
	}else if($_POST[types]=='week'){
		$weekday=array('월','화','수','목','금','토','일');
		$arr=array();
		
		$s_date1=G5_SERVER_TIME-(date('w')*86400);
		$fr_date1=date('Y-m-d', $s_date1);
		$to_date1=date('Y-m-d', $s_date1+(86400*7));

		$sql="select WEEKDAY(vs_date) as weekday_date, SUM(vs_count) as cnt
            from {$g5['visit_sum_table']}
            where vs_date between '{$fr_date1}' and '{$to_date1}'
            group by weekday_date
            order by weekday_date";

		$result=sql_query($sql);

		for ($i=0; $row=sql_fetch_array($result); $i++) {
			$arr[$row['weekday_date']]=$row['cnt'];
		}

		for ($i=0; $i<7; $i++) {
            $count=(int)$arr[$i];

			$to_ary['kind'][]=$weekday[$i];
			$to_ary['count'][]=$count;
		}

		$ary[to_data]=$to_ary;

		$arr=array();
		
		$s_date2=$s_date1-(86400*7);
		$fr_date2=date('Y-m-d', $s_date2);
		$to_date2=date('Y-m-d', $s_date2+(86400*7));

		$sql="select WEEKDAY(vs_date) as weekday_date, SUM(vs_count) as cnt
            from {$g5['visit_sum_table']}
            where vs_date between '{$fr_date2}' and '{$to_date2}'
            group by weekday_date
            order by weekday_date";

		$result=sql_query($sql);

		for ($i=0; $row=sql_fetch_array($result); $i++) {
			$arr[$row['weekday_date']]=$row['cnt'];
		}

		for ($i=0; $i<7; $i++) {
            $count=(int)$arr[$i];

			$fr_ary['kind'][]=$weekday[$i];
			$fr_ary['count'][]=$count;
		}

		$ary[fr_data]=$fr_ary;

		echo json_encode($ary);
	}else if($_POST[types]=='day'){	
		$arr=array();

		$s_year=date('Y',G5_SERVER_TIME);
		$s_month=date('m',G5_SERVER_TIME);
		$s_day=date('t',G5_SERVER_TIME);

		$fr_date=date('Y-m-d', strtotime("{$s_year}-{$s_month}-01"));
		$to_date=date('Y-m-d', strtotime("{$s_year}-{$s_month}-{$s_day}"));

		$sql="select vs_date, vs_count as cnt
            from {$g5['visit_sum_table']}
            where vs_date between '{$fr_date}' and '{$to_date}'
            order by vs_date desc ";
		$result=sql_query($sql);

		for ($i=0; $row=sql_fetch_array($result); $i++) {
			$arr[$row['vs_date']] = $row['cnt'];
		}

		for ($i=1; $i<=31; $i++) {
			$k=$i;

			if($k<10) $k="0".$i;

			$date="{$s_year}-{$s_month}-{$k}";

            $count=(int)$arr[$date];

			$to_ary['kind'][]=$k;
			$to_ary['count'][]=$count;
		}

		$ary[to_data]=$to_ary;

		$arr=array();
		
		$fr_datetime=strtotime("{$s_year}-{$s_month}-01")-86400;

		$f_year=date('Y',$fr_datetime);
		$f_month=date('m',$fr_datetime);
		$f_day=date('t',$fr_datetime);

		$fr_date=date('Y-m-d', strtotime("{$f_year}-{$f_month}-01"));
		$to_date=date('Y-m-d', strtotime("{$f_year}-{$f_month}-{$f_day}"));

		$sql="select vs_date, vs_count as cnt
            from {$g5['visit_sum_table']}
            where vs_date between '{$fr_date}' and '{$to_date}'
            order by vs_date desc ";
		$result=sql_query($sql);

		for ($i=0; $row=sql_fetch_array($result); $i++) {
			$arr[$row['vs_date']] = $row['cnt'];
		}

		for ($i=1; $i<=31; $i++){
			$k=$i;

			if($k<10) $k="0".$i;

			$date="{$f_year}-{$f_month}-{$k}";

            $count=(int)$arr[$date];

			$fr_ary['kind'][]=$k;
			$fr_ary['count'][]=$count;
		}

		$ary[fr_data]=$fr_ary;

		echo json_encode($ary);
	}else if($_POST[types]=='month'){
		$arr=array();

		$s_year=date('Y',G5_SERVER_TIME);

		$fr_date=date('Y-m-d', strtotime("{$s_year}-01-01"));
		$to_date=date('Y-m-d', strtotime("{$s_year}-12-31"));

		$sql = " select SUBSTRING(vs_date,1,7) as vs_month, SUM(vs_count) as cnt
            from {$g5['visit_sum_table']}
            where vs_date between '{$fr_date}' and '{$to_date}'
            group by vs_month
            order by vs_month desc ";
		$result = sql_query($sql);
		for ($i=0; $row=sql_fetch_array($result); $i++) {
			$arr[$row['vs_month']] = $row['cnt'];
		}
		
		for ($i=1; $i<=12; $i++){
			$k=$i;

			if($k<10) $k="0".$i;

			$date="{$s_year}-{$k}";

            $count=(int)$arr[$date];

			$to_ary['kind'][]=$k;
			$to_ary['count'][]=$count;
		}

		$ary[to_data]=$to_ary;

		$arr=array();

		$s_year=date('Y',G5_SERVER_TIME)-1;

		$fr_date=date('Y-m-d', strtotime("{$s_year}-01-01"));
		$to_date=date('Y-m-d', strtotime("{$s_year}-12-31"));

		$sql = " select SUBSTRING(vs_date,1,7) as vs_month, SUM(vs_count) as cnt
            from {$g5['visit_sum_table']}
            where vs_date between '{$fr_date}' and '{$to_date}'
            group by vs_month
            order by vs_month desc ";
		$result = sql_query($sql);
		for ($i=0; $row=sql_fetch_array($result); $i++) {
			$arr[$row['vs_month']] = $row['cnt'];
		}
		
		for ($i=1; $i<=12; $i++){
			$k=$i;

			if($k<10) $k="0".$i;

			$date="{$s_year}-{$k}";

            $count=(int)$arr[$date];

			$fr_ary['kind'][]=$k;
			$fr_ary['count'][]=$count;
		}
		
		$ary[fr_data]=$fr_ary;

		echo json_encode($ary);
	}else if($_POST[types]=='year'){
		$s_year=date('Y',G5_SERVER_TIME)-10;
		$l_year=date('Y',G5_SERVER_TIME);

		$fr_date=date('Y-m-d', strtotime("{$s_year}-01-01"));
		$to_date=date('Y-m-d', strtotime("{$l_year}-12-31"));

		$sql = " select SUBSTRING(vs_date,1,4) as vs_year, SUM(vs_count) as cnt
            from {$g5['visit_sum_table']}
            where vs_date between '{$fr_date}' and '{$to_date}'
            group by vs_year
            order by vs_year desc ";
		$result = sql_query($sql);
		for ($i=0; $row=sql_fetch_array($result); $i++) {
			$arr[$row['vs_year']] = $row['cnt'];
		}
		
		for ($i=$s_year; $i<=$l_year; $i++){
            $count=(int)$arr[$i];

			$to_ary['kind'][]=$i;
			$to_ary['count'][]=$count;
		}
		
		$ary[fr_data]="";
		$ary[to_data]=$to_ary;

		echo json_encode($ary);
	}
}else if($_POST[mode]=='browser'){
	$sql="select * from {$g5['visit_table']}";
	$result=sql_query($sql);
	
	$sum_count=0;
	while($row=sql_fetch_array($result)){
		$s=$row['vi_browser'];

		if(!$s){
			$s = get_brow($row['vi_agent']);
		}

		$arr[$s]++;

		$sum_count++;
	}

    if(count($arr)){
		arsort($arr);
		$n=0;
		foreach ($arr as $key=>$value){
			$count=$arr[$key];

			$rate=($count/$sum_count*100);
			$s_rate=number_format($rate,1);
			
			if($n>6){
				$brow_ary[title][$n]="그 외";
				$brow_ary[rate][$n]+=$count;
			}else{
				$brow_ary[title][$n]=$key;
				$brow_ary[rate][$n]=$count;

				$n ++;
			}
		}
		
		$ary[data]=$brow_ary;

		echo json_encode($ary);
	}
}else if($_POST[mode]=='new'){	
	$period_array = array(
		'오늘'=>array('시간', 0),
		'어제'=>array('시간', 0),
		'7일전'=>array('일', 7),
		'14일전'=>array('일', 14),
		'30일전'=>array('일', 30),
		'3개월전'=>array('주', 90),
		'6개월전'=>array('주', 180),
		'1년전'=>array('월', 365),
		'2년전'=>array('월', 365*2),
		'3년전'=>array('월', 365*3),
		'5년전'=>array('년', 365*5),
		'10년전'=>array('년', 365*10),
	);

	$is_period = false;
	foreach($period_array as $key=>$value) {
		if ($key == $period) {
			$is_period = true;
			break;
		}
	}
	if (!$is_period)
		$period = '오늘';

	$day = $period_array[$period][0];

	$today = date('Y-m-d', G5_SERVER_TIME);
	$yesterday = date('Y-m-d', G5_SERVER_TIME - 86400);

	if ($period == '오늘') {
		$from = $today;
		$to = $from;
	} else if ($period == '어제') {
		$from = $yesterday;
		$to = $from;
	} else if ($period == '내일') {
		$from = date('Y-m-d', G5_SERVER_TIME + (86400 * 2));
		$to = $from;
	} else {
		$from = date('Y-m-d', G5_SERVER_TIME - (86400 * $period_array[$period][1]));
		$to = $yesterday;
	}

	$sql_bo_table = '';
	if ($bo_table){
		$sql_bo_table = "and bo_table = '$bo_table'";
	}	

	$fr_ary=array("kind"=>array(),"count"=>array());
	$to_ary=array("kind"=>array(),"count"=>array());

	switch ($day) {
		case '시간' :
			$sql = " select substr(bn_datetime,6,8) as hours, sum(if(wr_id=wr_parent,1,0)) as wcount, sum(if(wr_id=wr_parent,0,1)) as ccount from {$g5['board_new_table']} where substr(bn_datetime,1,10) between '$from' and '$to' {$sql_bo_table} group by hours order by bn_datetime ";
			$result = sql_query($sql);
			for ($i=0; $row=sql_fetch_array($result); $i++) {
				// 월-일 시간
				$fr_ary['kind'][]=substr($row['hours'],0,8);
				$fr_ary['count'][]=$row['wcount'];

				$to_ary['kind'][]=substr($row['hours'],0,8);
				$to_ary['count'][]=$row['ccount'];
			}
			break;
		case '일' :
			$sql  = " select substr(bn_datetime,1,10) as days, sum(if(wr_id=wr_parent,1,0)) as wcount, sum(if(wr_id=wr_parent,0,1)) as ccount from {$g5['board_new_table']} where substr(bn_datetime,1,10) between '$from' and '$to' {$sql_bo_table} group by days order by bn_datetime ";
			$result = sql_query($sql);
			for ($i=0; $row=sql_fetch_array($result); $i++) {
				// 월-일
				$fr_ary['kind'][]=substr($row['days'],5,5);
				$fr_ary['count'][]=$row['wcount'];

				$to_ary['kind'][]=substr($row['days'],5,5);
				$to_ary['count'][]=$row['ccount'];
			}
			break;
		case '주' :
			$sql  = " select concat(substr(bn_datetime,1,4), '-', weekofyear(bn_datetime)) as weeks, sum(if(wr_id=wr_parent,1,0)) as wcount, sum(if(wr_id=wr_parent,0,1)) as ccount from {$g5['board_new_table']} where substr(bn_datetime,1,10) between '$from' and '$to' {$sql_bo_table} group by weeks order by bn_datetime ";
			$result = sql_query($sql);
			for ($i=0; $row=sql_fetch_array($result); $i++) {
				// 올해의 몇주로 보여주면 바로 확인이 안되므로 주를 날짜로 바꾼다.
				// 년-월-일
				list($lyear, $lweek) = explode('-', $row['weeks']);
				$date = date('y-m-d', strtotime($lyear.'W'.str_pad($lweek, 2, '0', STR_PAD_LEFT)));

				$fr_ary['kind'][]=$date;
				$fr_ary['count'][]=$row['wcount'];

				$to_ary['kind'][]=$date;
				$to_ary['count'][]=$row['ccount'];
			}
			break;
		case '월' :
			$sql  = " select substr(bn_datetime,1,7) as months, sum(if(wr_id=wr_parent,1,0)) as wcount, sum(if(wr_id=wr_parent,0,1)) as ccount from {$g5['board_new_table']} where substr(bn_datetime,1,10) between '$from' and '$to' {$sql_bo_table} group by months order by bn_datetime ";
			$result = sql_query($sql);
			for ($i=0; $row=sql_fetch_array($result); $i++) {
				// 년-월
				$fr_ary['kind'][]=substr($row['months'],2,5);
				$fr_ary['count'][]=$row['wcount'];

				$to_ary['kind'][]=substr($row['months'],2,5);
				$to_ary['count'][]=$row['ccount'];
			}
			break;
		case '년' :
			$sql  = " select substr(bn_datetime,1,4) as years, sum(if(wr_id=wr_parent,1,0)) as wcount, sum(if(wr_id=wr_parent,0,1)) as ccount from {$g5['board_new_table']} where substr(bn_datetime,1,10) between '$from' and '$to' {$sql_bo_table} group by years order by bn_datetime ";
			$result = sql_query($sql);
			for ($i=0; $row=sql_fetch_array($result); $i++) {
				// 년(4자리)
				$fr_ary['kind'][]=substr($row['years'],0,4);
				$fr_ary['count'][]=$row['wcount'];

				$to_ary['kind'][]=substr($row['years'],0,4);
				$to_ary['count'][]=$row['ccount'];
			}
			break;
	}

	$ary[fr_data]=$fr_ary;
	$ary[to_data]=$to_ary;

	echo json_encode($ary);
}
?>