<?php
if (!defined('HUCART_COM')) exit('HuCart.COM No direct script access allowed');
class Sort extends HC_Controller
{
	//注意无须构造函数,使用了构造函数得重构父类 parent::__construct();
	var $master_arr = array('dow_sort','art_sort','vid_sort','pic_sort');
	var $master_val = '';
	function __construct()
	{
		parent::__construct();
		$this->load_lang('adminsys/sort');
		$this -> load('admin'); 
		$this -> admin -> online_admin();
		$this -> load_db();
		$this -> load('helper'); //加载辅助类
		$this -> load_model('sort');
		$this -> master_val = $this -> input ->  req["master"];
		if( $this -> master_val =='' or !in_array( $this -> master_val,$this->master_arr) ){
			exit('error!');
		}
	}
	
	function info_list()
	{
 
			$this -> load('page',$this->lang['page']);
			$this -> page -> set_page_size(1000);
			
			//列表字段显示
			$show_arr = array('sort_id' => array('th'=>0,'td'=>''),'sort_name'=> array('th'=>0,'td'=>'') ,'sort_count'=> array('th'=>0,'td'=>''),'sort_enabled'=> array('th'=>0,'td'=>''),'sort_list'=> array('th'=>0,'td'=>''));
			$field_show = $this -> helper -> get_th_list( $show_arr , $list_val , $list_type , $lang_arr = $this->lang['field_list'] );
	
			$table_arr['table']		  = $this -> master_val;
			$table_arr['prefix'] 	  = $this -> db -> prefix;
			$table_arr['field_arr']   = array_keys($show_arr);
			$table_arr['field_arr'][] = 'sort_parent_id'; //新添加一个父级ID
			$table_arr['def_where']   = ' 1 ';
			$table_arr['group_by']    = '';
			$table_arr['order_by']    = 'sort_id';
			$table_arr['limit_val']   = $this -> page ->get_limit_val();
			
			$re_sql   = $this -> helper -> get_list_sql( $table_arr );
			
			$infosm   = $this -> db -> select_info( $re_sql['sql_list'] );
			
			$info 	  = $this -> helper -> grade_info_sort( $infosm , $key_id = 'sort_id', $parent_id = 'sort_parent_id', $grade_val = 6 );
			
			//var_dump($info);
			$count  = $this -> db -> select_one( $re_sql['sql_count']  );
			
			$page_str 	= $this -> page -> get_admin_page(  $count["CNUM"] ,'info_list');
			
			$this -> view -> assign('add_info',	 	$add_info);
			$this -> view -> assign('master',	 	$this -> master_val);
			$this -> view -> assign('master_title',	$this->lang['title_arr'][$this -> master_val]);
			$this -> view -> assign('colspan_val',	count($show_arr)+1);
			$this -> view -> assign('field_show',	$field_show);
			$this -> view -> assign('page_str',		$page_str);
			$this -> view -> assign('info',			$info);
			$this -> view -> display('sort_list.tpl');
			

	}

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

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

	}
 
	function delete( )
	{
		$idval = $this -> input ->  get["id"];
		
		if(!$idval){
			$indel = implode(',', $delArray);
		}else{
			$indel = $idval;
		}
		
		$deletesql ="DELETE FROM `".$this -> db -> prefix.$this -> master_val."` WHERE `sort_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=sort&master=".$this -> master_val."&act=info_list");
		$this -> view ->  display('suggestion.tpl');
	}

 
	function info()
	{
		$act_type 	= $this -> input -> get["act_type"];
		
		if($act_type=='edit'){
			
			$id_val = $this -> input ->  get["id"];

			$sql_query = " SELECT *  FROM  ".$this -> db -> prefix.$this -> master_val."  WHERE sort_id = '".$id_val."' ORDER BY `sort_id` ";
			$info = $this -> db -> select_one($sql_query);
			
			//可选择的一级分类 
			$asInfo 	= $this -> sort_mod -> get_sort($this -> master_val,'sort_');
			$sminfo 	= $this -> helper -> grade_info_sort( $asInfo , $key_id = 'sort_id', $parent_id = 'sort_parent_id', $grade_val = 4 );
			//var_dump($sminfo);
			
			$this -> view ->  assign('txt_title',$this->lang['add_sort_title']);
			$this -> view -> assign('edit_submit', $this-> lang['edit']);
			$this -> view -> assign('info',$info);
			$this -> view -> assign('act_type',$act_type);
			$this -> view -> assign('sminfo',$sminfo);
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=sort&master=".$this -> master_val."&act=edit_info&act_type=edit");
			
			
		}else{
		

			$asInfo 	= $this -> sort_mod -> get_sort($this -> master_val,'sort_');
			$sminfo 	= $this -> helper -> grade_info_sort( $asInfo , $key_id = 'sort_id', $parent_id = 'sort_parent_id', $grade_val = 4 );
			
			$this -> view -> assign('act_url',ADMIN_WEBURL."/index.php?load=sort&master=".$this -> master_val."&act=edit_info&act_type=add");
			
			$this -> view -> assign('act_type',$act_type);
			$this -> view ->  assign('txt_title',$this->lang['add_sort_title']);
			$this -> view -> assign('edit_submit', $this-> lang['add']);
			
		}

		$this -> view -> assign('master_title',	$this->lang['title_arr'][$this -> master_val]);
		$this -> view -> assign('txt_title', $this-> lang['add_title']);
		$this -> view -> assign('sminfo',$sminfo);
		$this -> view ->  assign('info',$info);
		$this -> view ->  display('sort_edit.tpl');
	
	}
	
	
	function edit_info( )
	{	
	
		$sort_idval 		= $this -> input ->  funpost("sort_id");
		$sort_nameval 		= $this -> input ->  funpost("sort_name");
		$sort_parent_idval 	= $this -> input ->  funpost("sort_parent_id");
		$sort_enabledval 	= $this -> input ->  funpost("sort_enabled");
		$sort_listval 		= $this -> input ->  funpost("sort_list");
		$act_type 			= $this -> input ->  funpost("act_type");
		
		if($act_type=='edit')
		{
			if($sort_idval !='' and $sort_nameval !='' )
			{
	
				$updatesql = "UPDATE `".$this -> db -> prefix.$this -> master_val."` SET  sort_name = '".$sort_nameval."' , sort_parent_id = '".$sort_parent_idval."',sort_enabled = '".$sort_enabledval."' , sort_list = '".$sort_listval."'   WHERE  sort_id = '".$sort_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=sort&master=".$this -> master_val."&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=sort&master=".$this -> master_val."&act=info&act_type=edit&id=".$sort_idval);
				$this -> view ->  display('suggestion.tpl');
			}
			
		//添加分类
		}else{
			
			if($sort_nameval !='' and $sort_enabledval !='' and $sort_listval !='')
			{
				//同级不能同名，不同级可以同名
				$sql_query = " SELECT count(1) as CNUM  FROM  ".$this -> db -> prefix.$this -> master_val."  WHERE sort_name = '".$sort_nameval."' and sort_parent_id ='".$sort_parent_idval."' ORDER BY `sort_id` "; 
				$info = $this -> db -> select_one($sql_query);
				
				if( $info["CNUM"] < 1 )
				{
	
					$insertip ="INSERT INTO `".$this -> db -> prefix.$this -> master_val."` (`sort_name` , `sort_parent_id` , `sort_enabled` , `sort_list` ) VALUES (  '$sort_nameval', '$sort_parent_idval', '$sort_enabledval', '$sort_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=sort&master=".$this -> master_val."&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=sort&master=".$this -> master_val."&act=info&act_type=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=sort&master=".$this -> master_val."&act=info&act_type=add");
				$this -> view ->  display('suggestion.tpl');	
					
			}
		}
		
	}
		
}
