<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Picsort
{
	function Picsort()
	{
		//$GLOBALS['WebSe'] -> isOnLine();

	}
	
	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 pis_id,sort_name,pis_count, pis_parentid, pis_enabled,pis_list FROM  ".$this -> db -> prefix."picsort  WHERE 1 GROUP BY pis_id ORDER BY pis_list,`pis_id` LIMIT  $limit_num , $page_size ";
		
		$infosm 		= $this -> db -> select_info($sql_query);
		
		$info = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["pis_parentid"] == 0)
				{
					$info[$rowval["pis_id"]]['pis_id'] 		 = $rowval["pis_id"] ;
					$info[$rowval["pis_id"]]['sort_name'] 	 = $rowval["sort_name"] ;
					$info[$rowval["pis_id"]]['pis_count'] 	 = $rowval["pis_count"] ;
					
					$info[$rowval["pis_id"]]['pis_parentid'] = $rowval["pis_parentid"] ;
					$info[$rowval["pis_id"]]['pis_enabled']  = $rowval["pis_enabled"] ;
					$info[$rowval["pis_id"]]['pis_list'] 	 = $rowval["pis_list"];
				}else{
					$info[$rowval["pis_parentid"]]['array'][] = $rowval;	
				}
		}
		
		//var_dump($info);
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."picsort  WHERE 1  ORDER BY `pis_id` ";
		$infocount  = $this -> db -> select_one($sql_query);
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=picsort&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('picsort.tpl');
	}
	

	
	function add(  )
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$sql_query = " SELECT pis_id, sort_name  FROM  ".$this -> db -> prefix."picsort  WHERE pis_parentid = 0  GROUP BY pis_id ORDER BY `pis_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=picsort&act=add_info");
		$this -> view -> display('picsortadd.tpl');
		
	}
	
	function add_info()
	{
		$sort_nameval 		= $this -> input ->  funpost("sort_name");
		$pis_parentidval 	= $this -> input ->  funpost("pis_parentid");
		$pis_enabledval 	= $this -> input ->  funpost("pis_enabled");
		$pis_listval 		= $this -> input ->  funpost("pis_list");

		if($sort_nameval !='' and $pis_enabledval !='' and $pis_listval !='')
		{
			//同级不能同名，不同级可以同名
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."picsort  WHERE sort_name = '".$sort_nameval."' and pis_parentid ='".$pis_parentidval."' ORDER BY `pis_id` "; 
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".$this -> db -> prefix."picsort` (`sort_name` , `pis_parentid` , `pis_enabled` , `pis_list` ) VALUES (  '$sort_nameval', '$pis_parentidval', '$pis_enabledval', '$pis_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=picsort&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=picsort&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=picsort&act=add");
			$this -> view ->  display('suggestion.tpl');	
				
		}

	}
	
	function numcount( $startsid = 0 )
	{
		//核算数量，是重新核算分类数量
		if($this -> input ->  req["startsid"]!='')
		{
			$startsid  = $this -> input ->  req["startsid"];
		}

			$sql_query = " SELECT pis_id  FROM  ".$this -> db -> prefix."picsort  WHERE pis_id > '$startsid' GROUP BY pis_id  ORDER BY `pis_id`  LIMIT  0,1 ";
 
			$infoOne = $this -> db -> select_one( $sql_query );
			
			$whereadd = '';
			$sinid  = '';
			
			//是否存在子分类
			if($infoOne["pis_id"] != '')
			{
				$sql_query = " SELECT pis_id  FROM  ".$this -> db -> prefix."picsort  WHERE pis_parentid = ".$infoOne["pis_id"]." GROUP BY pis_id  ORDER BY `pis_id` ";
				$sortInfo = $this -> db -> select_info( $sql_query );
				
				
				$sinid = $infoOne["pis_id"];
				if($sortInfo!='')
				{
					foreach( $sortInfo as $skey => $sval )
					{
						$sinid .= ','.$sval["pis_id"];
					}
					$whereadd .= " AND  pis_id IN ( ".$sinid." ) ";
					
				}else{
					$whereadd .= " AND  pis_id IN ( ".$infoOne["pis_id"]." ) ";
				}
				
				$sql_querycount = " SELECT count( pic_id ) as NUMCOUNT   FROM  ".$this -> db -> prefix."picture WHERE pic_delete = 0   $whereadd  ";
				$infocount = $this -> db -> select_one( $sql_querycount );
				
				$sqlupdate = " UPDATE ".$this -> db -> prefix."picsort SET pis_count = '".$infocount["NUMCOUNT"]."' WHERE pis_id = '".$infoOne["pis_id"]."'";
				$this -> db -> query_sql( $sqlupdate );
					
				$this -> view ->  assign('info_title',$_LANG['bd_numing']);
				$this -> view ->  assign('URL_TIME',0);
				
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=picsort&act=numcount&startsid=".$infoOne["pis_id"]);
				$this -> view ->  display('suggestion.tpl');	
				
				
			}else{
			
				$this -> view ->  assign('info_title',$_LANG['bd_numsusse']);
				$this -> view ->  assign('URL_TIME',URL_TIME);
				$this -> view ->  assign('get_url',ADMIN_WEBURL."/index.php?load=picsort&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}
			//查询数量

	}
	
	
	
	function del()
	{
		$idval = $this -> input ->  get["id"];
		
		if(!$idval){
			$indel = implode(',', $delArray);
		}else{
			$indel = $idval;
		}
		
		$deletesql ="DELETE FROM `".$this -> db -> prefix."picsort` WHERE `pis_id` in ( $indel ) ";
		$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=picsort&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}
	
	function info(  )
	{
		$idval = $this -> input ->  get["id"];
		
		if(!$idval){
			$infoid = $infoArray[0];
		}else{
			$infoid = $idval;
		}

		$sql_query = " SELECT *  FROM  ".$this -> db -> prefix."picsort  WHERE pis_id = '".$infoid."' ORDER BY `pis_id` ";
		
		$info = $this -> db -> select_one($sql_query);
		
		//可选择的一级分类 
		$sql_query = " SELECT pis_id, sort_name  FROM ".$this -> db -> prefix."picsort  WHERE pis_parentid = 0 and pis_id <> '".$infoid."'    GROUP BY pis_id ORDER BY `pis_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=picsort&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('picsortedit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$pis_idval 			= $this -> input ->  funpost("pis_id");
		$sort_nameval 		= $this -> input ->  funpost("sort_name");
		$pis_parentidval 	= $this -> input ->  funpost("pis_parentid");
		$pis_enabledval 	= $this -> input ->  funpost("pis_enabled");
		$pis_listval 		= $this -> input ->  funpost("pis_list");
		
		if($pis_idval !='' and $sort_nameval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."picsort` SET  sort_name = '".$sort_nameval."' , pis_parentid = '".$pis_parentidval."',pis_enabled = '".$pis_enabledval."' , pis_list = '".$pis_listval."'   WHERE  pis_id = '".$pis_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=picsort&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=picsort&act=info&id=".$pis_idval);
			$this -> view ->  display('suggestion.tpl');
		}
		
		
	}
		
}

$Picsort = new Picsort();

?>