<?php
define('THIS_SCRIPT', 'boardstat');
require_once 'global.php';
include_once CACHE_PATH.'board/board_all.php';
include_once CACHE_PATH.'Forum_Postcache_File.php';
include_once $fsetting['manageurl'].'inc/page.class.php';
$lang = load_lang($lang, 'boardstat' );
$statobj = new forum_stat();
class forum_stat{
	var $title_num;
	var $Stattitle1;
	var $Stattitle2;
	var $Stattitle3;
	var $stats;
	var $bbs_stat;
	var $board_title;
	var $index_forumnum;
	var $today_post;
	var $topic_post;
	var $allpost;
	var $linkurlfile;
	var $boardinfolist;
	var $stattitle;
	var $redcolor;
	var $page_size;
	var $page;
	var $total;
	var $page_count;
	var $offset;
	function forum_stat(){//初始化
		global $lang,$fsetting,$action,$index_forumnum,$_DCACHE,$boardid,$dv,$db;
		$this->today_post=$index_forumnum['forumnum']['todaynum'];//今日贴数
		$this->topic_post=$index_forumnum['forumnum']['topicnum'];//主题贴数
		$this->allpost=$index_forumnum['forumnum']['forumnum'];//贴子总数
		$this->stats = $lang['template.strings0'].' - '.$lang['hometitle'];
		$this->bbs_stat=$this->board_title=$fsetting['forumname'];//论坛名称
		$this->Stattitle1=$lang['template.strings9'];
		$this->Stattitle2=$lang['template.strings10'];
		$this->linkurlfile='boardstat.php';
		$this->navigation_title();
		$this->forum_online_count();
	}

	function forum_online_num(){//计算在线人数;
		global $dv,$db,$lang;
		$dv_online=$db->scalar("SELECT count({$dv}online.boardid) as cou from {$dv}online group by {$dv}online.boardid");
		if($o_query=$db->query("select b.boardid,count(b.boardid) as id_num from {$dv}board AS b ,{$dv}online AS o where b.boardid=o.boardid group by o.boardid")){
			while ($o_arr=$db->fetch_array($o_query)){
				$other_count+=$o_arr['id_num'];
			}
			$person_count=$other_count+$dv_online;
			$db->free_result($o_query);
			return ($person_count);
		}
		else{
			return 0;
		}
	}

	function forum_online_count(){//，其中{$boardtype}有{$boardusernum}位在线用户与{$boardguestnum}位客人
		global $action,$lang;
		switch ($action){
			case '':
			case 'lasttopicnum':
			case 'lastbbsnum':
				$this->boardinfolist=str_replace('{$todaynum}',$this->today_post,$lang['template.strings8']);
				$this->boardinfolist=str_replace('{$bbsnum}',$this->allpost,$this->boardinfolist);//帖子总数;
				$this->boardinfolist=str_replace('{$topicnum}',$this->topic_post,$this->boardinfolist);//主题总数;
				$this->boardinfolist=str_replace('{$boardtype}',$this->board_title,$this->boardinfolist);
				break;
			case 'online':
			case 'onlineinfo':
			case 'onlineUserinfo':
				$this->boardinfolist=str_replace('{$Allonlinenum}',$this->forum_online_num(),$lang['template.strings6']);
			break;
		}
	}
	
	function stat_titleCyc(){//主题数图列和总题数图列循环部分;
		global $db,$pic_num,$dv,$selboard,$boardtype,$statpic,$statwidth,$statnum,$lang,$_infoList,$action;
		$_infoList = array();
		switch ($action){
			case '':
				$query=$db->query("select boardid,topicnum,postnum,todaynum,boardtype from {$dv}board order by todaynum desc");
				break;
			case 'lasttopicnum':
				$query=$db->query("select boardid,topicnum,postnum,todaynum,boardtype from {$dv}board order by topicnum desc");
				break;
			case 'lastbbsnum':
				$query=$db->query("select boardid,topicnum,postnum,todaynum,boardtype from {$dv}board order by postnum desc");
				break;
			default:
				$query=$db->query("select boardid,topicnum,postnum,todaynum,boardtype from {$dv}board order by topicnum desc");	
				break;
		}
		if($query){
			while ($titlecyc=$db->fetch_array($query)){
				$_infoList[] = $titlecyc;
			}
			$db->free_result($query);
		}
	}

	function stat_online_cutline(){//在线图列,需要 dv_board和dv_online表的数据;
		global $dv,$db,$lang,$_infoList,$id_num,$action,$boardid;
		$_infoList = array();
		$s_query=$db->query("select b.boardid,b.boardtype,count(b.boardid) as id_num from {$dv}board AS b ,{$dv}online AS o where b.boardid=o.boardid group by o.boardid");
		if($s_query){
			while ($on_arr=$db->fetch_array($s_query)){
				$_infoList[] = $on_arr;
			}
			$db->free_result($s_query);
		}
	}
	function stat_other_online(){//在线图列的其它在线人数;
		global $dv,$db,$lang,$other_list,$other_type,$reaction,$action,$boardid;
		$other_list = array();
		$other_type=$lang['templace.strings12'];
		$o_query=$db->query("select count(boardid) as o_num from {$dv}online where boardid=0 or boardid is NULL");
		if($o_query){
			while ($o_arr=$db->fetch_array($o_query)){
				$other_list[] = $o_arr;
			}
			$db->free_result($o_query);
		}
	}

	function stat_online_stat(){
		global $dv,$db,$lang,$oninfo_list,$fsetting,$PageNum,$totalrec,$Pcount,$CurrentPage,$redcolor,$_DCACHE;
		global $action,$boardid,$SearchStr;
		global $page_size,$page,$total,$page_count,$offset,$p;
		$this->page_size=intval($page_size);
		$this->page=intval($page);
		
		$oninfo_list = array();
		$SearchStr="boardid=".$boardid."&action=onlineinfo";
		if(empty($boardid)){
			switch ($action){
				case 'online':
				case 'onlineinfo':
					$this->total=$total=intval($db->scalar("select count(o.usergroupid) as col from {$dv}online o,{$dv}usergroups u where u.usergroupid=o.usergroupid"));//得到总记录;
					break;
			}
		}
		else{
			switch ($action){
				case 'online':
				case 'onlineinfo':
					$this->total=$total=intval($db->scalar("select count(o.usergroupid) as col from {$dv}online o,{$dv}usergroups u where o.boardid=".intval($boardid)." and u.usergroupid=o.usergroupid"));//得到总记录;
					break;
			}
		}

		$this->page_size=20;
		if ($this->page=="" or $this->page<1){
			$this->page=1;
		}
		 $this->page_count=$page_count=ceil($this->total/$this->page_size);
		if ($this->page>$this->page_count)
			$this->page=$this->page_count;
		$this->offset=$offset=$this->page;
		if( $this->offset < 1 ) $this->offset = 1;
		if(empty($boardid)){
			switch ($action){
				case 'online':
				case 'onlineinfo':
					$info_sql = "select 					o.username,o.stats,o.browser,o.ip,o.userhidden,o.lastimebk,o.startime,o.usergroupid,o.id,u.titlepic from {$dv}online o,{$dv}usergroups u where u.usergroupid=o.usergroupid order by startime";
					break;
			}
		}
		else{
			switch ($action){
				case 'online':
				case 'onlineinfo':
					$info_sql = "select o.username,o.stats,o.browser,o.ip,o.userhidden,o.lastimebk,o.startime,o.usergroupid,o.id,u.titlepic from {$dv}online o,{$dv}usergroups u where o.boardid=".intval($boardid)." and u.usergroupid=o.usergroupid order by startime";
					break;
			}
		}
		if($info_query=$db->query($info_sql,array('absolutePage'=>$this->offset, 'pageSize'=>$this->page_size))){
			while ($info_arr=$db->fetch_array($info_query)){
				$oninfo_list[] = $info_arr;
			}
			$this->redcolor = $_DCACHE['stylecss'][19];
			$db->free_result($info_query);
		}
	}

	function onlineUserinfo(){//用户组;
		global $dv,$db,$lang,$user_list;
		global $action,$reaction,$boardid;
		$user_list=array();
		$result=$db->query("select u.usergroupid,u.parentgid,u.usertitle,count(o.id) as id_num from {$dv}online o,{$dv}usergroups u where o.usergroupid=u.usergroupid and u.parentgid>0 group by usergroupid");
		if($result){
			while ($u_arr=$db->fetch_array($result)){
				$user_list[]=$u_arr;	
			}
			$db->free_result($result);
		}
	}

	function forum_func_transfer(){
		global $action,$boardid;
		if(empty($boardid)){
			switch ($action){
				case '':
				case 'lasttopicnum':
				case 'lastbbsnum':
					$this->stat_titleCyc();//今日帖数图例  //主题数图列  //总题数图列循环部分;
					break;
				case 'online':
					$this->stat_online_cutline();//在线图例
					$this->stat_other_online();//在线图列的其它在线人数
					break;
				case 'onlineinfo':
					$this->stat_online_stat();//在线情况
					break;
				case 'onlineUserinfo':
					$this->onlineUserinfo();//用户组在线图例
					break;
			}
		}
		else{
			switch ($action){
				case '':
				case 'lasttopicnum':
				case 'lastbbsnum':
					$this->stat_titleCyc();//今日帖数图例  //主题数图列  //总题数图列循环部分;
					break;
				case 'online':
					$this->stat_online_cutline();//在线图例
					$this->stat_other_online();//在线图列的其它在线人数
					break;
				case 'onlineinfo':
					$this->stat_online_stat();//在线情况
					break;
				case 'onlineUserinfo':
					$this->onlineUserinfo();//用户组在线图例
					break;					
			}			
		}
	}

	function get_titlepic($imgvalue){//返回用户等级图片标志
		global $imgurl,$lang;
		switch ($imgvalue){
			case 'ao1.gif':
				$imgname=$imgurl.$lang['template.pic1'];
				break;
			case 'ao.gif':
				$imgname=$imgurl.$lang['template.pic2'];
				break;
			case 'messages1.gif':
				$imgname=$imgurl.$lang['template.pic4'];
				break;
			case 'messages2.gif':
				$imgname=$imgurl.$lang['template.pic7'];
				break;
			case 'gb(1).gif':
				$imgname=$imgurl.$lang['template.pic8'];
				break;
			case 'vip.gif':
				$imgname=$imgurl.$lang['template.pic0'];
				break;
		}
		return $imgname;
	}

	function get_boardtype($boardvalue){
		global $lang;
		switch (intval($boardvalue)){
			case 1:
				$reboard=$lang['tpl.str18'];
				break;
			case 2:
				$reboard=$lang['tpl.str19'];
				break;
			case 3:
				$reboard=$lang['tpl.str20'];
				break;
			case 4:
				$reboard=$lang['tpl.str21'];
				break;
			case 5:
				$reboard=$lang['tpl.str22'];
				break;
			case 0:
				$reboard=$lang['tpl.str22'];
				break;				
		}
		return $reboard;
	}

	function get_statnum($todaynum,$topicnum,$postnum,$id_num){
		global $lang,$boardid,$action;
		if(empty($boardid)){
			switch ($action){
				case '':
					$re_statnum=$todaynum;
					break;
				case 'lasttopicnum':
					$re_statnum=$topicnum;
					break;
				case 'lastbbsnum':
					$re_statnum=$postnum;
					break;
				case 'online':
					$re_statnum+=$id_num;
					break;
			}
		}
		else{
			switch ($action){
				case '':
					$re_statnum=$todaynum;
					break;
				case 'lasttopicnum':
					$re_statnum=$topicnum;
					break;
				case 'lastbbsnum':
					$re_statnum=$postnum;
					break;
				case 'online':
					$re_statnum=$id_num;
					break;
			}			
		}
		return $re_statnum;
	}

	function get_postnum($ac){
		global $action,$lang;
		switch ($ac){
			case 'lasttopicnum':
				$forumpostnum=$this->topic_post;
				break;
			case 'lastbbsnum':
				$forumpostnum=$this->allpost;
				break;
			case '':
				$forumpostnum=$this->today_post;
				break;
			case 'online':				
				$allonline=str_replace($lang['tpl.str25'],'',str_replace($lang['tpl.str26'],'',$this->boardinfolist));
				$allonline=str_replace('<b>','',str_replace('</b>','',$allonline));
				settype($allonline, "integer");
				$forumpostnum=(int) $allonline;
		}
		return $forumpostnum;
	}

	function navigation_title(){//论坛状态标题;
		global $lang,$action,$_DCACHE,$boardid,$fsetting;
		if(empty($boardid)){//版面boardid为0、空的情况下
			switch ($action){
				case ''://今日帖数图例
					$this->title_num = array(
						0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>$this->linkurlfile)
					   ,1=>array('linktitle'=>$lang['template.strings5'])
					);
					$this->Stattitle3=$lang['template.strings5'];
					break;
				case 'lasttopicnum'://主题数图例
					$this->title_num = array(
						0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>$this->linkurlfile)
					   ,1=>array('linktitle'=>$lang['template.strings2'])
					);
					$this->Stattitle3=$lang['template.strings2'];
					break;
				case 'lastbbsnum'://总帖数图例
					$this->title_num = array(
						0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>$this->linkurlfile)
					   ,1=>array('linktitle'=>$lang['template.strings1'])
					);
					$this->Stattitle3=$lang['template.strings1'];
					break;
				case 'online'://在线图例//在线情况
				case 'onlineinfo':
					$this->title_num = array(
						0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>$this->linkurlfile)
					   ,1=>array('linktitle'=>$lang['template.strings3'])
					);
					$this->Stattitle3=$lang['template.strings4'];
					break;
				case 'onlineUserinfo'://用户组在线图例
					$this->title_num = array(
						0=>array('linktitle'=>$lang['template.strings0'],'linkurl'=>$this->linkurlfile)
					   ,1=>array('linktitle'=>$lang['template.strings3'])
					);
					$this->Stattitle1=$lang['template.strings17'];
					$this->Stattitle3=$lang['template.strings4'];
					break;
			}
		}
		else{//版面boardid不为0、不为空的情况下
			$board_type=&$_DCACHE['all_forums'][intval($boardid)];
			$parentid = $board_type['parentid'];
			$parentboardinfo =& $_DCACHE['all_forums'][$parentid];
			$this->board_title = $board_type['boardtype'];
			switch ($action){
				case ''://今日帖数图例
					$this->title_num = array(
						0=>array('linktitle'=>$parentboardinfo['boardtype'],'linkurl'=>'index.php?boardid='.$board_type['parentid'])
						,1=>array('linktitle'=>$board_type['boardtype'],'linkurl'=>'index.php?boardid='.$boardid)
					   ,2=>array('linktitle'=>$lang['template.strings5'])
					);
					$this->Stattitle3=$lang['template.strings5'];
					break;
				case 'lasttopicnum':////主题数图例
					$this->title_num = array(
						0=>array('linktitle'=>$parentboardinfo['boardtype'],'linkurl'=>'index.php?boardid='.$board_type['parentid'])
						,1=>array('linktitle'=>$board_type['boardtype'],'linkurl'=>'index.php?boardid='.$boardid)
					   ,2=>array('linktitle'=>$lang['template.strings2'])
					);
					$this->Stattitle3=$lang['template.strings2'];
					break;
				case 'lastbbsnum'://总帖数图例
					$this->title_num = array(
						0=>array('linktitle'=>$parentboardinfo['boardtype'],'linkurl'=>'index.php?boardid='.$board_type['parentid'])
						,1=>array('linktitle'=>$board_type['boardtype'],'linkurl'=>'index.php?boardid='.$boardid)
					   ,2=>array('linktitle'=>$lang['template.strings1'])
					);
					$this->Stattitle3=$lang['template.strings1'];
					break;
				case 'online':
				case 'onlineinfo'://在线图例//在线情况
					$this->title_num = array(
						0=>array('linktitle'=>$parentboardinfo['boardtype'],'linkurl'=>'index.php?boardid='.$board_type['parentid'])
						,1=>array('linktitle'=>$board_type['boardtype'],'linkurl'=>'index.php?boardid='.$boardid)
					   ,2=>array('linktitle'=>$board_type['boardtype'].$lang['template.strings4'])
					);
					$this->Stattitle3=$lang['template.strings4'];
					break;
				case 'onlineUserinfo'://用户组在线图例
					$this->title_num = array(
						0=>array('linktitle'=>$parentboardinfo['boardtype'],'linkurl'=>'index.php?boardid='.$board_type['parentid'])
						,1=>array('linktitle'=>$board_type['boardtype'],'linkurl'=>'index.php?boardid='.$boardid)
					   ,2=>array('linktitle'=>$board_type['boardtype'].$lang['template.strings4'])
					);
					$this->Stattitle3=$lang['template.strings4'];
					break;
			}			
		}
	}
}
head(1,0,0,$statobj->title_num);
PageLoad();
footer();
function PageLoad(){
	global $lang,$todaynum,$action,$boardid,$_infoList,$topicnum,$pic_num,$boardinfolist,$Stattitle1,$Stattitle2,$Stattitle3,$selboard;
	global $boardtype,$statpic,$statwidth,$statnum,$reaction,$id_num,$other_type,$other_list,$oninfo_list,$groupsetting,$user_list;
	global $PageNum,$totalrec,$Pcount,$CurrentPage,$redcolor,$board_title,$SearchStr,$title_num,$statobj,$imgurl;
	global $page_size,$page,$total,$page_count,$offset,$p;
	$statobj->forum_func_transfer();
	include_once printout('boardstat');
}
?>