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

183 lines
4.2 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\Config;
use Sookon\Helpers\Table;
use Zend\Http\PhpEnvironment\Request;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
use Sookon\Article\Listener\ArticleListener as Listener;
class Article
{
private $db; //传入PDO对象
private $config; //站点设置
private $table;
public $opt;
protected $events = NULL;
function __construct()
{
$this->db = new PDO;
$this->config = Config::get();
$this->table = new Table;
$this->opt = new \stdClass();
$this->opt->sort = "DESC";
$this->opt->logic = "AND";
$Listener = new Listener();
$this->getEventManager()->attachAggregate($Listener);
}
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;
}
public function fetchAll()
{
$wheresql = array();
if(isset($this->opt->where) && !empty($this->opt->where))
$wheresql = array_merge($wheresql,$this->opt->where);
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join($wheresql," ".$this->opt->logic." ");
}else{
$wheresql = '';
}
if(!empty($this->opt->order))
{
$order = $this->opt->order;
}else{
$order = "ar.id";
}
$sql = "SELECT
ar.* ,mb.username
FROM {$this->table->arc_article} ar
LEFT JOIN {$this->table->member} mb ON ar.userid = mb.id
$wheresql
ORDER BY $order {$this->opt->sort} ";
if(!empty($this->opt->start))
{
$sql .= " START {$this->opt->start} ";
}
if(!empty($this->opt->limit)){
$sql .= " LIMIT {$this->opt->limit} ";
}
//view::Dump($sql);
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
public function fetch($id)
{
if(is_numeric($id))
{
$sql = "SELECT * FROM {$this->table->arc_article} WHERE id=$id";
}else{
if(preg_match("/\'/",$id))
{
str_replace("'","''",$id);
}
$sql = "SELECT * FROM {$this->table->arc_article} WHERE title='$id'";
}
$rs = $this->db->query($sql);
return $rs->fetch();
}
public function getParam()
{
$request = new Request;
$data = array(
'uuid' => $request->getPost('uuid'),
'title' => $request->getPost('title'),
'source' => $request->getPost('source'),
'image' => $request->getPost('image'),
'body' => $request->getPost('content'),
'userid' => (int)view::User('id'),
'tags' => $request->getPost('tags'),
'ts_published' => $request->getPost('ts_published'),
'category' => $request->getPost('category'),
'is_pub' => $request->getPost('pub')
);
return $data;
}
//添加
public function add($data,$id = 0)
{
$params = compact('data');
$results = $this->getEventManager()->trigger('submit.checkParam', $this, $params);
$cache_data = $results->last();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->getEventManager()->trigger('submit.processData', $this, $params);
$data = $results->last();
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->table->arc_article,$data,true);
if(!empty($id) && is_numeric($id))
{
$this->getEventManager()->trigger('submit.recordPosted', $this, compact('params','data','id'));
return true;
}else{
if($id === false)
{
return '服务器开小差了,请稍后再试';
}else{
return '服务器处理中遇到错误,请联系管理员';
}
}
}//add
else{
if(!$dbh->update($this->table->arc_article,$data," id=$id ",true))
{
return "修改失败!请重试";
}else{
$this->getEventManager()->trigger('submit.recordChanged', $this, compact('params','data','id'));
return true;
}
}//edit
}// add()
}