<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Helpsort
{
	function Helpsort()
	{
		//$GLOBALS['WebSe'] -> isOnLine();
		//$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?/login/actlogin");
	}
	
	function info_list(  )
	{
		$page_size = 200 ;		
		$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 ;
		}
		
		$sql_query = " SELECT hes_id,hes_name, hes_parentid, hes_enabled,hes_list FROM  ".$this -> db -> prefix."helpsort  WHERE 1 GROUP BY hes_id ORDER BY hes_list,`hes_id` LIMIT  $limit_num , $page_size ";
		
		$infosm 		= $this -> db -> select_info($sql_query);
		$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;	
				}
		   }
		
		
		
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."helpsort  WHERE 1  ORDER BY `hes_id` ";
		$infocount  = $this -> db -> select_one($sql_query);

		
		$page_str = $this -> page ->get_admin_page( $url = '?load=helpsort&act=info_list', $page_num  , $page_size  , $result_count = $infocount["CNUM"] );
		
		$this -> view -> assign('page_str',$page_str);
		$this -> view -> assign('info',$info);
		$this -> view -> display('helpsort.tpl');
		
	}
	
	function add(  )
	{
		$this -> view ->  caching = true;
		$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24

		$sql_query = " SELECT hes_id, hes_name  FROM ".$this -> db -> prefix."helpsort WHERE hes_parentid = 0  GROUP BY hes_id ORDER BY `hes_id` limit 1000 ";
		
		$asInfo = $this -> db -> select_info($sql_query);
		$this -> view -> assign('asInfo',$asInfo);
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=helpsort&act=add_info");
		$this -> view -> display('helpsortadd.tpl');
	}
	
	function add_info()
	{
		$hes_nameval 		= $this -> input ->  funpost("hes_name");
		$hes_parentidval 	= $this -> input ->  funpost("hes_parentid");
		$hes_enabledval 		= $this -> input ->  funpost("hes_enabled");
		$hes_listval 		= $this -> input ->  funpost("hes_list");
		
		//$hes_addtimeval 	= time();
		
		if($hes_nameval !='' and $hes_enabledval !='' and $hes_listval !='')
		{
			$sql_query = " SELECT count(1) as CNUM  FROM ".$this -> db -> prefix."helpsort WHERE hes_name = '".$hes_nameval."' ORDER BY `hes_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".$this -> db -> prefix."helpsort` (`hes_name` , `hes_parentid` , `hes_enabled` , `hes_list` ) VALUES (  '$hes_nameval', '$hes_parentidval', '$hes_enabledval', '$hes_listval')";
				
				$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=helpsort&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=helpsort&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=helpsort&act=add");
			$this -> view ->  display('suggestion.tpl');		
		}

	}
	
	function del( )
	{
		$infoid = $this -> input ->  get["id"];
		
		//存在分类信息不能删除
				
		$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."help  WHERE hes_id in ( $infoid )  ORDER BY hes_id  ";
		
		$infoOne = $this -> db -> select_one($sql_query);
		
		if( $infoOne["CNUM"] < 1 )
		{
		
			$sqllock = " SELECT hes_id ,hes_locked  FROM  ".$this -> db -> prefix."helpsort  WHERE hes_id in ( $infoid )  ORDER BY hes_id  ";
		
			$infoLock = $this -> db -> select_one($sqllock);
		  
		  
		   if( $infoLock["ars_locked"] == 0 )
		   {
			
				$deletesql ="DELETE FROM `".$this -> db -> prefix."helpsort` WHERE `hes_id` in ( $infoid ) ";
				
				$this -> db -> query_sql($deletesql);
				
				$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=artsort&act=info_list");
				$this -> view ->  display('suggestion.tpl');
				
			}else{

				//被锁定不能删除
				$this -> view ->  assign('info_title',$_LANG['bd_lockerror'] );
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=artsort&act=info_list");
				$this -> view ->  display('suggestion.tpl');

			}
			
		}else{
		
			$this -> view ->  assign('info_title',$_LANG['bd_delerror']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=artsort&act=info_list");
			$this -> view ->  display('suggestion.tpl');
		}
		
		
	}
	
	function info(  )
	{
		$infoid = $this -> input ->  get["id"];
		
		$sql_query = " SELECT *  FROM ".$this -> db -> prefix."helpsort WHERE hes_id = '".$infoid."' ORDER BY `hes_id` ";
		$info = $this -> db -> select_one($sql_query);
		
		$sql_query = " SELECT hes_id, hes_name  FROM ".$this -> db -> prefix."helpsort WHERE hes_parentid = 0 and hes_id <> '".$infoid."'    GROUP BY hes_id ORDER BY `hes_id` limit 1000 ";
		
		$asInfo = $this -> db -> select_info($sql_query);
		$this -> view -> assign('asInfo',$asInfo);
		
		$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=helpsort&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('helpsortedit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$hes_idval 			= $this -> input ->  funpost("hes_id");
		$hes_nameval 		= $this -> input ->  funpost("hes_name");
		$hes_parentidval 	= $this -> input ->  funpost("hes_parentid");
		$hes_enabledval 	= $this -> input ->  funpost("hes_enabled");
		$hes_listval 		= $this -> input ->  funpost("hes_list");
		
		if($hes_idval !='' and $hes_nameval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."helpsort` SET  hes_name = '".$hes_nameval."' , hes_parentid = '".$hes_parentidval."',hes_enabled = '".$hes_enabledval."' , hes_list = '".$hes_listval."'   WHERE  hes_id = '".$hes_idval."' ";
			
			$this -> db -> query_sql( $updatesql );
			
			$this -> view ->  assign('info_title',$this->lang['edit_success']);
			$this -> view ->  assign('URL_TIME',URL_TIME);
			$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=helpsort&act=info_list");
			$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=helpsort&act=info&id=".$hes_idval);
			$this -> view ->  display('suggestion.tpl');
		}
		
	}
		
}

//

$Helpsort = new Helpsort();

?>