<?php
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2005-2010 GV32.COM，并保留所有权利。
 * 网站地址: http://www.gv32.com；
 * 作者: GV32.COM
 *----------------------------------------------------------------------------*
**/


class Vote
{
	function Vote()
	{
		$getconfig = $GLOBALS['Helpe'] -> get_config();
		$GLOBALS['Templ'] -> assign('getconfig',$getconfig);

		$getmenu = $GLOBALS['Helpe'] -> get_menu();
		$GLOBALS['Templ'] -> assign('getmenu',$getmenu);
		
	
		$getlink = $GLOBALS['Helpe'] -> get_link();
		$GLOBALS['Templ'] -> assign('getlink',$getlink);

		$getads = $GLOBALS['Helpe'] -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );
		$GLOBALS['Templ'] -> assign('getads',$getads);
		
		$getuserlink = $GLOBALS['WebSe']->getuserlink();
		
		$GLOBALS['Templ']->assign('getuserlink',$getuserlink);
		
		$actval = trim($_REQUEST["act"]);
		if($actval=='')
		{
			$this -> infolist();
		}
		
	}
	
	function infolist(  )
	{
		$pageSize = 20 ;
		//$pageNum = $GLOBALS['Reque'] -> getval["pageNum"];	
		
		if(  $GLOBALS['Reque'] -> getval["pageNum"] > 1 )
		{
			$pageNum  = $GLOBALS['Reque'] -> getval["pageNum"];
			$LIMITnum = ceil( ( $GLOBALS['Reque'] -> getval["pageNum"] -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		
		$webtitle = '在线调查';
		//处理你现在的位置
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/vote.html">在线调查</a>';
		}else{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/?load=vote&act=infolist">在线调查</a>';
		}
		//vot_id 	vot_title 	vot_starttime 	vot_endtime 	
		$nowtime = time();
		
		$sqlQuery = " SELECT vot_id, vot_title, vot_starttime, vot_endtime, vot_selecttype, vot_count, vot_addtime FROM  ".SQL_PREFIX."vote   WHERE vot_starttime <  $nowtime and  vot_endtime > $nowtime  GROUP BY  vot_id ORDER BY  `vot_id` LIMIT  $LIMITnum , $pageSize ";
		
		$info 		= $GLOBALS['MySql']  ->  selectInfo($sqlQuery);
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."vote  WHERE vot_starttime <  $nowtime and  vot_endtime > $nowtime   ORDER BY `vot_id`  ";
		
		$infocount 	= $GLOBALS['MySql']  ->  selectOne($sqlQuery);
		
		$PageStr = $GLOBALS['Page'] -> buildPageStr( $url = '?load=vote&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
		$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);
		
		$GLOBALS['Templ'] -> assign('PageStr',$PageStr);
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('votelist_tpl.html');
		
	}
	
	function detail( )
	{
		$webtitle = '在线调查';
		//处理你现在的位置
		if( $GLOBALS["WebConfig"]["webhtml"] == 1 )
		{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/vote.html">在线调查</a> > 详细内容 ';
		}else{
			$nowaddress = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/?load=vote&act=infolist">在线调查</a> > 详细内容 ';
		}
		$id  = $GLOBALS['Reque'] -> getval["id"];
		
		if( $id > 0 )
		{
			$sqlQuery 	= " SELECT *  FROM  ".SQL_PREFIX."vote  WHERE vot_id = ".$id." ORDER BY  vot_id  ";

			$infodetail = $GLOBALS['MySql']  ->  selectOne($sqlQuery);

			$sqlitemsql = " SELECT  *  FROM  ".SQL_PREFIX."voteitem  WHERE vot_id = '".$id."' LIMIT 100 ";
			
			$infoItem   = $GLOBALS['MySql'] -> selectInfo($sqlitemsql);
			
			if($infoItem)
			{
				foreach($infoItem as $key =>$val)
				{
					if($val["voi_count"] > 0)
					{
						$infoItem[$key]["sent"] = ceil(($val["voi_count"]/$infodetail["vot_count"])*100);
					}else{
						$infoItem[$key]["sent"] = 0; 
					}
				}
			}


			$GLOBALS['Templ'] -> assign('webtitle',$webtitle);
			
			$GLOBALS['Templ'] -> assign('nowaddress',$nowaddress);

			$GLOBALS['Templ'] -> assign('infoItem',$infoItem);
			
			$GLOBALS['Templ'] -> assign('info',$infodetail);
			
			$GLOBALS['Templ'] -> display('vote_tpl.html');
			
		}else{
			$GLOBALS['Helpe'] -> error404();
		}	
	}
	
	function addinfo()
	{
		$vot_idval 		 = $GLOBALS['Reque'] -> funpost("vot_id");
		$vot_idvalcount  = 1;
		$vot_idvalcount  = count($vot_idval);
		
		if(is_array($vot_idval)){   $vot_idval = implode(',', $vot_idval);  }
		
		$vot_selecttypeval 	= $GLOBALS['Reque'] -> funpost("vot_selecttype");
		$vot_idval  		= $GLOBALS['Reque'] -> funpost("vot_id");
		$vol_ipaddressval	= $GLOBALS['Helpe'] -> getip();
		$vol_addtimeval 	= time();
		
		if($vot_idval=='')
		{
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losserror']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',WEBBASE_URL."/?load=vote&act=detail&id=".$vot_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');				
			exit('');
		}
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."votelog  WHERE vot_id = '".$vot_idval."' and vol_ipaddress = '".$vol_ipaddressval."'  ORDER BY `vot_id` ";
			
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
		if( $info["CNUM"] < 6 )
		{

				$vot_idval = $vot_idval;
				$insertsql ="INSERT INTO `".SQL_PREFIX."votelog` (`vot_id` , `vol_ipaddress` , `vol_addtime` , `vot_idstr` ) VALUES (  '$vot_idval', '$vol_ipaddressval', '$vol_addtimeval', '$vot_idval')";
				$GLOBALS['MySql'] -> querySql($insertsql);
				
				$updatevote = "UPDATE `".SQL_PREFIX."vote` SET  vot_count = vot_count + $vot_idvalcount   WHERE  vot_id = '".$vot_idval."' ";
				$GLOBALS['MySql'] -> querySql( $updatevote );
				
				
				$updatevote = "UPDATE `".SQL_PREFIX."voteitem` SET  vot_count = vot_count + 1   WHERE  vot_id in ( ".$vot_idval." ) and vot_id = '".$vot_idval."' ";
				$GLOBALS['MySql'] -> querySql( $updatevote );
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',WEBBASE_URL."/?load=vote");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');
			
		}else{
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_iserror']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',WEBBASE_URL."/?load=vote&act=detail&id=".$vot_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		
		}
		
	}	

}

$Vote = new Vote();
?>