<?php
/*
===========================注册模块控制器
*/
class cls_tab_article_channel extends cls_base_table
{
	private $obj_db;
	//构造函数
	function __construct()
	{
		parent::__construct();
		$this->perms["cfg_path"]=WEB_ROOT."/data/config.article.channel.php";
	}
	//保存
	function on_save($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		(!isset($arr_fields["channel_id"]))? $arr_fields["channel_id"]=0:$arr_fields["channel_id"]=intval($arr_fields["channel_id"]);
		//保存操作
		$lng_id=$arr_fields["channel_id"];
		unset($arr_fields["channel_id"]);//释放id
		if(isset($arr_fields["channel_savehtml"])&&$arr_fields["channel_savehtml"]==1&&isset($arr_fields["channel_htmldir"])){
			$str_path=get_real_path("",$arr_fields["channel_htmldir"]);
			if(!is_dir($str_path)){
				$arr_return["err"]="生成HTML文件的目录不存在！请先创建目录";
				return $arr_return;
			}
		}
		$arr_fields["channel_updatetime"]=TIME;
		if($lng_id<1){
			$arr_fields["channel_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
		}else{
			$str_where="channel_id='".$lng_id."'";
			$arr_return=self::_update($arr_fields,$str_where);
			$arr_return["id"]=$lng_id;
		}
		self::on_refresh();
		return $arr_return;
	}
	//添加
	private function _insert($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0);
		if(!isset($arr_fields["channel_name"])||$arr_fields["channel_name"]==""){
			$arr_return["err"]="请输入文章频道名称！";
			return $arr_return;
		}

		$arr_return=parent::on_insert(DB_PRE."article_channel",$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_channel",$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_channel","channel_id in(".$str_id.")");
		self::on_refresh();
		return $arr_return;
	}
	//生成配置文件
	function on_refresh(){
		global $obj_db;
		$arr_article_channel=array();
		$obj_result=$obj_db->query("select * from ".DB_PRE."article_channel");
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_article_channel["channel_".$obj_rs["channel_id"]]=$obj_rs;
		}
		$str_channel=serialize($arr_article_channel);
		$str_val="<?php".chr(10);
		$str_val.='$str_x=\''.$str_channel.'\';'.chr(10);
		$str_val.='$cfg_article_channel=unserialize($str_x);'.chr(10);
		$str_val.="?>";
		file_create($this->perms["cfg_path"],$str_val);
	}
	function get_url($msg_channel_id,$msg_id,$msg_addtime){
		global $cfg_article_channel;
		if(!isset($cfg_article_channel) || !is_array($cfg_article_channel)){
			include_once $this->perms["cfg_path"];
		}
		$str_url="";
		if(isset($cfg_article_channel["channel_".$msg_channel_id])){
			$arr_cfg=$cfg_article_channel["channel_".$msg_channel_id];
			if($arr_cfg["channel_savehtml"]==1){
				$str_dir="";
				if(!empty($arr_cfg["channel_htmldir_split"])) $str_dir=date($arr_cfg["channel_htmldir_split"],$msg_addtime)."/";
				$str_url=WEB_URL.$arr_cfg["channel_htmldir"].$str_dir.$msg_id.".html";
			}
		}
		if($str_url==""){
			$str_url=WEB_URL."/index.php?app=article&id=".$msg_id;
		}
		return $str_url;
	}
}
?>
