<?php
/**
 *----------------------------------------------------------------------------*
 * 版权所有 2010-2016 HuCart.COM，并保留所有权利。
 * 网站地址: http://Www.HuCart.Com；
 * 作者: HuCart.COM
 *----------------------------------------------------------------------------*
**/


class Vot
{
	function Vot()
	{
		$getmenu = $this -> helper -> get_menu();
		$this -> view -> assign('getmenu',$getmenu);
		
	
		$getlink = $this -> helper -> get_link();
		$this -> view -> assign('getlink',$getlink);

		$getads = $this -> helper -> get_ads( $instr = '1,2,3,4,5,6,7,8,9,10,11' );
		$this -> view -> assign('getads',$getads);
		
		$getuserlink = $GLOBALS['WebSe']->getuserlink();
		$this -> view ->assign('getuserlink',$getuserlink);
		
		$getprosort = $this -> personal -> get_prosort();
		$this -> view -> assign('getprosort',$getprosort);
		
		

	}
	
	function infolist(  )
	{
		$page_size = 20 ;
		//$page_num = $GLOBALS['Reque'] -> getval["page_num"];	
		
		if(  $GLOBALS['Reque'] -> getval["page_num"] > 1 )
		{
			$page_num  = $GLOBALS['Reque'] -> getval["page_num"];
			$limit_num = ceil( ( $GLOBALS['Reque'] -> getval["page_num"] -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 0 ;
		}
		
		$webtitle = '在线调查';
		//处理你现在的位置
		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'/vote.html">在线调查</a>';
 
		//vot_id 	vot_title 	vot_starttime 	vot_endtime 	
		$nowtime = time();
		
		$sql_query = " SELECT vot_id, vot_title, vot_starttime, vot_endtime, vot_selecttype, vot_count, vot_addtime FROM  ".$this -> db -> prefix."vote   WHERE vot_delete = 0 AND  vot_starttime <  $nowtime and  vot_endtime > $nowtime  GROUP BY  vot_id ORDER BY  `vot_id` LIMIT  $limit_num , $page_size ";
		
		$info 		= $this -> db ->  select_info($sql_query);
		
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."vote  WHERE vot_starttime <  $nowtime and  vot_endtime > $nowtime   ORDER BY `vot_id`  ";
		
		$infocount 	= $this -> db ->  select_one($sql_query);
		
		$page_str = $GLOBALS['Page'] -> buildpage_str( $url = BASE_WEBURL.'?/vote-list.html', $page_num  , $page_size  , $result_count = $infocount["CNUM"] );
		
		//左则自定义内容
		$sql_query 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'votlist' ORDER BY `per_id` ";	
		$infoper 	= $this -> db ->  select_one($sql_query);
		$this -> personal_mod -> get_column( $infoper["per_column"] );
		
		$this -> view -> assign('webtitle',$webtitle);
		$this -> view -> assign('now_address',$now_address);
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('votelist_tpl.html');
		
	}
	
	function detail( )
	{
		$webtitle = '在线调查';
		//处理你现在的位置

		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'?/vote.html">在线调查</a> > 详细内容 ';

		$id 		= intval($GLOBALS['Webur'] -> getval["idval"]);
		$page_num 	= $GLOBALS['Webur'] -> getval["page"];
		
		if( $id > 0 )
		{
			$sql_query 	= " SELECT *  FROM  ".$this -> db -> prefix."vote  WHERE vot_delete = 0 AND vot_id = ".$id."  ORDER BY  vot_id  ";

			$infodetail = $this -> db ->  select_one($sql_query);

			$sqlitemsql = " SELECT  *  FROM  ".$this -> db -> prefix."voteitem  WHERE vot_id = '".$id."' LIMIT 100 ";
			
			$infoItem   = $this -> db -> select_info($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; 
					}
				}
			}

			//左则自定义内容
			$sql_query 	= " SELECT per_id,per_column,per_main  FROM  ".$this -> db -> prefix."personal  WHERE per_type = 'vot' ORDER BY `per_id` ";	
			$infoper 	= $this -> db ->  select_one($sql_query);
			$this -> personal_mod -> get_column( $infoper["per_column"] );


			$this -> view -> assign('webtitle',$webtitle);
			
			$this -> view -> assign('now_address',$now_address);

			$this -> view -> assign('infoItem',$infoItem);
			
			$this -> view -> assign('info',$infodetail);
			
			$this -> view -> display('vote_tpl.html');
			
		}else{
			$this -> error('404');
		}	
	}
	
	function add_info()
	{
		$vot_idval 		 = $this -> input -> req["vot_id"];
		$vot_idvalcount  = 1;
		$vot_idvalcount  = count($vot_idval);
		
		if(is_array($vot_idval)){   $vot_idval = implode(',', $vot_idval);  }
		
		$vot_selecttypeval 	= $this -> input -> req["vot_selecttype"];
		$vot_idval  		= $this -> input -> req["vot_id"];
		$vol_ipaddressval	= $this -> helper -> get_ip();
		$vol_addtimeval 	= time();
		
		if($vot_idval=='')
		{
			$this -> view -> assign('info_title',$_LANG['bd_losserror']);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/?vote-".$vot_idval.".html");
			$this -> view -> display('suggestion.tpl');				
			exit('');
		}
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."votelog  WHERE vot_id = '".$vot_idval."' and vol_ipaddress = '".$vol_ipaddressval."'  ORDER BY `vot_id` ";
			
		$info = $this -> db -> select_one($sql_query);
			
		if( $info["CNUM"] < 1 )
		{

				$vot_idval = $vot_idval;
				$insertsql ="INSERT INTO `".$this -> db -> prefix."votelog` (`vot_id` , `vol_ipaddress` , `vol_addtime` , `vol_idstr` ) VALUES (  '$vot_idval', '$vol_ipaddressval', '$vol_addtimeval', '$vot_idval')";
				$this -> db -> query_sql($insertsql);

				$updatevote = "UPDATE `".$this -> db -> prefix."vote` SET  vot_count = vot_count + $vot_idvalcount   WHERE  vot_id = '".$vot_idval."' ";
				$this -> db -> query_sql( $updatevote );

				$updatevote = "UPDATE `".$this -> db -> prefix."voteitem` SET  voi_count = voi_count + 1   WHERE  voi_id in ( ".$vot_idval." ) and vot_id = '".$vot_idval."' ";
				$this -> db -> query_sql( $updatevote );
				
				$this -> view -> assign('info_title',$_LANG['bd_addsusse']);
				$this -> view -> assign('URL_TIME',URL_TIME);
				$this -> view -> assign('get_url',BASE_WEBURL."/?vote-".$vot_idval.".html");
				$this -> view -> display('suggestion.tpl');
			
		}else{
			$this -> view -> assign('info_title',$_LANG['bd_iserror']);
			$this -> view -> assign('URL_TIME',URL_TIME);
			$this -> view -> assign('get_url',BASE_WEBURL."/?vote-".$vot_idval.".html");
			$this -> view -> display('suggestion.tpl');	
		
		}
		
	}	

	function index()
	{
		$webtitle = '投票';
		
		//处理你现在的位置
		$now_address = '您的位置：<a href="'.BASE_WEBURL.'/">首页</a> > <a href="'.BASE_WEBURL.'?/vote.html">投票</a>';
		
		//查询一级菜单分类数量
		$sql_query = " SELECT vot_id, vot_title , vot_starttime,vot_endtime   FROM  ".$this -> db -> prefix."vote   WHERE vot_delete = 0   GROUP BY  vot_id ORDER BY vot_endtime DESC, vot_starttime DESC, `vot_id` LIMIT  0 , 8 ";
		$info = $this -> db -> select_info($sql_query);
 		
		//var_dump($info);
		$this -> view -> assign('webtitle',$webtitle);
		$this -> view -> assign('now_address',$now_address);
		$this -> view -> assign('info',$info); 
		$this -> view -> display('votelist_tpl.html',$cache_id);
	}


}


$Vot = new Vot();
if($GLOBALS['Webur'] -> getval["act"]=='list')
{
	$Vot -> infolist();
}else if(intval($GLOBALS['Webur'] -> getval["idval"]) != '')
{
	$Vot -> detail();
}else if($GLOBALS['Webur'] -> getval["type"] != '')
{
	$Vot -> add_info();
}else{
	$Vot -> index();
}

?>