295 lines
5.8 KiB
PHP
295 lines
5.8 KiB
PHP
<?php
|
||
namespace Archive\Operation;
|
||
|
||
use Helper\View as view;
|
||
use Helper\dbh;
|
||
use Helper\Cache;
|
||
|
||
//事件中存在的操作
|
||
class ArchiveOperate implements \Archive\Event\ArchiveEvent
|
||
{
|
||
private $db; //传入PDO对象
|
||
public $tbl_archives = "tbl_archive"; //文档表
|
||
public $tbl_categorys = "tbl_ar_category"; //栏目表
|
||
public $tbl_catalog = "tbl_ar_catalog"; //文档栏目关联表
|
||
public $tbl_tag = "tbl_ar_tag"; //关键字表
|
||
public $tbl_member = "tbl_member";
|
||
private $DefaultFetchMode = \PDO::FETCH_BOTH; //默认检索模式,防止出现sdtClass错误
|
||
private $config; //全局配置
|
||
|
||
function __construct($db = NULL)
|
||
{
|
||
if(empty($db))
|
||
{
|
||
$this->db = \Zend_Registry::get('db');
|
||
}else{
|
||
$this->db = $db;
|
||
}
|
||
|
||
$this->config = \Zend_Registry::get('config');
|
||
}
|
||
|
||
//扫描字段
|
||
public function scanField(\Zend_EventManager_Event $e){
|
||
|
||
$data = $e->getParam('data');
|
||
|
||
if(!is_array($data))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//对特殊字段进行特别处理
|
||
$data['title'] = $this->replaceHtml($data['title']);
|
||
$data['description'] = $this->replaceHtml($data['description']);
|
||
$data['keywords'] = $this->replaceHtml($data['keywords']);
|
||
|
||
if(!empty($data['pagename']))
|
||
{
|
||
if(!preg_match("/^[a-zA-Z][a-zA-Z0-9_]{4,25}$/",$data['pagename']))
|
||
{
|
||
return "自定义文件名只能由小写字母、数字、下划线组成,并且长度在5到25个字符之间";
|
||
}
|
||
}
|
||
|
||
return $data;
|
||
}//scanField
|
||
|
||
//过滤UTF-8字符的html代码
|
||
public function replaceHtml($html)
|
||
{
|
||
$newString = htmlentities($html, ENT_QUOTES, "UTF-8");
|
||
return $newString;
|
||
}
|
||
|
||
//添加新文档
|
||
public function archivePosted(\Zend_EventManager_Event $e)
|
||
{
|
||
$data = $e->getParam('cache_data');
|
||
$id = $e->getParam('id');
|
||
|
||
try{
|
||
if(!empty($data['typeid']))
|
||
{
|
||
$this->AddToCatalog($id,$data['typeid']);
|
||
}
|
||
if(!empty($data['keywords']))
|
||
{
|
||
$this->MakeTags($id,$data['keywords']);
|
||
}
|
||
|
||
$cache = new Cache(NULL,$this->db);
|
||
@$cache-> creatArchiveCache($id);
|
||
return $id;
|
||
}catch(\Exception $e)
|
||
{
|
||
return $e->getMessage();
|
||
}
|
||
}//添加新文档
|
||
|
||
//文档更新
|
||
public function archiveUpdated(\Zend_EventManager_Event $e){
|
||
$data = $e->getParam('cache_data');
|
||
$aid = $e->getParam('aid');
|
||
|
||
try{
|
||
if(!empty($data['keywords']))
|
||
{
|
||
$this->DeleteTags($aid);
|
||
$this->MakeTags($aid,$data['keywords']);
|
||
}
|
||
if(!empty($data['typeid']))
|
||
{
|
||
$this->ChangeCatalog($aid,$data['typeid']);
|
||
}
|
||
|
||
$cache = new Cache(NULL,$this->db);
|
||
@$cache->updateArchiveCache($aid);
|
||
return true;
|
||
}catch(\Exception $e)
|
||
{
|
||
return $e->getMessage();
|
||
}
|
||
}//文档更新
|
||
|
||
//写入关系表,外部只能调用此接口
|
||
public function AddToCatalog($aid,$cid,$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)
|
||
{
|
||
$this->ToCatalog($aid,$v,$status);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
//单个cid
|
||
else{
|
||
return $this->ToCatalog($aid,$cid,$status);
|
||
}
|
||
}//AddToCatalog
|
||
|
||
//写入关系表
|
||
private function ToCatalog($aid,$cid,$status=0){
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if(empty($cid))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
$data = array(
|
||
"aid" => $aid,
|
||
"cid" => $cid,
|
||
"status" => $status
|
||
);
|
||
|
||
if($this->db->insert($this->tbl_catalog,$data))
|
||
{
|
||
return true;
|
||
}else{
|
||
return false;
|
||
}
|
||
|
||
}//ToCatalog()
|
||
|
||
//写入标签关系
|
||
public 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("aid"=>$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("aid"=>$aid,"tag"=>$v));
|
||
}
|
||
}
|
||
}
|
||
|
||
return true;
|
||
|
||
}//MakeTags
|
||
|
||
//删除标签
|
||
public 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
|
||
|
||
//删除一个关系
|
||
public function DeleteCatalog($id)
|
||
{
|
||
$sql = "DELETE FROM ".$this->tbl_catalog." WHERE id=$id";
|
||
@$this->db->exec($sql);
|
||
}
|
||
|
||
|
||
/*
|
||
* 更改栏目 ChangeCatalog()
|
||
*/
|
||
public function ChangeCatalog($aid,$typeid,$status=0){
|
||
|
||
try{
|
||
$sql = "SELECT * FROM ".$this->tbl_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->AddToCatalog($aid,$v,$status);
|
||
}
|
||
}
|
||
}else{
|
||
$this->ToCatalog($aid,$typeid,$status);
|
||
}
|
||
return true;
|
||
}catch(Exception $e){
|
||
return $e->getMessage();
|
||
}
|
||
|
||
}//ChangeCatalog
|
||
|
||
//onArchiveDelete
|
||
public function onArchiveDelete(\Zend_EventManager_Event $e){
|
||
$aid = $e->getParam('aid');
|
||
|
||
if(empty($aid) || !is_numeric($aid))
|
||
{
|
||
return;
|
||
}
|
||
|
||
$this->DeleteTags($aid);
|
||
|
||
return;
|
||
}
|
||
|
||
} |