<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Moderator
{
	function Moderator()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		
		//$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/login/actlogin");
	}
	
	function info_list(  )
	{
		$page_size = 20 ;		
		$page_num_val 	= $this -> input ->  req["page_num"];
		
		if( $page_num_val > 1 )
		{
			$page_num  = $page_num_val;
			$limit_num = ceil( ( $page_num_val -1 ) * $page_size );
		}else{
			$page_num  = 1;
			$limit_num = 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 
		
		$sql_query = " SELECT f.for_id, m.mod_id, m.mod_enabled, m.use_id, m.mod_type, u.use_name ,f.for_name FROM ".$this -> db -> prefix."moderator as m  left join   ".$this -> db -> prefix."forum as f on m.for_id = f.for_id  left join ".$this -> db -> prefix."user as u on u.use_id = m.use_id  WHERE 1 GROUP BY mod_id ORDER BY `mod_id` LIMIT  $limit_num , $page_size ";
		
		$info = $this -> db -> select_info( $sql_query );
		
		$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."moderator  WHERE 1  ORDER BY `mod_id`  ";
		
		$infocount = $this -> db -> select_one($sql_query);
		
		//var_dump();
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=moderator&act=info_list', $page_num  , $page_size  , $result_count = $infocount["LNUM"]);
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('moderator.tpl');
		
	}
	
	function add(  )
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ; //60 *  60 * 24
		
		$sql_query = " SELECT for_id,for_name  FROM  ".$this -> db -> prefix."forum  WHERE for_enabled = 1 ORDER BY `for_id` ";
		
		$info = $this -> db -> select_info($sql_query);
		
		$this -> view -> assign('info',$info);

		$this -> view -> display('moderatoradd.tpl');
	}
	
	function getforum()
	{
		$sql_query = " SELECT for_id, for_name,  for_list  FROM  ".$this -> db -> prefix."forum  WHERE 1 GROUP BY for_id ORDER BY  for_list , `for_id` LIMIT  100 ";
		
		$infoforum = $this -> db -> select_info($sql_query);
		
		$this -> view -> assign('infoforum',$infoforum);
	}
	
	
	
	function add_info()
	{
		$for_idval 			= $this -> input ->  funpost("for_id");
		$use_nameval 		= $this -> input ->  funpost("use_name");
		$mod_typeval 		= $this -> input ->  funpost("mod_type");
		$mod_enabledval 	= $this -> input ->  funpost("mod_enabled");

		
		if($for_idval !='' and $mod_typeval !='' and $use_nameval != '')
		{
			
			$sql_queryuser = " SELECT use_id  FROM  ".$this -> db -> prefix."user  WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
			
			$infoUser = $this -> db -> select_one($sql_queryuser);
			//var_dump($infoUser);
			//exit();
			if($infoUser["use_id"] !='')
			{
					$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."moderator  WHERE for_id = '".$for_idval."' and use_id ='".$infoUser["use_id"]."' ORDER BY `mod_id` ";
					
					$info = $this -> db -> select_one($sql_query);
					
					if( $info["LNUM"] < 1 )
					{
						$insertip =" INSERT INTO `".$this -> db -> prefix."moderator` (`for_id` , `use_id` , `mod_type` , `mod_enabled` ) VALUES (  '$for_idval', '".$infoUser["use_id"]."', '$mod_typeval',  '$mod_enabledval')";
						
						$this -> db -> query_sql( $insertip );
						$this -> view ->  assign('info_title',$this->lang['add_success']);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=info_list");
						$this -> view ->  display('suggestion.tpl');	
					}else{
						
						$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=add");
						$this -> view ->  display('suggestion.tpl');	
					}
			}else{
						$this -> view ->  assign('info_title',$_LANG["LA_nousererror"]);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=add");
						$this -> view ->  display('suggestion.tpl');	
			}
		}else{
			
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=add");
			$this -> view ->  display('suggestion.tpl');		}

	}
	
	function del( )
	{
		//$indel = implode(',', $delArray);
		$indel 	= $this -> input ->  req["id"];
		
		$insertip = " DELETE FROM `".$this -> db -> prefix."moderator` WHERE `mod_id` in ( $indel ) ";
		
		$this -> db -> query_sql($insertip);
		$this -> view ->  assign('info_title',$this->lang['del_success']);
		$this -> view ->  assign('URL_TIME',URL_TIME);
		$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info(  )
	{
		$infoid = $this -> input ->  req["id"];
		
		$sql_query = " SELECT m.*, u.use_id ,u.use_name  FROM  ".$this -> db -> prefix."moderator as m  , ".$this -> db -> prefix."user as u  WHERE u.use_id = m.use_id AND m.mod_id = '".$infoid."' ORDER BY m.`mod_id` ";
		
		$info = $this -> db -> select_one($sql_query);
		//var_dump($info);
		$this -> view ->  assign('info',$info);
		
		
		$this -> getforum();
		
		$this -> view ->  display('moderatoredit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$mod_idval 			= $this -> input ->  funpost("mod_id");
		$for_idval 			= $this -> input ->  funpost("for_id");
		$use_nameval 		= $this -> input ->  funpost("use_name");
		$mod_typeval 		= $this -> input ->  funpost("mod_type");
		$mod_enabledval 	= $this -> input ->  funpost("mod_enabled");
		
		if($mod_idval !='' and $for_idval !='' and $use_nameval !='')
		{
			
			$sql_queryuser = " SELECT use_id  FROM  ".$this -> db -> prefix."user  WHERE use_name = '".$use_nameval."' ORDER BY `use_id` ";
			
			$infoUser = $this -> db -> select_one($sql_queryuser);
			//var_dump($infoUser);
			//exit();
			if($infoUser["use_id"] !='')
			{
					$sql_query = " SELECT count(1) as LNUM  FROM  ".$this -> db -> prefix."moderator  WHERE for_id = '".$for_idval."' and use_id ='".$infoUser["use_id"]."' and mod_id <> '".$mod_idval."' ORDER BY `mod_id` ";
					
					$info = $this -> db -> select_one($sql_query);
					
					if( $info["LNUM"] < 1 )
					{
						$insertip =" UPDATE  `".$this -> db -> 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;


						$this -> db -> query_sql( $insertip );
						$this -> view ->  assign('info_title',$_LANG['LA_editsusse']);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=info_list");
						$this -> view ->  display('suggestion.tpl');
						
					}else{
						
						$this -> view ->  assign('info_title',$this->lang['err_info_exist']);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=info&id=".$mod_idval);
						$this -> view ->  display('suggestion.tpl');
							
					}
					
			}else{
						$this -> view ->  assign('info_title',$_LANG["LA_nousererror"]);
						$this -> view ->  assign('URL_TIME',URL_TIME);
						$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=info&id=".$mod_idval);
						$this -> view ->  display('suggestion.tpl');
			}
			
			
 
				
		}else{
		
			$this -> view ->  assign('info_title',$this->lang['err_info_missing']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=moderator&act=info&id=".$mod_idval);
			$this -> view ->  display('suggestion.tpl');	
		}
		
	}
		
}

//


$Moderator = new Moderator();

?>