656 lines
13 KiB
PHP
656 lines
13 KiB
PHP
<?php
|
||
/**
|
||
* Archive - 统一文档类,实现各类文档的读取和IO操作,包括新闻、帮助、想法等。
|
||
*/
|
||
|
||
class Archive
|
||
{
|
||
private $db; //传入PDO对象
|
||
public $ptype; //文档分类
|
||
public $tbl_archives; //文档表
|
||
public $tbl_categorys; //栏目表
|
||
public $tbl_catalog; //文档栏目关联表
|
||
public $tbl_tag; //关键字表
|
||
private $DefaultFetchMode; //默认检索模式,防止出现sdtClass错误
|
||
|
||
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";
|
||
|
||
$this->DefaultFetchMode = "PDO::FETCH_BOTH";
|
||
}
|
||
|
||
|
||
/*
|
||
写入关系表
|
||
不能外部调用
|
||
*/
|
||
private 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()
|
||
|
||
//写入关系表,外部只能调用此接口
|
||
public function AddToCatalog($aid,$cid,$uuid='',$status=0)
|
||
{
|
||
if(!is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(!is_array($cid) && !is_numeric($cid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(is_array($cid) && count($cid)<1)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//多个cid
|
||
if(is_array($cid))
|
||
{
|
||
|
||
foreach($cid as $v)
|
||
{
|
||
//多个UUID
|
||
if(is_array($uuid))
|
||
{
|
||
foreach ($uuid as $vv)
|
||
{
|
||
$this->ToCatalog($aid,$v,$vv,$status);
|
||
}
|
||
}
|
||
|
||
//单个UUID
|
||
else{
|
||
$this->ToCatalog($aid,$v,$uuid,$status);
|
||
}
|
||
|
||
}
|
||
return true;
|
||
|
||
}
|
||
|
||
//单个cid
|
||
else{
|
||
|
||
//多个UUID
|
||
if(is_array($uuid))
|
||
{
|
||
foreach($uuid as $v)
|
||
{
|
||
$this->ToCatalog($aid,$cid,$v,$status);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//单个cid和uuid
|
||
else{
|
||
return $this->ToCatalog($aid,$cid,$uuid,$status);
|
||
}
|
||
}
|
||
}//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(is_array($typeid))
|
||
{
|
||
if(!in_array($v['cid'],$typeid))
|
||
{
|
||
//删除一个栏目
|
||
$this->DeleteCatalog($v['id']);
|
||
}
|
||
}else{
|
||
$this->DeleteCatalog($v['id']);
|
||
}
|
||
}
|
||
|
||
if(is_array($typeid))
|
||
{
|
||
foreach($typeid as $v)
|
||
{
|
||
if(!in_array($v,$types))
|
||
{
|
||
//添加一个栏目
|
||
$this->ToCatalog($aid,$v,$uuid,$status);
|
||
}
|
||
}
|
||
}else{
|
||
$this->ToCatalog($aid,$typeid,$uuid,$status);
|
||
}
|
||
return true;
|
||
}catch(Exception $e){
|
||
return $e->getMessage();
|
||
}
|
||
|
||
}//ChangeCatalog
|
||
|
||
//ChangeCatalogByUUID 根据UUID更改记录
|
||
function ChangeCatalogByUUID($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['uuid'];
|
||
if(is_array($uuid))
|
||
{
|
||
if(!in_array($v['uuid'],$uuid))
|
||
{
|
||
//删除一个栏目
|
||
$this->DeleteCatalog($v['id']);
|
||
}
|
||
}else{
|
||
$this->DeleteCatalog($v['id']);
|
||
}
|
||
}
|
||
|
||
if(is_array($uuid))
|
||
{
|
||
|
||
foreach($uuid as $v)
|
||
{
|
||
if(!in_array($v,$types))
|
||
{
|
||
//添加一个记录
|
||
$this->ToCatalog($aid,$typeid,$v,$status);
|
||
}
|
||
}
|
||
}else{
|
||
$this->ToCatalog($aid,$typeid,$uuid,$status);
|
||
}
|
||
return true;
|
||
}catch(Exception $e){
|
||
return $e->getMessage();
|
||
}
|
||
}//ChangeCatalogByUUID
|
||
|
||
//修改文档状态
|
||
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);
|
||
|
||
$config = \Zend_Registry::get('config');
|
||
$sql = "DELETE FROM ".$this->tbl_archives." WHERE id=$aid and sub='".$config->sub->news."'";
|
||
if ($this->db->exec($sql))
|
||
{
|
||
$sql = "DELETE FROM ".$this->tbl_catalog." WHERE aid=$aid";
|
||
@$this->db->exec($sql);
|
||
return true;
|
||
} else
|
||
return false;
|
||
}//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"=>$keywords)))
|
||
{
|
||
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
|
||
|
||
/*
|
||
* addArchive() 添加档案
|
||
*
|
||
* @param array $data
|
||
*
|
||
* @return int;
|
||
*/
|
||
function addArchive($data,$typeid,$keyword='',$uuid='')
|
||
{
|
||
|
||
$data = $this->scanField($data);
|
||
if($data == false)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
$config = \Zend_Registry::get('config');
|
||
|
||
//执行SQL语句后返回插入文章表的ID
|
||
$sql = "INSERT INTO ".$this->tbl_archives." (userid,title,description,image,source,ts_published,is_pub,body,sub)
|
||
VALUES (
|
||
".$data['userid'].",
|
||
".$data['title'].",
|
||
".$data['description'].",
|
||
".$data['image'].",
|
||
".$data['source'].",
|
||
".$data['ts_published'].",
|
||
".$data['is_pub'].",
|
||
".$data['body'].",
|
||
'".$config->sub->news."'
|
||
)
|
||
RETURNING id";
|
||
try{
|
||
$sth = $this->db->prepare($sql);
|
||
if($sth->execute())
|
||
{
|
||
$temp = $sth->fetch(PDO::FETCH_ASSOC);
|
||
$newAid = $temp['id'];
|
||
$this->AddToCatalog($newAid,$typeid,$uuid);
|
||
if(!empty($keyword))
|
||
{
|
||
$this->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='',$uuid='')
|
||
{
|
||
if(!is_numeric($aid))
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
$data = $this->scanField($data);
|
||
if($data == false)
|
||
{
|
||
return 0;
|
||
}
|
||
|
||
$config = \Zend_Registry::get('config');
|
||
|
||
$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 and sub='".$config->sub->news."'";
|
||
try{
|
||
if($this->db->exec($sql))
|
||
{
|
||
if(!empty($keyword))
|
||
{
|
||
$this->DeleteTags($aid);
|
||
$this->MakeTags($aid,$keyword);
|
||
}
|
||
if(!is_array($uuid))
|
||
{
|
||
$this->ChangeCatalog($aid,$typeid,$uuid);
|
||
}
|
||
else{
|
||
$this->ChangeCatalogByUUID($aid,$typeid,$uuid);
|
||
}
|
||
return $aid;
|
||
}else{
|
||
return 0;
|
||
}
|
||
}catch(Exception $e)
|
||
{
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
//扫描字段
|
||
function scanField($data){
|
||
|
||
if(!is_array($data))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//对特殊字段进行特别处理
|
||
$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;
|
||
}
|
||
|
||
/************* 读取 ****************/
|
||
|
||
|
||
/*
|
||
* makeArchiveUrl()
|
||
* 创建一个文档的链接
|
||
* 连接地址需要和路由规则对应
|
||
*
|
||
* @param array $parts
|
||
*
|
||
* @return string;
|
||
*/
|
||
public function makeArchiveUrl($parts)
|
||
{
|
||
$http_host = "http://".$_SERVER ['HTTP_HOST'];
|
||
$controller = "archives";
|
||
$url = $http_host."/".$controller."/".$parts['ptype']."/".$parts['type_code']."/archive-".$parts['archive_id'].".html";
|
||
//$url = $http_host."/".$controller."/".$parts['type_code']."/".$parts['archive_id'];
|
||
return $url;
|
||
}
|
||
|
||
public function makeCategoryUrl($parts)
|
||
{
|
||
$http_host = "http://".$_SERVER ['HTTP_HOST'];
|
||
$controller = "archives";
|
||
$url = $http_host."/".$controller."/".$parts['ptype']."/".$parts['type_code']."";
|
||
return $url;
|
||
}
|
||
|
||
|
||
/*
|
||
* getArchiveUrl()
|
||
* 通过文档ID和栏目ID获得某个文档的url
|
||
* 仅适用于单个栏目的文档且已经得知其栏目ID
|
||
* 如果是多个栏目的文档,则获取第一个栏目的文档连接
|
||
*
|
||
* @param int $aid
|
||
*
|
||
* @return string;
|
||
*/
|
||
function getArchiveUrlByCid($aid,$cid=0)
|
||
{
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$ecField = array($aid);
|
||
$wheresql = "";
|
||
|
||
if(!empty($cid) && is_numeric($cid))
|
||
{
|
||
$wheresql = " AND c.id=? ";
|
||
$ecField[] = $cid;
|
||
}
|
||
|
||
$sql = "SELECT arc.id,c.id as cid,c.title,c.code,c.ptype FROM ".$this->tbl_archives." arc
|
||
LEFT JOIN ".$this->tbl_catalog." ct ON arc.id=ct.aid
|
||
LEFT JOIN ".$this->tbl_categorys." c ON ct.cid = c.id
|
||
WHERE arc.id=? $wheresql
|
||
LIMIT 1";
|
||
$sth = $this->db->prepare($sql);
|
||
$sth->execute($ecField);
|
||
$row = $sth->fetch(PDO::FETCH_BOTH);
|
||
|
||
$url_parts = array(
|
||
"ptype"=>$row['ptype'],
|
||
"type_code"=>$row['code'],
|
||
"archive_id"=>$row['id']
|
||
);
|
||
|
||
return array(
|
||
'archive_url'=>$this->makeArchiveUrl($url_parts),
|
||
'type_title'=>$row['title'],
|
||
'type_id'=>$row['cid'],
|
||
'type_url'=>$this->makeCategoryUrl($url_parts),
|
||
);
|
||
|
||
}//getArchiveUrlByCid()
|
||
|
||
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;
|
||
}
|
||
|
||
//getOneArchive 获取单个文档
|
||
function getOneArchive($title,$ptype='news')
|
||
{
|
||
$config = \Zend_Registry::get('config');
|
||
$subnews=$config->sub->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=? and a.sub='$subnews'";
|
||
$sth=$this->db->prepare($sql);
|
||
$sth->execute(array($title,$ptype));
|
||
$rows=$sth->fetch();
|
||
return $rows;
|
||
}//getOneArchive
|
||
|
||
/*
|
||
* getArchiveField() 读取某个文档的某个字段
|
||
*
|
||
* @param int $aid
|
||
*
|
||
* @return string
|
||
*/
|
||
function getArchiveField($aid,$field)
|
||
{
|
||
if(!is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$sql = "SELECT $field as output_string FROM ".$this->tbl_archives." WHERE id=$aid";
|
||
$sth = $this->db->query($sql);
|
||
$row = $sth->fetch();
|
||
|
||
return $row['output_string'];
|
||
}//getArchiveField
|
||
|
||
/*
|
||
* getArchiveByUUID() 通过UUID获得数据新闻
|
||
*
|
||
* @param uuid $uuid
|
||
*
|
||
* @return array
|
||
*/
|
||
function getArchiveByUUID($uuid){
|
||
|
||
if(!preg_match("/^[0-9A-Za-z]{8}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{4}-[0-9A-Za-z]{12}$/",$uuid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$sql = "SELECT arc.* FROM archive arc
|
||
LEFT JOIN ar_catalog ct ON arc.id = ct.aid
|
||
WHERE ct.uuid='$uuid'";
|
||
$sth = $this->db->query($sql);
|
||
$rows = $sth->fetchAll(PDO::FETCH_BOTH);
|
||
|
||
return $rows;
|
||
|
||
}//getArchiveByUUID()
|
||
}
|