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

		$arr_fields["folder_updatetime"]=TIME;
		if($lng_id<1){
			if(!isset($arr_fields["folder_pid"])){
				$arr_fields["folder_pid"]=0;
			}
			$arr_fields["folder_addtime"]=TIME;
			$arr_return=self::_insert($arr_fields);
			if($arr_return["err"]==""){
				$str_pids="";
				$str_sql="select folder_pids from ".DB_PRE."article_folder where folder_id='".$arr_fields["folder_pid"]."'";
				$obj_result  = $this->obj_db->query($str_sql);
				if($obj_rs=$this->obj_db->fetch_array($obj_result)){
					$str_pids=$obj_rs["folder_pids"].",".$arr_return["id"];
				}else{
					$str_pids=$arr_return["id"];
				}
				//更新目录树
				$str_sql="update ".DB_PRE."article_folder set folder_pids='".$str_pids."' where folder_id='".$arr_return["id"]."'";
				$this->obj_db->execute($str_sql);
			}
		}else{
			$str_where="folder_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);
		$str_sql="select folder_id from ".DB_PRE."article_folder where folder_pid='".$arr_fields["folder_pid"]."' and folder_name='".$arr_fields["folder_name"]."'";
		$obj_result  = $this->obj_db->query($str_sql);
		if($obj_rs=$this->obj_db->fetch_array($obj_result)){
			$arr_return["err"]="同一层次的目录名不能重复！";
			return $arr_return;
		}
		$arr_return=parent::on_insert(DB_PRE."article_folder",$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_folder",$arr_fields,$msg_where);
		return $arr_return;
	}
	//删除
	function on_del($arr_id)
	{
		$arr_return=array();
		$str_id=get_arr_id($arr_id);
		//当删除目录时要连同子目录一起删除
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_ids=array();
		$str_sql="select folder_id from ".DB_PRE."article_folder where folder_pid in (".$str_id.")";
		$obj_result = $this->obj_db->query($str_sql);
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$arr_ids[]=$obj_rs["folder_id"];
		}
		if(count($arr_ids)>0)
		{
			self::on_del($arr_ids);
		}
		
		//删除目录下的文章
		$arr_article_id=array();
		$str_sql="select article_id from ".DB_PRE."article where article_folder_id in(".$str_id.")";
		$obj_result = $this->obj_db->query($str_sql);
		while($obj_rs=$this->obj_db->fetch_array($obj_result))
		{
			$arr_article_id[]=$obj_rs["article_id"];
		}

		if(count($arr_article_id)>0){
			require_once(SYS_ROOT."/cls_tab/tab.article.php");
			$obj_tab_article=new cls_tab_article();
			$obj_tab_article->on_del($arr_article_id);
		}

		//删除目录数据
		$arr_return=parent::on_del(DB_PRE."article_folder","folder_id in(".$str_id.")");
		return $arr_return;
	}
	function on_paste($arr_fields)
	{
		$arr_id=$arr_fields["folderid"];
		unset($arr_fields["folderid"]);
		$str_pasteact=$arr_fields["pasteact"];
		unset($arr_fields["pasteact"]);
		$str_ids=get_arr_id($arr_id);
		if($str_ids==""){
			$arr_return["err"]="请选择要粘贴的目录！";
			return $arr_return;
		}
		//检查，目录不能复制或剪贴到其子目录里
		$str_sql="select * from ".DB_PRE."article_folder where folder_id in(".$str_ids.") and (concat(',',folder_pid,',') like '%,".$arr_fields["folder_pid"].",%' or folder_id='".$arr_fields["folder_pid"]."')";
		$obj_result=$this->obj_db->query($str_sql);
		if($obj_rs=$this->obj_db->fetch_array($obj_result)){
			$arr_return["err"]="不能向其子目录粘贴！";
			return $arr_return;
		}
		//检查，目标目录是否存在
		if($arr_fields["folder_pid"]!=0){
			$str_sql="select * from ".DB_PRE."article_folder where folder_id='".$arr_fields["folder_pid"]."'";
			$obj_result=$this->obj_db->query($str_sql);
			if($obj_rs=$this->obj_db->fetch_array($obj_result)){
				$arr_pid_fields=$obj_rs;
			}else{
				$arr_return["err"]="目标目录不存在！";
				return $arr_return;
			}
		}else{
			$arr_pid_fields["folder_channel_id"]=$arr_fields["folder_channel_id"];
			$arr_pid_fields["folder_pids"]="";
		}
		$str_sql="select * from ".DB_PRE."article_folder where folder_id in(".$str_ids.")";
		$obj_result=$this->obj_db->query($str_sql);
		$str_pids=$arr_pid_fields["folder_pids"];
		if($str_pasteact=="cut"){
			if($str_pids=="") $str_pids="0";
			while($obj_rs=$this->obj_db->fetch_array($obj_result))
			{
				$arr_fields["folder_channel_id"]=$arr_pid_fields["folder_channel_id"];
				$arr_fields["folder_id"]=$obj_rs["folder_id"];
				$arr_fields["folder_pids"]=$str_pids.",".$obj_rs["folder_id"];
				$arr_msg=self::on_save($arr_fields);
				$arr_next_id=array();
				$str_sql2="select folder_id from ".DB_PRE."article_folder where folder_pid='".$obj_rs["folder_id"]."'";
				$obj_result2=$this->obj_db->query($str_sql2);
				while($obj_rs2=$this->obj_db->fetch_array($obj_result2))
				{
					$arr_next_id[]=$obj_rs2["folder_id"];
				}
				if(count($arr_next_id)>0){
				//改变下级栏目
					$arr_next_fiuelds=array();
					$arr_next_fields["folderid"]=$arr_next_id;
					$arr_next_fields["pasteact"]=$str_pasteact;
					$arr_next_fields["folder_channel_id"]=$arr_fields["folder_channel_id"];
					$arr_next_fields["folder_pid"]=$obj_rs["folder_id"];
					self::on_paste($arr_next_fields);
				}
			}
		}else{
			require_once(SYS_ROOT. '/cls_tab/tab.article.php');
			$obj_tab_article=new cls_tab_article();
			while($obj_rs=$this->obj_db->fetch_array($obj_result))
			{
				$arr_fields["folder_id"]=0;
				$arr_fields["folder_name"]=$obj_rs["folder_name"];
				$arr_fields["folder_tpl"]=$obj_rs["folder_tpl"];
				$arr_fields["folder_channel_id"]=$arr_pid_fields["folder_channel_id"];
				$arr_fields["folder_article_tpl"]=$obj_rs["folder_article_tpl"];
				$arr_folder_return=self::on_save($arr_fields);
				if($arr_folder_return["err"]==""){
					$arr_next_id=array();
					//复制子目录
					$str_sql2="select folder_id from ".DB_PRE."article_folder where folder_pid='".$obj_rs["folder_id"]."'";
					$obj_result2=$this->obj_db->query($str_sql2);
					while($obj_rs2=$this->obj_db->fetch_array($obj_result2))
					{
						$arr_next_id[]=$obj_rs2["folder_id"];
					}
					if(count($arr_next_id)>0){
					//改变下级栏目
						$arr_next_fiuelds=array();
						$arr_next_fields["folderid"]=$arr_next_id;
						$arr_next_fields["pasteact"]=$str_pasteact;
						$arr_next_fields["folder_pid"]=$arr_folder_return["id"];
						self::on_paste($arr_next_fields);
					}
					//复制文章
					$arr_next_id=array();
					$str_sql2="select article_id from ".DB_PRE."article where article_folder_id='".$obj_rs["folder_id"]."'";
					$obj_result2=$this->obj_db->query($str_sql2);
					while($obj_rs2=$this->obj_db->fetch_array($obj_result2))
					{
						$arr_next_id[]=$obj_rs2["article_id"];
					}
					if(count($arr_next_id)>0){
					//复制下级文章
						$str_next_ids=get_arr_id($arr_next_id);
						$arr_next_fiuelds=array();
						$arr_next_fiuelds["articleid"]=$str_next_ids;
						$arr_next_fiuelds["pasteact"]=$str_pasteact;
						$arr_next_fiuelds["article_folder_id"]=$arr_folder_return["id"];
						$arr_next_fiuelds["article_channel_id"]=$arr_pid_fields["folder_channel_id"];
						$obj_tab_article->on_paste($arr_next_fiuelds);
					}
				}
			}
		}
	}
	function get_select_folder($msg_name,$msg_folder="")
	{
		
		$str_sql="select * from ".DB_PRE."article_folder where folder_pid=0";
		$obj_result = $this->obj_db->query($str_sql);
		$str_folder="";
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			$str_folder.="<option value='".$obj_rs["folder_id"]."'>".$obj_rs["folder_name"]."</option>";
		}
		if($msg_folder==""){
			$str_button="";
			$str_input="style='display:none'";
			$str_select="";
		}else{
			$str_select="style='display:none'";
			$str_input="";
			$str_button="<a href=\"javascript:on_area_edit('id_folder_".$msg_name."');\">编辑</a>";
		}
		$str_folder="<span id='id_folder_".$msg_name."'><input type='text' name='".$msg_name."' id='".$msg_name."' value='".$msg_folder."' readonly ".$str_input." style='border:0px'><select onchange=\"get_folder_select_1('id_folder_".$msg_name."',this.value,1);\" ".$str_select."><option value=''>--请选择--</option>".$str_folder;
		$str_folder.="</select> ".$str_button."</span>";
		return $str_folder;
	}
	function get_next_folder($msg_id)
	{		
		$msg_id=intval($msg_id);
		$str_sql="select * from ".DB_PRE."article_folder where folder_pid=".$msg_id;
		$obj_result = $this->obj_db->query($str_sql);
		$str_folder="";
		$arr_folder=array();
		$lng_lay=-1;
		while($obj_rs = $this->obj_db->fetch_array($obj_result))
		{
			if($lng_lay<1){
				$str_pids=$obj_rs["folder_pids"];
				$arr_pids=explode(",",$str_pids);
				$lng_lay=count($arr_pids);
			}
			$arr_folder[]=$lng_lay.",".$obj_rs["folder_id"]."=".$obj_rs["folder_name"];
		}
		$str_folder=implode("|",$arr_folder);
		return $str_folder;
	}
}
?>
