添加元数据浏览量统计和调用

This commit is contained in:
Li Jianxuan 2014-03-17 03:15:06 +00:00
parent f6df7c525b
commit 16b989c383
2 changed files with 234 additions and 225 deletions

View File

@ -58,6 +58,7 @@ class IndexController extends AbstractActionController
$tags = new \Westdc\Metadata\Tags;
$md = $metadata->fetch($uuid);
@$metadata->updateClickCount($uuid);
$this->ViewModel->setVariable('data',$md);
$this->ViewModel->setVariable('keys',$tags->getMetadataTags($md['id']));

View File

@ -1,226 +1,234 @@
<?php
namespace Westdc\Metadata;
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 Westdc\Metadata\Listener\OutlinkListener as Listener;
class Outlink
{
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 = "m.id";
}
$sql = "SELECT
m.*,md.viewed, ds.id as datasetid ,ol.url
FROM {$this->table->metadata} m
LEFT join {$this->table->metadata_stat} md on m.uuid=md.uuid
LEFT JOIN mdstatus st ON md.uuid=st.uuid
left join dataset ds on m.uuid=ds.uuid
LEFT JOIN mdoutlink ol ON m.uuid = ol.uuid
$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 fetchAllOutlink()
{
$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 = "ol.id";
}
$sql = "SELECT
ol.*
FROM {$this->table->metadata_outlink} ol
$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)
{
$sql = "SELECT m.*,ol.url FROM {$this->table->metadata} m
LEFT JOIN mdoutlink ol ON m.uuid = ol.uuid \r\n";
if(is_numeric($id))
{
$sql .= "WHERE m.id=$id";
}else if(\Sookon\Helpers\Uuid::test($id)){
$sql .= "WHERE m.uuid='$id'";
}else{
return false;
}
//view::Dump($sql);
$rs = $this->db->query($sql);
return $rs->fetch();
}
public function getParam()
{
$request = new Request;
$data = array(
'uuid' => $request->getPost('uuid'),
'url' => $request->getPost('url'),
'remark' => $request->getPost('remark'),
'status' => $request->getPost('status')
);
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->metadata_outlink,$data,true);
if(!empty($id) && is_numeric($id))
{
return true;
}else{
if($id === false)
{
return '服务器开小差了,请稍后再试';
}else{
return '服务器处理中遇到错误,请联系管理员';
}
}
}//add
else{
if(!$dbh->update($this->table->metadata_outlink,$data," id=$id ",true))
{
return "修改失败!请重试";
}else{
return true;
}
}//edit
}// add()
//通过uuid获得url
public function getOutlinkByUuid($uuid)
{
$sql = "SELECT * FROM {$this->table->metadata_outlink} WHERE uuid='$uuid'";
$rs = $this->db->query($sql);
return $rs->fetch();
}
<?php
namespace Westdc\Metadata;
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 Westdc\Metadata\Listener\OutlinkListener as Listener;
class Outlink
{
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 = "m.id";
}
$sql = "SELECT
m.*,md.viewed, ds.id as datasetid ,ol.url
FROM {$this->table->metadata} m
LEFT join {$this->table->metadata_stat} md on m.uuid=md.uuid
LEFT JOIN mdstatus st ON md.uuid=st.uuid
left join dataset ds on m.uuid=ds.uuid
LEFT JOIN mdoutlink ol ON m.uuid = ol.uuid
$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 fetchAllOutlink()
{
$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 = "ol.id";
}
$sql = "SELECT
ol.*
FROM {$this->table->metadata_outlink} ol
$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)
{
$sql = "SELECT m.*,ol.url,ms.viewed as viewedcount FROM {$this->table->metadata} m
LEFT JOIN mdoutlink ol ON m.uuid = ol.uuid \r\n
LEFT JOIN mdstat ms ON m.uuid=ms.uuid";
if(is_numeric($id))
{
$sql .= "WHERE m.id=$id";
}else if(\Sookon\Helpers\Uuid::test($id)){
$sql .= "WHERE m.uuid='$id'";
}else{
return false;
}
//view::Dump($sql);
$rs = $this->db->query($sql);
return $rs->fetch();
}
public function getParam()
{
$request = new Request;
$data = array(
'uuid' => $request->getPost('uuid'),
'url' => $request->getPost('url'),
'remark' => $request->getPost('remark'),
'status' => $request->getPost('status')
);
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->metadata_outlink,$data,true);
if(!empty($id) && is_numeric($id))
{
return true;
}else{
if($id === false)
{
return '服务器开小差了,请稍后再试';
}else{
return '服务器处理中遇到错误,请联系管理员';
}
}
}//add
else{
if(!$dbh->update($this->table->metadata_outlink,$data," id=$id ",true))
{
return "修改失败!请重试";
}else{
return true;
}
}//edit
}// add()
//通过uuid获得url
public function getOutlinkByUuid($uuid)
{
$sql = "SELECT * FROM {$this->table->metadata_outlink} WHERE uuid='$uuid'";
$rs = $this->db->query($sql);
return $rs->fetch();
}
//更新计数
public function updateClickCount($uuid)
{
$sql = "UPDATE {$this->table->metadata_stat} SET viewed=viewed+1 WHERE uuid='$uuid'";
@$this->db->exec($sql);
}
}