westdc-zf1/application/models/news.php

266 lines
4.7 KiB
PHP
Raw Normal View History

2012-09-04 07:16:19 +00:00
<?php
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()
2012-09-04 07:16:19 +00:00
/*
递归获得所有栏目
*/
2012-11-23 09:54:37 +00:00
function getAllCategory($hide = true){
2012-09-04 07:16:19 +00:00
2012-11-23 09:54:37 +00:00
if($hide == -1)
{
$sql = "SELECT c.* FROM ".$this->tbl_categorys." c
ORDER BY displayorder asc";
}else{
$sql = "SELECT c.* FROM ".$this->tbl_categorys." c
WHERE is_pub = $hide
2012-09-04 07:16:19 +00:00
ORDER BY displayorder asc";
2012-11-23 09:54:37 +00:00
}
2012-09-04 07:16:19 +00:00
$re = $this->db->query($sql);
$categorys = $re->fetchAll();
return $categorys;
}
2012-11-23 09:54:37 +00:00
//取得最顶级的栏目
function getTopType(){
$sql = "SELECT c.* FROM ".$this->tbl_categorys." c
WHERE is_pub = true AND fid=0
ORDER BY displayorder asc";
$re = $this->db->query($sql);
$categorys = $re->fetchAll();
return $categorys;
}
//获得栏目
function getCategory($id)
{
$sql = "SELECT * FROM ".$this->tbl_categorys." WHERE id=?";
$sth = $this->db->prepare($sql);
$sth->execute(array($id));
$rows = $sth->fetch();
return $rows;
}
/*
写入关系表
*/
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;
}
if($this->db->update($this->tbl_catalog,$data,"id=$aid"))
{
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-09-04 07:16:19 +00:00
}