<?php
define('THIS_SCRIPT', 'forum_savvy');
include_once 'global.php';
require_once INC_PATH.'dv_savvy.class.php';
include_once CACHE_PATH.'savvy_board.php';
$lang = load_lang($lang, 'forum_savvy' );
$stats = $lang['template1'];//ÂÛÌ³±êÌâ;
$substype="savvy";

$svy= new dv_forum_savvy();

if ( $boardid == 0 ){
	$arrNavMenu = array(
		0=>array('linktitle'=>$stats,'linkurl'=>'forum_savvy.php')
	);
	savvyhead(1,0,0,$arrNavMenu);
}else{
	$gettitle = $stats;
	savvyhead();
}
PageLoad();
footer();
function PageLoad(){
	global $dv,$db,$userid,$lang,$_DCACHE,$svy,$oper,$savvy_board,$boardid,$_forum,$_topics,$boardinfo,$board_settings,$forum_ads;	
	global $page,$topicmode,$topicfont;
	MainSavvyInfo();
	include_once printout('forum_savvy');
}

function Get_OperValue(){
	global $oper;
	if($oper=='all' or $oper==''){
		$opervalue='class="svytd"';
	}
	elseif($oper=='untie'){
		$opervalue='class="svytd"';		
	}
	elseif($oper=='nountie'){
		$opervalue='class="svytd"';		
	}
	elseif($oper=='high'){
		$opervalue='class="svytd"';		
	}
	elseif($oper=='zero'){
		$opervalue='class="svytd"';		
	}
	else{
		$opervalue='class="tablebody3"';			
	}
	echo $opervalue;
}
function MainSavvyInfo(){
	global $lang,$dv,$db,$boardid,$_DCACHE,$fsetting,$_forum,$_topics,$boardinfo,$board_settings,$forum_ads;
	global $page,$topicmode,$topicfont,$oper;
	if ($boardid > 0){
		$_sql = $topicmode && is_numeric($topicmode) ? ' AND mode='.$topicmode : '';
		$colorstr = array('','','red','blue','green');
		if( $fsetting['forum_alltopnum'] != '' ){
			if( trim($boardinfo['boardtopstr']) !== '' ){
				$boardtopstr = $boardinfo['boardtopstr']. ',' .$fsetting['forum_alltopnum'];
			}else{
				$boardtopstr = $fsetting['forum_alltopnum'];
			}
		}else{
			if( $boardinfo['boardtopstr'] != '' ){
				$boardtopstr = $boardinfo['boardtopstr'];
			}
		}
		if ($page == '' || !is_numeric($page) || $page <= 0){
			$page = 1;
		}
		$result1_hasmore = true;
		if ( $boardtopstr != '' && $page == 1 ){
			$getdotype = GetdoneType();
			$boardtop_str=explode(",",$boardtopstr);
			$boardtop_num=count($boardtop_str);
			for($btopid=0;$btopid<$boardtop_num;$btopid++){
				if($boardtop_str[$btopid]!=''){
					$board_topstr.=$boardtop_str[$btopid].",";
				}
			}
			$boardtopstr=substr($board_topstr,0,-1);
			$query1=$db->query("Select /*!40001 SQL_CACHE */ topicid,boardid,title,postusername,postuserid,dateandtime,child,hits,votetotal,lastpost,lastposttime,istop,isvote,isbest,locktopic,expression,topicmode,topicfont,mode,getmoney,getmoneytype,usetools,issmstopic,hidename,solve,anonymity From {$dv}savvy_topic Where topicid in ({$boardtopstr}) AND istop>0{$getdotype} AND boardid NOT IN (444,777){$_sql} Order By istop Desc,lastposttime Desc");
			$boardtopstr = explode(',',$boardtopstr);
			$boardtopcount = count($boardtopstr);
		}else{
			$boardtopcount = 0;
			$result1_hasmore = false;
		}
		$board_settings[60] = 25;
		$boardsql=(empty($boardid)?"":"AND boardid={$boardid}");
		if ($page == 1){
			$query2=$db->query("Select /*!40001 SQL_CACHE */ topicid,boardid,title,postusername,postuserid,dateandtime,child,hits,votetotal,lastpost,lastposttime,istop,isvote,isbest,locktopic,expression,topicmode,topicfont,mode,getmoney,getmoneytype,usetools,issmstopic,hidename,solve,anonymity From {$dv}savvy_topic /*!32312 USE INDEX(board_topic_list) */ Where istop=0{$getdotype} {$boardsql}{$_sql} Order By lastposttime Desc Limit ".$board_settings[60]);
		}else{
			$query2=$db->query("Select /*!40001 SQL_CACHE */ topicid,boardid,title,postusername,postuserid,dateandtime,child,hits,votetotal,lastpost,lastposttime,istop,isvote,isbest,locktopic,expression,topicmode,topicfont,mode,getmoney,getmoneytype,usetools,issmstopic,hidename,solve,anonymity From {$dv}savvy_topic /*!32312 USE INDEX(board_topic_list) */ Where istop=0{$getdotype} {$boardsql}{$_sql} Order By lastposttime Desc",array('absolutePage'=>$page,'pageSize'=>$board_settings[60]));
		
		}
		
		$result = false;
		while( true ){
			if( $result1_hasmore === true && $page == 1 ){
				$result = $db->fetch_array($query1);
			}
			if( $result === false ){
				$result1_hasmore = false;
			}
			if( $result1_hasmore === false ){
				$result = $db->fetch_array($query2);
			}
			if( $result === false ){
				break;
			}
			if ($result['istop'] > 0){
				switch($result['istop']){
				case 1:
					$result['istop'] = 0;
					break;
				case 2:
					$result['istop'] = 1;
					break;
				case 3:
					$result['istop'] = 2;
					break;
				}
			}else{
				if (intval($result['isbest']) == 1){
					$result['istop'] = 3;
				}elseif($result['locktopic'] == 1){
					$result['istop'] = 4;
				}elseif($result['isvote'] == 1){
					$result['istop'] = 5;
				}elseif($result['child'] > $fsetting['hottopicreply']){
					$result['istop'] = 6;
				}else{
					$result['istop'] = 7;
				}
			}
			$result['title'] = ChkBadWords($result['title']);
			$result['locktopic'] = htmlspecialchars($result['title']);
			if( $result['topicmode'] != 1 ){
				if ( strlen($result['title']) > intval($board_settings[59]) ){
					$result['title'] = substr($result['title'],0,$board_settings[59]).'...';
				}
			}
			$result['topicmode']= ShowCodeTitle($result['topicmode'],$result['topicfont'],1);

			if($board_settings[65] && $timestamp - $result['dateandtime'] < $board_settings['64'] * 60) {
				$result['newsign'] = '<img src="'.$board_settings[65].'" />';
			} else {
				$result['newsign'] = '';
			}
			$_topics[] = $result;
		}
	}
}



function GetdoneType(){
	global $db,$dv,$lang,$action,$boardid,$oper,$savvy_board;
	$highfen=$savvy_board['boardkey']['highsetup'];
	switch ($oper){
		case 'all':
			$Getdone='';
		break;
		case 'untie':
			$Getdone=' and solve=1';
		break;
		case 'nountie':
			$Getdone=' and solve=0';
		break;
		case 'high':
			$Getdone=' and getmoney > '.$highfen;
		break;
		case 'zero':
			$Getdone=' and child=0';
		break;
	}
	return $Getdone;
}
?>