<?php
require(dirname(__FILE__)."/config.php");
CheckPurview('temp_One');
if(empty($dopost)) $dopost = "";
if(empty($aid)) $aid = "";
$dsql = new DedeSql(false);
//////////////////////////////////////////
if($dopost=="saveedit")
{
  require_once(dirname(__FILE__)."/../include/inc_arcpart_view.php");
	$uptime = mytime();
	$body = str_replace('&quot;','\\"',$body);
	$filename = ereg_replace("^/","",$nfilename);
	//如果更改了文件名，删除旧文件
	if($oldfilename!=$filename)
	{
		$oldfilename = $cfg_basedir.$cfg_cmspath."/".$oldfilename;
		if(is_file($oldfilename)) unlink($oldfilename);
	}
	$inQuery = "
	 update #@__sgpage set
	 title='$title',
	 ismake='$ismake',
	 filename='$filename',
	 uptime='$uptime',
	 body='$body'
	 where aid='$aid';
	";
	$dsql->SetQuery($inQuery);
	if(!$dsql->ExecuteNoneQuery())
	{
		$dsql->Close();
		ShowMsg("更新页面数据时失败，请检查长相是否有问题！","-1");
	  exit();
	}
	$dsql->Close();
	$filename = $cfg_basedir.$cfg_cmspath."/".$filename;
	if($ismake==1){
	  $pv = new PartView();
    $pv->SetTemplet(stripslashes($body),"string");
    $pv->SaveToHtml($filename);
    $pv->Close();
  }
  else{
  	$fp = fopen($filename,"w") or die("创建：{$filename} 失败，可能是没有权限！");
  	fwrite($fp,stripslashes($body));
  	fclose($fp);
  }
	ShowMsg("成功更新一个页面！","templets_one.php");
	exit();
}
else if($dopost=="delete")
{
   $row = $dsql->GetOne("Select filename From #@__sgpage where aid='$aid'");
   $filename = $cfg_basedir.$cfg_cmspath."/".$row['filename'];
   $dsql->SetQuery("Delete From #@__sgpage where aid='$aid'");
   $dsql->ExecuteNoneQuery();
   $dsql->Close();
   if(is_file($filename)) unlink($filename);
   ShowMsg("成功删除一个页面！","templets_one.php");
   exit();
}
else if($dopost=="make")
{
	require_once(dirname(__FILE__)."/../include/inc_arcpart_view.php");
	$dsql->SetQuery("update #@__sgpage set uptime='".mytime()."' where aid='$aid'");
  $dsql->ExecuteNoneQuery();
	$row = $dsql->GetOne("Select * From #@__sgpage where aid='$aid'");
	$fileurl = $cfg_cmspath."/".$row['filename'];
	$filename = $cfg_basedir.$cfg_cmspath."/".$row['filename'];
	if($row['ismake']==1)
	{
	  $pv = new PartView();
      $pv->SetTemplet($row['body'],"string");
      $pv->SaveToHtml($filename);
      $pv->Close();
   }
   else
   {  
    	$fp = fopen($filename,"w") or die("创建：{$filename} 失败，可能是没有权限！");
  	  fwrite($fp,$row['body']);
      fclose($fp);
   }
	$dsql->Close();
	ShowMsg("成功更新一个页面！",$fileurl);
	exit();
}
$row = $dsql->GetOne("Select  * From #@__sgpage where aid='$aid'");
$dsql->Close();

require_once(dirname(__FILE__)."/templets/templets_one_edit.htm");

?>