<?php

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

require '../../include/loader.php';

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

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

	//module
	require 'naver.php';
	
	Cached :: loader($appid,"table.adsarea");

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

	$q = getgpc('q');
	$s = getgpc("s");
	
	$parent = getgpc("parent");
	$id = getnum('id',0);
	
	$action = getgpc('action');
	
	if ($action){
	
		switch($action){
			case "create":
			
				Adsview :: create("SELECT * FROM `mod:adsarea` WHERE parent =".$id);
				$_G['project']['message']="已生成所选广告！";
				
			break;
				
			case "delete":
				
				//检查权限
				System :: check_func("mod-adsarea-del", false );
				System :: check_func("mod-adsview-del", false );
				
				//直接删除不经过回收站
				if( System :: check_func('sys-system-recycle') ){
					
					//删除子分类及广告
					Adsview :: delete("SELECT * FROM `mod:adsarea` WHERE parent =".$id ,$mysql);
					
					//删除父分类
					$sql="DELETE FROM `mod:adsarea` WHERE id=".$id;
					System :: $db -> execute( $sql );
					
				}else{
				
					$sql="UPDATE `mod:adsarea` SET `state`=-1 WHERE id in =".$id;
					System :: $db -> execute( $sql );
					
				}				
								
				$_G['project']['message']="成功删除广告！";
				
			break;
		}
		
	}
	
	
	//缓存广告分类和广告位
	if($action){
		Cached :: table( $appid, 'mod:adsarea',array( 'jsonde' => array('config') ));
	}
	
?>
    <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="70">操作</td>
      </tr>
    
    <?php
    $sql="SELECT * FROM `mod:adsarea` WHERE 1=1 ";

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

    //显示回收站
    if( System :: check_func('sys-system-recycle') == false ) $sql.=" and state > -1 ";    
    
    $sql.=" and  parent=0";
    
    $sql.=" order BY id desc";

    //查询数据库_总记录数
    $row_count = System :: $db -> getCount( $sql );    

    //分页参数
    $page=getpage("page");
    $page_start=$_G['setting']['global']['pagesize']*($page-1);
    $sql=$sql." limit $page_start,".($page_start+$_G['setting']['global']['pagesize']);	
	
    //分页链接
    $url="?s=".$s."&q=$q&id=$id&page=";	
      
    $result = System :: $db -> getAll( $sql );
         
    foreach( $result as $row ){

        //统计广告位数量
        $sql="SELECT count(id) as count from `mod:adsarea` WHERE parent=".$row['id'];
        $t_count = System :: $db -> getValue( $sql );
        
        //统计广告数量
	$sql="SELECT count(id) as count from `mod:adsview` WHERE parent=".$row['id'];
        $a_count = System :: $db -> getValue( $sql );
        
        echo '
          <tr class="'.zebra( $i, array( "line" , "band" ) ).'" edit="adsarea.edit.php?action=edit&id='.$row['id'].'&jump={jump}">
            <td>'.$row['id'].'</td>
            <td>';
            
        echo $row['name'];
        
        if($row['state']==-1){
            echo '<sup class="text-yes">[回收站]</sup>';
        }
            
        echo '</td>
            <td>'.$row["key"].'</td>
            <td><a href="adsarea.list.php?parent='.$row['id'].'">浏览广告位</a> <sup class="text-yes">['.$t_count.']</sup></td>
            <td><a href="adsview.list.php?parent='.$row['id'].'">浏览广告列表</a><sup class="text-yes"> ['.$a_count.']</sup></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>
        ';
        
    }
    
	if( count( $result ) == 0 ){
		echo '<tr><td colspan="13" class="notice">没有检索到相关频道，<a href="adsarea.edit.php">创建一个？</a></td></tr>';
	}
		
	//关闭数据库
	System :: connect();
			
	?>
    </table>
    
    <div id="saving">
		<?php
            echo multipage($page,$row_count,$_G['setting']['global']['pagesize'],$url,"page");
        ?>
    </div>

<!--body end-->

<?php html_close();?>