<?php

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

require("../../include/header.php");

html_start("广告位列表 - VeryIDE");
?>

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

	//module
	require("naver.php");
	
	loader_cache($appid,"table.adsarea");
	
	//mysql open
	reader_mysql();

	//***********************
	$sel = GetQuery("sel");
	
	$q = GetQuery("q");
	$s = GetQuery("s");
	$e = GetQuery("e");
	if(!$e){
		$e="none";
	}
	$parent = GetQuery("parent");
	
	$action = GetQuery("action");
	
	$list = GetQuery("list");
	
	if ($action){
		switch($action){
			case "all":
				ad_create("SELECT * FROM ".VI_DBMODPRE."adsarea where parent>0 order by id",$mysql);
				$Project["message"]="已更新所有文件！";
			break;
			
			case "create":
				ad_create("SELECT * FROM ".VI_DBMODPRE."adsarea where id in(".$list.")",$mysql);
				$Project["message"]="已生成所选广告！";
			break;
			
			case "delete":
				
				//检查权限
				checker_func("mod-adsarea-del");
				checker_func("mod-adsview-del");
				
				//直接删除不经过回收站
				if( $Group["sys-system-recycle"]=="Y" ){
					
					ad_delete("SELECT * FROM ".VI_DBMODPRE."adsarea where id in(".$list.")",$mysql);
				
					//清空统计
					require("../analytic/function.php");
					clear($appid,$list);				
					
				}else{
				
					$sql="update ".VI_DBMODPRE."adsarea set status=-1 where id in(".$list.")";
					mysql_query($sql);				
					
				}				
				
				$Project["message"]="成功删除广告分类！";
				
			break;
			
			case "cache":

			break;
		}
		
		//更新分类数组缓存
		cache_table($appid,$mysql,VI_DBMODPRE."adsarea",'<option value=\'{table:id}\'>{table:name}</option>');		
		
		//更新分类脚本缓存
		cache_script($appid,$mysql,"SELECT id,parent,name,`show`,width,height,number FROM ".VI_DBMODPRE."adsarea where parent>0","ADSAREA","mod.adsarea",'	#var[{table:id}] = [{table:id},{table:parent},"{table:name}",{table:show},"{table:width}×{table:height}",{table:number}];'.chr(13));
		
		//转到
		if( $jump ){
			reader_redirect($jump,"成功能更新缓存!");
		}
	}
?>

	
    <!--div id="board">
        <p><b>排序显示说明</b>  
        <br>以下列表按 <b class="text-key">频道</b> 进行分类并（依据添加先后进行倒序，ID从大到小）排序显示管理 
        </p>
    </div-->
	
    <div id="search">
        <!--用于动作处理_开始-->
		<form name="formPost" id="formPost" method="post" action="?">
		   <input name="list" id="list" type="hidden" value="">
			<input name="action" id="action" type="hidden" value="" />
			<input name="url" id="url" type="hidden" value="" />
        </form>
         <!--用于动作处理_结束-->
        
	    <form name="formSeacrh" id="formSeacrh" method="get" action="">
            <span class="action">
                <select name="sel" id="sel" onChange="Mo('#formSeacrh').submit();" ignore="true">
                    <optgroup label="筛选状态.." />
                    <option value="">全部位置</option>
                    <option value="free">空闲位置</option>
                    <option value="full">已满位置</option>
                    <option value="space">零广告位置</option>
                    <option value="zero">零预定位置</option>
                    <option value="engage">被预定位置</option>
                    <option value="expire">快到期位置</option>
                </select>
                
                <select name="parent" id="parent" onChange="Mo('#formSeacrh').submit();" ignore="true">
                    <optgroup label="筛选分类.." />
                    <option value="0">全部分类..</option>
                    <?php
                        foreach($VICACHE["mod.adsarea"] as $key){
                            if($key["parent"]=="0"){
                                echo '<option value="'.$key["id"].'">'.$key["name"].'</option>';
                            }
                        }
                    ?>
                </select>
                
              <input type="button" value="更新缓存" onclick="if(confirm('确定现在更新广告缓存吗？')){location.href='?action=all';}" class='button'>
            </span>
            
            <select name="s" id="s">
                <option value="key">位置标识</option>
                <option value="name" checked>广告位</option>
                <option value="account">添加人</option>
                <option value="link">所在页面</option>
                <option value="show">广告类型</option>
            </select>
            <input name="q" type="text" class="text" id="q" value="<?php echo $q;?>">
            <button class="go" type="submit"></button>
	    </form>
        <script type="text/javascript">
			Mo("#s").value("<?php echo $s;?>");
			Mo("#sel").value("<?php echo $sel;?>");
			Mo("#parent").value("<?php echo $parent;?>");
        </script>
	</div>
    
    
    <!--用于动作处理_开始-->
    <form name="formPost" id="formPost" method="post">
        <input name="action" id="action" type="hidden" value="" />        
        <input name="status" id="status" type="hidden" value="" />
        
        <input name="jump" type="hidden" value="" />
        <script>Mo('#formPost input[name=jump]').value( location.href );</script>
    
	<?php
    
    $sql="SELECT * FROM ".VI_DBMODPRE."adsarea where 1=1 ";

    if ($q && $s){
        
        if( strpos($s,"id") !== false ){
            $sql.=" and  `".$s."` = ".$q;
        }else{
            $sql.=" and  `".$s."` like '%".$q."%'";
        }     
        
    }

    //显示回收站
    if( $Group["sys-system-recycle"]!="Y" ) $sql.=" and status > -1 ";    
    
    if ($parent){
        $sql.=" and  parent ='".$parent."'";
    }else{
        $sql.=" and  parent >0";
    }
    
    $sql.=" {$sel} order BY parent desc";
    
    //广告筛选
    switch($sel){
        //全部
        case "":
        
        break;
        
        //空闲
        case "free":
            $sql = str_replace("{$sel}"," and (".VI_DBMODPRE."adsarea.number-(SELECT count(".VI_DBMODPRE."adsview.id) FROM ".VI_DBMODPRE."adsview where ".VI_DBMODPRE."adsview.status>0 and ".VI_DBMODPRE."adsview.type=".VI_DBMODPRE."adsarea.id and ".VI_DBMODPRE."adsview.expire>".time()."))>0",$sql);
        break;
        
        //被预定
        case "engage":
            $sql = str_replace("{$sel}"," and ((SELECT count(".VI_DBMODPRE."adsview.id) FROM ".VI_DBMODPRE."adsview where ".VI_DBMODPRE."adsview.status=3 and ".VI_DBMODPRE."adsview.type=".VI_DBMODPRE."adsarea.id and ".VI_DBMODPRE."adsview.expire>".time()." order by ".VI_DBMODPRE."adsview.sort asc,".VI_DBMODPRE."adsview.id desc))>0",$sql);
        break;
        
        //空广告位
        case "space":
            $sql = str_replace("{$sel}"," and ((SELECT count(".VI_DBMODPRE."adsview.id) FROM ".VI_DBMODPRE."adsview where ".VI_DBMODPRE."adsview.status>0 and ".VI_DBMODPRE."adsview.type=".VI_DBMODPRE."adsarea.id and ".VI_DBMODPRE."adsview.expire>".time()." order by ".VI_DBMODPRE."adsview.sort asc,".VI_DBMODPRE."adsview.id desc)=0)",$sql);
        break;
        
        //零预定
        case "zero":
            $sql = str_replace("{$sel}"," and ((SELECT count(".VI_DBMODPRE."adsview.id) FROM ".VI_DBMODPRE."adsview where ".VI_DBMODPRE."adsview.status=3 and ".VI_DBMODPRE."adsview.type=".VI_DBMODPRE."adsarea.id and ".VI_DBMODPRE."adsview.expire>".time()." order by ".VI_DBMODPRE."adsview.sort asc,".VI_DBMODPRE."adsview.id desc)=0)",$sql);
        break;
        
        //已满
        case "full":
            $sql = str_replace("{$sel}"," and (".VI_DBMODPRE."adsarea.number-(SELECT count(".VI_DBMODPRE."adsview.id) FROM ".VI_DBMODPRE."adsview where ".VI_DBMODPRE."adsview.status>0 and ".VI_DBMODPRE."adsview.type=".VI_DBMODPRE."adsarea.id and ".VI_DBMODPRE."adsview.expire>".time()." order by ".VI_DBMODPRE."adsview.sort asc,".VI_DBMODPRE."adsview.id desc))=0",$sql);
        break;
        
        //快到期
        case "expire":
            $sql = str_replace("{$sel}"," and ((SELECT count(".VI_DBMODPRE."adsview.id) FROM ".VI_DBMODPRE."adsview where ".VI_DBMODPRE."adsview.status>0 and ".VI_DBMODPRE."adsview.type=".VI_DBMODPRE."adsarea.id and status>0 and spare=0 and ".VI_DBMODPRE."adsview.expire<". (time() + (3600*24*$Setting[$appid]["expire"]) ) ." and ".VI_DBMODPRE."adsview.expire>".mktime()." order by ".VI_DBMODPRE."adsview.sort asc,".VI_DBMODPRE."adsview.id desc))>0",$sql);
        break;
    }
    
    //echo $sql;

    //查询数据库_总记录数
    $row_count = $db -> getValue( str_replace("*","count(id)",$sql) );   

    
    $Project["queries"]++;

    //分页参数
    $page=GetPage("page");
    $page_start=$Global["pagesize"]*($page-1);
    $sql=$sql." limit $page_start,".$Global["pagesize"];
    
    //echo $sql;
    
    
    //分页链接
    $url="?s=".$s."&q=$q&e=$e&parent=$parent&id=$id&sel=$sel&page=";	


    ?>
        <table id="table" width="100%" border="0" cellpadding="1" cellspacing="1" class="table">
          <tr class="title" id="thead">
            <td width="30"></td>
            <td>预览</td>
            <td>所在频道</td>
            <td>广告位名称</td>
            <td>标识<script type="text/javascript">Mo.Tools("Tip","每个频道或广告位的唯一标识");</script></td>
            <td>价格</td>
            <td>最多</td>
            <td>现有</td>
            <td>空闲</td>
            <td>快到期</td>
            <td>已预定</td>
            <td>尺寸</td>
            <td>类型</td>
            <td>扩展</td>
            <td width="50">操作</td>
          </tr>
        <?php
        
        //MySQL
        $result=mysql_query($sql,$mysql);
        
        $Project["queries"]++;
        
        $i=0;
        
        while($row=mysql_fetch_assoc($result)){
            
            //已有广告数
            $sql="SELECT * FROM ".VI_DBMODPRE."adsview where status>0 and type=".$row["id"]." and expire>".time()." order by sort asc,id desc ";
            $result2=mysql_query($sql,$mysql);	
            
            $Project["queries"]++;
            
            $ad_count=mysql_num_rows($result2);
            
            //找到最近到期的广告
            $sql="SELECT expire FROM ".VI_DBMODPRE."adsview where status>0 and type=".$row["id"]." and expire>".time()." order by expire asc limit 0,1 ";
            $result2=mysql_query($sql,$mysql);
            $row2=mysql_fetch_assoc($result2);
            
            $Project["queries"]++;
            
            $a_firsexpire=$row2["expire"];
            
            //统计快要到期的广告
            $sql="SELECT count(id) as a_count FROM ".VI_DBMODPRE."adsview where status>0 and spare=0 and id=".$row["id"]." and expire<". (time() + (3600*24*$Setting[$appid]["expire"]) ) ." and expire>". time() ." ";
            $result2=mysql_query($sql,$mysql);	
            $row2=mysql_fetch_assoc($result2);
            
            $Project["queries"]++;
            
            $a_counexpire=$row2["a_count"];
            
            //现有预定
            $sql="SELECT * FROM ".VI_DBMODPRE."adsview where status=3 and type=".$row["id"]." and expire>".time()." order by sort asc,id desc ";
            $result2=mysql_query($sql,$mysql);
            
            $ad_engage=mysql_num_rows($result2);
            
            $Project["queries"]++;                
            
            echo '
            <tr class="line" edit="adsarea.edit.php?action=edit&id='.$row["id"].'&jump={jump}">
                <td>
                    <input name="list" type="checkbox" class="checkbox" value="'.$row["id"].'">
                </td>
                <td>';
                
                if( $row["preview"] ){
                    echo '<a href="'.$row["preview"].'" target="_blank">[查看]</a>';
                }else{
                    echo '无';
                }
                
                echo '</td>
                <td><a href="?parent='.$row["parent"].'">'.$VICACHE["mod.adsarea"][$row["parent"]]["name"].'</a></td>
                <td>
                    <a href="adsview.list.php?parent='.$row["parent"].'&type='.$row["id"].'" title="'.$row["key"].'">'.$row["name"].'</a>
                    ';
                    
                    if( $row["quote"] ){
                        echo ' <a href="'.$row["quote"].'#vad_'.$row["key"].'" target="_blank" title="'.$row["key"].'">'.loader_image("link.gif","定位").'</a>';
                    }
                    
                    if($row["status"]==-1){
                        echo '<sup class="text-yes">[回收站]</sup>';
                    }
                    
                echo '
                </td>
                <td>'.$row["key"].'</td>
                <td align="right"><span class="text-yes">'.number_format($row["price"]).'/'.$row["unit"].'</span></td>
                <td>'.($row["number"] ? $row["number"] : '<span class="text-no">'.$row["number"].'</span>').'</td>
                <td>'.($ad_count ? $ad_count : '<span class="text-yes">'.$ad_count.'</span>').'</td>
                <td>'.(($number- $ad_count) ? '<span class="text-no">'.($row["number"]- $ad_count).'</span>' : ($row["number"]- $ad_count)).'</td>
                <td>'.($a_counexpire ? '<span class="text-no">'.$a_counexpire.'</span>' : $a_counexpire).'</td>
                <td>'.($ad_engage ? '<span class="text-no">'.$ad_engage.'</span>' : $ad_engage).'</td>
                <td>'.$row["width"].'*'.$row["height"].'</td>
                <td><a href="?s=show&q='.$row["show"].'">'.$Modules["adsview"]["type"][$row["show"]].'</a></td>
                <td>
                    <a href="adsarea.code.php?id='.$row["id"].'&key='.$row["key"].'" onclick="new parent.Mo.Dialog( \'dialog\',\'广告代码\', \'<iframe src=\'+this.href+\' frameborder=0></iframe>\' ,\'90%\',\'510\',{ \'remove\' : true, \'draged\' : true, \'create\' : true }); return false;">代码</a>
                    /
                    <a href="adsview.preview.php?action=channel&key='.$row["key"].'&show='.$row["show"].'" onclick="new parent.Mo.Dialog( \'dialog\',\'广告代码\', \'<iframe src=\'+this.href+\' frameborder=0></iframe>\' ,\'90%\',\'510\',{ \'remove\' : true, \'draged\' : true, \'create\' : true }); return false;">预览</a>						
                </td>
                <td>
                    <script type="text/javascript">
                    Mo.Tools("RsEdit","adsarea.edit.php?action=edit&id='.$row["id"].'","");
                    Mo.Tools("RsDelete","?action=delete&list='.$row["id"].',","");
                    </script>
                </td>
            </tr>
            ';

            $i++;
        }
        ?>
      <tr class="line">
        <td colspan="15"><?php echo $Modules[$appid]["tool"];?></td>
      </tr>
            
	</table>
	<?php
    //mysql close
    reader_mysql(true);
    ?>
    </form>
    <!--用于动作处理_结束-->
    
    <div id="saving">
		<?php
            echo GetPageLink($page,$row_count,$Global["pagesize"],$url,"page");
        ?>
    </div>

<!--body end-->

<?php html_close();?>