<?php
if (!defined('GV32_COM')) exit('GV32.COM No direct script access allowed');
class Helpsort
{
	function Helpsort()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php/login/actlogin");
	}
	
	function infolist(  )
	{
		$pageSize = 200 ;		
		$pageNumval 	= $GLOBALS['Reque'] -> reqval["pageNum"];
		
		if( $pageNumval > 1 )
		{
			$pageNum  = $pageNumval;
			$LIMITnum = ceil( ( $pageNumval -1 ) * $pageSize );
		}else{
			$pageNum  = 1;
			$LIMITnum = 0 ;
		}
		
		$sqlQuery = " SELECT hes_id,hes_name, hes_parentid, hes_enabled,hes_list FROM  ".SQL_PREFIX."helpsort  WHERE 1 GROUP BY hes_id ORDER BY hes_list,`hes_id` LIMIT  $LIMITnum , $pageSize ";
		
		$infosm 		= $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$info = array();
		   foreach ($infosm as $rowkey => $rowval)
		   {
		   		if($rowval["hes_parentid"] == 0)
				{
				
					$info[$rowval["hes_id"]]['hes_id'] = $rowval["hes_id"] ;
					$info[$rowval["hes_id"]]['hes_name'] = $rowval["hes_name"] ;
					$info[$rowval["hes_id"]]['hes_parentid'] = $rowval["hes_parentid"] ;
					$info[$rowval["hes_id"]]['hes_enabled'] = $rowval["hes_enabled"] ;
					$info[$rowval["hes_id"]]['hes_list'] = $rowval["hes_list"] ;
				}else{
					$info[$rowval["hes_parentid"]]['array'][] = $rowval;	
				}
		   }
		
		
		
		$sqlQuery 	= " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."helpsort  WHERE 1  ORDER BY `hes_id` ";
		$infocount  = $GLOBALS['MySql'] -> selectOne($sqlQuery);

		
		$PageStr = $GLOBALS['Page']->buildPageStr( $url = '?load=helpsort&act=infolist', $pageNum  , $pageSize  , $resultCount = $infocount["CNUM"] );
		
		$GLOBALS['Templ']->assign('PageStr',$PageStr);
		$GLOBALS['Templ']->assign('info',$info);
		$GLOBALS['Templ']->display('helpsort_tpl.html');
		
	}
	
	function add(  )
	{
		$GLOBALS['Templ'] -> caching = true;
		$GLOBALS['Templ'] -> cache_lifetime = 86400 ;  //60 *  60 * 24

		$sqlQuery = " SELECT hes_id, hes_name  FROM ".SQL_PREFIX."helpsort WHERE hes_parentid = 0  GROUP BY hes_id ORDER BY `hes_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=helpsort&act=addinfo");
		$GLOBALS['Templ']->display('helpsortadd_tpl.html');
	}
	
	function addinfo()
	{
		$hes_nameval 		= $GLOBALS['Reque'] -> funpost("hes_name");
		$hes_parentidval 	= $GLOBALS['Reque'] -> funpost("hes_parentid");
		$hes_enabledval 		= $GLOBALS['Reque'] -> funpost("hes_enabled");
		$hes_listval 		= $GLOBALS['Reque'] -> funpost("hes_list");
		
		//$hes_addtimeval 	= time();
		
		if($hes_nameval !='' and $hes_enabledval !='' and $hes_listval !='')
		{
			$sqlQuery = " SELECT count(1) as CNUM  FROM ".SQL_PREFIX."helpsort WHERE hes_name = '".$hes_nameval."' ORDER BY `hes_id` ";
			$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".SQL_PREFIX."helpsort` (`hes_name` , `hes_parentid` , `hes_enabled` , `hes_list` ) VALUES (  '$hes_nameval', '$hes_parentidval', '$hes_enabledval', '$hes_listval')";
				
				$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=helpsort&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=helpsort&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=helpsort&act=add");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');		
		}

	}
	
	function del( )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		//存在分类信息不能删除
				
		$sqlQuery = " SELECT count(1) as CNUM  FROM  ".SQL_PREFIX."help  WHERE hes_id in ( $infoid )  ORDER BY hes_id  ";
		
		$infoOne = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		if( $infoOne["CNUM"] < 1 )
		{
		
			$sqllock = " SELECT hes_id ,hes_locked  FROM  ".SQL_PREFIX."helpsort  WHERE hes_id in ( $infoid )  ORDER BY hes_id  ";
		
			$infoLock = $GLOBALS['MySql'] -> selectOne($sqllock);
		  
		  
		   if( $infoLock["ars_locked"] == 0 )
		   {
			
				$deletesql ="DELETE FROM `".SQL_PREFIX."helpsort` WHERE `hes_id` in ( $infoid ) ";
				
				$GLOBALS['MySql'] -> querySql($deletesql);
				
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delsusse']);
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=artsort&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');
				
			}else{

				//被锁定不能删除
				$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_lockerror'] );
				$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
				$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=artsort&act=infolist");
				$GLOBALS['Templ'] -> display('suggestion_tpl.html');

			}
			
		}else{
		
			$GLOBALS['Templ'] -> assign('infomessage',$GLOBALS['_LANG']['bd_delerror']);
			$GLOBALS['Templ'] -> assign('URL_TIME',URL_TIME);
			$GLOBALS['Templ'] -> assign('geturl',EMPLOYEE_WEBURL."/index.php?load=artsort&act=infolist");
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
		
	}
	
	function info(  )
	{
		$infoid = $GLOBALS['Reque'] -> getval["id"];
		
		$sqlQuery = " SELECT *  FROM ".SQL_PREFIX."helpsort WHERE hes_id = '".$infoid."' ORDER BY `hes_id` ";
		$info = $GLOBALS['MySql'] -> selectOne($sqlQuery);
		
		$sqlQuery = " SELECT hes_id, hes_name  FROM ".SQL_PREFIX."helpsort WHERE hes_parentid = 0 and hes_id <> '".$infoid."'    GROUP BY hes_id ORDER BY `hes_id` limit 1000 ";
		
		$asInfo = $GLOBALS['MySql'] -> selectInfo($sqlQuery);
		$GLOBALS['Templ']->assign('asInfo',$asInfo);
		
		$GLOBALS['Templ']->assign('actUrl',EMPLOYEE_WEBURL."/index.php?load=helpsort&act=editinfo");
		$GLOBALS['Templ'] -> assign('info',$info);
		$GLOBALS['Templ'] -> display('helpsortedit_tpl.html');
	
	}
	
	
	function editinfo( )
	{	
	
		$hes_idval 			= $GLOBALS['Reque'] -> funpost("hes_id");
		$hes_nameval 		= $GLOBALS['Reque'] -> funpost("hes_name");
		$hes_parentidval 	= $GLOBALS['Reque'] -> funpost("hes_parentid");
		$hes_enabledval 	= $GLOBALS['Reque'] -> funpost("hes_enabled");
		$hes_listval 		= $GLOBALS['Reque'] -> funpost("hes_list");
		
		if($hes_idval !='' and $hes_nameval !='' )
		{

			$updatesql = "UPDATE `".SQL_PREFIX."helpsort` SET  hes_name = '".$hes_nameval."' , hes_parentid = '".$hes_parentidval."',hes_enabled = '".$hes_enabledval."' , hes_list = '".$hes_listval."'   WHERE  hes_id = '".$hes_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=helpsort&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=helpsort&act=info&id=".$hes_idval);
			$GLOBALS['Templ'] -> display('suggestion_tpl.html');
		}
		
	}
		
}

//nmon_x86_rhel4

$Helpsort = new Helpsort();

?>