<?php

if(!defined('DEDEINC'))
{
    exit("Request Error!");
}

function lib_kaifu(&$ctag,&$refObj)

{
    global $dsql;
    $attlist="row|12,titlelen|24,type|0,orderby|-1,keyword|";
    FillAttsDefault($ctag->CAttribute->Items,$attlist);
    extract($ctag->CAttribute->Items, EXTR_SKIP);
    $innertext = trim($ctag->GetInnerText());
    $totalrow = $row;
    $revalue = '';
	$ticai = $ticai;
	$time = $time;

	$addquery = " where arcrank='0' ";
	$wquery = ' ORDER BY kftime DESC';	
	if($type=='today'){//今日开服
	$addquery .= " and from_unixtime(kftime,'%Y-%m-%d')=curdate()";
	$wquery = ' ORDER BY tj DESC,kftime DESC ';
	}	
	if($type=='dayc'){
	$addquery .= " and from_unixtime(kftime,'%Y-%m-%d')=curdate() and tj='1'";
	$wquery = ' ORDER BY kftime DESC ';
	}	
	if($type=='day'){
	$addquery .= " and from_unixtime(kftime,'%Y-%m-%d')=curdate() and from_unixtime(kftime,'%H')='$time' and tj='0'";
	$wquery = ' ORDER BY kftime DESC ';
	}	
	if($type=='tomorrow'){//明日开服
	$addquery .= " and from_unixtime(kftime,'%Y-%m-%d')=curdate() +INTERVAL 1 day";
	$wquery = ' ORDER BY tj DESC,kftime DESC ';
	}
	if($type=='hot'){//每周分类排行
	$addquery .= " and leixing='$ticai' and YEARWEEK(from_unixtime(kftime,'%Y-%m-%d')) = YEARWEEK(now())";
	$wquery = ' ORDER BY click DESC';
	}	
	if($type=='hotall'){//每周全部排行
	$addquery .= " and YEARWEEK(from_unixtime(kftime,'%Y-%m-%d')) = YEARWEEK(now())";
	$wquery = ' ORDER BY click DESC';
	}	

	if($type=='has'){//已经开服
	$addquery .= "and DAY(from_unixtime(kftime)) < DAY(now())";
	$wquery = ' ORDER BY kftime DESC ';
	}
	if($type=='recent'){//近期开服发号
	$addquery .= "and fahaourl LIKE '%card%'";
	$wquery = ' ORDER BY kftime DESC ';
	}
	if($type=='tj'){//推荐开服
	$addquery .= " and tj='1'";
	$wquery = ' ORDER BY id DESC';
	}
	
	if($type=='pt-kf'){//平台相关开服
	$aid = $refObj->Fields['aid'];
	$title = $refObj->Fields['title'];
	$nrow = $dsql->GetOne("SELECT * FROM `#@__addonpt` where aid='$aid'");
	$addquery .= " and pingtai='$title'";
	$wquery = ' ORDER BY kftime DESC ';
	}
		
	if($type=='kf-yx'){//开服相关游戏
	$id = $refObj->Fields['aid'];
	$title = $refObj->Fields['title'];
	$nrow = $dsql->GetOne("SELECT * FROM `#@__archives` where id = '$id' ");
	$addquery .= " and channel='4' and title='$title'";
	$wquery = ' ORDER BY id DESC ';
	}
	
	if($type=='kf-kf'){//相关开服
	$aid = $refObj->Fields['aid'];
	$title = $refObj->Fields['title'];
	$nrow = $dsql->GetOne("SELECT * FROM `#@__addonkf` where aid='$aid'");
	$addquery .= " and title='$title'";
	$wquery = ' ORDER BY kftime DESC ';
	}

    if($keyword=='ok'){//关联开服
	$id = $refObj->Fields['aid'];
	$nrow = $dsql->GetOne("SELECT * FROM `#@__archives` where id = '$id' ");
	$keyword = trim($nrow['title']);
	$addquery .= " and CONCAT(`title`) LIKE '%$keyword%' ";
	$wquery = ' ORDER BY kftime DESC ';
	}
	if($type=='is'){//即将开服
	$addquery .= "and DAY(from_unixtime(kftime)) > DAY(now())";
	$wquery = ' ORDER BY kftime asc';
	}
    $equery = "SELECT * FROM `#@__addonkf` $addquery $wquery LIMIT 0 , $totalrow";


    $ctp = new DedeTagParse();
    $ctp->SetNameSpace('field','[',']');
    $ctp->LoadSource($innertext);
    $dsql->Execute('fb',$equery);
	$GLOBALS['autoindex'] = 0;
    while($arr=$dsql->GetArray('fb'))
    {
        $arr['title'] = trim($arr['title']);
		$nrow = $dsql->GetOne("SELECT id,title,arcrank FROM `#@__archives` WHERE title='{$arr['title']}' and arcrank>-1 and typeid='7'");
        if (!is_array($nrow)){
		$arr['game'] = '-';
		}
		else{
		$arr['game'] = '<a target="_blank" href="/game'.$nrow['id'].'.html">专区</a>';
		}		
		$nrow = $dsql->GetOne("SELECT id,title,arcrank FROM `#@__archives` WHERE title='{$arr['pingtai']}' and arcrank>-1 and typeid='8'");
        if (!is_array($nrow)){
		$arr['pt'] = ''.$arr['pingtai'].'';
		}
		else{
		$arr['pt'] = '<a target="_blank" href="/pt'.$nrow['id'].'.html" class="pt">'.$arr['pingtai'].'</a>';
		}		
		if($type!='already2'){
        if($arr['tj']>0){$arr['toping']='<img src="/style/images/hot1.gif" />';}else{$arr['toping']=" ";}
		}		
        if($arr['tj']>0 && $type!='already2'){
		if($GLOBALS['autoindex']%2==0){
		$arr['css'] = 'tab01_rowbg rand_rowbg';
		}else{
		$arr['css'] = 'tab01_rowbg rand_rowbg zebra';
		}		
		}else{
		if($GLOBALS['autoindex']%2==0){
		$arr['css'] = ' ';
		}else{
		$arr['css'] = 'zebra';
		}
		}
        $GLOBALS['autoindex']++;
        foreach($ctp->CTags as $tagid=>$ctag)
        {
            if(!empty($arr[$ctag->GetName()]))
            {
                $ctp->Assign($tagid,$arr[$ctag->GetName()]);
            }
        }
        $revalue .= $ctp->GetResult();
    }
    return $revalue;
}

