<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Thread
{
	function Thread()
	{
		//$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 ;
		}
		
		if($GLOBALS['Reque'] -> reqval["keyword"] !='')
		{   
			$keywordval 	= $GLOBALS['Reque'] -> reqval["keyword"];
			$keywordval  	= urldecode($keywordval); 
		}
		if($keywordval!='')
		{ 	
			$endArray["keyword"] =$keywordval;
			
			$endurl 	   .= '&keyword='.urlencode($keywordval);
			$whereadd .= " AND ( thr_title LIKE  '%".$keywordval."%' )";  
		}
		
		$for_idval 		= $GLOBALS['Reque'] -> reqval["for_id"];
		
		if($for_idval!='' and $for_idval !='A' )
		{ 
			
			$endArray["for_id"] = $for_idval;
			 
			$whereadd .= " AND ( t.for_id  = ".$for_idval."  )";

		 }else{
		 
		 	$endurl 	   .= '&for_id=A'; 	
		 }
		
		
		$this -> getforum();
		
		$sqlQuery = " SELECT thr_id, thr_title, thr_file, thr_list ,f.for_name,thr_enabled FROM  ".SQL_PREFIX."thread as t left join ".SQL_PREFIX."forum as f on t.for_id = f.for_id   WHERE 1  $whereadd GROUP BY thr_id ORDER BY `thr_id` LIMIT  $LIMITnum , $pageSize ";
		
		$info = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		
		$sqlQuery = " SELECT count(1) as LNUM  FROM  ".SQL_PREFIX."thread as t  WHERE 1  $whereadd  ORDER BY `thr_id`  ";
		
		$infocount = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		//var_dump();
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=thread&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["LNUM"], $endArray);
		
		$GLOBALS['Templ']->assign('for_idval',$for_idval);
		$GLOBALS['Templ']->assign('keywordval',$keywordval);
		
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('thread_tpl.html');
		
	}
	
	function add(  )
	{
		//$GLOBALS['Templ'] -> caching = true;
		///$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$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);
		
		
 		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'thr_desc' ,'','Basic' );	//加载文本编辑器  具体描述

		$GLOBALS['Templ']->display('threadadd_tpl.html');
		
		
	}
	
	function addinfo()
	{
		$thr_titleval 	= $GLOBALS['Reque'] -> funpost("thr_title");
		$for_idval 		= $GLOBALS['Reque'] -> funpost("for_id");
		$thr_descval 	= $GLOBALS['Reque'] -> funpost("thr_desc");
		$thr_fileval 	= $GLOBALS['Reque'] -> funpost("thr_file");
		$thr_listval 	= $GLOBALS['Reque'] -> funpost("thr_list");
		$thr_enabledval  = $GLOBALS['Reque'] -> funpost("thr_enabled");
		
		if($thr_titleval and $thr_descval)
		{
			$sqlQuery = " SELECT count(1) as LNUM  FROM  ".SQL_PREFIX."thread  WHERE thr_title = '".$thr_titleval."' and for_id = '".$for_idval."'  ORDER BY `thr_id` ";
			
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["LNUM"] < 1 )
			{
				$insertip =" INSERT INTO `".SQL_PREFIX."thread` (`thr_title` , `for_id` ,  `thr_desc` , `thr_file` , `thr_list` , `thr_enabled` ) VALUES (  '$thr_titleval', '$for_idval','$thr_descval', '$thr_fileval', '$thr_listval', '$thr_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=thread&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=thread&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=thread&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		}
			//var_dump($_REQUEST);
	}
	
	function del( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		$insertip = " DELETE FROM `".SQL_PREFIX."thread` WHERE `thread`.`thr_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=thread&act=infolist");
		$GLOBALS['Templ'] -> display('suggestion_tpl.html');
	}
	
	function info(  )
	{
		$infoid = $GLOBALS['Reque'] -> reqval["id"];
		
		$sqlQuery = " SELECT *  FROM  ".SQL_PREFIX."thread  WHERE thr_id = '".$infoid."' ORDER BY `thr_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		//var_dump($info);
		$GLOBALS['Templ'] -> assign('info',$info);
		
		$this -> getforum();
		
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Helpe'] -> load_ckeditor( $textName = 'thr_desc' ,$info["thr_desc"],'Basic' );	//加载文本编辑器  具体描述
		
		$GLOBALS['Templ'] -> display('threadedit_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 editinfo( )
	{	
	
		$thr_idval 		= $GLOBALS['Reque'] -> funpost("thr_id");
		$thr_titleval 	= $GLOBALS['Reque'] -> funpost("thr_title");
		$for_idval 		= $GLOBALS['Reque'] -> funpost("for_id");
		$thr_descval 	= $GLOBALS['Reque'] -> funpost("thr_desc");
		$thr_fileval 	= $GLOBALS['Reque'] -> funpost("thr_file");
		$thr_listval 	= $GLOBALS['Reque'] -> funpost("thr_list");
		$thr_enabledval  = $GLOBALS['Reque'] -> funpost("thr_enabled");
		
		if($thr_titleval !='' and $thr_descval !='' and $thr_idval !='')
		{
			$updatethread = "UPDATE `".SQL_PREFIX."thread` SET  thr_title = '".$thr_titleval."' , thr_desc = '".$thr_descval."' , thr_file = '".$thr_fileval."', thr_list = '".$thr_listval."',thr_enabled = '".$thr_enabledval."'  WHERE  thr_id = '".$thr_idval."' ";
			
			$GLOBALS['MySql'] -> querySql( $updatethread );
			
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_editsusse']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=thread&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=thread&act=info&id=".$thr_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');	
		}
		
	}
		
}

//nmon_x86_rhel4


$Thread = new Thread();

?>