westdc-zf1/vendor/Sookon/Article/Catelog.php

261 lines
4.8 KiB
PHP

<?php
namespace Sookon\Article;
use Sookon\Helpers\View as view;
use Sookon\Helpers\Dbh as dbh;
use Sookon\Helpers\PDO;
use Sookon\Helpers\Table;
class Catelog
{
private $db; //传入PDO对象
private $config; //站点设置
private $table;
public $opt;
protected $events = NULL;
function __construct()
{
$this->db = new PDO;
$this->table = new Table;
$this->opt = new \stdClass();
$this->opt->sort = "DESC";
$this->opt->logic = "AND";
}
public function setEventManager(EventManagerInterface $events)
{
$events->setIdentifiers(array(
__CLASS__,
get_called_class(),
));
$this->events = $events;
return $this;
}
public function getEventManager()
{
if (NULL === $this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
/*
写入关系表
不能外部调用
*/
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;
}
$dbh = new dbh();
if($dbh->insert($this->table->arc_catelog,$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
//删除一个关系
public function DeleteCatalog($id)
{
$sql = "DELETE FROM ".$this->table->arc_catelog." WHERE id=$id";
@$this->db->exec($sql);
}
/*
* 更改栏目 ChangeCatalog()
*/
public function ChangeCatalog($aid,$typeid,$uuid='',$status=0){
try{
$sql = "SELECT * FROM {$this->table->arc_catelog} 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更改记录
public function ChangeCatalogByUUID($aid,$typeid,$uuid,$status=0)
{
try{
$sql = "SELECT * FROM {$this->table->arc_catelog} 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
//根据文章ID获得栏目
public function getCategoryByAid($aid)
{
$sql = "SELECT
cate.*
FROM {$this->table->arc_catelog} log
LEFT JOIN {$this->table->arc_category} cate ON log.cid=cate.id
WHERE aid=$aid";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
return $rows;
}
}