<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Moderator
{
	function Moderator()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		
		//$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/login/actlogin");
	}
	
	function infolist(  )
	{
		$pageSize = 20 ;		
		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		
		//left join user as u on u.use_id = m.use_id
		//left join user as u on u.use_id = m.use_id 
		
		$sqlQuery = " SELECT f.for_id, m.mod_id, m.mod_enabled, m.use_id, m.mod_type, u.use_name ,f.for_name FROM ".SQL_PREFIX."moderator as m  left join   ".SQL_PREFIX."forum as f on m.for_id = f.for_id  left join ".SQL_PREFIX."user as u on u.use_id = m.use_id  WHERE 1 GROUP BY mod_id ORDER BY `mod_id` LIMIT  $LIMITnum , $pageSize ";
		
		$info = $GLOBALS['MySql'] -> selectInfo( $sqlQuery );
		
		$sqlQuery = " SELECT count(1) as LNUM  FROM  ".SQL_PREFIX."moderator  WHERE 1  ORDER BY `mod_id`  ";
		
		$infocount = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		//var_dump();
		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=moderator&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["LNUM"]);
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('moderator_tpl.html');
		
	}
	
	function add(  )
	{
		//$GLOBALS['Templ'] -> caching = true;
		//$GLOBALS['Templ'] -> cache_lifetime = 86400 ; //60 *  60 * 24
		
		$sqlQuery = " SELECT for_id,for_name  FROM  ".SQL_PREFIX."forum  WHERE for_enabled = 1 ORDER BY `for_id` ";
		
		$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$GLOBALS['Templ']->assign('info',$info);

		$GLOBALS['Templ']->display('moderatoradd_tpl.html');
	}
	
	function getforum()
	{
		$sqlQuery = " SELECT for_id, for_name,  for_list  FROM  ".SQL_PREFIX."forum  WHERE 1 GROUP BY for_id ORDER BY  for_list , `for_id` LIMIT  100 ";
		
		$infoforum = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$GLOBALS['Templ']->assign('infoforum',$infoforum);
	}
	
	
	
	function addinfo()
	{
		$for_idval 			= $GLOBALS['Reque'] -> funpost("for_id");
		$use_nameval 		= $GLOBALS['Reque'] -> funpost("use_name");
		$mod_typeval 		= $GLOBALS['Reque'] -> funpost("mod_type");
		$mod_enabledval 	= $GLOBALS['Reque'] -> funpost("mod_enabled");

		
		if($for_idval !='' and $mod_typeval !='' and $use_nameval != '')
		{
			
			$sqlQueryuser = " SELECT use_id  FROM  ".SQL_PREFIX."user  WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
			
			$infoUser = $GLOBALS['MySql'] -> selectOne($sqlQueryuser);
			//var_dump($infoUser);
			//exit();
			if($infoUser["use_id"] !='')
			{
					$sqlQuery = " SELECT count(1) as LNUM  FROM  ".SQL_PREFIX."moderator  WHERE for_id = '".$for_idval."' and use_id ='".$infoUser["use_id"]."' ORDER BY `mod_id` ";
					
					$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
					
					if( $info["LNUM"] < 1 )
					{
						$insertip =" INSERT INTO `".SQL_PREFIX."moderator` (`for_id` , `use_id` , `mod_type` , `mod_enabled` ) VALUES (  '$for_idval', '".$infoUser["use_id"]."', '$mod_typeval',  '$mod_enabledval')";
						
						$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=moderator&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=moderator&act=add");
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
					}
			}else{
						$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["LA_nousererror"]);
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=moderator&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=moderator&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}

	}
	
	function del( )
	{
		//$indel = implode(',', $delArray);
		$indel 	= $GLOBALS['Reque'] -> reqval["id"];
		
		$insertip = " DELETE FROM `".SQL_PREFIX."moderator` WHERE `mod_id` in ( $indel ) ";
		
		$GLOBALS['MySql'] -> querySql($insertip);
		$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
		$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
		$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=moderator&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info(  )
	{
		$infoid = $GLOBALS['Reque'] -> reqval["id"];
		
		$sqlQuery = " SELECT m.*, u.use_id ,u.use_name  FROM  ".SQL_PREFIX."moderator as m  , ".SQL_PREFIX."user as u  WHERE u.use_id = m.use_id AND m.mod_id = '".$infoid."' ORDER BY m.`mod_id` ";
		
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		//var_dump($info);
		$GLOBALS['Templ'] -> assign('info',$info);
		
		
		$this -> getforum();
		
		$GLOBALS['Templ'] -> display('moderatoredit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$mod_idval 			= $GLOBALS['Reque'] -> funpost("mod_id");
		$for_idval 			= $GLOBALS['Reque'] -> funpost("for_id");
		$use_nameval 		= $GLOBALS['Reque'] -> funpost("use_name");
		$mod_typeval 		= $GLOBALS['Reque'] -> funpost("mod_type");
		$mod_enabledval 	= $GLOBALS['Reque'] -> funpost("mod_enabled");
		
		if($mod_idval !='' and $for_idval !='' and $use_nameval !='')
		{
			
			$sqlQueryuser = " SELECT use_id  FROM  ".SQL_PREFIX."user  WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
			
			$infoUser = $GLOBALS['MySql'] -> selectOne($sqlQueryuser);
			//var_dump($infoUser);
			//exit();
			if($infoUser["use_id"] !='')
			{
					$sqlQuery = " SELECT count(1) as LNUM  FROM  ".SQL_PREFIX."moderator  WHERE for_id = '".$for_idval."' and use_id ='".$infoUser["use_id"]."' and mod_id <> '".$mod_idval."' ORDER BY `mod_id` ";
					
					$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
					
					if( $info["LNUM"] < 1 )
					{
						$insertip =" UPDATE  `".SQL_PREFIX."moderator` SET  `for_id` = '".$for_idval."' , `use_id` =  '".$infoUser["use_id"]."' , `mod_type` = '".$mod_typeval."'  , `mod_enabled`  = '".$mod_enabledval."' WHERE mod_id = ".$mod_idval;


						$GLOBALS['MySql'] -> querySql( $insertip );
						$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['LA_editsusse']);
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=moderator&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=moderator&act=info&id=".$mod_idval);
						$GLOBALS['Templ'] -> display('suggestion_tpl.html');
							
					}
					
			}else{
						$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']["LA_nousererror"]);
						$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
						$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=moderator&act=info&id=".$mod_idval);
						$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=moderator&act=info&id=".$mod_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
		
}

//nmon_x86_rhel4


$Moderator = new Moderator();

?>