<?
class glo_category 
{
	function get_category($cat_type,$is_num=false)
	{
		global $db_obj;
		$ctype	=	($is_num)?'is_num':'';
		$ctype	=	$cat_type.$ctype;
		$this->cat_type		=	$cat_type;
		if($this->list_res[$ctype]){
			return $this->list_res[$ctype];
		}
		if($is_num){
			if($cat_type=='software'){
				$category_tmp_res		=	$db_obj->getRecords("SELECT a.* ,count(b.software_id) AS soft_num  FROM {$db_obj->db_pre}category a LEFT JOIN {$db_obj->db_pre}software  b ON a.cat_id=b.cat_id  WHERE cat_type='software' AND a.is_open=1 GROUP BY a.cat_id   ");
			}elseif($cat_type=='article'){
				$category_tmp_res		=	$db_obj->getRecords("SELECT a.* ,count(b.article_id) AS art_num  FROM {$db_obj->db_pre}category a LEFT JOIN {$db_obj->db_pre}article  b ON a.cat_id=b.cat_id  WHERE cat_type='article' AND a.is_open=1 i GROUP BY a.cat_id ");
			}		
		}else{
			$category_tmp_res		=	$db_obj->getRecords("SELECT sort_order,cat_id,seo_desc,seo_keywords,cat_name,parent_id FROM {$db_obj->db_pre}category WHERE  cat_type='".$db_obj->escape($cat_type)."' AND is_open=1  ");
		}
		if(!$category_tmp_res){
			return false;
		}else{
			$this->list_res[$ctype]	=	$this->structural_category($category_tmp_res);
			return $this->list_res[$ctype];
		}
	}

	function structural_category($category_tmp_res,$name_length=50)
	{
		$category_res			=	array();
		foreach($category_tmp_res as $val){
			if($this->cat_type=='article'){
				$url		=		'article_list.php';
			}else{
				global $list_type_url;
				$url		=		$list_type_url?$list_type_url:'product.php';
			}
			
			$val['cat_url']		=	get_url($url.'?cat_id='.$val['cat_id'],$val['cat_name']);
			$val['cat_name']		=	htmlspecialchars(sub_str($val['cat_name'],$name_length),ENT_QUOTES);
			if($val['parent_id']==0){
				$category_res[$val['cat_id']]['parent']		=	$val;	
			}else{
				$category_res[$val['parent_id']]['children'][$val['cat_id']]	=	$val;
			}
		}	
		$num_key		=	isset($val['soft_num'])?'soft_num':(isset($val['art_num'])?'art_num':'');
		if($num_key){
			foreach($category_res as $key=>$val){
				$num	=	$val['parent'][$num_key];
				if($val['children']){
					foreach($val['children'] as $c_key=>$c_val){
						$num	+=	$c_val[$num_key];
					}
				}
				$category_res[$key]['parent'][$num_key]	=	$num;
			}
		}
		return $category_res;
	}	

	function recursion_category($cat_type,$cat_id,$return_name=false)
	{
			$cat_res			=		$this->get_category($cat_type);
			$cat_id_str	=	$cat_id.',';
			foreach($cat_res as $cat_val){
				if($cat_val['parent']['cat_id']==$cat_id&&$cat_val['children']){
						foreach($cat_val['children'] as $c_cat_val){
							$cat_id_str.=$c_cat_val['cat_id'].',';	
						}
						break;
				}	
			}	
			if($return_name){
				foreach($cat_res as $cat_val){
					if($cat_val['parent']['cat_id']==$cat_id){
						$cat_name	=	$cat_val['parent']['cat_name'];
					}else{
						$cat_name	=	$cat_val['children'][$cat_id]?$cat_val['children'][$cat_id]['cat_name']:'';
					}
					if($cat_name){
						break;
					}
				}
			}
			$cat_id_str	=	substr($cat_id_str,0,-1);
			if($return_name){
				return array(
					'cat_id_str'	=>$cat_id_str,
					'cat_name'		=>$cat_name,		
				);
			}else{
				return $cat_id_str;	
			}
	}

	function get_cat_this_is_here($cat_type,$cat_id,$type)
	{
		global $glo_this_is_here,$glo_config_arr;
		$cat_res			=		$this->get_category($cat_type);
		foreach($cat_res as $cat_val){
			if($cat_val['parent']['cat_id']==$cat_id){
				$glo_this_is_here[]	=	array(
						'name'	=>	$cat_val['parent']['cat_name'],
						'url'		=>	get_url($type.'.php?cat_id='.$cat_id,$cat_val['parent']['cat_name']),
				);		
				$this_cat	=	$cat_val['parent'];
				break;
			}elseif($cat_val['children']){
				foreach($cat_val['children'] as $c_cat_val){
					if($c_cat_val['cat_id']==$cat_id){
						$glo_this_is_here[]	=	array(
								'name'	=>	$cat_val['parent']['cat_name'],
								'url'		=>	get_url($type.'.php?cat_id='.$cat_val['parent']['cat_id'],$cat_val['parent']['cat_name']),
						);		
						$glo_this_is_here[]	=	array(
								'name'	=>	$c_cat_val['cat_name'],
								'url'		=>	get_url($type.'.php?cat_id='.$c_cat_val['cat_id'],$c_cat_val['cat_name']),
						);	
						$this_cat	=	$c_cat_val;
						$x	=	true;
						break;
					}else{
						$x	=	false;
					}
				}	
			}
			if($x){
				break;
			}	
		}
		$this->this_cat	=	$this_cat;	
		return $this_cat;
	}

}
function get_soft_cat_box($setting,&$block_val)
{
	global $glo_category_obj,$db_obj;
	$glo_category_obj->cat_type	=	'software';
	$p_num			=		(int)$setting['p_num']?(int)$setting['p_num']:10;
	$c_num			=		(int)$setting['c_num']?(int)$setting['c_num']:10;
	$order_type		=		in_array($setting['order_type'],array('sort_order','soft_num','soft_click_count','soft_download_num','children_count'))?$setting['order_type']:'sort_order';
	$order_sort		=		in_array($setting['order_sort'],array('ASC','DESC'))?$setting['order_sort']:'DESC';
	$name_length	=		(int)$setting['name_length']?(int)$setting['name_length']:50;
	$is_softnum		=		(int)$setting['is_softnum']?(int)$setting['is_softnum']:0;
	if($order_type	=='sort_order'||$order_type=='children_count'){
		$cat_res			=		$glo_category_obj->get_category('software',$is_softnum);
		if(!$cat_res){
			return false;
		}	
	}elseif($order_type=='soft_num'&&$is_softnum){
		$cat_res			=		$glo_category_obj->get_category('software',$is_softnum);
		if(!$cat_res){
			return false;
		}	
		foreach($cat_res as $key=>$val){
			$cat_res[$key]['parent']['sort']	=	$val['parent']['soft_num'];
			if($val['children'] ){
				foreach($val['children'] as $c_key=>$c_val){
					$cat_res[$key]['children'][$c_key]['sort']	=	$c_val['soft_num'];
				}
			}
		}
	}else{
		if($order_type=='soft_num'){
			$sort_sql		=		' count(b.software_id) AS sort ';
		}elseif($order_type='click_count'){
			$sort_sql		=		' sum(b.click_count) AS sort ';	
		}elseif($order_type=='download_num'){
			$sort_sql		=		' sum(b.download_num) AS sort ';
		}
		if($is_softnum){
				$is_softnum_sql	= ' , count(b.software_id) AS soft_num ';	
		}	
		$cat_originality_res			=		$db_obj->getRecords("SELECT a.*, $sort_sql $is_softnum_sql  FROM {$db_obj->db_pre}category a LEFT JOIN {$db_obj->db_pre}software b ON a.cat_id =b.cat_id WHERE  a.is_open=1 AND cat_type='software'  GROUP BY a.cat_id ");
		if(!$cat_originality_res){
			return false;
		}
		$cat_res							=		$glo_category_obj->structural_category($cat_originality_res,$name_length);
	}
	if($cat_res){
		if($is_softnum){
			foreach($cat_res as $key=>$val){
				$cat_res[$key]['parent']['cat_name']	.=		'('.$val['parent']['soft_num'].')';
				if($val['children']){
					foreach($val['children'] as $c_key =>$c_val){
						$cat_res[$key]['children'][$c_key]['cat_name']	.=	'('.$c_val['soft_num'].')';
					}
				}
			}	
		}
		$p_sort_arr		=		array();
		$i					=		0;
		$multisort_type	=	$order_sort=='ASC'?SORT_ASC:SORT_DESC;
		foreach($cat_res as $key=>$val){
			if($order_type=='children_count'){
				$p_sort_arr[$i]	=	count($val['children']);
			}elseif($order_type=='sort_order'){
				$p_sort_arr[$i]	=	$val['parent']['sort_order'];
			}else{
				$p_sort_arr[$i]	=	$val['parent']['sort'];
			}
			$c_sort_arr	=	array();
			if($val['children']&&$order_type!='children_count'){
				foreach($val['children'] as $c_key=>$c_val){
					if($order_type!='sort_order'){
						$p_sort_arr[$i]	+=$c_val['sort'];
					}
					$c_sort_arr[]		=	$c_val['sort']?$c_val['sort']:$c_val['sort_order'];
				}
				array_multisort($c_sort_arr, $multisort_type, SORT_NUMERIC, $cat_res[$key]['children']);
			}
			$i++;
		}
		array_multisort($p_sort_arr, $multisort_type, SORT_NUMERIC, $cat_res);
		$cat_limit_res		=	array();
		$p_i	=	0;
		foreach($cat_res as $key=>$val){
			if($p_i>=$p_num){
				break;
			}else{
				$cat_limit_res[$p_i]['parent']	=	$val['parent'];
				if($val['children']){
					$c_i	=	0;
					foreach($val['children'] as $c_key=>$c_val){
						if($c_i>=$c_num){
							break;			
						}else{
							$cat_limit_res[$p_i]['children'][$c_i]	=	$c_val;
						}
						$c_i++;
					}
				}
			}
			$p_i++;
		}
		return $cat_limit_res;
	}else{
		return false;
	}
}

function get_art_cat_box($setting,&$block_val)
{
	global $glo_category_obj,$db_obj;
	$glo_category_obj->cat_type	=	'article';
	$c_num			=	(int)$setting['c_num']?(int)$setting['c_num']:10;
	$p_num			=	(int)$setting['p_num']?(int)$setting['p_num']:10;
	$order_type		=	in_array($setting['order_type'],array('sort_order','art_num','children_count'))?$setting['order_type']:'sort_order';
	$order_sort		=	in_array($setting['order_sort'],array('ASC','DESC'))?$setting['order_sort']:'DESC';
	$name_length	=	(int)$setting['name_length']?(int)$setting['name_length']:50;
	$is_artnum		=	(int)$setting['is_artnum']?(int)$setting['is_artnum']:0;
		
	if($order_type	=='sort_order'||$order_type=='children_count'){
		$cat_res			=		$glo_category_obj->get_category('article',$is_artnum);
		if(!$cat_res){
			return false;
		}	
	}elseif($order_type=='art_num'&&$is_artnum){
		$cat_res			=		$glo_category_obj->get_category('article',$is_artnum);
		if(!$cat_res){
			return false;
		}	
		foreach($cat_res as $key=>$val){
			$cat_res[$key]['parent']['sort']	=	$val['parent']['art_num'];
			if($val['children'] ){
				foreach($val['children'] as $c_key=>$c_val){
					$cat_res[$key]['children'][$c_key]['sort']	=	$c_val['art_num'];
				}
			}
		}
	}elseif($order_type=='art_num'){
		$sort_sql		=		' count(b.article_id) AS sort ';

		$cat_originality_res			=		$db_obj->getRecords("SELECT a.*, $sort_sql  FROM {$db_obj->db_pre}category a LEFT JOIN {$db_obj->db_pre}article b ON a.cat_id =b.cat_id WHERE  a.is_open=1 AND cat_type='article'  GROUP BY a.cat_id ");
		if(!$cat_originality_res){
			return false;
		}
		$cat_res							=		$glo_category_obj->structural_category($cat_originality_res,$name_length);
	}
	if($cat_res){
		if($is_artnum){
			foreach($cat_res as $key=>$val){
				$cat_res[$key]['parent']['cat_name']	.=		'('.$val['parent']['art_num'].')';
				if($val['children']){
					foreach($val['children'] as $c_key =>$c_val){
						$cat_res[$key]['children'][$c_key]['cat_name']	.=	'('.$c_val['art_num'].')';
					}
				}
			}	
		}
		$p_sort_arr		=		array();
		$i					=		0;
		$multisort_type	=	$order_sort=='ASC'?SORT_ASC:SORT_DESC;
		foreach($cat_res as $key=>$val){
			if($order_type=='children_count'){
				$p_sort_arr[$i]	=	count($val['children']);
			}elseif($order_type=='sort_order'){
				$p_sort_arr[$i]	=	$val['parent']['sort_order'];
			}else{
				$p_sort_arr[$i]	=	$val['parent']['sort'];
			}
			$c_sort_arr	=	array();
			if($val['children']&&$order_type!='children_count'){
				foreach($val['children'] as $c_key=>$c_val){
					if($order_type!='sort_order'){
						$p_sort_arr[$i]	+=$c_val['sort'];
					}
					$c_sort_arr[]		=	$c_val['sort']?$c_val['sort']:$c_val['sort_order'];
				}
				array_multisort($c_sort_arr, $multisort_type, SORT_NUMERIC, $cat_res[$key]['children']);
			}
			$i++;
		}
		array_multisort($p_sort_arr, $multisort_type, SORT_NUMERIC, $cat_res);
		$cat_limit_res		=	array();
		$p_i	=	0;
		foreach($cat_res as $key=>$val){
			if($p_i>=$p_num){
				break;
			}else{
				$cat_limit_res[$p_i]['parent']	=	$val['parent'];
				if($val['children']){
					$c_i	=	0;
					foreach($val['children'] as $c_key=>$c_val){
						if($c_i>=$c_num){
							break;			
						}else{
							$cat_limit_res[$p_i]['children'][$c_i]	=	$c_val;
						}
						$c_i++;
					}
				}
			}
			$p_i++;
		}
		return $cat_limit_res;
	}else{
		return false;
	}		

}

