使用的xajax版本是0.5.4beta,fckeditor版本是2.4.3,通过xajax给fckeditor增加自动保存草稿功能,
本文侧重于演示,使用sessionid记录对应关系,用户关闭浏览器後数据就会消失,在实际应用中可以使用数据库存储数据,修改保存函数autosave和调用函数loadcache 即可.
<?php
session_start();
define('ROOT_DIR',str_replace("/","/",dirname(__FILE__)));
$cachedir = './cache/';
//清除超过1天的记录文件
function clearcache($dir,$date = 1) {
$now = time();
$time = $date * 60 * 60 * 24;
if(!is_dir($dir)){
die('error :dir' . $dir . ' is not exist');
}
$handle = opendir($dir);
while(false !== ($filename = readdir($handle))) {
if($filename == '.' OR $filename == '..' OR $filename == ''){
continue;
}
if(($now - filemtime($dir.$filename)) > $time){
@unlink($dir.$filename);
}
}
}
function xml_escape($string) {
return str_replace(array('&','"',"'",'<','>'),
array('&','"',''','<','>'),
$string
);
}
//执行清理
clearcache($cachedir);
require_once('./xajax/xajax.inc.php');
$xajax = new xajax();
$xajax->registerFunction('autosave');
$xajax->registerFunction('loadcache');
$xajax->processRequest();
echo $xajax->getJavascript('./xajax');
//定义载入数据函数
function loadcache() {
global $cachedir;
$filename = $cachedir . session_id().'.txt';
if(file_exists($filename)){
$content = '';
$handle = file($filename);
foreach($handle as $key=>$val) {
$content .= $val;
}
}else{
$content = '';
}
$content = xml_escape($content);
$obj = new xajaxResponse();
$obj->call("SetContents('$content')");
//$obj->alert($content);
$obj->assign('autosavemsg','innerHTML',"成功载入数据");
return $obj;
}
//定义自动保存函数
function autosave($content) {
global $cachedir;
$obj = new xajaxResponse();
$content = mysql_escape_string($content);
$filename = $cachedir . session_id() . ".txt";
$fp = fopen($filename,"w+");
fwrite($fp,$content);
fclose($fp);
if(file_exists($filename)){
$obj->assign("autosavemsg","innerHTML","成功保存数据...");
}
//$obj->alert($content);
return $obj;
}
function editor($editorid,$content = '') {
//载入编辑器
require_once('../../includes/fckeditor/fckeditor.php');
$editor = new fckeditor($editorid);
$editor->BasePath = '../../includes/fckeditor/';
$editor->Value = $content;
return $editor->createhtml();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta name="Description" content="" />
</head>
<style type="text/css">
<!--
a {
cursor: pointer;
}
-->
</style>
<body>
<form name="f1" id="f1" method="post" action="" enctype="multipart/form-data">
<?php
//载入编辑器
echo editor('content');
?>
<script language="javascript">
<!--
// 自动保存时间间隔
var AutoSaveTime=90000;
// 计时器对象
var AutoSaveTimer;
// 首先设置一次自动保存状态
SetAutoSave();
// 设置自动保存状态函数
function SetAutoSave() {
AutoSaveTimer=setInterval("GetContents('content')",AutoSaveTime);
}
//获取内容
function GetContents(contentid) {
var oEditor = FCKeditorAPI.GetInstance(contentid) ;
content = oEditor.GetXHTML( true );
xajax_autosave(content);
}
//设置编辑器内容
function SetContents(content)
{
// Get the editor instance that we want to interact with.
var oEditor = FCKeditorAPI.GetInstance('content') ;
// Set the editor contents (replace the actual one).
oEditor.SetHTML( content ) ;
}
//-->
</script>
</form>
<div id="autosavemsg">
</div>
<a href="#" onclick="xajax_loadcache();" title="">载入自动保存内容</a> <a href="#" onclick="GetContents('content');" title="">手动保存</a>
</body>
</html>