<?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();

	$q = GetQuery("q");
	$s = GetQuery("s");
	
	$parent = GetQuery("parent");
	$id = GetNumber("id",0);
	
	$action = GetQuery("action");
	
	/*
	$a_list = GetQuery("list");
	if ($a_list){
		$a_list=substr($a_list,0,strlen($a_list)-1);
	}
	*/
	
	if ($action){
	
		switch($action){
			case "create":
				ad_create("SELECT * FROM ".VI_DBMODPRE."adsarea where parent =".$id ,$mysql);
				//call getVeryIDE_Msg(url,"已生成所选页面！")
				$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 parent =".$id ,$mysql);
					
					//删除父分类
					$sql="delete from ".VI_DBMODPRE."adsarea where id=".$id;
					mysql_query($sql,$mysql);				
					
				}else{
				
					$sql="update ".VI_DBMODPRE."adsarea set status=-1 where id in =".$id;
					mysql_query($sql);				
					
				}				
				
								
				$Project["message"]="成功删除广告！";
				
			break;
		}
		
	}
	
	
	//缓存广告分类和广告位
	if($action){
		cache_table($appid,$mysql,VI_DBMODPRE."adsarea",'<option value=\'{table:id}\'>{table:name}</option>');
	}
	
?>
    <div id="search">
        <form name="formSeacrh" id="formSeacrh" method="get" action="">

            <select name="s" id="s">
                <option value="key">位置标识</option>
                <option value="name">频道名</option>
                <option value="quote">所在页面</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;?>");				
        </script>
    </div>
    
    <!--up-->
    <table width="100%" border="0" cellpadding="1" cellspacing="1" class="table" id="table">
      <tr class="title" id="thead">
        <td>ID</td>
        <td>频道名称</td>
        <td>标识</td>
        <td>广告位列表 [广告位数量]</td>
        <td>广告列表 [广告数量]</td>
        <td>相关页面</td>
        <td width="50">操作</td>
      </tr>
    
    <?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 ";    
    
    $sql.=" and  parent=0";
    
    $sql.=" order BY id desc";

    //查询数据库_总记录数
    $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,".($page_start+$Global["pagesize"]);	
    
    
    //分页链接
    $url="?s=".$s."&q=$q&id=$id&page=";	
      
    $result=mysql_query($sql,$mysql);
    
    $Project["queries"]++;
         
    while($row=mysql_fetch_assoc($result)){

        //统计广告位数量
        $sql="select count(id) as count from ".VI_DBMODPRE."adsarea where parent=".$row["id"];
        $result2=mysql_query($sql,$mysql);
        $row2=mysql_fetch_assoc($result2);
        $t_count=$row2["count"];
        
        $Project["queries"]++;
        
        //统计广告数量
        $sql="select count(id) as count from ".VI_DBMODPRE."adsview where parent=".$row["id"];
        $result2=mysql_query($sql,$mysql);
        $row2=mysql_fetch_assoc($result2);
        $a_count=$row2["count"];
        
        $Project["queries"]++;
        
        echo '
          <tr class="line" edit="adsarea.edit.php?action=edit&id='.$row["id"].'&jump={jump}">
            <td>'.$row["id"].'</td>
            <td>';
            
            
        echo $row["name"];
        
        if($row["status"]==-1){
            echo '<sup class="text-yes">[回收站]</sup>';
        }
            
            
        echo '</td>
            <td>'.$row["key"].'</td>
            <td><a href="adsarea.list.php?parent='.$row["id"].'">浏览广告位</a> ['.$t_count.']</td>
            <td><a href="adsview.list.php?parent='.$row["id"].'">浏览广告列表</a> ['.$a_count.']</td>
            <td>
            <a href="'.$row["quote"].'" target="_blank">'.$row["quote"].'</a>
            </td>
            <td>
            <script type="text/javascript">
            Mo.Tools("RsEdit","adsarea.edit.php?action=edit&id='.$row["id"].'","");
            Mo.Tools("RsDelete","?action=delete&id='.$row["id"].'","");
            </script></td>
          </tr>
        ';
        
         $i++;
    }	
			
	//mysql close
	reader_mysql(true);
			
	?>
    </table>
    
    <div id="saving">
		<?php
            echo GetPageLink($page,$row_count,$Global["pagesize"],$url,"page");
        ?>
    </div>

<!--body end-->

<?php html_close();?>