<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	2011/6/5 Lay
*/

require '../../include/loader.php';
html_start("广告分析 - VeryIDE");
?>

<!--body start-->
<?php

	//module
	require 'function.php';	

	//连接数据库
	System :: connect();

	//***********************

	$do = getgpc('do');
	$appid = getgpc('appid');
	$type = getgpc("type");
	$id  = getgpc("id");
	$name  = getgpc('name');
	$multiple  = getnum("multiple",1);
	
	if( !$do ) $do = "Y";	
	
	//统计标识
	$appkey = $appid."-".$id;

?>

	<script>
	function doChange( ele ){		
		ele.innerHTML = ( ele.innerHTML == '图表展示' ) ? '详细数据' : '图表展示';
		Mo('#bind_data,#bind_chart').toggle();		
	}	
	</script>

	<!--settting-->
    <div class="item">
    	<span class="y">
		
		<?php
        if( function_exists("mcrypt_module_open") ){
			
			$appkey = $appid."-".$id;
			
			//这里很奇怪，只支持长度为4的数组，所以要注意
            $hash = array("appkey"=>$appkey,"name"=>$name,"type"=>$type, 'multiple'=> $multiple, "expire"=>time());	
            
            $keys = authcrypt( serialize($hash), VI_START );
        ?>
			<a href="analytic.share.php?keys=<?php echo rawurlencode($keys);?>" target="_blank">分享统计&raquo;</a>
        <?php
        }else{
		?>
			<a href="http://www.veryide.com/archive.php?id=45" target="_blank">启用 mcrypt 模块，使用分享功能</a>
        <?php
        
        }
        ?>
        </span>
        <strong><?php echo $name;?> 统计分析</strong> 	
    </div>
	<?php
	
	echo '
    <ul id="naver">
        <li class="menu"><a href="javascript:;" onclick="doChange( this );">图表展示</a></li>
        <li'.($do=='Y'?' class="active"':'').'><a href="?do=Y&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'&multiple='.$multiple.'">年份统计</a></li>
        <li'.($do=='M'?' class="active"':'').'><a href="?do=M&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'&multiple='.$multiple.'">月份统计</a></li>
        <li'.($do=='D'?' class="active"':'').'><a href="?do=D&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'&multiple='.$multiple.'">每日统计</a></li>
        <li'.($do=='H'?' class="active"':'').'><a href="?do=H&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'&multiple='.$multiple.'">时段统计</a></li>
    </ul>
	';
	?>
	<!--/settting-->
    
	<?php
	
	//广告名称
	if( $appkey && $do && $type ){
	
		$sql="SELECT date,".$type." from `mod:analytic` WHERE appkey='".$appkey."' and category='".$do."'";
		
		if( in_array($type,array("views","clicks")) ){
			$sql .=" and $type>0";
		}
		
		$sql .=" ORDER BY date asc";
		
		System :: $db -> query($sql);
		$array = System :: $db -> fetchAll();
	}
	
		
        //关闭数据库
        System :: connect();
	?>
    <script language="JavaScript" src="<?php echo VI_BASE;?>source/fusionchart/JSClass/FusionCharts.js"></script>
    
	<?php
    include(VI_ROOT."source/fusionchart/Includes/FusionCharts.php");
    include(VI_ROOT."source/fusionchart/Includes/FC_Colors.php");
    ?>

	<div id="box">
		<?php
		
		$active = array( "Y"=>" 年份", "M"=>" 月份", "D"=>" 每日", "H"=>" 时段" );
		
		///////////////////////////////
		
		echo '<table id="bind_data" class="table" width="100%" border="0" cellpadding="1" cellspacing="1">';
		echo '<tr class="title"><td>'.$active[$do].'</td>'.( $do == 'D' ? '<td>星期</td>' : '' ).'<td>展示</td><td>点击</td><td>点击率</td></tr>';
		
		foreach ( $array as $val ){
			
			echo '<tr class="'.zebra( $i, array( "line" , "band" ) ).'"><td class="text-bold">' .$val["date"]. '</td>'.( $do == 'D' ? '<td>'.$_G['project']['weeks'][date("w",strtotime($val["date"]))].'</td>' : '' ).'<td class="text-yes text-bold">' . $val['views']*$multiple . '</td><td class="text-no text-bold">' . $val['clicks']*$multiple . '</td><td class="text-key text-bold">'.( $val['views'] ? round( $val['clicks'] / $val['views'] * 100, 2 ) . '%' : '' ).'</td></tr>';
			
		}
		
		if( count( $array ) == 0 ){
			echo '<tr><td colspan="5" class="notice">没有相关统计数据</td></tr>';
		}
		
		echo '</table>';
		
		///////////////////////////////
		
		echo '<div id="bind_chart" style="display:none;">';
		
		if( in_array($type,array("views","clicks")) ){
			
			$strXML = "<graph baseFontSize='12' caption='".$name.$active[$do]." 统计' numberPrefix='' formatNumberScale='0' decimalPrecision='0' >";		
			
			foreach ($array as $val){
				
				//简化日期
				if( $do == "D" ){
					$val["date"] =  preg_replace("/[0-9]{4}-[0-9]{2}-/", "", $val["date"]);
				}
				
				$strXML .= "<set name='" . $val["date"] .$active[$do]. "' value='" . $val[$type]*$multiple . "' colo='" .getFCColor() . "' />";
			}
			
			$strXML .= "</graph>";
			
		}else{
			
			$strXML = "<graph xaxisname='类别' baseFontSize='12' yaxisname='Export' hovercapbg='DEDEBE' hovercapborder='889E6D' rotateNames='0' yAxisMaxValue='100' numdivlines='9' divLineColor='CCCCCC' divLineAlpha='80' decimalPrecision='0' showAlternateHGridColor='1' AlternateHGridAlpha='30' AlternateHGridColor='CCCCCC' caption='".$name.$active[$do]." 统计' subcaption='展示与点击效果报表'>";
			
				$strXML .= "<categories font='Arial' fontSize='11' fontColor='000000'>";
				foreach ($array as $val){
					
					//简化日期
					if( $do == "D" ){
						$val["date"] =  preg_replace("/[0-9]{4}-[0-9]{2}-/", "", $val["date"]);
					}
					
					$strXML .= "<category name='" . $val["date"] . "' />";
				}				
				$strXML .= "</categories>";				
				
				$strXML .= "<dataset seriesname='展示' color='" .getFCColor() . "'>";				
				foreach ($array as $val){
					$strXML .= "<set value='" . $val["views"]*$multiple . "' />";
				}				
				$strXML .= "</dataset>";
				
				$strXML .= "<dataset seriesname='点击' color='" .getFCColor() . "'>";				
				foreach ($array as $val){
					$strXML .= "<set value='" . $val["clicks"]*$multiple . "' />";
				}				
				$strXML .= "</dataset>";

			$strXML .= "</graph>";
			
		}
		
		switch($do){
			
			case "Y":				
				if( in_array($type,array("views","clicks")) ){
					//swf / xml / data / id / width / height
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_Column2D.swf", "", $strXML, "productSales", 800, 350);
				}else{
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_MSColumn2D.swf", "", $strXML, "productSales", 800, 350);					
				}
			
			break;
			
			case "M":
				if( in_array($type,array("views","clicks")) ){
					//swf / xml / data / id / width / height
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_Column2D.swf", "", $strXML, "productSales", 800, 350);
				}else{
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_MSColumn2D.swf", "", $strXML, "productSales", 800, 350);					
				}			
			break;
			
			case "D":				
				if( in_array($type,array("views","clicks")) ){
					//swf / xml / data / id / width / height
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_Column2D.swf", "", $strXML, "productSales", 800, 350);
				}else{
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_MSColumn2D.swf", "", $strXML, "productSales", 800, 350);					
				}			
			break;
			
			case "H":
				
				if( in_array($type,array("views","clicks")) ){
					//swf / xml / data / id / width / height
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_Line.swf", "", $strXML, "productSales", 800, 350);
				}else{
					echo renderChart(VI_BASE."source/fusionchart/Charts/FCF_MSLine.swf", "", $strXML, "productSales", 800, 350);					
				}
			break;			
		
		}
		
		echo '</div>';
	?>
		
	<?php
    if( $_G['licence']['type'] == 'full' ){
    ?>
    
	<form action="<?php echo GetCurUrl();?>" method="post" verify="true">
	
	<table width="100%" cellpadding="0" cellspacing="0" class="form">
	
		<td colspan="2" class="section pointer" onclick=" Mo('#box-expand').toggle( 0, function( ele, vis ){ if( vis ){ Mo('#sel-expand').attr({ 'class' : 'open' }); }else{ Mo('#sel-share').attr({ 'class' : 'close' }); } } ); "><a id="sel-expand" class="close"></a><strong>高级选项</strong></td>
	    
		<tr id="box-expand" style="display:<?php echo $multiple>1 ? '' : 'none';?>;">
		<td width="60"><strong>倍数关系：</strong></td>
		<td>
			<p>
				<input name="multiple" value="<?php echo $multiple;?>" />
				<button class="go" type="submit"></button>
				注意: 此处为数据测试方法，仅供参考。
			</p>
		</td>
	    </tr>
	
	</table>
	
	</form>
    
	<?php
    }
    ?>
	
	</div>
<!--body end-->

<?php html_close();?>