<?
class article extends action{

	function save_addarticle($insert_arr='')
	{
		global $_ADMIN_LANG;
		$t	=	false;
		if(!$insert_arr){
			if(!($insert_arr	=	$this->check_submit_article())){
				$this->system_message($_ADMIN_LANG['adding_article_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'),$this->message_info);
				exit;		
			}
			$t	=	true;
		}
		$result	=	$this->db->addRecord($this->db->db_pre."article",
			$insert_arr	
		);
		if($t){
			if($result){
				$this->system_message($_ADMIN_LANG['adding_article_success'],'success',array(
							$_ADMIN_LANG['continue_adding']		=>	'index.php?act=article.addarticle',
							$_ADMIN_LANG['back_to_list_page']=>'index.php?act=article.dlist'));
				exit;
			}else{
				$this->system_message($_ADMIN_LANG['adding_article_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
				exit;		
			}			
		}else{
			return $result;
		}
	}	
	function updatearticle()
	{
		$this->init_tpl();
		$article_id		=	(int)$_GET['article_id'];
		$article_res	=	$this->db->getRecord(" SELECT * FROM {$this->db->db_pre}article WHERE article_id=$article_id ");	
		
		require_once(ROOT_PATH.'admin/includes/category.class.php');
		$cat_obj			=		new category;
		
		$this->tpl->out['category_res']		=	$cat_obj->get_category('article');	
		$this->tpl->out['category_soft_res']	=	$cat_obj->get_category('software');
			

		//related software
		if($soft_id_str	=	$article_res['rel_software_id']){
			$rel_software	=	$this->db->getRecords(" SELECT software_id,software_name,software_version FROM {$this->db->db_pre}software WHERE software_id IN ($soft_id_str)");
			if($rel_software){
				$this->tpl->out['rel_software']	=	$rel_software;
			}
		}
		$this->tpl->out['article_res']	=	$article_res;

		//fck
		create_html_editor('content',$article_res['content'],$this->tpl->tpl);
		
		$this->tpl->display("article.updatearticle.html");
	}

	function check_submit_article()
	{
		$title	=	$_POST['title'];
		global $_ADMIN_LANG;
		$article_id =  isset($_GET['article_id'])?(int)$_GET['article_id']:0;
		
		if(!$title){
			return false;
		}else{
			if($this->db->getNumber("SELECT  article_id FROM  {$this->db->db_pre}article WHERE title='".$this->db->escape($title)."' AND article_id!=$article_id LIMIT 1")){
				$this->message_info			=				str_replace('$1',$title,$_ADMIN_LANG['article_exist']);
				return false;
			}
		}
		$cat_id	=	(int)$_POST['cat_id'];
		if(!$cat_id){
			return false;
		}

		if($rel_software_id	=	$_POST['target_select']){
			$rel_software_id	=	array_unique($rel_software_id);	
			$rel_software_id	=	array_map('intval',$rel_software_id);
			$rel_software_id	=	implode(',',$rel_software_id);
		}else{
			$rel_software_id	=	'';
		}
	
		$sql_data_arr	=	array(
			'title'					=>	$title,
			'is_article'			=>	(int)$_POST['is_article'],		
			'seo_title'				=>	$_POST['seo_title'],
			'seo_keywords'			=>	$_POST['seo_keywords'],
			'content'				=>	$_POST['content'],
			'cat_id'					=>	$cat_id,
			'rel_software_id'		=>	$rel_software_id,
			'seo_desc'				=>	$_POST['seo_desc'],
			'is_support'			=>	(int)$_POST['is_support'],
			'is_news'				=>	(int)$_POST['is_news'],
			'last_update'			=>	time(),
		);
		return $sql_data_arr;
	}



	function save_updatearticle($article_id='',$update_arr='')
	{
		if(!$article_id){
			$article_id	=	(int)$_GET['article_id'];
		}
		global $_ADMIN_LANG;
		$t	=	false;
		if(!$update_arr){
			if(!($update_arr=$this->check_submit_article())){
				$this->system_message($_ADMIN_LANG['article_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'),$this->message_info);
				exit;		
			}
			$t	=	true;	
		}
		$result	=	$this->db->updateRecord($this->db->db_pre."article"," article_id='".$article_id."'" , $update_arr);		
		if($t){
			if($result){
				$this->system_message($_ADMIN_LANG['article_modification_success'],'success',array(
							$_ADMIN_LANG['continue_modification']=>	'index.php?act=article.updatearticle&article_id='.$article_id,
							$_ADMIN_LANG['back_to_list_page']=>'index.php?act=article.dlist'));
				exit;
			}else{
				$this->system_message($_ADMIN_LANG['article_modification_failure'],'error',array($_ADMIN_LANG['back_to_the_sheet_page']=>'javascript:history.go(-1);'));
				exit;		
			}
		}else{
			return $result;
		}
	}

	function dlist()
	{
		$this->init_tpl();
		
		$limit			=		10;
		$page   			=  (int)$_GET['page']?(int)$_GET['page']:1;
		$this_page     =  ($page-1)*$limit;

		$sql				=		"SELECT * FROM {$this->db->db_pre}article  WHERE 1=1 ";
		$count_sql		=		"SELECT COUNT(article_id) as count FROM {$this->db->db_pre}article WHERE 1=1";	

		//all category
		require_once(ROOT_PATH.'admin/includes/category.class.php');
		$cat_obj			=		new category;
		$this->tpl->out['category_res']		=	$cat_obj->get_category('article');	
		

		if($title=$_GET['title']){
			$sql			.=		" AND title LIKE '%".$this->db->like_escape($title)."%'";
			$count_sql  .= 	" AND title LIKE '%".$this->db->like_escape($title)."%'";	
		}
		if($is_support=(int)$_GET['is_support']){
			$sql			.=		" AND is_support = $is_support";
			$count_sql  .= 	" AND is_support = $is_support";	
		}	
		if($is_news	=	(int)$_GET['is_news']){
			$sql			.=		" AND is_news = $is_news";
			$count_sql  .= 	" AND is_news = $is_news";	
		}
		if($is_article=	(int)$_GET['is_article']){
			$sql			.=		" AND is_article = $is_article";
			$count_sql  .= 	" AND is_article = $is_article";	
		}

		if($cat_id=(int)$_GET['cat_id']){
			$cat_id_str	=	$cat_id.',';
			foreach($this->tpl->out['category_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;
				}
			}
			$cat_id_str	=	substr($cat_id_str,0,-1);
			$sql					.=		" AND cat_id in ($cat_id_str)";
			$count_sql			.=		" AND cat_id in ($cat_id_str)";
		}


		$sql				.=" ORDER BY last_update DESC  LIMIT $this_page,$limit ";
		
		$article_res	=		$this->db->getRecords($sql);		
		if($article_res){
			foreach($article_res as $a_key=>$a_v){
					foreach($this->tpl->out['category_res'] as $val){
						if($val['parent']['cat_id']==$a_v['cat_id']){
							$article_res[$a_key]['cat_name']	=	$val['parent']['cat_name'];
						}elseif($val['children']){
							foreach($val['children'] as $c_val){
								if($c_val['cat_id']==$a_v['cat_id']){
									$article_res[$a_key]['cat_name']	=	$val['parent']['cat_name'].' / '.$c_val['cat_name'];
								}
							}
						
						}
					}	
				}	
		}

		$this->tpl->out['article_res']		=	$article_res;
		
		
		
		$count			=		$this->db->getNumber($count_sql);
		$this->tpl->out['page_arr']	=		pagination_arr($page,$count,$limit,10,'page');	
		
		$this->tpl->display("article.dlist.html");
	}

	function addarticle()
	{
		$this->init_tpl();	
		require_once(ROOT_PATH.'admin/includes/category.class.php');
		$cat_obj			=		new category;
		$this->tpl->out['category_res']			=	$cat_obj->get_category('article');	
		$this->tpl->out['category_soft_res']	=	$cat_obj->get_category('software');
		//fck
		create_html_editor('content','',$this->tpl->tpl);
		$this->tpl->display("article.addarticle.html");
	}

	

	function batch_handle()
	{
		global $_ADMIN_LANG;
		$article_id_arr	=	array_map('intval',$_POST['checkboxes']);
		$article_id_str	=	implode(',',$article_id_arr);
		$handle				=	$_POST['handle'];
		if($handle	==	'is_del'){
			$result	=	$this->del_article($article_id_str);
			if(!$result){
				$this->system_message($_ADMIN_LANG['batch_deletion_failure'],'error',array($_ADMIN_LANG['back_to_list_page']=>'javascript:history.go(-1);'));
				exit;		
			}else{
				$this->system_message($_ADMIN_LANG['batch_deletion_success'],'success',array($_ADMIN_LANG['back_to_list_page']=>'index.php?act=article.dlist'));
				exit;		
			}
		}

	}
	function del_article($article_id)
	{
		if(strstr($article_id,',')){
			$where_sql	=	" article_id in ({$article_id})";
		}else{
			$where_sql	=	" article_id = ".(int)$article_id;
		}		
		$result			=	$this->db->deleteRecord($this->db->db_pre."article", $where_sql);
		return $result;	
	}
}
