<?php

/*
*	Copyright VeryIDE,2007-2010
*	http://www.veryide.com/
*
*	$Id: ,v1 10:39 2008-7-27 Leilei $
*/

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

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

	//module
	require("function.php");	

	//mysql open
	reader_mysql();
	

	//'***********************

	$do = GetQuery("do");
	$appid = GetQuery("appid");
	$type = GetQuery("type");
	$id  = GetQuery("id");
	$name  = GetQuery("name");
	
	if( !$do ) $do = "Y";
	
	
	//统计标识
	$appkey = $appid."-".$id;
	
	
?>

	<!--settting-->
    <div id="nav">
    	<span class="action">
		
		<?php
        if( function_exists("mcrypt_module_open") ){
			
			$appkey = $appid."-".$id;
			
			//这里很奇怪，只支持长度为4的数组，所以要注意
            $hash = array("appkey"=>$appkey,"name"=>$name,"type"=>$type,"expire"=>time());	
            
            $keys = do_mencrypt(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'.($do=='Y'?' class="active"':'').'><a href="?do=Y&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'">年份统计</a></li>
        <li'.($do=='M'?' class="active"':'').'><a href="?do=M&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'">月份统计</a></li>
        <li'.($do=='D'?' class="active"':'').'><a href="?do=D&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'">天统计</a></li>
        <li'.($do=='H'?' class="active"':'').'><a href="?do=H&appid='.$appid.'&id='.$id.'&type='.$type.'&name='.urlencode($name).'">时段统计</a></li>
    </ul>
	';
	?>
	<!--/settting-->
    
	<?php
	
	//广告名称
	if( $appkey && $do && $type ){
	
		$sql="select date,".$type." from ".VI_DBMODPRE."analytic where appkey='".$appkey."' and category='".$do."'";
		//$result=mysql_query($sql,$mysql);
		
		if( in_array($type,array("views","clicks")) ){
			$sql .=" and $type>0";
		}
		
		//echo $sql;
		
		$db -> query($sql);
		$array = $db -> fetchAll();
		
		//var_dump($array);
		
		/*
		if(mysql_num_rows($result)==1){
			$row=mysql_fetch_assoc($result);	
			$views=$row["views"];
			$clicks=$row["clicks"];
		}
		*/
	}
	
		
	//mysql close
	reader_mysql(true);
	?>
    <script language="JavaScript" src="../../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
		
		//$array=Array ( 0 => Array ( 'key' => '', 'value' => 1 ) );
		
		$active = array( "Y"=>" 年", "M"=>" 月", "D"=>" 天", "H"=>" 时" );
		
		if( in_array($type,array("views","clicks")) ){
			
			$strXML = "<graph caption='".$name.$active[$do]." 统计' numberPrefix='' formatNumberScale='0' decimalPrecision='0' >";		
			
			foreach ($array as $arSubData){
				
				//简化日期
				if( $do == "D" ){
					$arSubData["date"] =  preg_replace("/[0-9]{4}-[0-9]{2}-/", "", $arSubData["date"]);
				}
				
				$strXML .= "<set name='" . $arSubData["date"] .$active[$do]. "' value='" . $arSubData[$type] . "' colo='" .getFCColor() . "' />";
			}
			
			$strXML .= "</graph>";
			
		}else{
			
			$strXML = "<graph xaxisname='类别' 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 $arSubData){
					
					//简化日期
					if( $do == "D" ){
						$arSubData["date"] =  preg_replace("/[0-9]{4}-[0-9]{2}-/", "", $arSubData["date"]);
					}
					
					$strXML .= "<category name='" . $arSubData["date"] . "' />";
				}				
				$strXML .= "</categories>";				
				
				$strXML .= "<dataset seriesname='展示' color='" .getFCColor() . "'>";				
				foreach ($array as $arSubData){
					$strXML .= "<set value='" . $arSubData["views"] . "' />";
				}				
				$strXML .= "</dataset>";
				
				$strXML .= "<dataset seriesname='点击' color='" .getFCColor() . "'>";				
				foreach ($array as $arSubData){
					$strXML .= "<set value='" . $arSubData["clicks"] . "' />";
				}				
				$strXML .= "</dataset>";

			$strXML .= "</graph>";
			
		}
		
		switch($do){
			
			case "Y":
			
				/*
				$strXML = "<graph caption='".$name." 年统计' numberPrefix='' formatNumberScale='0' decimalPrecision='0' >";		
				$strXML .= $strSET;				
				$strXML .= "</graph>";
				
				*/				
				
				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":
			
				/*
				$strXML = "<graph caption='".$name." 月统计' numberPrefix='' formatNumberScale='0' decimalPrecision='0' >";
				foreach ($array as $arSubData){
					$strXML .= "<set name='" . $arSubData["date"] . " 月' value='" . $arSubData["views"] . "' colo='" .getFCColor() . "' />";
				}
				$strXML .= "</graph>";
				
				echo renderChart("source/fusionchart/Charts/FCF_Column2D.swf", "", $strXML, "productSales", 800, 350);
				*/
				
				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);					
				}
			
				/*
				$strXML = "<graph caption='".$name." 时段统计' numberPrefix='' formatNumberScale='0' decimalPrecision='0' >";
				foreach ($array as $arSubData){
					$strXML .= "<set name='" . $arSubData["date"] . " 时' value='" . $arSubData["views"] . "' colo='" .getFCColor() . "' />";
				}
				$strXML .= "</graph>";
				
				echo renderChart("source/fusionchart/Charts/FCF_Line.swf", "", $strXML, "productSales", 800, 350);
				*/
			break;
			
			/*
			
			//month
			case "";

				$sql="SELECT CONCAT(s_year,'年', s_month,'月') as 'key',count(s_id) as 'value' FROM ".VI_DBMODPRE."ads_stat where a_id=".$a_id." group by s_month ";

				$db -> query($sql);
				$array = $db -> fetchAll();
				
				$i=0;
				
				$series = '';
				$graph1 = '';
				
				foreach ($array as $arr) {
					$series .= '<value xid="'.$i.'">'.$arr['key'].'</value>';
					$graph1 .= '<value xid="'.$i.'">'.($arr['value']*$Modules["adsview"]["multiple"]).'</value>';
					
					$i++;
				}			
				
				$xml='
				<?xml version="1.0" encoding="UTF-8"?>
				<chart>
					<series>
						'.$series.'
					</series>
					<graphs>
						<graph gid="1">
							'.$graph1.'
						</graph>
						<graph gid="2">
							'.$graph2.'
						</graph>
					</graphs>
					<guides>	                                   
					 <guide>                                     
					   <start_value></start_value>               
					   <title></title>                           
					   <color>#00CC00</color>                             
					   <inside>true</inside>                         
					 </guide>  
					</guides>	
				</chart>';
				
				//转编码
				if($Project["charset"]=="gbk" && is_gb2312($xml)){
					$xml = iconv("gbk","utf-8",$xml);
				}
				
				$cache = 'cache/xml/'.$a_id.'.month.xml';
				
				create_file($cache,$xml);
				
				echo '<script type="text/javascript">Mo.Flash("src","source/amcolumn/amcolumn?settings_file=source/amcolumn/amcolumn_settings.xml&data_file='.$cache.'","width","100%","height",400);</script>';
				 
			break;
			
			case "day";
				
				$sql="SELECT CONCAT(s_month,'月', s_day,'日') as 'key',count(s_id) as 'value' FROM ".VI_DBMODPRE."ads_stat where a_id=".$a_id." group by s_month,s_day ";

				$db -> query($sql);
				
				if($db -> getNumRows()){
					$array = $db -> fetchAll();
				}
				
				$i=0;
				
				$series = '';
				$graph1 = '';
				
				foreach ($array as $arr) {
					$series .= '<value xid="'.$i.'">'.$arr['key'].'</value>';
					$graph1 .= '<value xid="'.$i.'">'.($arr['value']*$Modules["adsview"]["multiple"]).'</value>';
					
					$i++;
				}			
				
				$xml='
				<?xml version="1.0" encoding="UTF-8"?>
				<chart>
					<series>
						'.$series.'
					</series>
					<graphs>
						<graph gid="1">
							'.$graph1.'
						</graph>
						<graph gid="2">
							'.$graph2.'
						</graph>
					</graphs>
					<guides>	                                   
					 <guide>                                     
					   <start_value></start_value>               
					   <title></title>                           
					   <color>#00CC00</color>                             
					   <inside>true</inside>                         
					 </guide>  
					</guides>	
				</chart>';
				
				//转编码
				if($Project["charset"]=="gbk" && is_gb2312($xml)){
					$xml = iconv("gbk","utf-8",$xml);
				}
				
				$cache = 'cache/xml/'.$a_id.'.day.xml';
				
				create_file($cache,$xml);
				
				echo '<script type="text/javascript">Mo.Flash("src","source/amline/amline?settings_file=source/amline/amline_settings.xml&data_file='.$cache.'","width","100%","height",400);</script>';
				 
			break;
			
			case "time";

				$sql="SELECT CONCAT(s_hour,' 时') as 'key',count(s_id) as 'value' FROM ".VI_DBMODPRE."ads_stat where a_id=".$a_id." group by s_hour ";

				$db -> query($sql);
				
				if($db -> getNumRows()){
					$array = $db -> fetchAll();
				}

				$i=0;
				
				$series = '';
				$graph1 = '';
				
				foreach ($array as $arr) {
					$series .= '<value xid="'.$i.'">'.$arr['key'].'</value>';
					$graph1 .= '<value xid="'.$i.'">'.($arr['value']*$Modules["adsview"]["multiple"]).'</value>';
					
					$i++;
				}			
				
				$xml='
				<?xml version="1.0" encoding="UTF-8"?>
				<chart>
					<series>
						'.$series.'
					</series>
					<graphs>
						<graph gid="1">
							'.$graph1.'
						</graph>
						<graph gid="2">
							'.$graph2.'
						</graph>
					</graphs>
					<guides>	                                   
					 <guide>                                     
					   <start_value></start_value>               
					   <title></title>                           
					   <color>#00CC00</color>                             
					   <inside>true</inside>                         
					 </guide>  
					</guides>	
				</chart>';
				
				//转编码
				if($Project["charset"]=="gbk" && is_gb2312($xml)){
					$xml = iconv("gbk","utf-8",$xml);
				}
				
				$cache = 'cache/xml/'.$a_id.'.time.xml';
				
				create_file($cache,$xml);
				
				echo '<script type="text/javascript">Mo.Flash("src","source/amline/amline?settings_file=source/amline/amline_settings.xml&data_file='.$cache.'","width","100%","height",400);</script>';
				 
			break;
		
			
			case "keyword";

				$sql="SELECT s_keyword as 'key',count(s_id) as 'value' FROM ".VI_DBMODPRE."ads_stat where a_id=".$a_id." group by s_keyword order by value desc limit 0,10";

				$db -> query($sql);
				$array = $db -> fetchAll();
				
				//重写数组
				$newarr=Array ( 0 => Array ( 'key' => '', 'value' => 1 ) );
				
				$i=0;
				foreach ($array as $arr) {
					
					$newarr[$i]=Array();
					if(!$arr['key']){
						$newarr[$i]['key']='直接访问';
					}else{
						$newarr[$i]['key']=$arr['key'];
					}
					$newarr[$i]['value']=$arr['value'];
					
					$i++;
				}			

				$i=0;
				
				$series = '';
				$graph1 = '';
				
				foreach ($newarr as $arr) {
					$slice .= '<value xid="'.$i.'">'.$arr['key'].'</value><slice title="'.$arr['key'].'" pull_out="true">'.($arr['value']*$Modules["adsview"]["multiple"]).'</slice>';
					
					$i++;
				}			
				
				$xml='
				<?xml version="1.0" encoding="UTF-8"?>
				<pie>
				  '.$slice.'
				</pie>
				';
				
				//转编码
				if($Project["charset"]=="gbk" && is_gb2312($xml)){
					$xml = iconv("gbk","utf-8",$xml);
				}
				
				$cache = 'cache/xml/'.$a_id.'.keyword.xml';
				
				create_file($cache,$xml);
				
				echo '<script type="text/javascript">Mo.Flash("src","source/ampie/ampie?settings_file=source/ampie/ampie_settings.xml&data_file='.$cache.'","width","100%","height",400);</script>';
				 
			break;
		
			
			case "search";
			

				$sql="SELECT s_search as 'key',count(s_id) as 'value' FROM ".VI_DBMODPRE."ads_stat where a_id=".$a_id." group by s_search order by value desc limit 0,14";

				$db -> query($sql);
				$array = $db -> fetchAll();
				
				//重写数组
				$newarr=Array ( 0 => Array ( 'key' => '', 'value' => 1 ) );
				
				$i=0;
				foreach ($array as $arr) {
					
					$newarr[$i]=Array();
					if(!$arr['key']){
						$newarr[$i]['key']='直接访问';
					}else{
						$newarr[$i]['key']=$arr['key'];
					}
					$newarr[$i]['value']=$arr['value'];
					
					$i++;
				}			
				
				$i=0;
				
				$series = '';
				$graph1 = '';
				
				foreach ($newarr as $arr) {
					$slice .= '<value xid="'.$i.'">'.$arr['key'].'</value><slice title="'.$arr['key'].'" pull_out="true">'.($arr['value']*$Modules["adsview"]["multiple"]).'</slice>';
					
					$i++;
				}			
				
				$xml='
				<?xml version="1.0" encoding="UTF-8"?>
				<pie>
				  '.$slice.'
				</pie>
				';
				
				//转编码
				if($Project["charset"]=="gbk" && is_gb2312($xml)){
					$xml = iconv("gbk","utf-8",$xml);
				}
				
				$cache = 'cache/xml/'.$a_id.'.search.xml';
				
				create_file($cache,$xml);
				
				echo '<script type="text/javascript">Mo.Flash("src","source/ampie/ampie?settings_file=source/ampie/ampie_settings.xml&data_file='.$cache.'","width","100%","height",400);</script>';
				
			break;
			
			case "variable";
			

				$sql="SELECT s_variable as 'key',count(s_id) as 'value' FROM ".VI_DBMODPRE."ads_stat where a_id=".$a_id." group by s_variable order by value desc limit 0,14";

				$db -> query($sql);
				$array = $db -> fetchAll();
				
				//重写数组
				$newarr=Array ( 0 => Array ( 'key' => '', 'value' => 1 ) );
				
				$i=0;
				foreach ($array as $arr) {
					
					$newarr[$i]=Array();
					if(!$arr['key']){
						$newarr[$i]['key']='直接访问';
					}else{
						$newarr[$i]['key']=$arr['key'];
					}
					$newarr[$i]['value']=$arr['value'];
					
					$i++;
				}			
				
				$i=0;
				
				$series = '';
				$graph1 = '';
				
				foreach ($newarr as $arr) {
					$slice .= '<value xid="'.$i.'">'.$arr['key'].'</value><slice title="'.$arr['key'].'" pull_out="true">'.($arr['value']*$Modules["adsview"]["multiple"]).'</slice>';
					
					$i++;
				}			
				
				$xml='
				<?xml version="1.0" encoding="UTF-8"?>
				<pie>
				  '.$slice.'
				</pie>
				';
				
				//转编码
				if($Project["charset"]=="gbk" && is_gb2312($xml)){
					$xml = iconv("gbk","utf-8",$xml);
				}
				
				$cache = 'cache/xml/'.$a_id.'.variable.xml';
				
				create_file($cache,$xml);
				
				echo '<script type="text/javascript">Mo.Flash("src","source/ampie/ampie?settings_file=source/ampie/ampie_settings.xml&data_file='.$cache.'","width","100%","height",400);</script>';
				
			break;
			
			case "week";
			

				$sql="SELECT DATE_FORMAT(from_unixtime(s_date,'%Y%m%d'),'%w') as 'key',count(s_id) as 'value' FROM ".VI_DBMODPRE."ads_stat where a_id=".$a_id." group by DATE_FORMAT(from_unixtime(s_date,'%Y%m%d'),'%w')";
				
				$db -> query($sql);
				$array = $db -> fetchAll();
				
				//重写数组
				$weeks=array("周日","周一","周二","周三","周四","周五","周六");   

				$i=0;
				
				$series = '';
				$graph1 = '';
				
				foreach ($array as $arr) {
					$slice .= '<value xid="'.$i.'">'.$arr['key'].'</value><slice title="'.$weeks[$arr['key']].'" pull_out="true">'.($arr['value']*$Modules["adsview"]["multiple"]).'</slice>';
					
					$i++;
				}			
				
				$xml='
				<?xml version="1.0" encoding="UTF-8"?>
				<pie>
				  '.$slice.'
				</pie>
				';
				
				//转编码
				if($Project["charset"]=="gbk" && is_gb2312($xml)){
					$xml = iconv("gbk","utf-8",$xml);
				}
				
				$cache = 'cache/xml/'.$a_id.'.search.xml';
				
				create_file($cache,$xml);
				
				echo '<script type="text/javascript">Mo.Flash("src","source/ampie/ampie?settings_file=source/ampie/ampie_settings.xml&data_file='.$cache.'","width","100%","height",400);</script>';
				
			break;
		
			
			case "address";
			
				
				
			break;
			
			*/
		
		}
	?>
	</div>
<!--body end-->

<?php html_close();?>