westdc-zf1/application/models/data/Author.php

147 lines
3.3 KiB
PHP

<?php
class Author extends Zend_Controller_Plugin_Abstract
{
public $db; //传入PDO对象.
private $auth = NULL; //Zend_Auth 对象
//使用到的公共变量
public $tbl_metadata = "metadata"; //元数据
public $tbl_author = "mdauthor"; //数据作者表
public $tbl_user = "users";
function __construct($db)
{
$this->db = $db;
}
//检查前台用户对某条数据的权限
function checkAuthor($uuid,$uid=0)
{
if(empty($uid))
{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$uid = $user->id;
}
}
if(!empty($uid))
{
$sql = "SELECT * FROM ".$this->tbl_author." WHERE uuid='$uuid' AND userid=$uid";
}else{
return false;
}
$rs = $this->db->query($sql);
$row = $rs->fetch(PDO::FETCH_BOTH);
if($row['status']>0)
{
return true;
}else{
return false;
}
}
//获取某数据的所有作者
function getAuthor($uuid,$field = "")
{
$sql = "SELECT md.title,u.email FROM ".$this->tbl_metadata." md
LEFT JOIN ".$this->tbl_author." a ON md.uuid=a.uuid
LEFT JOIN ".$this->tbl_user." u ON a.userid=u.id
WHERE md.uuid='$uuid'";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
if(empty($field))
{
return $rows;
}else{
$datas = array();
foreach($rows as $k=>$v)
{
$datas[] = $v[$field];
}
return $datas;
}
}
}
class Literature extends Author
{
public function __construct($db)
{
$this->db = $db;
}
function byuuid($uuid){
include_once("helper/view.php");
$uid = view::User('id');
$sql = "SELECT md.title,md.uuid,r.id,r.reference,r.link,mr.place,mr.id as mrid FROM mdref mr
LEFT JOIN metadata md ON md.uuid=mr.uuid
LEFT JOIN mdauthor a ON md.uuid=a.uuid
left join reference r on mr.refid=r.id
WHERE md.title IS NOT NULL AND a.userid=? and mr.uuid=? AND a.status=1
order by mr.place ASC,r.id DESC,md.ts_created desc
";
$sth = $this->db->prepare($sql);
$sth->execute(array($uid,$uuid));
$rows = $sth->fetchAll();
return $rows;
}
function edit($id,$content){
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
$data = array(
'reference' => $content
);
$s = $dbh->update("reference",$data," id=$id ",false);
return $s;
}
function order($id,$order){
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
$data = array(
'place' => $order
);
$s = $dbh->update("mdref",$data," id=$id ",false);
return $s;
}
function bydata($keywords=""){
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$uid = $user->id;
}else{
return false;
}
$sql = "SELECT md.title,md.uuid,count(mr.id) as c FROM metadata md
LEFT JOIN mdref mr ON md.uuid=mr.uuid
LEFT JOIN mdauthor a ON md.uuid=a.uuid
left join reference r on mr.refid=r.id
WHERE md.title IS NOT NULL AND a.userid=$uid AND a.status=1";
if(!empty($keywords))
{
include_once('SimpleSearch.php');
$search=new SimpleSearch($keywords);
$where=$search->sql_expr(array("md.title","md.description"));
$sql.=' and '.$where;
}
$sql.=" group by md.uuid,md.title";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
return $rows;
}
}