<?php

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

	//载入全局配置和函数包
	require '../../include/global.php';
	require '../../include/cacher.php';
	
	//加载下载函数
	require("../../function/client.php");
	
	//检查登录状态
	System :: check_login();
			
	//检查权限
	System :: check_func("mod-adsview-dow", false );				

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

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

	$q = getgpc('q');
	$s = getgpc("s");

	$filter = getgpc("filter");
	$parent = getgpc("parent");
	$type = getgpc("type");
	
	$action = getgpc('action');
	
	$list = getgpc('list');
	
	$full = getnum("full",-1);
	$owner = getnum("owner",0);
	
	//日期筛选
	$start = getgpc('start');
	$end = getgpc('end');

	//时间
	if ($start && $end){
	
		//开始时间
		$e_array=explode("-",$start);
		$time1=mktime(0, 0, 0, $e_array[1], $e_array[2], $e_array[0]);
		
		//到期时间
		$e_array=explode("-",$end);
		$time2=mktime(23, 59, 59, $e_array[1], $e_array[2], $e_array[0]);
		
		//$ect.=" and  start >=".$time1." and expire <= ".$time2;
	}else{
		
		//
		//$start = $end = date("Y-m-d");
		
		//今日
		//$time1 = $time2 = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
	
	}

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

    $sql="SELECT * FROM `mod:adsview` WHERE 1=1";
    
    if ($q && $s){
        
        if( strpos($s,"id") !== false ){
            $sql.=" and  `".$s."` = '".$q."'";
        }else{
            $sql.=" and `".$s."` like '%".$q."%'";
        }     
        
    }
    
    if ($parent){
        $sql.=" and  parent ='".$parent."'";
    }
    
    if ($type){
        $sql.=" and  type ='".$type."'";
    }
    
    if ($owner){
        $sql.=" and  owner ='".$owner."'";
    }
    
    //广告筛选
    switch($filter){
        
        //新增广告
        case "dateline":
			
			if( $time1 && $time2 ){			
				$sql.=" and  dateline >=".$time1." and dateline <= ".$time2;			
			}
			
        break;
        
        //修改广告
        case "modify":
			
			if( $time1 && $time2 ){			
				$sql.=" and  modify >=".$time1." and modify <= ".$time2;			
			}
			
        break;
        
        //全部类型
        default:
			
			if( $time1 && $time2 ){			
				$sql.=" and  dateline >=".$time1." and dateline <= ".$time2." or ( modify >=".$time1." and modify <= ".$time2.")";			
			}
			
        break;
		
    }
    
    //if ($ect) $sql.= $ect;
    
    $sql.=" order BY id desc";
    
    $result = System :: $db -> getAll( $sql );

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

	//构造表头
	$report ='ID';
	$report.='	所在位置';
	$report.='	广告名称';
	$report.='	开始时间';
	$report.='	失效时间';
	$report.='	共投放';
	$report.='	停止时间';
	$report.='	添加时间';
	$report.='	修改时间';
	$report.='	所有者';
	$report.='	状态'.chr(13);

	/************************/
	
	//加载分类缓存
	Cached :: loader($appid,"table.adsarea");

	//今天
	$today = date("Y-m-d"); 
	 
	foreach( $result as $row ){
		
		//到期倒记时
		$days=DateDiff("d",mktime(0, 0, 0, date("m"), date("j"), date("Y")),$row['expire']);
		
		$report.=$row['id'];
		
		$report.='	'.$_CACHE['adsview']['adsarea'][$row["parent"]]["name"].' > '.$_CACHE['adsview']['adsarea'][$row["type"]]["name"];
		
		$report.='	'.$row['name'];
		
		$report.='	'.date("y-m-d",$row['start']);
		
		$report.='	'.date("y-m-d",$row['expire']);
		
		$report.='	'.FormatDateDiff($row['start'],$row['expire']);
		
		$report.='	'.( $row["end"] ? date("y-m-d H:i:s",$row["end"]) : '' );
		
		$report.='	'.date("y-m-d H:i:s",$row['dateline']);
		
		$report.='	'.( $row['modify'] ? date("y-m-d H:i:s",$row['modify']) : '' );
		
		$report.='	'.$row['account'];
		
		//广告状态
		if( $row['expire']>time() ){
			$report.='	'.strip_tags($_G['module']['adsview']["state"][$row['state']]);
		}else{
			$report.='	'.strip_tags($_G['module']['adsview']["state"][4]);
		}

		$report.= chr(13);
		
	}
	
	//关闭数据库
	System :: connect();
	
	report( "广告系统报告" , $report );
	
		
?>