 <?php
require 'inc.php';
$obj_tab_article=new cls_tab_article();
$obj_tab_folder=new cls_tab_article_folder();

$arr_article_attribute=$obj_tab_article->get_article_attribute();
//设置本页默认参数
(isset($get_channel_id))? $get_channel_id=intval($get_channel_id):$get_channel_id=0;
(isset($get_me_root))?$get_me_root=intval($get_me_root):$get_me_root=0;

//查询参数
$get_issearch="";
if(!isset($get_s_title)) $get_s_title="";
if(!isset($get_s_updatetime1)) $get_s_updatetime1="";
if(!isset($get_s_updatetime2)) $get_s_updatetime2="";
if(!isset($get_list_style)){
	$str_list_style=get_cookie("admin_article_list_style");
	$get_list_style=$str_list_style;
}else{
	set_cookie("admin_article_list_style",$get_list_style);
}
if($get_list_style=="dir"){
	(isset($get_me_folder_id))? $get_me_folder_id=intval($get_me_folder_id):$get_me_folder_id=0;
}else{
	(isset($get_me_folder_id))? $get_me_folder_id=intval($get_me_folder_id):$get_me_folder_id=-1;
}

(!isset($get_s_folder_id))? $get_s_folder_id=0:$get_s_folder_id=intval($get_s_folder_id);

if(!isset($get_folderids)) $get_folderids="";
if(!isset($get_articleids)) $get_articleids="";
if(!isset($get_article_act)) $get_article_act="";


$this_channel=array();
$this_app=array("path"=>"","app"=>"article","htmldir"=>"");
$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."article_channel where channel_id='".$get_channel_id."'");
if($obj_rs = $obj_db->fetch_array($obj_result))
{
	$this_channel = $obj_rs;
	if($this_channel["channel_savehtml"]==1){
		$this_app=$obj_tab_article->get_app($obj_rs["channel_article_tpl"],$obj_rs["channel_htmldir"]);
	}
}else{
	exit("选择频道不存在！");
}

$folder_path="<a href=\"javascript:meOpenFolder('".$get_me_root."');\">".$this_channel["channel_name"]."</a>";
$folder_path.=me_folder_path($get_me_folder_id,$get_me_root);

if($get_act=="del_folder"){
	$arr_return=$obj_tab_folder->on_del($get_id);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}
if($get_act=="del_article"){
	$arr_return=$obj_tab_article->on_del($get_id);
	if($arr_return["err"]!="") $obj_msg->alert($arr_return["err"]);
}

if($get_act=="del_sel"){
	//删除文章
	if(isset($get_articleid))
	{
		$arr_msg=$obj_tab_article->on_del($get_articleid);
		if($arr_msg["err"]!="")
		{
			$obj_msg->alert($arr_msg["err"]);
		}
	}
	//删除目录
	if(isset($get_folderid))
	{
		$arr_msg=$obj_tab_folder->on_del($get_folderid);
		if($arr_msg["err"]!="")
		{
			$obj_msg->alert($arr_msg["err"]);
		}
	}
}
if($get_act=="attribute"){
	if(isset($get_articleid))
	{
		$arr_msg=$obj_tab_article->on_attribute($get_articleid,$get_attribute_val);
		if($arr_msg["err"]!="")
		{
			$obj_msg->alert($arr_msg["err"]);
		}
	}
}
if($get_act=="paste"){
	if(isset($get_articleids) && $get_articleids!=""){
		$arr_fields=array();
		$arr_fields["articleid"]=$get_articleids;
		$arr_fields["pasteact"]=$get_article_act;
		$arr_fields["article_folder_id"]=$get_me_folder_id;
		$arr_fields["article_channel_id"]=$get_channel_id;
		$arr_msg=$obj_tab_article->on_paste($arr_fields);
		if($arr_msg["err"]!="")
		{
			$obj_msg->alert($arr_msg["err"]);
		}
	}
	if(isset($get_folderids)&&$get_folderids!=""){
		$arr_fields=array();
		$arr_fields["folderid"]=$get_folderids;
		$arr_fields["pasteact"]=$get_article_act;
		$arr_fields["folder_pid"]=$get_me_folder_id;
		$arr_fields["folder_channel_id"]=$get_channel_id;
		$arr_msg=$obj_tab_folder->on_paste($arr_fields);
		if($arr_msg["err"]!="")
		{
			$obj_msg->alert($arr_msg["err"]);
		}
	}
	if($get_article_act=="cut"){
		$get_article_act="";
		$get_folderids="";
		$get_articleids="";
	}
}

$page_footer_info=<<<HTML
<script>
function meOpenFolder(msgid){
	frmSet('me_folder_id',msgid);
	frmExecute('');
}
function meSearchClear(){
	if(document.frmMain.s_folder_id) document.frmMain.s_folder_id.value="0";
	document.frmMain.s_title.value="";
	document.frmMain.s_updatetime1.value="";
	document.frmMain.s_updatetime2.value="";
	frmExecute('');
}
</script>
<input type='hidden' name='me_folder_id' value='$get_me_folder_id'>
<input type='hidden' name='channel_id' value='$get_channel_id'>
<input type='hidden' name='folderids' value='$get_folderids'>
<input type='hidden' name='articleids' value='$get_articleids'>
<input type='hidden' name='article_act' value='$get_article_act'>
<input type='hidden' name='me_root' value='$get_me_root'>
<input type='hidden' name='list_style' value='$get_list_style'>
HTML;

switch($get_act)
{	
	case 'save_article':
	{
		//文章属性
		$arr_fields=array();
		$str_article_attribute="";
		if(isset($get_article_attribute)){
			if(is_array($get_article_attribute)&&count($get_article_attribute)>0){
				$str_article_attribute=implode(",",$get_article_attribute);
			}else{
				$str_article_attribute=$get_article_attribute;
			}
		}
		//自动取简介
		if($get_article_intro==""&&$get_id==0&&$get_article_content!=""){
			$str_content=strip_tags($get_article_content);
			if(strlen($str_content)>150){
				$get_article_intro=format_len($str_content,150);
			}else{
				$get_article_intro=$str_content;
			}
		}
		$arr_fields["article_css"]="";
		if(is_array($get_article_css)){
			$arr_fields["article_css"]=implode(";",$get_article_css);
		//	echo $arr_fields["article_css"];exit;
		}else{
			$arr_fields["article_css"]=$get_article_css;
		}
		$arr_fields["article_id"]=$get_id;
		if(isset($get_article_addtime)) $arr_fields["article_addtime"]=$get_article_addtime;
		$arr_fields["article_title"]=$get_article_title;
		$arr_fields["article_attribute"]=$str_article_attribute;
		$arr_fields["article_tpl"]=$get_article_tpl;
		if(isset($get_article_islink)) $arr_fields["article_islink"]=$get_article_islink;
		$arr_fields["article_linkurl"]=$get_article_linkurl;
		$arr_fields["article_pic"]=$get_article_pic;
		$arr_fields["article_intro"]=$get_article_intro;
		$arr_fields["article_content"]=$get_article_content;
		$arr_fields["article_tag"]=$get_article_tag;
		$arr_fields["article_folder_id"]=$get_article_folder_id;
		$arr_fields["article_channel_id"]=$get_article_channel_id;
		$arr_msg=$obj_tab_article->on_save($arr_fields);
		if($arr_msg["err"]==""){
			$get_id=$arr_msg["id"];
			$str_html_frame="";
			if($this_channel["channel_savehtml"]){
				$str_html_frame='<iframe src="article.php?act=html_exe&channel_id='.$get_article_channel_id.'&articleid='.$get_id.'" width=0 height=0></iframe>';
			}
			$arr_msg["act_info"]="成功保存文章！".$str_html_frame;
			$arr_msg["act_act"]="<a href=\"javascript:frmExecute('edit_article');\">修改</a> <a href=\"javascript:frmExecute('add_article');\">添加</a> <a href=\"javascript:frmExecute('');\">管理</a>";
		}
		$page_footer_info.="<input type='hidden' name='s_title' value='".$get_s_title."'><input type='hidden' name='s_updatetime1' value='".$get_s_updatetime1."'><input type='hidden' name='s_updatetime2' value='".$get_s_updatetime2."'>";
		include 'tpl/_msg.tpl.php';
	    break;
	}
	case 'edit_article':
	{
		$arr_article=array();
		$str_selfolder="";
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."article where article_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_article = $obj_rs;
			$str_selfolder=me_folder_next($get_channel_id,$obj_rs["article_folder_id"]);
		}
		include 'tpl/article.edit.tpl.php';
		break;
	}
	case 'add_article':
	{
		$get_id=0;//将id置0
		$str_selfolder=me_folder_next($get_channel_id,$get_me_folder_id);
		include "tpl/article.add.tpl.php";
		break;
	}
	case "save_folder":
	{
		$arr_fields=array();
		$arr_fields["folder_id"]=$get_id;
		$arr_fields["folder_name"]=$get_folder_name;
		$arr_fields["folder_article_tpl"]=$get_folder_article_tpl;
		$arr_fields["folder_tpl"]=$get_folder_tpl;
		$arr_fields["folder_channel_id"]=$get_channel_id;
		$arr_fields["folder_pid"]=$get_me_folder_id;
		$arr_msg=$obj_tab_folder->on_save($arr_fields);
		if($arr_msg["err"]==""){
			$get_id=$arr_msg["id"];
			$arr_msg["act_info"]="成功保存目录！";
			$arr_msg["act_act"]="<a href=\"javascript:frmExecute('edit_folder');\">修改</a> <a href=\"javascript:frmExecute('add_folder');\">添加</a> <a href=\"javascript:frmExecute('');\">管理</a>";
		}
		include 'tpl/_msg.tpl.php';
	    break;
	}
	case "add_folder":
	{
		$get_id=0;//将id置0
		include "tpl/article.folder.add.tpl.php";
		break;
	}
	case 'edit_folder':
	{
		$arr_folder=array();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."article_folder where folder_id='".$get_id."'");
		if($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_folder = $obj_rs;
		}
		include 'tpl/article.folder.edit.tpl.php';
		break;
	}
	case "html_exe":
	{
		//分页
		$int_pagesize=100;
		$str_where=" where article_channel_id=".$get_channel_id;
		$str_where_article="";
		$str_where_folder="";
		if($get_articleid){
			$str_where_article="article_id in(".$get_articleid.")";
		}

		if(isset($get_folderid)){
			$arr_folderid=array();
			$str_x_where=" where 0";
			$arr_folderid=explode(",",$get_folderid);
			foreach($arr_folderid as $item){
				$str_x_where.=" or concat(',',folder_pid,',') like '%,".$item.",%'";
			}
			$obj_result = $obj_db->query("SELECT folder_id FROM ".DB_PRE."article_folder".$str_x_where);
			while($obj_rs = $obj_db->fetch_array($obj_result))
			{
				$arr_folderid[] = $obj_rs["folder_id"];
			}
			$str_folderid=implode(",",$arr_folderid);
			if($str_folderid!=""){
				$str_where_folder="article_folder_id in(".$str_folderid.")";
			}
		}
		if($str_where_article!=""&&$str_where_folder!=""){
			$str_where.=" and (".$str_where_article." or ".$str_where_folder.")";
		}else if($str_where_article!=""){
			$str_where.=" and ".$str_where_article;
		}else if($str_where_folder!=""){
			$str_where.=" and ".$str_where_folder;
		}
		$arr_doc=array();
		$obj_rs   = $obj_db->get_one("SELECT count(*) as num FROM ".DB_PRE."article".$str_where);
		$int_total=$obj_rs["num"];
		$int_pages= ceil($int_total/$int_pagesize);
		$get_page = min($int_pages, $get_page);
		$get_page = max($get_page, 1);
		$page_split_page=get_pages($int_total, $get_page, $int_pagesize);
		$int_offset = $int_pagesize*($get_page-1);
		$str_limit = " limit ".$int_offset.",".$int_pagesize;
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."article".$str_where." order by article_id desc".$str_limit);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_article[]=$obj_rs;
		}
		include 'tpl/article.html.exe.tpl.php';
		break;
	}
	case "html_frame":
	{
		if(isset($get_folderid)){
			if(is_array($get_folderid)){
				$get_folderid=implode(",",$get_folderid);
			}
		}else{
			$get_folderid="";
		}
		if(isset($get_articleid)){
			if(is_array($get_articleid)){
				$get_articleid=implode(",",$get_articleid);
			}
		}else{
			$get_articleid="";
		}
		include 'tpl/article.html.frame.tpl.php';
		break;
	}
	default:
	{
		$str_selfolder=me_folder_next($get_channel_id,$get_s_folder_id);
		$str_paste_folder="";

		//分页,读取文章
		$str_where=" where 1";
		if(!$obj_ctl_limit->issuper){
			$arr_folder_id=$obj_ctl_limit->get_folder_id();
			$str_where_limit="";
			foreach($arr_folder_id as $item){
				if($get_me_folder_id>0 && $item!=$get_me_folder_id) continue;
				$str_where_limit.=" or concat(',',folder_pids,',') like '%,".$item.",%'";
			}
			$str_where.=" and article_folder_id in(select folder_id from ".DB_PRE."article_folder where 1>2".$str_where_limit.")";
		}
		//查询
		if($get_s_title!=""){
			$get_s_title=str_replace(" ","%",$get_s_title);
			$str_where.=" and article_title like '%".$get_s_title."%'";
			$get_issearch="1";
		}
		if(is_date($get_s_updatetime1)){
			$str_where.=" and FROM_UNIXTIME(article_updatetime)>='".$get_s_updatetime1."'";
			$get_issearch="1";
		}
		if(is_date($get_s_updatetime2)){
			$str_where.=" and FROM_UNIXTIME(article_updatetime)<='".$get_s_updatetime2."'";
			$get_issearch="1";
		}
		if($get_s_folder_id>0){
			$str_where.=" and article_folder_id=".$get_s_folder_id;
			$get_issearch="1";
		}
		if($get_issearch==""){
			$str_where.=" and article_channel_id='".$get_channel_id."'";
			if($get_list_style=="dir") $str_where.=" and article_folder_id='".$get_me_folder_id."'";
			if($get_list_style=="dir"){
				$str_temp="tpl/article.tpl.php";
			}else{
				$str_temp="tpl/article.list.tpl.php";
				$str_paste_folder=$obj_tab_folder->get_select_folder("paste_folder");
			}
		}else{
				$str_temp="tpl/article.search.tpl.php";
		}
		$arr_article=array();
		$obj_rs   = $obj_db->get_one("SELECT count(*) as num FROM ".DB_PRE."article".$str_where);
		$int_total=$obj_rs["num"];
		$int_pages= ceil($int_total/$int_pagesize);
		$get_page = min($int_pages, $get_page);
		$get_page = max($get_page, 1);
		$page_split_page=get_pages($int_total, $get_page, $int_pagesize);
		$int_offset = $int_pagesize*($get_page-1);
		$str_limit = " limit ".$int_offset.",".$int_pagesize;
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."article".$str_where." order by article_id desc".$str_limit);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_article[] = $obj_rs;
		}
		$arr_folder=array();
		if($obj_ctl_limit->issuper){
			$str_sql="SELECT * FROM ".DB_PRE."article_folder where folder_pid='".$get_me_folder_id."' and folder_channel_id='".$get_channel_id."' order by folder_id desc";
		}else{
			$arr_folder_id=$obj_ctl_limit->get_folder_id();
			$str_ids=implode(",",$arr_folder_id);
			if($str_ids=="") $str_ids="0";
			$str_sql="SELECT * FROM ".DB_PRE."article_folder where  (folder_pid='".$get_me_folder_id."' or '0'='".$get_me_folder_id."') and (1>2".$str_where_limit.") order by folder_id desc";
		}
		$obj_result = $obj_db->query($str_sql);
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_folder[] = $obj_rs;
		}
		include $str_temp;
	}
}

function me_folder_path($msg_folderid,$msg_rootid=0)
{
	global $obj_tab_folder,$obj_db,$obj_ctl_limit;
	if(!$obj_ctl_limit->chk_article_folder($msg_folderid)) return "";
	$str_sql="select folder_id,folder_name,folder_pid from ".DB_PRE."article_folder where folder_id='".$msg_folderid."'";
	$obj_result = $obj_db->query($str_sql);
	$str_val="";
	if($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$str_val=" -> <a href=\"javascript:meOpenFolder('".$obj_rs['folder_id']."');\">".$obj_rs['folder_name']."</a>";
		$str_val=me_folder_path($obj_rs['folder_pid'],$msg_rootid).$str_val;
	}
	return $str_val;
}
function me_folder_next($msg_channelid,$msg_id=0,$msg_pid=0,$msg_lng=0)
{
	global $obj_tab_folder,$obj_db,$obj_ctl_limit;
		if($obj_ctl_limit->issuper || $msg_pid>0){
			$str_sql="select folder_id,folder_name,folder_pid from ".DB_PRE."article_folder where folder_pid=".$msg_pid." and folder_channel_id=".$msg_channelid;
		}else{
			$arr_folder_id=$obj_ctl_limit->get_folder_id();
			$str_ids=implode(",",$arr_folder_id);
			if($str_ids=="") $str_ids="0";
			$str_sql="select folder_id,folder_name,folder_pid from ".DB_PRE."article_folder where folder_id in(".$str_ids.")";
		}
	$obj_result = $obj_db->query($str_sql);
	$str_val="";
	$str_space="";
	for($i=0;$i<$msg_lng;$i++){
		$str_space.="&nbsp;&nbsp;&nbsp;&nbsp;";
	}
	while($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$str_sel="";
		if($obj_rs["folder_id"]==$msg_id) $str_sel=" selected";
		$str_val.="<option value='".$obj_rs["folder_id"]."'".$str_sel.">".$str_space.$obj_rs["folder_name"]."</option>";
		$str_val=$str_val.me_folder_next($msg_channelid,$msg_id,$obj_rs['folder_id'],$msg_lng+1);
	}
	return $str_val;
}
?>
