<?php
/*
===========================注册模块控制器
*/
class cls_tab_article extends cls_base_table
{
	private $obj_db;
	//构造函数
	function __construct($msg_db="")
	{
		global $obj_db;
		parent::__construct();
		$this->obj_db=$obj_db;
	}
	//保存
	function on_save($arr_fields,$str_where="")
	{
		$arr_return=array("err"=>"","id"=>0);
		(!isset($arr_fields["article_id"]))? $arr_fields["article_id"]=0:$arr_fields["article_id"]=intval($arr_fields["article_id"]);
		//保存操作
		$lng_id=$arr_fields["article_id"];
		unset($arr_fields["article_id"]);//释放id

		$arr_fields["article_updatetime"]=TIME;
		if($lng_id<1){
			if(isset($arr_fields["article_addtime"])&&is_date($arr_fields["article_addtime"])){
				$arr_fields["article_addtime"]=format_date_int($arr_fields["article_addtime"]);
			}else{
				$arr_fields["article_addtime"]=TIME;
			}
			$arr_return=self::_insert($arr_fields);
		}else{
			if(isset($arr_fields["article_addtime"])&&is_date($arr_fields["article_addtime"])){
				$arr_fields["article_addtime"]=format_date_int($arr_fields["article_addtime"]);
			}
			$str_where="article_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		return $arr_return;
	}
	//添加
	private function _insert($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		if(!isset($arr_fields["article_hits"])) $arr_fields["article_hits"]=0;
		$arr_return=parent::on_insert(DB_PRE."article",$arr_fields);
		return $arr_return;
	}
	//修改
	private function _update($arr_fields,$msg_where)
	{
		$arr_return=array("err"=>"","id"=>0);
		$arr_return=parent::on_update(DB_PRE."article",$arr_fields,$msg_where);
		return $arr_return;
	}
	//删除
	function on_del($arr_id)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_return=parent::on_del(DB_PRE."article","article_id in(".$str_id.")");
		return $arr_return;
	}
	function get_article_attribute(){
		global $obj_tab_sys_config;
		$arr_cfg_article=$obj_tab_sys_config->get_model("article");
		if(isset($arr_cfg_article["article_attribute"]) && $arr_cfg_article["article_attribute"]!=""){
			if(is_array($arr_cfg_article["article_attribute"])){
				$arr_x=$arr_cfg_article["article_attribute"];
			}else{
				$arr_x=explode(chr(13),$arr_cfg_article["article_attribute"]);
			}
			return $arr_x;
		}else{
			return array();
		}
	}
	//粘贴文章
	function on_paste($arr_fields)
	{
		$arr_id=$arr_fields["articleid"];
		unset($arr_fields["articleid"]);
		$str_pasteact=$arr_fields["pasteact"];
		unset($arr_fields["pasteact"]);
		$str_ids=get_arr_id($arr_id);
		$str_sql="select * from ".DB_PRE."article where article_id in(".$str_ids.")";
		$obj_result=$this->obj_db->query($str_sql);
		while($obj_rs=$this->obj_db->fetch_array($obj_result))
		{
			$arr_fields["article_id"]=0;
			$arr_fields["article_title"]=$obj_rs["article_title"];
			$arr_fields["article_linkurl"]=$obj_rs["article_linkurl"];
			$arr_fields["article_islink"]=$obj_rs["article_islink"];
			$arr_fields["article_pic"]=$obj_rs["article_pic"];
			$arr_fields["article_intro"]=$obj_rs["article_intro"];
			$arr_fields["article_content"]=$obj_rs["article_content"];
			$arr_fields["article_tpl"]=$obj_rs["article_tpl"];
			$arr_fields["article_keyword"]=$obj_rs["article_keyword"];
			$arr_msg=self::on_save($arr_fields);
		}
		if($str_pasteact=="cut"){
			self::on_del($arr_id);
		}
	}
	function get_app($msg_tpl="",$msg_dir=""){
		$str_x=$msg_tpl;
		$this_app=array();
		if($str_x==""){
			$this_app["path"]="";
			$this_app["app"]="article";
		}else{
			$str_app=basename($str_x);
			$arr_app=explode(".",$str_app);
			$lng_app=count($arr_app);
			if($lng_app>1){
				$lng_x=$lng_app-1;
				unset($arr_app[$lng_x]);
				$str_app=implode(".",$arr_app);
			}
			$this_app["app"]=$str_app;
			$this_app["path"]=dirname($str_x);
		}
		$str_x=$msg_dir;
		$str_x=str_replace("\\","/",$str_x);
		if($str_x=="/"){
			$str_x="";
		}else{
			if(substr($str_x,-1,1)=="/") $str_x=substr($str_x,0,-1);
			if(substr($str_x,0,1)!="/") $str_x="/".$str_x;
		}
		$this_app["htmldir"]=$str_x;
		return $this_app;
	}
	function on_attribute($arr_id,$msg_val){
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$str_article_attribute="";
		if(is_array($msg_val)&&count($msg_val)>0){
			$str_article_attribute=implode(",",$msg_val);
		}else{
			$str_article_attribute=$msg_val;
		}
		$str_sql="update ".DB_PRE."article set article_attribute='".$str_article_attribute."' where article_id in(".$str_id.")";
		$arr_return["err"]=$this->obj_db->execute($str_sql);
		return $arr_return;
	}
}
?>
