<?
require_once(ROOT_PATH.'includes/glo_category.php');
class category extends action
{
	function get_category($cat_type)
	{
		$glo_category	=	new glo_category();
		return $glo_category->get_category($cat_type);	
	}	
	
	function  structural_category($category_tmp_res)
	{
		$glo_category	=	new glo_category();
		return $glo_category->structural_category($category_tmp_res);
	}

	function dlist()	
	{
		$this->init_tpl();
		$this->diff_cat_type();
		if($this->cat_type=='software'){
			$cat_res	=	$this->db->getRecords("SELECT a.*,count(b.software_id) as count FROM {$this->db->db_pre}category a LEFT JOIN {$this->db->db_pre}software b ON a.cat_id=b.cat_id  WHERE cat_type='$this->cat_type' GROUP BY a.cat_id ");
		}else{
			$cat_res	=	$this->db->getRecords("SELECT a.*,count(b.article_id) as count FROM {$this->db->db_pre}category a LEFT JOIN {$this->db->db_pre}article b ON a.cat_id=b.cat_id  WHERE cat_type='$this->cat_type' GROUP BY a.cat_id ");
		}
		if($cat_res){
			$cat_res		=		$this->structural_category($cat_res);	
			foreach($cat_res as $key=>$val){
				if(!$val['children']){
					continue;	
				}
				$count_soft	=	$val['parent']['count_soft'];
				foreach($val['children'] as $c_key=>$c_val){
						$count_soft	+=$c_val['count_soft'];	
				}	
				$cat_res[$key]['parent']['count_soft']	=	$count_soft;
			}
			$this->tpl->out['cat_res']	=	$cat_res;
		}	
		$this->tpl->display("category.dlist.html");
	
	}

	function addcategory()
	{
		$this->init_tpl();
		$this->diff_cat_type();
		$this->tpl->out['top_cat']	=	$this->db->getRecords("SELECT cat_name,cat_id FROM {$this->db->db_pre}category WHERE parent_id=0 AND cat_type='$this->cat_type'");
		$this->tpl->display("category.addcategory.html");
	}

	function save_addcategory()
	{
		global $_ADMIN_LANG;
		if(!($insert_arr=$this->check_submit_category())){
			$this->system_message($_ADMIN_LANG['adding_category_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
			exit;
		}
		$this->diff_cat_type();
		$insert_arr['cat_type']		=	$this->cat_type;
		$cat_id	=	$this->db->addRecord("{$this->db->db_pre}category",$insert_arr);
		if($cat_id){
			if($this->is_ajax){
				$message_info	=	$cat_id;
			}else{
				$message_info	=	'';
			}
			$this->system_message($_ADMIN_LANG['adding_category_success'],'success',array(
				$_ADMIN_LANG['continue_adding']	  => 'index.php?act=category.addcategory&cat_type='.$this->cat_type,	
				$_ADMIN_LANG['back_to_category_page']=>'index.php?act=category.dlist&cat_type='.$this->cat_type),$message_info);
			exit;
		}else{
			$this->system_message($_ADMIN_LANG['adding_category_failure'],'error',array($_ADMIN_LANG['back_to_category_page']=>'javascript:history.go(-1);'));
			exit;
		}

	}

	function check_submit_category() 
	{
		$cat_name	=	$_POST['cat_name'];	
		if(!$cat_name){
			return false;
		}
		$sql_data_arr	=	array(
			'cat_name'		=>	$cat_name,
			'parent_id'		=> (int)$_POST['parent_id'],
			'seo_keywords'		=> $_POST['seo_keywords'],
			'seo_desc'		=>	$_POST['seo_desc'],
			'sort_order'	=> (int)$_POST['sort_order'],
			'is_open'		=>	(int)$_POST['is_open'],
		);
		return $sql_data_arr;
	}

	function updatecategory()
	{
		$cat_id	=	(int)$_GET['cat_id'];
		$this->init_tpl();
		$this->diff_cat_type();
		$cat_res	=	$this->db->getRecord("SELECT * FROM {$this->db->db_pre}category WHERE cat_id='".$cat_id."' ");

		$this->tpl->out['cat_res']	=	$cat_res;
		$this->tpl->out['top_cat']	=	$this->db->getRecords("SELECT cat_name,cat_id FROM {$this->db->db_pre}category WHERE parent_id=0 AND cat_type='$this->cat_type'");
		$this->tpl->display('category.updatecategory.html');
	}

	function save_updatecategory()
	{
		$cat_id	=	(int)$_GET['cat_id'];
		global $_ADMIN_LANG;
		if(!($update_arr=$this->check_submit_category())){
			$this->system_message($_ADMIN_LANG['category_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
			exit;	
		}		
		$result	=	$this->db->updateRecord("{$this->db->db_pre}category"," cat_id=$cat_id",$update_arr);
		$this->diff_cat_type();
		global $_ADMIN_LANG;
		if($result){
			$this->system_message($_ADMIN_LANG['category_modification_success'],'success',array(
						$_ADMIN_LANG['continue_modification']	  => 'index.php?act=category.updatecategory&cat_id='.$cat_id.'&cat_type='.$this->cat_type,
						$_ADMIN_LANG['back_to_category_page']=>'index.php?act=category.dlist&cat_type='.$this->cat_type));
			exit;	
		}else{
			$this->system_message($_ADMIN_LANG['category_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
			exit;	
		}

	}

	function del_category_ajax()
	{
		$cat_id	=	(int)$_GET['cat_id'];
		if($this->db->deleteRecord("{$this->db->db_pre}category"," cat_id=$cat_id ")){
			echo json_return('ok');
		}
	}

	function handle_category_is_open()
	{
		$cat_id		=	(int)$_GET['cat_id'];
		$is_open		=	(int)$_GET['is_open'];
		if($this->db->updateRecord("{$this->db->db_pre}category"," cat_id=$cat_id ",array('is_open'=>$is_open))){
			echo json_return('ok');
		}	
	}


	function move_cat()
	{
		$this->init_tpl();	
		$this->diff_cat_type();
		$this->tpl->out['cate_res']	=	$this->get_category($this->cat_type);
		$this->tpl->display("category.move_cat.html");	
	}

	function save_move()
	{
		$old_cat_id			=	(int)$_POST['old_cat_id'];
		$target_cat_id		=	(int)$_POST['target_cat_id'];
		$this->diff_cat_type();
		global $_ADMIN_LANG;
		if($old_cat_id&&$target_cat_id){
			if($this->cat_type=='software'){
				$result	=	$this->db->updateRecord("{$this->db->db_pre}software"," cat_id=$old_cat_id", array('cat_id'=>$target_cat_id));
			}else{
				$result	=	$this->db->updateRecord("{$this->db->db_pre}article"," cat_id=$old_cat_id", array('cat_id'=>$target_cat_id));
			}
			if($result){
				$this->system_message($_ADMIN_LANG['transfer_software_category_success'],'success',array($_ADMIN_LANG['back_to_category_page']=>'index.php?act=category.dlist&cat_type='.$_GET['cat_type']));
				exit;
			}else{
				$this->system_message($_ADMIN_LANG['transfer_software_category_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
				exit;
			}
		}else{
				$this->system_message($_ADMIN_LANG['transfer_software_category_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
				exit;	
		}
	}

	function diff_cat_type()
	{
		$cat_type	=	$_GET['cat_type']?$_GET['cat_type']:'software';
		$this->init_tpl();
		global $_ADMIN_LANG;
		if($cat_type=='software'){
			$this->tpl->out['cat_type_name']	=	$_ADMIN_LANG['software'];
		}elseif($cat_type=='article'){
			$this->tpl->out['cat_type_name']	=	$_ADMIN_LANG['article'];
		}	
		$this->cat_type	=	$cat_type;
	}

}
