246 lines
4.6 KiB
PHP
246 lines
4.6 KiB
PHP
<?php
|
||
/**
|
||
* Archive - 统一文档类,实现各类文档的读取和IO操作,包括新闻、帮助、想法等。
|
||
*/
|
||
|
||
class Archive
|
||
{
|
||
private $db; //传入PDO对象
|
||
public $ptype; //文档分类
|
||
public $tbl_archives; //文档表
|
||
public $tbl_categorys; //栏目表
|
||
public $tbl_catalog; //文档栏目关联表
|
||
public $tbl_tag; //关键字表
|
||
|
||
function __construct($db)
|
||
{
|
||
$this->db = $db;
|
||
//文档的大分类
|
||
//在数组中设置文档分类
|
||
//"中文名称" => "数据库中标识"
|
||
$this->ptype = array(
|
||
"新闻" => "news",
|
||
"想法" => "idea",
|
||
"帮助" => "help",
|
||
"关于" => "about"
|
||
);
|
||
$this->tbl_archives = "archive";
|
||
$this->tbl_categorys = "ar_category";
|
||
$this->tbl_catalog = "ar_catalog";
|
||
$this->tbl_tag = "ar_tag";
|
||
}
|
||
|
||
|
||
/*
|
||
写入关系表
|
||
*/
|
||
function ToCatalog($aid,$cid,$uuid='',$status=0){
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(empty($cid) && empty($uuid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$data = array(
|
||
"aid" => $aid,
|
||
"cid" => $cid,
|
||
"status" => $status
|
||
);
|
||
|
||
if(!empty($uuid))
|
||
{
|
||
$data['uuid'] = $uuid;
|
||
}
|
||
|
||
if($this->db->insert($this->tbl_catalog,$data))
|
||
{
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
|
||
}//ToCatalog()
|
||
|
||
/*
|
||
* 更改栏目 ChangeCatalog()
|
||
*/
|
||
function ChangeCatalog($aid,$cid,$uuid='',$status=0){
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(empty($cid) && empty($uuid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$data = array(
|
||
"cid" => $cid,
|
||
"status" => $status
|
||
);
|
||
|
||
if(!empty($uuid))
|
||
{
|
||
$data['uuid'] = $uuid;
|
||
}
|
||
|
||
if($this->db->update($this->tbl_catalog,$data,"aid=$aid"))
|
||
{
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
|
||
}//ChangeCatalog
|
||
|
||
//修改文档状态
|
||
function ChangeStatus($aid,$pub){
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(!is_bool($pub))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$data = array(
|
||
"is_pub" => $pub
|
||
);
|
||
|
||
if($this->db->update($this->tbl_archives,$data,"id=$aid"))
|
||
{
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
}//修改文档状态
|
||
|
||
//删除文档
|
||
function DeleteArchives($aid){
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$this->DeleteTags($aid);
|
||
|
||
$sql = "DELETE FROM ".$this->tbl_archives." WHERE id=$aid";
|
||
@$this->db->exec($sql);
|
||
|
||
$sql = "DELETE FROM ".$this->tbl_catalog." WHERE aid=$aid";
|
||
@$this->db->exec($sql);
|
||
|
||
return true;
|
||
}//DeleteArchives
|
||
|
||
//写入标签关系
|
||
function MakeTags($aid,$keywords){
|
||
|
||
$keywords = trim($keywords);
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(preg_match("/,/",$keywords))
|
||
{
|
||
$tag = explode(",",$keywords);
|
||
}else{
|
||
if($this->db->insert($this->tbl_tag,array("id"=>$aid,"tag"=>$tag)))
|
||
{
|
||
return true;
|
||
}else{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
if(is_array($tag))
|
||
{
|
||
foreach($tag as $v)
|
||
{
|
||
$v = trim($v);
|
||
if($v!='')
|
||
{
|
||
$this->db->insert($this->tbl_tag,array("id"=>$aid,"tag"=>$v));
|
||
}
|
||
}
|
||
}
|
||
|
||
return true;
|
||
|
||
}//MakeTags
|
||
|
||
//读取标签
|
||
function GetTags($aid,$limit=0){
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$sql = "SELECT * FROM ".$this->tbl_tag." WHERE id=$aid";
|
||
|
||
if(!empty($limit))
|
||
{
|
||
$sql .= " LIMIT $limit";
|
||
}
|
||
|
||
$sth = $this->db->query($sql);
|
||
$rows = $sth->fetchAll();
|
||
|
||
$keywords = array();
|
||
foreach($rows as $k=>$v)
|
||
{
|
||
$keywords[] = $v['tag'];
|
||
}
|
||
|
||
return $keywords;
|
||
}//GetTags
|
||
|
||
//删除标签
|
||
function DeleteTags($aid){
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$sql = "DELETE FROM ".$this->tbl_tag." WHERE id=$aid";
|
||
@$this->db->exec($sql);
|
||
return true;
|
||
}//DeleteTags
|
||
|
||
function getArchive($code='')
|
||
{
|
||
$sql="select a.* from ".$this->tbl_archives." a left join ".$this->tbl_catalog." ac on a.id=ac.aid left join ".$this->tbl_categorys." c on ac.cid=c.id ";
|
||
if (!empty($code)) $sql.=" where c.code='".$code."'";
|
||
$rs=$this->db->query($sql);
|
||
$rows=$rs->fetchAll();
|
||
return $rows;
|
||
}
|
||
|
||
function getOneArchive($title,$ptype='news')
|
||
{
|
||
$sql="update ".$this->tbl_archives." set click=click+1";
|
||
$this->db->query($sql);
|
||
$sql="select a.* from ".$this->tbl_archives." a left join ".$this->tbl_catalog." ac on a.id=ac.aid left join ".$this->tbl_categorys." c on ac.cid=c.id ";
|
||
$sql.=" where a.title=? and c.ptype=?";
|
||
$sth=$this->db->prepare($sql);
|
||
$sth->execute(array($title,$ptype));
|
||
$rows=$sth->fetch();
|
||
return $rows;
|
||
|
||
}
|
||
}
|