<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Vidsort
{
	function Vidsort()
	{
		//$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 vis_id,vis_name, vis_count, vis_parentid, vis_enabled,vis_list FROM  ".$this -> db -> prefix."vidsort  WHERE 1 GROUP BY vis_id ORDER BY vis_list,`vis_id` LIMIT  $limit_num , $page_size ";
		
		$infosm 		= $this -> db -> select_info($sql_query);
		$info = array();
		foreach ($infosm as $rowkey => $rowval)
		{
				if($rowval["vis_parentid"] == 0)
				{
					$info[$rowval["vis_id"]]['vis_id'] 		 = $rowval["vis_id"] ;
					$info[$rowval["vis_id"]]['vis_name'] 	 = $rowval["vis_name"] ;
					$info[$rowval["vis_id"]]['vis_count'] 	 = $rowval["vis_count"] ;
					
					$info[$rowval["vis_id"]]['vis_parentid'] = $rowval["vis_parentid"] ;
					$info[$rowval["vis_id"]]['vis_enabled']  = $rowval["vis_enabled"] ;
					$info[$rowval["vis_id"]]['vis_list']     = $rowval["vis_list"];
				}else{
					$info[$rowval["vis_parentid"]]['array'][] = $rowval;	
				}
		}
		
		$sql_query 	= " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."vidsort  WHERE 1  ORDER BY `vis_id` ";
		$infocount  = $this -> db -> select_one($sql_query);
		
		$page_str = $this -> page ->get_admin_page( $url = '?load=vidsort&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('vidsort.tpl');
	}
	
	function numcount( $startsid = 0 )
	{
		//核算数量，是重新核算分类数量
		if($this -> input ->  req["startsid"]!='')
		{
			$startsid  = $this -> input ->  req["startsid"];
		}

			$sql_query = " SELECT vis_id  FROM  ".$this -> db -> prefix."vidsort  WHERE vis_id > '$startsid' GROUP BY vis_id  ORDER BY `vis_id`  LIMIT  0,1 ";
			$infoOne = $this -> db -> select_one( $sql_query );
			
			$whereadd = '';
			$sinid  = '';
			//是否存在子分类
			if($infoOne["vis_id"] != '')
			{
				$sql_query = " SELECT vis_id  FROM  ".$this -> db -> prefix."vidsort  WHERE vis_parentid = ".$infoOne["vis_id"]." GROUP BY vis_id  ORDER BY `vis_id` ";
				$sortInfo = $this -> db -> select_info( $sql_query );
				
				
				$sinid = $infoOne["vis_id"];
				if($sortInfo!='')
				{
					foreach( $sortInfo as $skey => $sval )
					{
						$sinid .= ','.$sval["vis_id"];
					}
					$whereadd .= " AND  vis_id IN ( ".$sinid." ) ";
					
				}else{
					$whereadd .= " AND  vis_id IN ( ".$infoOne["vis_id"]." ) ";
				}
				
				$sql_querycount = " SELECT count( vid_id ) as NUMCOUNT   FROM  ".$this -> db -> prefix."video WHERE vid_delete = 0   $whereadd  ";
				$infocount = $this -> db -> select_one( $sql_querycount );
				
				$sqlupdate = " UPDATE ".$this -> db -> prefix."vidsort SET vis_count = '".$infocount["NUMCOUNT"]."' WHERE vis_id = '".$infoOne["vis_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=vidsort&act=numcount&startsid=".$infoOne["vis_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=vidsort&act=info_list");
				$this -> view ->  display('suggestion.tpl');	
			}
			//查询数量

	}

	
	
	function add(  )
	{
		//$this -> view ->  caching = true;
		//$this -> view ->  cache_lifetime = 86400 ;  //60 *  60 * 24
		
		$sql_query = " SELECT vis_id, vis_name  FROM  ".$this -> db -> prefix."vidsort  WHERE vis_parentid = 0  GROUP BY vis_id ORDER BY `vis_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=vidsort&act=add_info");
		$this -> view -> display('vidsortadd.tpl');
		
	}
	
	function add_info()
	{
		$vis_nameval 		= $this -> input ->  funpost("vis_name");
		$vis_parentidval 	= $this -> input ->  funpost("vis_parentid");
		$vis_enabledval 	= $this -> input ->  funpost("vis_enabled");
		$vis_listval 		= $this -> input ->  funpost("vis_list");

		if($vis_nameval !='' and $vis_enabledval !='' and $vis_listval !='')
		{
			//同级不能同名，不同级可以同名
			$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix."vidsort  WHERE vis_name = '".$vis_nameval."' and vis_parentid ='".$vis_parentidval."' ORDER BY `vis_id` "; 
			$info = $this -> db -> select_one($sql_query);
			
			if( $info["CNUM"] < 1 )
			{

				$insertip ="INSERT INTO `".$this -> db -> prefix."vidsort` (`vis_name` , `vis_parentid` , `vis_enabled` , `vis_list` ) VALUES (  '$vis_nameval', '$vis_parentidval', '$vis_enabledval', '$vis_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=vidsort&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=vidsort&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=vidsort&act=add");
			$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."vidsort` WHERE `vis_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=vidsort&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."vidsort  WHERE vis_id = '".$infoid."' ORDER BY `vis_id` ";
		
		$info = $this -> db -> select_one($sql_query);
		
		//可选择的一级分类 
		$sql_query = " SELECT vis_id, vis_name  FROM ".$this -> db -> prefix."vidsort  WHERE vis_parentid = 0 and vis_id <> '".$infoid."'    GROUP BY vis_id ORDER BY `vis_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=vidsort&act=edit_info");
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('vidsortedit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$vis_idval 			= $this -> input ->  funpost("vis_id");
		$vis_nameval 		= $this -> input ->  funpost("vis_name");
		$vis_parentidval 	= $this -> input ->  funpost("vis_parentid");
		$vis_enabledval 	= $this -> input ->  funpost("vis_enabled");
		$vis_listval 		= $this -> input ->  funpost("vis_list");
		
		if($vis_idval !='' and $vis_nameval !='' )
		{

			$updatesql = "UPDATE `".$this -> db -> prefix."vidsort` SET  vis_name = '".$vis_nameval."' , vis_parentid = '".$vis_parentidval."',vis_enabled = '".$vis_enabledval."' , vis_list = '".$vis_listval."'   WHERE  vis_id = '".$vis_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=vidsort&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=vidsort&act=info&id=".$vis_idval);
			$this -> view ->  display('suggestion.tpl');
		}
		
		
	}
		
}

$Vidsort = new Vidsort();

?>