无限分类&生成多页面静态新闻
时间:2006-01-16 来源:wleige
/*=======================================
类名: cNews
作者: Yupei
功能: 新闻类
参数:
返回:
使用:
最后更新时间:2005-1-11
=======================================*/
class cNews
{
var $mNid;
var $mRoot;
var $mFileExt;
var $mStyleFile;
/*+++++++++++++++++++++++++
名称: 构造方法
功能: 设定初始值
++++++++++++++++++++++++++*/
Function __construct()
{
$this -> mFileExt = ".shtml"; //生成静态的默认扩展名
$this -> mRoot = "";
$this -> mStyleFile = $this -> mRoot."/admin/news/style/default.shtml"; //默认模板
}
Function __destruct()
{
}
/*+++++++++++++++++++++++++
名称: setRoot(绝对路径)
功能: 设置系统根目录
++++++++++++++++++++++++++*/
Function setRoot($root)
{
$this -> mRoot = $root;
}
/*+++++++++++++++++++++++++
名称: setStyleFile(样式文件绝对路径)
功能: 设置生成静态时,取用的样式文件
++++++++++++++++++++++++++*/
Function setStyleFile($stylefile)
{
$this -> mStyleFile = $stylefile;
}
Function setFileExt ($ext)
{
$this -> mFileExt = $ext ;
}
/*+++++++++++++++++++++++++
名称:CreateType(栏目名,路径)
功能: 创建新栏目
++++++++++++++++++++++++++*/
Function createType(&$rTypeName,&$rPath)
{
$insert_id = 0;
$layer = count(explode(',',$rPath));
$db = new cDB ;
$db -> connect(DBserver);
$arrayData = array(
'n_name' => $rTypeName ,
'n_path' => $rPath,
'n_layer'=> $layer
) ;
$insert_id = $db -> insert('news_type' ,$arrayData,TRUE);
$db ->close() ;
return $insert_id ;
}
function bulidSelect($rOption,$rCpath="0",$opt_val="n_id",$default=null)
{
static $htmlStr ="";
foreach($rOption as $key => $value)
{
if ($value['n_path'] == $rCpath )
{
if($value['n_layer']>1)
{
$space = str_repeat(" ",$value['n_layer']-2);
$tline=" |_";
}
else
{
$space = "";
$tline = "";
}
($value['n_id'] == $default)?$select="selected":$select="";
$htmlStr .= "{$space}{$tline}{$value[n_name] }\n";
$this->bulidSelect($rOption,$value['absPath'],$opt_val,$default);
}
}
return $htmlStr;
}
/*+++++++++++++++++++++++++
名称:insertInfoToDB(array=>表单信息)
功能: 发布新闻到数据库
++++++++++++++++++++++++++*/
Function insertInfoToDB(&$rArrayData)
{
$ArrayData = array(
n_id => '',
n_title => $rArrayData['title'] ,
n_fid => $rArrayData['fid'],
n_keyword => $rArrayData['keyword'] ,
n_author => $rArrayData['sauthor'] ,
n_editor => $rArrayData['editor'] ,
n_source => $rArrayData['source'] ,
n_creattime => date('Y-m-d H:i:s') ,
n_edittime => date('Y-m-d H:i:s') ,
n_linkflag => $rArrayData['linkflag'] ,
n_linkurl => $rArrayData['linkurl'] ,
n_activeflag=> 0,
n_ip => $this -> GetIp( ),
n_html => $rArrayData['html'],
n_page => $rArrayData['page']
);
$db = new cDB ;
$db -> connect(DBserver);
$insert_id = $db -> insert('news_info' ,$ArrayData,TRUE);
//新闻基本信息插入完毕.插入新闻内容表.
$ArrayData = array(
n_id => $insert_id,
n_content => $rArrayData['content1']
);
$db -> insert('news_content',$ArrayData);
$db -> close();
return TRUE;
}
/*+++++++++++++++++++++++++
名称:MakeHtml(array=>表单信息)
功能: 生成新闻静态页面
++++++++++++++++++++++++++*/
function makeHtml(&$rContent,&$rHtmlDir,$filename="")
{
$writetofile = $rHtmlDir.'/'.$filename;
$handle = fopen($writetofile, "w");
fwrite($handle, $rContent);
fclose($handle);
return $writetofile;
}
/*+++++++++++++++++++++++++
名称ublishNews(内容)
功能: 发表新新闻
++++++++++++++++++++++++++*/
function publishNews(&$rArrayData)
{
$file_url = "news/html/".date("Y/m/d"); //设置相对目录
$file_dir = $this -> mRoot."/".$file_url; //设置静态生成绝对目录
$this -> makeDir($file_dir,0777); //建立目录
$tmpfile = tempnam (realpath($file_dir), date("Hi") ); //生成临时文件
$file_name = subStr($tmpfile,0,-4) ; //去掉原扩展名
rename($tmpfile,$file_name.$this -> mFileExt); //改文件名
$file_name =basename($file_name);
$html = new SmartTemplate($this -> mStyleFile);
$html -> assign ($rArrayData);
//分页开始
$contentArr = $this -> parsePage($rArrayData['content1']); //分割页面
$page_count = count($contentArr);
if ($page_count >0)
{
$p_nav = "
内容导航:";
$p_link = "页数:";
for($i=0;$imFileExt} class=page>".($i+1)." ";
if($contentArr[$i][0]!="")
{
$p_nav.="第".($i+1)."页:mFileExt} class=page>".$contentArr[$i][0]."
";
}
else
{
$p_nav.="第".($i+1)."页:mFileExt} class=page>".$rArrayData['title']."({$i})
";
}
}
$p_link .="";
$p_nav.="";
for($i=0;$i assign('content',$contenttmp);
$htmlcontent = $html ->result();
$fp=$file_name.$add.$this->mFileExt;
$this -> makeHtml($htmlcontent,$file_dir,$fp); //写入静态文件
}
}
else
{
$html -> assign('content',$rArrayData['content1']);
$htmlcontent = $html ->result();
$fp=$file_name.$this->mFileExt;
$this -> makeHtml($htmlcontent,$file_dir,$fp); //写入静态文件
}
//分页结束
$rArrayData['html'] = $file_url."/".$file_name.$this->mFileExt;
$rArrayData['page'] = $page_count;
$this -> insertInfoToDB($rArrayData);
unset($htmlcontent,$html,$file_url,$file_dir); //注销HTML网页内容变量,HTML类.
unset($contentmp) ; //注销临时内容变量
}
/*+++++++++++++++++++++++++
名称:ParsePage(内容)
功能: 分割页面
++++++++++++++++++++++++++*/
function parsePage(&$rStr)
{
//分页分析标签字符串数组
$infoTagArr = array("{informationpage}","{informationTag}");
$tmpArr = explode($infoTagArr[0],$rStr);
$pageNum = count($tmpArr);
for($i=1;$i
/*+++++++++++++++++++++++++
名称:MakeDir(路径)
功能: 建立目录
++++++++++++++++++++++++++*/
function makeDir($dir,$param = 0777)
{
$dir = str_replace('','/',$dir);
$list[] = $dir;
While ($dir = substr($dir,0,strrpos($dir,'/')))
{
$list[] = $dir;
if(is_dir($dir)) break;
}
for($i=count($list)-1;$i>0;$i--) @mkdir($list[$i],$param);
return @mkdir($list[0],$param);
}
/*+++++++++++++++++++++++++
名称: GetIp( )
功能: 取得客户IP地址
作者: Yupei
参数:
返回:
使用:
++++++++++++++++++++++++++*/
Function GetIp()
{
return $_SERVER['REMOTE_ADDR'] ;
}
//对页面提交的内容进行分页分析,返回数组($infoReStrArr[页面数][2])
Function parsePageInfo(&$rContent)
{
//分页分析标签字符串数组
$infoTagArr=array("{informationpage}","{informationTag}");
$tmpArr=explode($infoTagArr[0],$rContent);
$pageNum=count($tmpArr);
for($i=1;$i
}
相关阅读 更多 +