<?php

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

require '../../include/loader.php';
html_start("链接列表 - VeryIDE");
?>

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

	//module
	require 'naver.php';
	
	//加载分析模块
	Module :: loader( 'analytic' );

	$q = getgpc('q');
	$s = getgpc("s");	
	
	//批处理列表
	$list = getgpc('list');
	$list = is_array($list) ? implode(",", $list) : $list;

	//上一页
	$jump = $_POST["jump"];
	
	$action = getgpc('action');

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

	if ($action){
	
		switch($action){
			//删除
			case "delete":
				
				//检查权限
				System :: check_func("mod-go-del", false );
				
				//删除记录
				System :: $db -> execute("DELETE FROM `mod:go` WHERE id in(".$list.")");
				
				//删除缓存
				$array = explode(",",$list);
				foreach( $array as $id ){
					Cached :: delete( $appid, 'go.'.$id.'.php' );
				}
				
				//清空统计
				Analytic :: clear( $appid, $list );				
				
				//记录日志
				System :: insert_event("sys-system-recycle",time(),time(),"删除数据：$appid (".$list.")");
				
				System :: redirect("go.list.php","链接删除成功!");
				
			break;
		
			case "create":
				//缓存广告缓存
				Cached :: rows( $appid,"SELECT * FROM `mod:go`");
				
				//生成关键字缓存
				Cached :: multi( $appid, "SELECT id,category,name,link FROM `mod:go` WHERE category <>'' and link<>'' order BY id desc", "table.go" );
				
				$_G['project']['message']="已生成所选数据！";
			break;
		}
	}
	
	$connect = getgpc('connect');
	$method = getgpc('method');
	
?>
	<div id="search">
		
	    <form name="form1" method="get" action="?">
            <span class="action">	    
	    		    
		<select name="method" onchange="Mo(this.form).submit();" ignore="true">
		    <optgroup label="数据类型" />
		    <option value="">全部类型</option>
		    <?php            
		    foreach( $_G['module']['go']["method"] as $key => $val ){
			echo '<option value="'.$key.'">'.$val.'</option>';
		    }            
		    ?>
		</select>
	    
		<select name="connect" onchange="Mo(this.form).submit();" ignore="true">
		    <optgroup label="连接方式" />
		    <option value="">全部方式</option>
		    <option value="http">HTTP</a>
			<option value="mail">Mail</a>
			<option value="qq">QQ</a>
			<option value="msn">MSN</a>
			<option value="ww">旺旺</a>
		</select>
	    
            	<input type="button" value="我的统计" onclick="location.href='?s=account&q=<?php echo urlencode($_SESSION['Manager']['account']);?>';" class='cancel'>
                <input type="button" value="新增统计" onclick="location.href='go.edit.php';" class='button'>
                <input type="button" value="更新缓存" onclick="if(confirm('确定现在要更新缓存吗？')){location.href='?action=create';}" class='button'>
            </span>      
			
            <select name="s" id="s">
		<option value="name" checked>标题</option>
		<option value="id"> ID</option>
		<option value="account">作者</option>
		<option value="mender"> 修改人</option>
		<option value="link">链接</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("select[name=method]").value("<?php echo $method;?>");
			Mo("select[name=connect]").value("<?php echo $connect;?>");
			
			Mo.reader(function(){
				
				Mo("#table tr").bind( 'mousedown', function( ele, index, event ){

					var target = Mo.Event( event ).target();
	
					//编号
					var mark = ele.getAttribute("mark");
	
					//忽略标题等行
					if( !mark || target.tagName != "TD" ){ return false; };
	
					//选中第一个复选框
					var obj = Mo("input[type=checkbox]",ele).item(0);
	
					if(obj.checked){
						Mo('#block-'+mark).show();
					}else{
						Mo('#block-'+mark).hide();
					}
	
				})
			
				<?php
				//只针对新增内容
				if($_GET['mark']){
				?>
				//操作提示
				Mo.Tips(null,$("mark-<?php echo $_GET['mark'];?>"),'info','请复制这里的 HTML 代码到你需要统计的页面中。',document,'click',0,40);
				<?php
				}
				?>
			});
		</script>
	</div>
    
    <!--用于动作处理_开始-->
    <form name="formPost" id="formPost" method="post">
        <input name="action" id="action" type="hidden" value="" />        
        <input name="state" id="state" type="hidden" value="" />
        
        <input name="jump" type="hidden" value="" />
        <script>Mo('#formPost input[name=jump]').value( location.href );</script>
    
	<?php		
    
	$sql="SELECT * FROM `mod:go` WHERE 1=1 ";

	if ($q && $s){    
		if( strpos($s,"id") !== false ){
			$sql.=" and  `".$s."` = '".$q."'";
		}else{
			$sql.=" and `".$s."` like '%".$q."%'";
		}        
	}
	
	if( $method ){
		$sql.=" and `method` = '".$method."'";
	}	
	
	if( $connect ){
		$sql.=" and `link` like '".$connect."://%'";
	}
        
	$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,".$_G['setting']['global']['pagesize'];	

	//分页链接
	$url="?q=".$q."&s=".$s."&method=".$method."&connect=".$connect."&page=";

	$result = System :: $db -> getAll( $sql );
	
	?>
	<table width="100%" border="0" cellpadding="1" cellspacing="1" id="table" class="table">
 		<?php
        if( count($result)> $_G['setting']['global']['pagesize']/2 ){
        ?>
        <tr class="thead">
            <td colspan="12" class="first">
                <?php echo $_G['module']['go']['tool'];?>
            </td>
        </tr>
      	<?php
		}
		?>
          
          <tr class="title">
            <td width="30"></td>
            <td>链接名称</td>
            <td>创建者</td>
			<td>修改者</td>
            <td>原始链接</td>
            <td>统计地址 <script>Mo.Tools("Tip","用于统计点击量或浏览量的链接地址");</script></td>
			<td>类型</td>
       		<td>数值</td>
       		<td>扩展</td>
            <td>修改时间</td>
            <td>创建时间</td>
            <td width="70">操作</td>
          </tr>
        <?php
         
        foreach( $result as $row ){
			
			$link = Go :: getlink( $row['method'], $row['id'] );
			
			switch( $row['method'] ){
			
				case "view":				
					$code = "<!--GO--><img src='$link' /><!--GO-->";			
				break;
			
				case "click":				
					$code = "<!--GO--><a href='$link'>".$row['name']."</a><!--GO-->";
				break;
			
			}
			
            ?>
              <tr class="<?php echo zebra( $i, array( "line" , "band" ) );?>" mark="<?php echo $row['id'];?>" edit="go.edit.php?action=edit&id=<?php echo $row['id'];?>&jump={jump}">
                <td title="<?php echo $row['id'];?>">
                	<input name="list[]" type="checkbox" class="checkbox" value="<?php echo $row['id'];?>">
                </td>
                <td title="<?php echo $row['name'];?>">
                	<a href="<?php echo $row['link'];?>" target="_blank"><?php echo format_substr($row['name'],22,"...");?></a>
					<?php
					if($row['state']==-1){
						echo '<sup class="text-yes">[回收站]</sup>';
					}
					?>
                </td>
				<td><a href='?s=account&q=<?php echo urlencode($row['account']);?>'><?php echo $row['account'];?></a></td>
				<td><a href='?s=mender&q=<?php echo urlencode($row['mender']);?>'><?php echo $row['mender'];?></a></td>
                <td>
                    <a href='<?php echo $row['link'];?>' target="_blank"><?php echo format_url($row['link'],30);?></a>
                </td>
                <td>
                    <a href='<?php echo $link;?>' target="_blank"><?php echo $link;?></a>
                </td>
				<td><a href='?method=<?php echo urlencode($row['method']);?>'><?php echo $_G['module']['go']["method"][$row['method']];?></a></td>
                <td class="text-yes">
                    <?php echo Analytic :: count( $appid, $row['id'], $row['method'] );?>
                </td>
                <td>
                    <?php
					
			echo '<a href="'.VI_BASE.'module/analytic/analytic.show.php?appid='.$appid.'&id='.$row['id'].'&type='.$row['method'].'s&name='.urlencode($row['name']).'" onclick="parent.Serv.Dialog( \'dialog\',\'统计分析\', \'<iframe src=\'+this.href+\' frameborder=0></iframe>\' ,\'80%\',\'60%\',{ \'remove\' : true, \'draged\' : true, \'create\' : true }); return false;">'.loader_image("icon/statistics.png","统计分析").'</a> ';
					
                    echo loader_image("icon/address.png","复制地址","","Mo.Clipboard('".$link."',function( text ){ alert('已复制如下信息到剪贴板:\\n\\n'+text); } );");
                    ?>
                </td>
                <td>
                <?php
                echo date("y-m-d",$row['modify']);
                ?>
                </td>
                <td title="<?php echo date("Y-m-d H:i:s",$row['dateline']);?>"><?php echo date("y-m-d",$row['dateline']);?></td>
                <td>                
                <script type='text/javascript'>
    	            Mo.Tools('RsEdit','go.edit.php?action=edit&id=<?php echo $row['id'];?>','');
	                Mo.Tools('RsDelete','?action=delete&list=<?php echo $row['id'];?>','');
                </script>
                </td>
                </tr>
                <tr id="block-<?php echo $row['id'];?>" <?php echo $_GET['mark'] != $row['id'] ? 'style="display:none"' : '' ?>>
                    <td colspan="12" class="block">
                    <?php
			echo loader_image("icon/document.png");
			?>
                    <textarea onmouseover="this.select();" style="vertical-align:middle; width:90%;" id="mark-<?php echo $row['id'];?>"><?php echo $code;?></textarea>                     
                    </td>
                </tr>
            <?php
            }
	    
	    if( count( $result ) == 0 ){
		echo '<tr><td colspan="12" class="notice">没有检索到相关统计，<a href="'.$appid.'.edit.php">创建一个？</a></td></tr>';
		}
            ?>
            
            <tr class="tfoot">
                <td colspan="12" class="first">
                 <?php echo $_G['module']['go']['tool'];?>
                </td>
            </tr>
        </table>
        
        <div id="saving">
	<?php
	//关闭数据库
	System :: connect();

	echo multipage($page,$row_count,$_G['setting']['global']['pagesize'],$url,"page");
	?>
        </div>
        
<!--body end-->

<?php html_close();?>