westdc-zf1/application/module/Reference/Reference.php

601 lines
14 KiB
PHP

<?php
namespace Reference;
use \Helpers\View as view;
use \Helpers\dbh;
use \Reference\Listener\ReferenceListener;
use \Files\Files;
class Reference
{
private $db; //传入PDO对象.
private $config; //站点设置
protected $events = NULL;
public $table;
public $keyword;
public $order;
public $sort = "DESC";
public $field;
public $reftype;
function __construct($db = NULL,$mail = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
$Listener = new ReferenceListener();
@$this->events()->attachAggregate($Listener);
$this->table = new \Helpers\Table();
}
public function events(\Zend_EventManager_EventCollection $events = NULL)
{
if ($events !== NULL) {
$this->events = $events;
} elseif ($this->events === NULL) {
$this->events = new \Zend_EventManager_EventManager(__CLASS__);
}
return $this->events;
}
public function reference($id = 0)
{
$data = $this->getReferenceParam();
$params = compact('data');
$results = $this->events()->trigger('submit.checkParam', $this, $params);
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->events()->trigger('submit.processData', $this, $params);
$data = $results->bottom();
unset($data['submit']);
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->table->reference,$data,true);
}else{
if(!$dbh->update($this->table->reference,$data," id=$id ",true))
{
return "修改失败!请重试";
}
}
if(!empty($id) && is_numeric($id))
{
return true;
}else{
return "修改失败";
}
}
//获得参数
public function getReferenceParam(\Zend_Controller_Request_Abstract $request = NULL)
{
$request = new \Zend_Controller_Request_Http();
$data = array(
'reference' => trim($request->getParam('reference')),
'link' => trim($request->getParam('link')),
'publisher' => trim($request->getParam('publisher')),
'year' => (int)$request->getParam('year'),
'title' => trim($request->getParam('title')),
'ris' => trim($request->getParam('ris')),
'note' => trim($request->getParam('note')),
'attid' => (int)$request->getParam('attid'),
'abstract' => $request->getParam('abstract'),
'type' => $request->getParam('type'),
'language' => $request->getParam('language'),
'doi' => $request->getParam('doi'),
);
return $data;
}
//上传文献PDF
public function uploadReferencePdf($file,$autoread = false)
{
$files = new Files();
$file_info = $files->upload($file,'literature/',true);
if(isset($file_info['error']) && !empty($file_info['error']))
{
return array("error" => $file_info['error']);
}
$file_data = array(
'filename' => $file_info['file_url'],
'filetype' => 'literature',
'filedesc' => $file_info['file_mime'],
'userid' => view::User('id'),
'filesize' => $file_info['file_size'],
'realname' => $file_info['realname']
);
$dbh = new dbh();
$file_id = $dbh->insert($this->table->attachments,$file_data,true);
$file_data['id'] = $file_id;
if($autoread)
{
$params = compact('file_data');
$results = $this->events()->trigger('upload.insertToReferenceTable', $this, $params);
$cache_data = $results->bottom();
$file_data = array_merge($file_data,$cache_data);
}
return $file_data;
}
//通过文件名自动提取文章标题
public function getReferenceTitleFromFilenName($filename)
{
$file = new Files();
$title = str_replace( ".".$file->getFileTextExt($filename),"",$filename);
return $title;
}
//删除文献文件
public function deleteReferenceAttchment($attid)
{
if(empty($attid) || !is_numeric($attid))
{
return array("error"=>"参数错误");
}
$files = new Files();
$status = $files->delete($attid);
if($status !== true)
{
return array("error"=>$status);
}else{
return array("success"=>1);
}
}
//所有文献
public function fetchReferences()
{
$wheresql = array();
if(!empty($this->keyword))
{
$wheresql[] = " ({$this->table->reference}.title LIKE '%{$this->keyword}%' OR {$this->table->reference}.reference LIKE '%{$this->keyword}%') ";
}
if(!empty($this->field))
{
foreach($this->field as $k=>$v)
{
if(!empty($v))
{
if(!is_numeric($v)) $v="'{$v}'";
$wheresql[] = " ({$this->table->reference}.{$k}={$v} ) ";
}else{
if(is_numeric($v))
$wheresql[] = " ({$this->table->reference}.{$k} IS NULL OR {$this->table->reference}.{$k}=0 ) ";
else
$wheresql[] = " ({$this->table->reference}.{$k} IS NULL ) ";
}//if(empty($v)
}//foreach
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "{$this->table->reference}.title";
}else{
$order = "{$this->table->reference}.{$this->order}";
}
$sql = "SELECT {$this->table->reference}.* FROM
{$this->table->reference}
$wheresql
ORDER BY $order {$this->sort}";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
//获取专题数据的文献
public function fetchThemeReferences($code)
{
$wheresql = array();
$wheresql[] = " s.code='$code' ";
if(!empty($this->keyword))
{
$wheresql[] = " (ref.title LIKE '%{$this->keyword}%' OR ref.reference LIKE '%{$this->keyword}%') ";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "ref.year,ref.title";
}else{
$order = "ref.{$this->order} {$this->sort}";
}
$sql="select distinct ref.* from mdref r left join {$this->table->reference} ref on r.refid=ref.id
left join datasource ds on r.uuid=ds.uuid left join {$this->table->source} s on s.id=ds.sourceid
$wheresql
ORDER BY $order";
$rs=$this->db->query($sql);
return $rs->fetchAll();
}
//Get WestDC references
public function fetchWestdcReferences()
{
$wheresql = array();
$wheresql[]=" r.uuid='e31f5ea7-a4af-4ae3-9ac1-1a84132c4338' ";
if(!empty($this->keyword))
{
$wheresql[] = " (ref.title LIKE '%{$this->keyword}%' OR ref.reference LIKE '%{$this->keyword}%') ";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "ref.year,ref.title";
}else{
$order = "ref.{$this->order} {$this->sort}";
}
$sql="select distinct ref.* from mdref r left join {$this->table->reference} ref on r.refid=ref.id
$wheresql
ORDER BY $order";
$rs=$this->db->query($sql);
return $rs->fetchAll();
}
//Get references which need to deal with
public function fetchTodoReferences()
{
$wheresql = array();
$wheresql[]=" ref.id not in (select distinct refid from mdref) ";
if(!empty($this->keyword))
{
$wheresql[] = " (ref.title LIKE '%{$this->keyword}%' OR ref.reference LIKE '%{$this->keyword}%') ";
}
if(count($wheresql)>0)
{
$wheresql = " WHERE ".join(" AND ",$wheresql);
}else{
$wheresql = "";
}
if(empty($this->order))
{
$order = "ref.year,ref.title";
}else{
$order = "ref.{$this->order} {$this->sort}";
}
$sql="select distinct ref.* from {$this->table->reference} ref
$wheresql
ORDER BY $order";
$rs=$this->db->query($sql);
return $rs->fetchAll();
}
//单条文献的信息
public function getOneReferenceData($id)
{
if(empty($id) || !is_numeric($id))
{
return false;
}
$sql = "SELECT * FROM {$this->table->reference} WHERE id=$id LIMIT 1";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if ($row['attid'])
{
$files = new Files();
$attfile = $files->getOne($row['attid']);
$row['file'] = $attfile;
}
return $row;
}
//获得reference类型的附件
public function getReferenceFiles()
{
$sql = "SELECT att.*,ref.attid,ref.id as refid FROM {$this->table->attachments} att
LEFT JOIN {$this->table->reference} ref ON att.id=ref.attid
WHERE att.filetype='literature'";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
//删除文献
public function deleteReference($id,$delete_att = false)
{
if(empty($id) || !is_numeric($id))
{
return false;
}
if($delete_att == false)
{
$sql = "DELETE FROM {$this->table->reference} WHERE id=$id";
@$this->db->exec($sql);
$sql = "DELETE FROM {$this->table->metadata_reference} WHERE refid=$id";
@$this->db->exec($sql);
$this->deleteReferenceAuthor($id);
$this->deleteReferenceTag($id);
return true;
}else{
}
}
//删除作者信息
public function deleteReferenceAuthor($id)
{
if(empty($id) || !is_numeric($id))
{
return false;
}
$sql = "DELETE FROM {$this->table->reference_author} WHERE id=$id ";
return $this->db->exec($sql);
}
//删除标签信息
public function deleteReferenceTag($id)
{
if(empty($id) || !is_numeric($id))
{
return false;
}
$sql = "DELETE FROM {$this->table->reference_tag} WHERE id=$id ";
return $this->db->exec($sql);
}
//建立文献与数据的关系
public function createRelationFromReferenceToData($refid,$uuid,$reftype,$place,$id = NULL)
{
if(empty($refid) || !is_numeric($refid))
{
return "参数错误";
}
if(!view::isUuid($uuid))
{
return "参数错误";
}
$data = array(
'uuid'=>$uuid,
'refid'=>$refid,
'reftype'=>$reftype,
'place'=>$place
);
$dbh = new dbh();
if(empty($id))
{
$id = $dbh->insert($this->table->metadata_reference,$data,true);
if(is_numeric($id))
{
return $id;
}else{
return "关系写入失败,请检查是否已经存在";
}
}else{
$status = $dbh->update($this->table->metadata_reference,$data," id=$id ");
if($status === true)
{
return $id;
}else{
return "修改失败";
}
}
}
//获得某个文献关联的数据 (根据文献获得数据)
public function getDataByReference($id)
{
if(empty($id) || !is_numeric($id))
{
return "参数错误";
}
$sql = "SELECT mr.id,mr.refid,mr.reftype,mr.place,md.title,md.uuid FROM {$this->table->metadata_reference} mr
LEFT JOIN {$this->table->metadata} md ON mr.uuid=md.uuid
WHERE mr.refid=$id
ORDER BY mr.place ASC";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
//获得某个数据关联的文献 (根据数据获得文献)
public function getReferenceByData($uuid)
{
if(!view::isUuid($uuid))
{
return "参数错误";
}
$sql = "SELECT mr.reftype,mr.place,md.title,md.uuid FROM {$this->table->metadata_reference} mr
LEFT JOIN {$this->table->metadata} md
WHERE mr.uuid = $uuid";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
//文献类型
public function referenceType()
{
return array(
0 => '相关文献',//作者建议的文献或数据中心建议的文献
1 => '施引文献',
2 => '参考文献',
3 => '多篇文献',
4 => '专题文献'
);
}
//数据文献参数
public function getMdrefParam(\Zend_Controller_Request_Abstract $request = NULL)
{
$request = new \Zend_Controller_Request_Http();
$data = array(
'uuid' => trim($request->getParam('uuid')),
'refid' => (int)$request->getParam('refid'),
'place' => (int)$request->getParam('place'),
'reftype' => (int)$request->getParam('reftype'),
);
return $data;
}
//写入数据文献
public function makeMdref($id = NULL)
{
$data = $this->getMdrefParam();
$results = $this->events()->trigger('mdref.checkParam', $this, compact('data'));
$cache_data = $results->bottom();
if($cache_data !== true)
{
return $cache_data;
}
$results = $this->events()->trigger('mdref.processData', $this, compact('data'));
$data = $results->bottom();
$id = $this->createRelationFromReferenceToData($data['refid'],$data['uuid'],$data['reftype'],$data['place'],$id);
if(is_numeric($id))
{
return true;
}else{
return $id;
}
}
//删除数据文献
public function delMdref($id)
{
if(empty($id) || !is_numeric($id))
{
return "参数错误";
}
$sql = "DELETE FROM {$this->table->metadata_reference} WHERE id=$id";
if($this->db->exec($sql))
{
return true;
}else{
return "删除失败";
}
}
//按年份获得文献数量
public function countByYear()
{
$sql = "SELECT count(id) as num,year FROM {$this->table->reference} GROUP BY year ORDER BY year DESC";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
//获得作者
public function getAuthorByReference($id,$join = false)
{
if(is_numeric($id))
{
$sql = "SELECT * FROM {$this->table->reference_author} WHERE id=$id ORDER BY place ASC";
$rs = $this->db->query($sql);
if(!$join)
{
return $rs->fetchAll();
}else{
foreach($rows = $rs->fetchAll() as $k=>$v)
{
$rows[$k] = (string)$v['firstname'].$v['lastname'];
}
return $rows;
}
}
if(is_array($id))
{
$sql = "SELECT * FROM {$this->table->reference_author} WHERE id IN (".join(",",$id).")";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
return;
}
//获得标签
public function getTagsByReference($id,$single = false)
{
if(is_numeric($id))
{
$sql = "SELECT * FROM {$this->table->reference_tag} WHERE id=$id";
$rs = $this->db->query($sql);
if(!$single)
{
return $rs->fetchAll();
}else{
foreach($rows = $rs->fetchAll() as $k=>$v)
{
$rows[$k] = (string)$v['tag'];
}
return $rows;
}
}
return;
}
}