<?php
include "inc.php";
include "getsql.table.lib.php";
$obj_tab_article=new cls_tab_article();
$arr_article_attribute=$obj_tab_article->get_article_attribute();
include SYS_ROOT.'/cls_tab/tab.temp.fun.folder.php';
$obj_tab_fun_folder=new cls_tab_temp_fun_folder();
if(!isset($get_funfolder_pid)) $get_funfolder_pid=0;
if(!isset($get_funid)) $get_funid=0;
if(!isset($get_isselfolder)) $get_isselfolder="";

$page_footer_info=<<<HTML
<input type='hidden' name='isselfolder' value='$get_isselfolder' id="id_isselfolder">
<input type='hidden' name='funfolder_pid' value='$get_funfolder_pid' id="funfolder_pid">
<input type='hidden' name='funid' value='$get_funid' id="id_funid">
HTML;

if($get_act=="del_folder"){
	$arr_msg=$obj_tab_fun_folder->on_del($get_id);
	if($arr_msg["err"]!=""){
		$obj_msg->alert($arr_msg["err"]);
	}
	$get_act="funfolder";
}
if($get_act=="save_folder"){
	$arr_fileds=array();
	$arr_fileds["folder_name"]=$get_folder_name;
	$arr_fileds["folder_id"]=$get_id;
	$arr_fileds["folder_pid"]=$get_funfolder_pid;
	$arr_msg=$obj_tab_fun_folder->on_save($arr_fileds);
	if($arr_msg["err"]!=""){
		$obj_msg->alert($arr_msg["err"]);
	}
	$get_act="funfolder";
}
switch($get_act){
	case "edit_folder":
		$arr_folder=array();
		$obj_result=$obj_db->query("select * from ".DB_PRE."temp_fun_folder where folder_id='".$get_id."'");
		if($obj_rs=$obj_db->fetch_array($obj_result)){
			$arr_folder=$obj_rs;
		}
		include "tpl/getsql.funfolder.edit.tpl.php";
		break;
	case "add_folder":
		$get_id=0;
		$arr_folder = $obj_db->get_fields(DB_PRE."temp_fun_folder",true);
		include "tpl/getsql.funfolder.edit.tpl.php";
		break;
		break;
	case "funfolder":
	{
		$this_folder_path="路径：<a href='javascript:meSetFunpid(0);'>根目录</a>".me_funfolder_path($get_funfolder_pid);
		$arr_folder = array();
		$arr_file=array();
		$obj_result = $obj_db->query("SELECT * FROM ".DB_PRE."temp_fun_folder where folder_pid='".$get_funfolder_pid."' order by folder_id");
		while($obj_rs = $obj_db->fetch_array($obj_result))
		{
			$arr_folder[] = $obj_rs;
		}
		if(empty($get_isselfolder)){
			$obj_result=$obj_db->query("select * from ".DB_PRE."temp_fun a left join ".DB_PRE."temp_fun_folder b on a.fun_folder_id=b.folder_id where fun_folder_id=".$get_funfolder_pid);
			while($obj_rs=$obj_db->fetch_array($obj_result)){
				$arr_file[]=$obj_rs;
			}
		}
		include "tpl/getsql.funfolder.tpl.php";
		break;
	}
	case "source":
	{
		(isset($get_source))? $str_key=$get_source:$str_key="company";

		$str_content="";
		foreach($arr_source[$str_key] as $item=>$key){
			$str_content.= "<li><a href=\"javascript:textarea_insertstr('id_content','{\$item[\'".$item."\']}');\">".$key."</a></li>";
		}
		$str_content.="|||";
		foreach($arr_source[$str_key."_sort"] as $item=>$key){
			$str_content.="<option value='".$item."'>".$key."</option>";
		}
		echo $str_content;
		break;
	}
	case "folderlist":
	{
		$arr_x=array();
		$obj_result = $obj_db->query("SELECT channel_id,channel_name FROM ".DB_PRE."article_channel");
		while($obj_rs = $obj_db->fetch_array($obj_result)){
			echo "<li>".$obj_rs["channel_name"]."</li>".me_folder_next($obj_rs["channel_id"]);
		}
		break;
	}
	case "savefun":
		include SYS_ROOT.'/cls_tab/tab.temp.fun.php';
		$obj_tab_fun=new cls_tab_temp_fun();
		$arr_exe=me_get_execont();
		$arr_fields=array();
		$arr_fields["fun_id"]=$get_funid;
		$arr_fields["fun_folder_id"]=$get_funfolderid;
		$arr_fields["fun_name"]=$get_funname;
		$arr_fields["fun_table"]=$get_table_name;
		$arr_fields["fun_perms"]=$arr_exe["perms"];
		$arr_fields["fun_cont"]=$arr_exe["cont"];
		$arr_fields["fun_code"]=$arr_exe["code"];
		if(isset($get_attribute_val) && is_array($get_attribute_val)){
			$arr_fields["attribute_val"]=serialize($get_attribute_val);
		}
		$arr_msg=$obj_tab_fun->on_save($arr_fields);
		if($arr_msg["err"]==""){
			$arr_msg["act_act"]="<a href=\"javascript:frmExecute('');\">继续生成</a>&nbsp;&nbsp;<a href=\"?funid=".$arr_msg["id"]."\">修改</a>&nbsp;&nbsp;<a href=\"javascript:parent.textarea_insertstr('file_content','{fun(".$arr_msg["path"].")}');parent.div_hide_show('wingetsql');\">插入</a>";
		}
		include 'tpl/_msg.tpl.php';
		break;
	case "exe":
	{
		$arr_exe=me_get_execont();
		echo $arr_exe["code"];
		break;
	}
	default:
		if($get_funid>0){
			$obj_result=$obj_db->query("select * from ".DB_PRE."temp_fun a left join ".DB_PRE."temp_fun_folder b on a.fun_folder_id=b.folder_id where fun_id='".$get_funid."'");
			if($obj_rs=$obj_db->fetch_array($obj_result)){
				$obj_rs["fun_perms"]=unserialize($obj_rs["fun_perms"]);
				if(isset($obj_rs["fun_perms"]["attribute_val"])){
					$obj_rs["fun_perms"]["attribute_val"]=unserialize($obj_rs["fun_perms"]["attribute_val"]);
				}else{
					$obj_rs["fun_perms"]["attribute_val"]=array();
				}
				$arr_fun=$obj_rs;
			}
	    }else{
			$arr_fun = $obj_db->get_fields(DB_PRE."temp_fun",true);
		}
		include "tpl/getsql.tpl.php";
		break;
}

function me_folder_next($msg_channelid,$msg_pid=0,$msg_lng=1)
{
	global $obj_tab_folder,$obj_db;
	$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;
	$obj_result = $obj_db->query($str_sql);
	$str_left="<li class='x_".$msg_lng."'>";
	$str_val="";
	while($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$str_val.="<input type='checkbox' name='folderlist[]' value='".$obj_rs["folder_id"]."'>".$obj_rs["folder_name"];
		$str_x=me_folder_next($msg_channelid,$obj_rs['folder_id'],$msg_lng+1);
		if($str_x!=""){
			$str_val.="</li>".$str_x."<li class='x_".$msg_lng."'>";
		}
	}
	if($str_val!="") $str_val=$str_left.$str_val."</li>";
	return $str_val;
}
function me_funfolder_path($msg_pid)
{
	global $obj_db,$tab_area;
	$str_sql="select folder_id,folder_name,folder_pid from ".DB_PRE."temp_fun_folder where folder_id='".$msg_pid."'";
	$obj_result = $obj_db->query($str_sql);
	$str_val="";
	if($obj_rs = $obj_db->fetch_array($obj_result))
	{
		$str_val=" -> <a href=\"javascript:meSetFunpid('".$obj_rs['folder_id']."');\">".$obj_rs['folder_name']."</a>";
		$str_val=me_funfolder_path($obj_rs['folder_pid']).$str_val;
	}
	return $str_val;

}

function me_get_execont(){
	global $get_table_name,$get_field_sort,$get_fields_sort_act,$get_field_limit,$get_limit_0,$get_limit_1,$get_limit_2;
	global $get_folderlist,$get_attribute_val,$get_content,$get_ispage,$get_ispagesize;
	$get_content=format_request_base($get_content,true);
	$arr_return=array();
	$str_table_name=$get_table_name;
	$str_order=$get_field_sort;
	$arr_perms=array();
	$str_order_act=$get_fields_sort_act;
	if(!in_array($str_order_act,array("asc","desc"))) $str_order_act="desc";

	if($str_order_act!="") $str_order_act=" ".$str_order_act;

	$str_orderstr="";
	if($str_order!="") $str_orderstr=" order by ".$str_order.$str_order_act;

	if($get_field_limit=="0"){
		(isset($get_limit_0))? $get_limit_0=intval($get_limit_0):$get_limit_0=10;
		$str_limit="limit 0,".intval($get_limit_0);
		$lng_limit1=0;
		$lng_limit2=$get_limit_0;
	}else{
		(isset($get_limit_1) && !empty($get_limit_1))? $get_limit_1=intval($get_limit_1):$get_limit_1=0;
		(isset($get_limit_2) && !empty($get_limit_2))? $get_limit_2=intval($get_limit_2):$get_limit_2=10;
		$str_limit=" limit ".$get_limit_1.",".$get_limit_2;
		$lng_limit1=$get_limit_1;
		$lng_limit2=$get_limit_2;
	}
	$arr_perms["table_name"]=$get_table_name;
	$arr_perms["limit1"]=$lng_limit1;
	$arr_perms["limit2"]=$lng_limit2;
	$arr_perms["field_limit"]=$get_field_limit;
	$arr_perms["fields_sort_act"]=$get_fields_sort_act;
	$arr_perms["field_sort"]=$get_field_sort;
	$arr_perms["ispage"]=$get_ispage;
	$arr_perms["ispagesize"]=$get_ispagesize;
	$str_return_cont="";
	$str_pagesize_val="";
	switch($str_table_name){
		case "hr_company":
		{
			if($get_ispage && intval($get_ispagesize)>0) $str_pagesize_val=",'',".$get_ispagesize;
			$str_return_cont= '{get_temp_company(\''.$str_table_name.'\','.$lng_limit1.','.$lng_limit2.',\''.$str_orderstr.'\''.$str_pagesize_val.')}';
			break;
		}
		case "hr_job":
		{
			if($get_ispage && intval($get_ispagesize)>0) $str_pagesize_val=",'',".$get_ispagesize;
			$str_return_cont= '{get_temp_job(\''.$str_table_name.'\','.$lng_limit1.','.$lng_limit2.',\''.$str_orderstr.'\''.$str_pagesize_val.')}';
			break;
		}
		case "hr_resume":
		{
			if($get_ispage && intval($get_ispagesize)>0) $str_pagesize_val=",'',".$get_ispagesize;
			$str_return_cont= '{get_temp_resume(\''.$str_table_name.'\','.$lng_limit1.','.$lng_limit2.',\''.$str_orderstr.'\''.$str_pagesize_val.')}';
			break;
		}
		case "company_job":
		{
			if($get_ispage && intval($get_ispagesize)>0) $str_pagesize_val=",'',".$get_ispagesize;
			$str_return_cont= '{get_temp_company_job(\''.$str_table_name.'\','.$lng_limit1.','.$lng_limit2.',\''.$str_orderstr.'\''.$str_pagesize_val.')}';
			break;
		}
		case "article":
		{
			if($get_ispage && intval($get_ispagesize)>0) $str_pagesize_val=",".$get_ispagesize;
			$str_folderids="";
			if(isset($get_folderlist)&&is_array($get_folderlist)){
				$str_folderids=implode(",",$get_folderlist);
			}
			$str_where="";
			if(isset($get_attribute_val) && !empty($get_attribute_val)){
				$arr_x=array();
				foreach($get_attribute_val as $item){
					$arr_x[]="article_attribute like '%".$item."%'";
				}
				if(count($arr_x)>0){
					$str_where.="(".implode(" or ",$arr_x).")";
				}
			}
			$str_return_cont= '{get_temp_article(\''.$str_table_name.'\',\''.$str_folderids.'\','.$lng_limit1.','.$lng_limit2.',"'.$str_where.'",\''.$str_orderstr.'\''.$str_pagesize_val.')}';
			break;
		}
	}
	$str_return_cont.= chr(10).'{foreach($sql_return[\''.$str_table_name.'\'] as $item)}'.chr(10);
	$str_return_cont.= $get_content.chr(10);
	$str_return_cont.= '{/foreach}';
	$arr_return["code"]=$str_return_cont;
	$arr_return["cont"]=$get_content;
	$arr_return["perms"]=$arr_perms;
	return $arr_return;
}
?>