<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Vote
{
	function Vote()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
	}
	
	function infolist( )
	{
		$pageSize = 20 ;
		
		$keywordval 	= $GLOBALS['Reque'] -> reqval["keyword"];		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		
		//帐号 	真实姓名 	Email地址 	登录次数 	加入时间 	最后登录时间 
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."vote  WHERE 1 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 1  ORDER BY `vot_id`  ";
		
		$infocount = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//var_dump($info);
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=vote&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('vote_tpl.html');
		
	}
	
	function add(  )
	{
		$GLOBALS['Templ'] -> caching = true;
		$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24

		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=vote&act=addinfo");
		$GLOBALS['Templ']->display('voteadd_tpl.html');
	}
	
	function addinfo()
	{
		 
		$vot_titleval 		= $GLOBALS['Reque'] -> reqval["vot_title"];
		$vot_starttimeval 	= strtotime($GLOBALS['Reque'] -> reqval["vot_starttime"]);
		$vot_endtimeval 	= strtotime($GLOBALS['Reque'] -> reqval["vot_endtime"]);
		$vot_selecttypeval 	= $GLOBALS['Reque'] -> reqval["vot_selecttype"];

		if($vot_titleval !='' and $vot_starttimeval !='' and $vot_endtimeval !='')
		{
			
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."vote  WHERE vot_title = '".$vot_titleval."' ORDER BY `vot_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{
				$vot_addtimeval 	= time();
				$insertip ="INSERT INTO `".SQL_PREFIX."vote` (`vot_title` , `vot_starttime` , `vot_endtime` , `vot_selecttype` , `vot_addtime` ) VALUES (  '$vot_titleval', '$vot_starttimeval', '$vot_endtimeval', '$vot_selecttypeval', '$vot_addtimeval')";
				
				$GLOBALS['MySql'] -> querySql($insertip);
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_addsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}else{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_isrealyerror']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=add");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}
			
		}else{
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
		//var_dump($_REQUEST);
	}
	
	function del()
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];

		$deletesql ="DELETE FROM `".SQL_PREFIX."vote` WHERE  `vot_id` in ( $infoid )  ";
		$GLOBALS['MySql'] -> querySql($deletesql);
		
		//删除选项内容
		$deleteitemsql ="DELETE FROM `".SQL_PREFIX."voteitem` WHERE `vot_id` in ( $infoid )  ";
		$GLOBALS['MySql'] -> querySql($deleteitemsql);
		
		//删除投票记录
		$deletelogsql ="DELETE FROM `".SQL_PREFIX."votelog` WHERE `vot_id` in ( $infoid )  ";
		$GLOBALS['MySql'] -> querySql($deletelogsql);
		
	
		
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info()
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."vote  WHERE vot_id = '".$infoid."' ORDER BY `vot_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=vote&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('voteedit_tpl.html');
	
	}
	
	
	function editinfo()
	{	
	
		$vot_idval 			= $GLOBALS['Reque'] -> funpost("vot_id");
		$vot_titleval 		= $GLOBALS['Reque'] -> funpost("vot_title");
		$vot_starttimeval 	= strtotime($GLOBALS['Reque'] -> funpost("vot_starttime"));
		$vot_endtimeval 	= strtotime($GLOBALS['Reque'] -> funpost("vot_endtime"));
		$vot_selecttypeval 	= $GLOBALS['Reque'] -> funpost("vot_selecttype");

		if($vot_idval !='' and $vot_titleval !='' )
		{

			
			$updatesql = "UPDATE `".SQL_PREFIX."vote` SET  vot_title = '".$vot_titleval."' , vot_starttime = '".$vot_starttimeval."' , vot_endtime = '".$vot_endtimeval."'  , vot_selecttype = '".$vot_selecttypeval."'     WHERE  vot_id = '".$vot_idval."' ";
			
			$GLOBALS['MySql'] -> querySql( $updatesql );
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_losssusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=info&id=".$vot_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
	
	
	
	function item( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."vote  WHERE vot_id = '".$infoid."' ORDER BY `vot_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$sqlitemsql = " SELECT  *  FROM  ".SQL_PREFIX."voteitem  WHERE vot_id = '".$infoid."' LIMIT 100 ";
		
		$infoItem = $GLOBALS['MySql'] -> selectInfo($sqlitemsql);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=vote&act=edititem");
		
		$GLOBALS['Templ'] -> assign('info',$info);
		
		$GLOBALS['Templ'] -> assign('infoItem',$infoItem);
		
		$GLOBALS['Templ'] -> display('voteitemedit_tpl.html');
	
	}
	
	
	function additem()
	{
		$vot_idval 		= $GLOBALS['Reque'] -> funpost("vot_id");
		$addvoi_name 	= $GLOBALS['Reque'] -> funpost("addvoi_name");

		
		//$vot_addtimeval 	= time();
		
		if($vot_idval !='' and $addvoi_name !=''  )
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."voteitem  WHERE vot_id = '".$vot_idval."' and  voi_name = '".$addvoi_name."' ORDER BY `voi_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{
				
				$insertip ="INSERT INTO `".SQL_PREFIX."voteitem` (`vot_id` , `voi_name`  ) VALUES (  '$vot_idval', '$addvoi_name')";
				
				$GLOBALS['MySql'] -> querySql($insertip);
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_additemsusse'] );
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=item&id=".$vot_idval);
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}else{
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_isrealyitemerror']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=item&id=".$vot_idval);
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
			}
			
		}else{
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_lossitemsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=item&id=".$vot_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		
		}
	}
	
	function delitem( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		$vot_idval   = $GLOBALS['Reque'] -> funpost("vot_id");
		$deletesql 	 = "DELETE FROM `".SQL_PREFIX."voteitem` WHERE  `voi_id` =  $infoid and  vot_id = $vot_idval ";
		$GLOBALS['MySql'] -> querySql($deletesql);
		
		//删除单选该项日志记录
		$deletesql 	 = "DELETE FROM `".SQL_PREFIX."votelog` WHERE  `vol_idstr` =  $infoid  and  vot_id = $vot_idval ";
		$GLOBALS['MySql'] -> querySql($deletesql);

		
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delitemsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=item&id=".$vot_idval);
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function edititem( )
	{	
		$vot_idval 	 = $GLOBALS['Reque'] -> funpost("vot_id");
		
		$voi_nameval = $GLOBALS['Reque'] -> funpost("voi_name");

		if($vot_idval !='' and $voi_nameval !='' )
		{

			foreach($voi_nameval as $vikey => $vival )
			{
				
				$updatesql = '';
				$updatesql = "UPDATE `".SQL_PREFIX."voteitem` SET  voi_name = '".$vival."'  WHERE  voi_id = '".$vikey."' ";
				$GLOBALS['MySql'] -> querySql( $updatesql );
				
			}
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_edititemsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=item&id=".$vot_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_lossitemsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=vote&act=item&id=".$vot_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
	
	
		
}

//nmon_x86_rhel4


$Vote = new Vote();

?>