2012-11-30 15:11:41 +00:00
|
|
|
|
<?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)
|
|
|
|
|
{
|
2012-12-03 04:00:47 +00:00
|
|
|
|
$this->db = $db;
|
2012-11-30 15:11:41 +00:00
|
|
|
|
//文档的大分类
|
|
|
|
|
//在数组中设置文档分类
|
|
|
|
|
//"中文名称" => "数据库中标识"
|
|
|
|
|
$this->ptype = array(
|
|
|
|
|
"新闻" => "news",
|
|
|
|
|
"想法" => "idea",
|
|
|
|
|
"帮助" => "help",
|
|
|
|
|
"关于" => "about"
|
2012-12-03 04:00:47 +00:00
|
|
|
|
);
|
2012-11-30 15:11:41 +00:00
|
|
|
|
$this->tbl_archives = "archive";
|
|
|
|
|
$this->tbl_categorys = "ar_category";
|
|
|
|
|
$this->tbl_catalog = "ar_catalog";
|
|
|
|
|
$this->tbl_tag = "ar_tag";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
写入关系表
|
2012-12-03 04:00:47 +00:00
|
|
|
|
不能外部调用
|
2012-11-30 15:11:41 +00:00
|
|
|
|
*/
|
2012-12-03 04:00:47 +00:00
|
|
|
|
private function ToCatalog($aid,$cid,$uuid='',$status=0){
|
2012-11-30 15:11:41 +00:00
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
2012-12-03 04:00:47 +00:00
|
|
|
|
//写入关系表,外部只能调用此接口
|
|
|
|
|
public function AddToCatalog($aid,$cid,$uuid='',$status=0)
|
|
|
|
|
{
|
|
|
|
|
if(!is_numeric($aid))
|
2012-11-30 15:11:41 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-03 04:00:47 +00:00
|
|
|
|
if(!is_array($cid) && !is_numeric($cid))
|
2012-11-30 15:11:41 +00:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-03 04:00:47 +00:00
|
|
|
|
if(is_array($cid) && count($cid)<1)
|
2012-11-30 15:11:41 +00:00
|
|
|
|
{
|
2012-12-03 04:00:47 +00:00
|
|
|
|
return false;
|
2012-11-30 15:11:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-12-03 04:00:47 +00:00
|
|
|
|
if(is_array($cid))
|
2012-11-30 15:11:41 +00:00
|
|
|
|
{
|
2012-12-03 04:00:47 +00:00
|
|
|
|
foreach($cid as $v)
|
|
|
|
|
{
|
|
|
|
|
$this->ToCatalog($aid,$v,$uuid,$status);
|
|
|
|
|
}
|
2012-11-30 15:11:41 +00:00
|
|
|
|
return true;
|
|
|
|
|
}else{
|
2012-12-03 04:00:47 +00:00
|
|
|
|
return $this->ToCatalog($aid,$cid,$uuid,$status);
|
2012-11-30 15:11:41 +00:00
|
|
|
|
}
|
2012-12-03 04:00:47 +00:00
|
|
|
|
}//AddToCatalog
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//删除一个关系
|
|
|
|
|
function DeleteCatalog($id)
|
|
|
|
|
{
|
|
|
|
|
$sql = "DELETE FROM ".$this->tbl_catalog." WHERE id=$id";
|
|
|
|
|
@$this->db->exec($sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* 更改栏目 ChangeCatalog()
|
|
|
|
|
*/
|
|
|
|
|
function ChangeCatalog($aid,$typeid,$uuid='',$status=0){
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
$sql = "SELECT * FROM ar_catalog WHERE aid=$aid";
|
|
|
|
|
$sth = $this->db->query($sql);
|
|
|
|
|
$rows = $sth->fetchAll();
|
|
|
|
|
|
|
|
|
|
$types = array();
|
|
|
|
|
|
|
|
|
|
foreach($rows as $k=>$v)
|
|
|
|
|
{
|
|
|
|
|
$types[] = $v['cid'];
|
|
|
|
|
if(!in_array($v['cid'],$typeid))
|
|
|
|
|
{
|
|
|
|
|
//删除一个栏目
|
|
|
|
|
$this->DeleteCatalog($v['id']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach($typeid as $v)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(!in_array($v,$types))
|
|
|
|
|
{
|
|
|
|
|
//添加一个栏目
|
|
|
|
|
$this->ToCatalog($aid,$v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}catch(Exception $e){
|
|
|
|
|
return $e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-30 15:11:41 +00:00
|
|
|
|
}//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;
|
2012-12-03 04:00:47 +00:00
|
|
|
|
}//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();
|
2012-11-30 18:23:22 +00:00
|
|
|
|
return $rows;
|
2012-12-03 04:00:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2012-12-01 12:25:12 +00:00
|
|
|
|
return $rows;
|
2012-12-03 04:00:47 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* addArchive() 添加档案
|
|
|
|
|
*
|
|
|
|
|
* @param array $data
|
|
|
|
|
*
|
|
|
|
|
* @return int;
|
|
|
|
|
*/
|
|
|
|
|
function addArchive($data,$typeid,$keyword='')
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$data = $this->scanField($data);
|
|
|
|
|
if($data == false)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//执行SQL语句后返回插入文章表的ID
|
|
|
|
|
$sql = "INSERT INTO ".$this->tbl_archives." (userid,title,description,image,source,ts_published,is_pub,body)
|
|
|
|
|
VALUES (
|
|
|
|
|
".$data['userid'].",
|
|
|
|
|
".$data['title'].",
|
|
|
|
|
".$data['description'].",
|
|
|
|
|
".$data['image'].",
|
|
|
|
|
".$data['source'].",
|
|
|
|
|
".$data['ts_published'].",
|
|
|
|
|
".$data['is_pub'].",
|
|
|
|
|
".$data['body']."
|
|
|
|
|
)
|
|
|
|
|
RETURNING id";
|
|
|
|
|
try{
|
|
|
|
|
$sth = $this->db->prepare($sql);
|
|
|
|
|
if($sth->execute())
|
|
|
|
|
{
|
|
|
|
|
$temp = $sth->fetch(PDO::FETCH_ASSOC);
|
|
|
|
|
$newAid = $temp['id'];
|
|
|
|
|
$News->AddToCatalog($newAid,$typeid);
|
|
|
|
|
if(!empty($keyword))
|
|
|
|
|
{
|
|
|
|
|
$News->MakeTags($newAid,$keyword);
|
|
|
|
|
}
|
|
|
|
|
return $newAid;
|
|
|
|
|
}else{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}catch(Exception $e)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}//addArchive()
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* updateArchive() 更改文档
|
|
|
|
|
*
|
|
|
|
|
* @param array $data
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
function updateArchive($aid,$data,$typeid,$keyword='')
|
|
|
|
|
{
|
|
|
|
|
if(!is_numeric($aid))
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = $this->scanField($data);
|
|
|
|
|
if($data == false)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sql = "UPDATE ".$this->tbl_archives." SET
|
|
|
|
|
userid=".$data['userid'].",
|
|
|
|
|
title=".$data['title'].",
|
|
|
|
|
description=".$data['description'].",
|
|
|
|
|
image=".$data['image'].",
|
|
|
|
|
source=".$data['source'].",
|
|
|
|
|
ts_published=".$data['ts_published'].",
|
|
|
|
|
is_pub=".$data['is_pub'].",
|
|
|
|
|
body=".$data['body']."
|
|
|
|
|
WHERE id=$aid";
|
|
|
|
|
try{
|
|
|
|
|
if($this->db->exec($sql))
|
|
|
|
|
{
|
|
|
|
|
if(!empty($keyword))
|
|
|
|
|
{
|
|
|
|
|
$this->DeleteTags($aid);
|
|
|
|
|
$this->MakeTags($aid,$keyword);
|
|
|
|
|
}
|
|
|
|
|
$this->ChangeCatalog($aid,$typeid);
|
|
|
|
|
return $aid;
|
|
|
|
|
}else{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}catch(Exception $e)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//扫描字段
|
|
|
|
|
function scanField($data){
|
|
|
|
|
|
|
|
|
|
if(!is_array($data))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
2012-12-01 12:25:12 +00:00
|
|
|
|
}
|
2012-12-03 04:00:47 +00:00
|
|
|
|
|
|
|
|
|
//对特殊字段进行特别处理
|
|
|
|
|
$data['title'] = $this->replaceHtml($data['title']);
|
|
|
|
|
$data['description'] = $this->replaceHtml($data['description']);
|
|
|
|
|
|
|
|
|
|
//将参数过滤一遍,如果是字符型字段
|
|
|
|
|
//就执行db->quote()
|
|
|
|
|
foreach($data as $k=>$v)
|
|
|
|
|
{
|
|
|
|
|
if(!is_numeric($v) || !is_bool($v))
|
|
|
|
|
{
|
|
|
|
|
$data[$k] = $this->db->quote($v);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
|
}//scanField
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//过滤UTF-8字符的html代码
|
|
|
|
|
function replaceHtml($html)
|
|
|
|
|
{
|
|
|
|
|
$newString = htmlentities($html, ENT_QUOTES, "UTF-8");
|
|
|
|
|
return $newString;
|
|
|
|
|
}
|
2012-11-30 15:11:41 +00:00
|
|
|
|
}
|