<?
//b_edit.php
include "config.inc.php";
include "class/member.class.php";
include "class/article.class.php";
$art  = new article();
$member=new member();
$user_info = $member->member_auth();
function getBlockTemplate($template = "default")
    {
        $dir = "template/" . $template . "/blocks/";
        $d = dir($dir);

        while ($file = $d->read()) {
            $ext_name = strtolower(str_replace(".", "", strrchr($file, ".")));

            if ($file == "." || $file == ".." || $ext_name != "tpl")
                continue;

            $ret[$file] = $file;
        } 

        return $ret;
    } 
function edit_b($id){
	global $PlusDB,$site_conf;
	$bdb = $PlusDB->prefix(blocks);
	$sql="select * from $bdb where id='$id'";
	$row = $PlusDB->getrow($sql);
	echo html_table_header("Edit Block",2);
	if(!$row){
		echo form_header("b_edit.php","saveBlock","bform","");
		echo html_input_hidden("id","$id");
		echo html_input_text("标题","title","",50,255);
		echo html_textarea("内容","content","",50,255);
	}else{
		echo form_header("b_edit.php","updateBlock","bform","");
		echo html_input_hidden("id","$id");
		echo html_input_text("标题","title",$row[title],50,255);
		echo html_textarea("内容","content",$row[content],50,255);
	}
	echo form_footer();
	echo html_table_footer();
	?>
	<script type="text/javascript" src="editor/fckeditor.js"></script>
<script>
function insertNCode(){
	var text;
	text='{%'+document.tcode.n_code.value+'%}';
	add(text);
}
function insertN2Code(){
	var text;
	text='{%'+document.tcode.n2_code.value+'%}';
	add(text);
}
function insertN3Code(){
	var text;
	text='{%'+document.tcode.n3_code.value+'%}';
	add(text);
}
function insertN4Code(){
	var text;
	text='{%'+document.tcode.n4_code.value+'%}';
	add(text);
}
function insertN5Code(){
	var text;
	text='{%'+document.tcode.n5_code.value+'%}';
	add(text);
}
function insertN6Code(){
	var text;
	text='{%'+document.tcode.n6_code.value+'%}';
	add(text);
}
function insertN7Code(){
	var text;
	text='{%'+document.tcode.n7_code.value+'%}';
	add(text);
}
function insertN8Code(){
	var text;
	text='{%'+'insert name=article cid='+document.tcode.a_cid.value+' limit='+document.tcode.a_limit.value+' ismax='+document.tcode.a_max.value+' ismore='+document.tcode.a_more.value+' template='+document.tcode.a_template.value+' order_by='+document.tcode.a_order.value+'%}';
	add(text);
}
function insertN9Code(){
	var text;
	text='{%insert name=poll id='+document.tcode.n9_code.value+' template=poll.tpl%}';
	add(text);
}
function insertN10Code(){
	var text;
	text='{%insert name=ad aid='+document.tcode.n10_code.value+'%}';
	add(text);
}
function insertN11Code(){
	var text;
	text='{%insert name=ad tid='+document.tcode.n11_code.value+'%}';
	add(text);
}
function insertN12Code(){
	var text;
	text='{%insert name=block bid='+document.tcode.n12_code.value+'%}';
	add(text);
}
function add(text){
	//opener.AddText(text);
	//top.window.close();
			var oEditor = FCKeditorAPI.GetInstance('content') ;
			oEditor.InsertHtml( text ) ;
			oEditor.Focus();
}
</script>
<a name=template_code>
<form name=tcode id=tcode action=# method=get>
<table width=100% border=0 cellpadding=0 cellpadding=3>
<tr><td colspan=3><hr size=1 noshade></td></tr>
<tr ><td>
<?=_LANG_1038?><br>
<?
	global $PlusDB;
	$sql="select * from ".$PlusDB->prefix(poll)." where 1=1";
	$rt=$PlusDB->query($sql);
	$num = $rt->RecordCount($rt);
	if($num==0){
	    echo _LANG_1039;
	}else{
	echo "<select name=n9_code>";
	   while($row= $rt->fetchRow($rt)){
		   echo "	<option value='$row[id]'>$row[title] </option>";
	   }
	   echo "</select>";
	}
?>
&nbsp;<a href="javascript:insertN9Code('')" ><?=_LANG_0962?></a>
</td><td><?=_LANG_1040?><br>
<?
	$sql="select * from ".$PlusDB->prefix(ad)." where 1=1";
	$rt=$PlusDB->query($sql);
	$num = $rt->RecordCount($rt);
	if($num==0){
	    echo _LANG_1041;
	}else{
	echo "<select name=n10_code>";
	   while($row= $rt->fetchRow($rt)){
		   echo "	<option value='$row[id]'>$row[title] </option>";
	   }
	   echo "</select>";
	}
?>
&nbsp;<a href="javascript:insertN10Code('')" ><?=_LANG_0962?></a>
</td></tr>
<tr><td colspan=3><hr size=1 noshade></td></tr>
<tr class=tablerow><td>

<?=_LANG_1042?><br>
<?
	echo "<select name=n11_code>";
	   for($i=1;$i<=40;$i++){
		   echo "	<option value='$i'>$i</option>";
	   }
	   echo "</select>";
?>
&nbsp;<a href="javascript:insertN11Code('')" ><?=_LANG_0962?></a>
</td><td><?=_LANG_1043?><br>
<?
	echo "<select name=n12_code>";
	$bdb=$PlusDB->prefix(blocks);
	$sql="select id,title from $bdb where 1=1";
	$rt=$PlusDB->query($sql);
	while($row=$rt->fetchRow($rt)){
			echo "<option value='$row[id]'>$row[title]</option>";
	}
	   echo "</select>";
?>
&nbsp;<a href="javascript:insertN12Code('')" ><?=_LANG_0962?></a>
</td></tr>
<tr><td colspan=3><hr size=1 noshade></td></tr>
<tr class=tablerow><td colspan=3>
<?=_LANG_1024?>
<?
	   global $art;
	   $newTree = $art->getArticleCategoryNoImage(0);
        $newTree[0] = _LANG_1025; 
        $tlist = getBlockTemplate($site_conf[template][site_template]);
?>
</td></tr>
<tr><td colspan=3><?=_LANG_1026?>
<select name=a_cid>
<?foreach($newTree as $cid=>$ctitle){?>
	<option value='<?=$cid?>'><?=$ctitle?></option>
	<?}?>
</select>
&nbsp;<?=_LANG_1027?><input type=text name=a_limit value=5 size=3>
&nbsp;<?=_LANG_1028?><input type=text name=a_max value=20 size=3>
&nbsp;<?=_LANG_1029?><select name=a_more><option value=yes><?=_LANG_1030?></option><option value=no><?=_LANG_1031?></option></select>
<br><?=_LANG_1032?><select name=a_template>
<?foreach($tlist as $tname){?>
	<option value='<?=$tname?>'><?=$tname?></option>
<?}?>
</select>
&nbsp;<?=_LANG_1033?>
<select name=a_order >
<option value=date_asc><?=_LANG_1034?></option>
<option value=date_desc><?=_LANG_1035?></option>
<option value=hit_asc><?=_LANG_1036?></option>
<option value=hit_desc><?=_LANG_1037?></option>
</select>
&nbsp;<a href="javascript:insertN8Code('')" ><?=_LANG_0962?></a>
</td></tr>
</table>
</form>
<?
}
?>
<HTML><HEAD><TITLE>::Cmsez Control Panel::</TITLE>
<META http-equiv=Content-Type content="text/html; charset=<?=_CHAR?>"><LINK 
href="images/style.css" type=text/css rel=stylesheet>
<META content="MSHTML 6.00.2800.1458" name=GENERATOR>
</HEAD>
<BODY leftMargin=0 topMargin=0>
<?
if($member->checkAdmin2()){	
$action=$_REQUEST["action"];
switch($action){
	case "updateBlock":
	updateBlock();
	break;
case "saveBlock":
	saveBlock();
	break;
default:
	edit_b($_GET[id]);
}
}

function saveBlock(){
	global $PlusDB;
	$id=$_POST[id];
	$title = $_POST[title];
	$content = $_POST[content];
	$bdb = $PlusDB->prefix(blocks);
	$sql="insert into $bdb (id,title,content) values('$id','$title','$content')";
	if($PlusDB->query($sql)){
	$filename ="temp/block_".$id.".tpl";
	$fp = fopen($filename,"w");
	fputs($fp,$content);
	fclose( $fp );
	echo "<a href='#' onclick=\"window.opener.location=window.opener.location;window.opener=null;window.close();\">Close</a>";
	}else{
		echo $sql;
		echo $PlusDB->errorMsg();exit;
		error("无法更新");
	}
}

function updateBlock(){
	global $PlusDB;
	$id=$_POST[id];
	$title = $_POST[title];
	$content = $_POST[content];
	$bdb = $PlusDB->prefix(blocks);
	$sql="update $bdb set title='$title', content = '$content' where id='$id' ";
	if($PlusDB->query($sql)){
	$filename ="temp/block_".$id.".tpl";
	$fp = fopen($filename,"w");
	fputs($fp,$content);
	fclose( $fp );
	echo "<a href='#' onclick=\"window.opener.location=window.opener.location;window.opener=null;window.close();\">Close</a>";
	}else{
		echo $PlusDB->errorMsg();exit;
		//error("无法更新");
	}
}
?>