2012-09-04 07:16:19 +00:00
|
|
|
|
<?php
|
2012-11-28 09:27:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Archives - Category
|
|
|
|
|
*
|
|
|
|
|
* @link NULL
|
|
|
|
|
* @copyright Copyright (c) 2012 Jianxuan Li
|
|
|
|
|
* @license http://www.lijianxuan.com/license
|
|
|
|
|
* @package Archives
|
|
|
|
|
*/
|
|
|
|
|
|
2012-09-04 07:16:19 +00:00
|
|
|
|
class news
|
|
|
|
|
{
|
2012-11-23 09:54:37 +00:00
|
|
|
|
private $db; //传入PDO对象
|
|
|
|
|
public $ptype; //文档分类
|
|
|
|
|
public $tbl_archives; //文档表
|
|
|
|
|
public $tbl_categorys; //栏目表
|
|
|
|
|
public $tbl_catalog; //文档栏目关联表
|
|
|
|
|
public $tbl_tag; //关键字表
|
2012-09-04 07:16:19 +00:00
|
|
|
|
|
|
|
|
|
function __construct($db)
|
|
|
|
|
{
|
|
|
|
|
$this->db = $db;
|
2012-11-23 09:54:37 +00:00
|
|
|
|
$this->ptype();
|
|
|
|
|
$this->tbl_archives = "archive";
|
|
|
|
|
$this->tbl_categorys = "ar_category";
|
|
|
|
|
$this->tbl_catalog = "ar_catalog";
|
|
|
|
|
$this->tbl_tag = "ar_tag";
|
2012-09-04 07:16:19 +00:00
|
|
|
|
}
|
2012-11-23 09:54:37 +00:00
|
|
|
|
|
|
|
|
|
//文档分类
|
|
|
|
|
//在数组中设置档案分类
|
|
|
|
|
// "中文名称" => "数据库中标识"
|
|
|
|
|
function ptype (){
|
|
|
|
|
$this->ptype = array(
|
|
|
|
|
"新闻" => "news",
|
|
|
|
|
"数据新闻" => "datanews",
|
|
|
|
|
"说明文档" => "help",
|
|
|
|
|
"FAQ" => "faq"
|
|
|
|
|
);
|
|
|
|
|
}//ptype()
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
写入关系表
|
|
|
|
|
*/
|
|
|
|
|
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()
|
|
|
|
|
|
2012-11-27 03:57:27 +00:00
|
|
|
|
/*
|
|
|
|
|
* 更改栏目 ChangeCatalog()
|
|
|
|
|
*/
|
|
|
|
|
function ChangeCatalog($aid,$cid,$uuid='',$status=0){
|
2012-11-23 09:54:37 +00:00
|
|
|
|
|
2012-11-27 03:57:27 +00:00
|
|
|
|
if(empty($aid) || !is_numeric($aid))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-11-23 09:54:37 +00:00
|
|
|
|
|
2012-11-27 03:57:27 +00:00
|
|
|
|
if(empty($cid) && empty($uuid))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
|
"cid" => $cid,
|
|
|
|
|
"status" => $status
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if(!empty($uuid))
|
|
|
|
|
{
|
|
|
|
|
$data['uuid'] = $uuid;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-28 09:27:53 +00:00
|
|
|
|
if($this->db->update($this->tbl_catalog,$data,"aid=$aid"))
|
2012-11-27 03:57:27 +00:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}else{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//ChangeCatalog
|
2012-11-23 09:54:37 +00:00
|
|
|
|
|
|
|
|
|
//修改文档状态
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-27 03:57:27 +00:00
|
|
|
|
$this->DeleteTags($aid);
|
|
|
|
|
|
2012-11-23 09:54:37 +00:00
|
|
|
|
$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);
|
2012-11-27 03:57:27 +00:00
|
|
|
|
|
2012-11-23 09:54:37 +00:00
|
|
|
|
return true;
|
|
|
|
|
}//DeleteArchives
|
2012-11-27 03:57:27 +00:00
|
|
|
|
|
|
|
|
|
//写入标签关系
|
|
|
|
|
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-11-28 09:27:53 +00:00
|
|
|
|
}//DeleteTags
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Category
|
|
|
|
|
{
|
|
|
|
|
//PDO对象
|
|
|
|
|
private $db;
|
|
|
|
|
|
|
|
|
|
//数据库中分类表
|
|
|
|
|
public $CategoryTable;
|
|
|
|
|
|
|
|
|
|
//分类左值字段名称
|
|
|
|
|
private $fld_left;
|
|
|
|
|
|
|
|
|
|
//分类右值字段名称
|
|
|
|
|
private $fld_right;
|
|
|
|
|
|
|
|
|
|
//顶级栏目标记字段
|
|
|
|
|
private $fld_tid;
|
|
|
|
|
|
|
|
|
|
//输出栏目中的深度信息
|
|
|
|
|
public $DeepTitle;
|
|
|
|
|
|
|
|
|
|
function __construct($db){
|
|
|
|
|
$this->db = $db;
|
|
|
|
|
$this->CategoryTable = "ar_category";
|
|
|
|
|
$this->fld_left = "lft";
|
|
|
|
|
$this->fld_right = "rgt";
|
|
|
|
|
$this->fld_tid = "tid";
|
|
|
|
|
$this->DeepTitle = "DEEP";
|
2012-11-27 03:57:27 +00:00
|
|
|
|
}
|
2012-11-28 09:27:53 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* GetTree() 获得栏目树
|
|
|
|
|
* 用来获得某个栏目下或者所有栏目的树结构
|
|
|
|
|
*
|
|
|
|
|
* 典型表结构
|
|
|
|
|
* (PostgreSql)
|
|
|
|
|
*
|
|
|
|
|
CREATE TABLE ar_category
|
|
|
|
|
(
|
|
|
|
|
id serial NOT NULL,
|
|
|
|
|
title character varying(80),
|
|
|
|
|
tid integer NOT NULL DEFAULT 0,
|
|
|
|
|
lft integer NOT NULL DEFAULT 0,
|
|
|
|
|
rgt integer NOT NULL DEFAULT 0,
|
|
|
|
|
CONSTRAINT ar_category_pkey PRIMARY KEY (id)
|
|
|
|
|
)
|
|
|
|
|
*
|
|
|
|
|
* @param int $tid
|
|
|
|
|
*
|
|
|
|
|
* @return array;
|
|
|
|
|
*/
|
|
|
|
|
function GetCategory($tid=0){
|
|
|
|
|
|
|
|
|
|
$left = $this->fld_left;
|
|
|
|
|
$right = $this->fld_right;
|
|
|
|
|
$categoryTable = $this->CategoryTable;
|
|
|
|
|
$tidField = $this->fld_tid;
|
|
|
|
|
|
|
|
|
|
$whereSql = array();
|
|
|
|
|
|
|
|
|
|
if(!empty($tid))
|
|
|
|
|
{
|
|
|
|
|
$sql = "SELECT $left,$right FROM $categoryTable
|
|
|
|
|
WHERE id=$tid";
|
|
|
|
|
$sth = $this->db->query($sql);
|
|
|
|
|
$row = $sth->fetch();
|
|
|
|
|
$whereSql[] = " $left BETWEEN {$row[$left]} AND {$row[$right]} ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(count($whereSql)>0)
|
|
|
|
|
{
|
|
|
|
|
$whereSql = " WHERE ".join(" AND ",$whereSql);
|
|
|
|
|
}else{
|
|
|
|
|
$whereSql = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT *
|
|
|
|
|
FROM $categoryTable
|
|
|
|
|
$whereSql
|
|
|
|
|
ORDER BY $left ASC ";
|
|
|
|
|
$sth = $this->db->query($sql);
|
|
|
|
|
$rows = $sth->fetchAll();
|
|
|
|
|
|
|
|
|
|
$stack = array();
|
|
|
|
|
$categorys = array();
|
|
|
|
|
|
|
|
|
|
foreach($rows as $k=>$v)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
如果它是一个顶级栏目,则为它重置深度
|
|
|
|
|
*/
|
|
|
|
|
if(empty($v[$tidField]))
|
|
|
|
|
{
|
|
|
|
|
$stack = array();
|
|
|
|
|
}else{
|
|
|
|
|
if(count($stack) > 1)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
在这个循环中必须追溯直至与其平级的分类
|
|
|
|
|
while循环无法重写条件中已经改变的变量
|
|
|
|
|
第一次max(array_keys($stack))赋值为当前深度
|
|
|
|
|
循环中的第一个if里max(array_keys($stack))是重新统计,而非使用一定义的
|
|
|
|
|
因为第二次循环到此时可能max(array_keys($stack))已经发生改变
|
|
|
|
|
第三处max(array_keys($stack))表示第一个if中没有break掉的深度值,所以也不能使用赋过值的变量代替。
|
|
|
|
|
|
|
|
|
|
加入brake 可使程序追溯至平级栏目即进行下一步操作
|
|
|
|
|
必须是倒序循环,多个平级栏目时,如果是正序列循环会到第一个平级的栏目后停止循环
|
|
|
|
|
*/
|
|
|
|
|
for($i=max(array_keys($stack));$i>=0;$i--)
|
|
|
|
|
if($v[$right]>$stack[max(array_keys($stack))])
|
|
|
|
|
{
|
|
|
|
|
if(count($stack)<1)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if($v[$right]>$stack[max(array_keys($stack))])
|
|
|
|
|
{
|
|
|
|
|
array_pop($stack);
|
|
|
|
|
}else{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}//end if
|
|
|
|
|
}//end if
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//放入输出数组
|
|
|
|
|
$rows[$k][$this->DeepTitle] = count($stack);
|
|
|
|
|
|
|
|
|
|
//将此节点加入栈中
|
|
|
|
|
$stack[] = $v[$right];
|
|
|
|
|
|
|
|
|
|
}// end foreach
|
|
|
|
|
|
|
|
|
|
return $rows;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert() 插入一个栏目
|
|
|
|
|
* 返回数组 left=>左值 right=>右值
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @param int $tid
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
function Insert($tid){
|
|
|
|
|
|
|
|
|
|
$left = $this->fld_left;
|
|
|
|
|
$right = $this->fld_right;
|
|
|
|
|
$categoryTable = $this->CategoryTable;
|
|
|
|
|
|
|
|
|
|
//分别处理有上级栏目和没上级栏目的
|
|
|
|
|
if(!empty($tid))
|
|
|
|
|
{
|
|
|
|
|
$sql = "SELECT $right
|
|
|
|
|
FROM $categoryTable
|
|
|
|
|
WHERE id=$tid";
|
|
|
|
|
$sth = $this->db->query($sql);
|
|
|
|
|
$row = $sth->fetch();
|
|
|
|
|
$right_start = $row[$right]-1;
|
|
|
|
|
|
|
|
|
|
//更新所有右值
|
|
|
|
|
$sql = "UPDATE $categoryTable SET $right=$right+2 WHERE $right>$right_start";
|
|
|
|
|
$this->db->exec($sql);
|
|
|
|
|
|
|
|
|
|
//更新所有左值
|
|
|
|
|
$sql = "UPDATE $categoryTable SET $left=$left+2 WHERE $left>$right_start";
|
|
|
|
|
$this->db->exec($sql);
|
|
|
|
|
|
|
|
|
|
//返回应该插入的左右值
|
|
|
|
|
$lft = $right_start + 1;
|
|
|
|
|
$rgt = $lft + 1;
|
|
|
|
|
return array(
|
|
|
|
|
'left'=>$lft,
|
|
|
|
|
'right'=>$rgt
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
//取最大的右值
|
|
|
|
|
$sql = "SELECT $right
|
|
|
|
|
FROM $categoryTable
|
|
|
|
|
WHERE tid=0
|
|
|
|
|
ORDER BY $left DESC LIMIT 1";
|
|
|
|
|
|
|
|
|
|
$sth = $this->db->query($sql);
|
|
|
|
|
$row = $sth->fetch();
|
|
|
|
|
$right_start = $row[$right]-1;
|
|
|
|
|
|
|
|
|
|
if($right_start<1)
|
|
|
|
|
{
|
|
|
|
|
return array(
|
|
|
|
|
'left'=>1,
|
|
|
|
|
'right'=>2
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$lft = $right_start + 2;
|
|
|
|
|
$rgt = $lft + 1;
|
|
|
|
|
return array(
|
|
|
|
|
'left'=>$lft,
|
|
|
|
|
'right'=>$rgt
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}//Insert
|
|
|
|
|
|
|
|
|
|
//获取栏目路径
|
|
|
|
|
function GetRouter($lft,$rgt){
|
|
|
|
|
$sql = "SELECT * FROM ".$this->CategoryTable."
|
|
|
|
|
WHERE ".$this->fld_left." < $lft AND ".$this->fld_right." > $rgt
|
|
|
|
|
ORDER BY ".$this->fld_left." ASC;";
|
|
|
|
|
$sth = $this->db->query($sql);
|
|
|
|
|
$rows = $sth->fetchAll();
|
|
|
|
|
return $rows;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function GetSunCount($left,$right){
|
|
|
|
|
return ($right-$left-1)/2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取一个栏目
|
|
|
|
|
function GetOne($tid){
|
|
|
|
|
|
|
|
|
|
$sql = "SELECT * FROM ".$this->CategoryTable." WHERE id=$tid";
|
|
|
|
|
$sth = $this->db->query($sql);
|
|
|
|
|
return $sth->fetch();
|
|
|
|
|
|
|
|
|
|
}//GetOne
|
|
|
|
|
|
2012-09-04 07:16:19 +00:00
|
|
|
|
}
|