<?php

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

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

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


	//module
    require("naver.php");	

	$q = GetQuery("q");
	$s = GetQuery("s");	
	
	//批处理列表
	$list = $_POST["list"];
	$list = implode(",", $list);

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

	//mysql open
	reader_mysql();

	if ($action){
	
		switch($action){
			//删除
			case "delete":
				
				//检查权限
				checker_func("mod-go-del");		
				
				//直接删除不经过回收站
				if( $Group["sys-system-recycle"]=="Y" ){
					
					//删除记录
					mysql_query("delete from ".VI_DBMODPRE."go where id in(".$list.")");
					
					//删除缓存
					$array = explode(",",$list);
					foreach( $array as $id ){
						$file = VI_ROOT."/".$Project["cache"]."/".$appid."/go.".$id.".php";
						unlink($file);
					}
					
					//清空统计
					require("../analytic/function.php");
					clear($appid,$list);				
					
					//记录日志										
					event_insert($mysql,"sys-system-recycle",time(),time(),"删除数据：$appid (".$list.")");
					
				}else{
				
					$sql="update ".VI_DBMODPRE."go set status=-1,modify=".time().",mender='".$_SESSION["Manager"]["account"]."' where appid='".$appid."' and id in(".$list.")";
					mysql_query($sql);
					
				}				
				
				reader_redirect("go.list.php","链接删除成功!");
				
			break;
		
			case "create":
				//缓存广告缓存
				cache_rows($appid,$mysql,VI_DBMODPRE."go","select * from ".VI_DBMODPRE."go");
				
				//生成关键字缓存
				parseCache($mysql,$appid);
				
				$Project["message"]="已生成所选数据！";
			break;
		}
	}
	
?>
	<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="" />
			<input name="status" id="status" type="hidden" value="" />
        </form>
         <!--用于动作处理_结束-->
		
	    <form name="form1" method="get" action="?">
            <span class="action">
            	<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.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>
    
	<?php		
    //echo $mysql;
    
    
    $sql="SELECT * FROM ".VI_DBMODPRE."go 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.=" order BY id desc";
    
    //查询数据库_总记录数
    $row_count = $db -> getValue( str_replace("*","count(id)",$sql) );   


    //分页参数
    $page=GetPage("page");
    $page_start=$Global["pagesize"]*($page-1);
    $sql=$sql." limit $page_start,".$Global["pagesize"];	
    
    //分页链接
    $url="?q=".$q."&s=".$s."&page=";
    
    $result=mysql_query($sql,$mysql);
	
    $Project["queries"]++;
	?>
	<table width="100%" border="0" cellpadding="1" cellspacing="1" id="table" class="table">
 		<?php
        if( mysql_num_rows($result)> $Global["pagesize"]/2 ){
        ?>
        <tr class="thead">
            <td colspan="9" class="first">
                <?php echo $Modules[$appid]["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>
          </tr>
        <?php
         
        while($row=mysql_fetch_assoc($result)){
			
			$link = go_link($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="line" 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["status"]==-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"],40);?></a>
                </td>
                <td>
                    <a href='<?php echo $link;?>' target="_blank"><?php echo $link;?></a>
                </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="new parent.Mo.Dialog( \'dialog\',\'统计分析\', \'<iframe src=\'+this.href+\' frameborder=0></iframe>\' ,\'90%\',\'70%\',{ \'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="10" 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
            
                $i++;
            }
            ?>
            
            <tr class="tfoot">
                <td colspan="10" class="first">
                 <?php echo $Modules[$appid]["tool"];?>
                </td>
            </tr>
        </table>
        
        <div id="saving">
			<?php
            //mysql close
            reader_mysql(true);
            
            echo GetPageLink($page,$row_count,$Global["pagesize"],$url,"page");
            ?>
        </div>
        
<!--body end-->

<?php html_close();?>