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

198 lines
4.6 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;
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'=>$request->getParam('reference'),
'link'=>$request->getParam('link'),
'publisher'=>$request->getParam('publisher'),
'year' => $request->getParam('year'),
'title' => $request->getParam('title'),
'bibtex' => $request->getParam('bibtex'),
'attid' => $request->getParam('attid')
);
return $data;
}
//上传文献PDF
public function uploadReferencePdf($file)
{
$files = new Files();
$file_info = $files->upload($file,'reference/',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' => 'reference',
'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;
return $file_data;
}
//删除文献文件
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()
{
$sql = "SELECT * FROM {$this->table->reference} ORDER BY id DESC";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
//获取专题数据的文献
public function fetchThemeReferences($code)
{
$sql="select distinct ref.* from mdref r left join reference ref on r.refid=ref.id
left join datasource ds on r.uuid=ds.uuid left join source s on s.id=ds.sourceid
where s.code='$code' and r.reftype=1
order by ref.year,ref.title";
$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='reference'";
$rs = $this->db->query($sql);
$rows = $rs->fetchAll();
return $rows;
}
}