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

237 lines
5.6 KiB
PHP
Raw Normal View History

<?php
namespace Reference;
use \Helpers\View as view;
use \Helpers\dbh;
use \Reference\Listener\ReferenceListener;
use \Files\Files;
use \Search\Search;
class Reference
{
private $db; //传入PDO对象.
private $config; //站点设置
protected $events = NULL;
public $table;
public $keyword;
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')),
'attid' => (int)$request->getParam('attid')
);
return $data;
}
//上传文献PDF
public function uploadReferencePdf($file,$autoread = false)
{
$files = new Files();
2013-09-28 06:16:55 +00:00
$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'],
2013-09-28 06:16:55 +00:00
'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()
{
if(!empty($this->keyword))
{
$search = new Search();
$keyword = $search->splitKeyword($this->keyword);
}
$sql = "SELECT * FROM {$this->table->reference} ORDER BY id DESC";
$rs = $this->db->query($sql);
return $rs->fetchAll();
}
2013-09-28 03:21:40 +00:00
//获取专题数据的文献
public function fetchThemeReferences($code)
{
$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
2013-09-28 03:24:44 +00:00
where s.code='$code'
2013-09-28 03:21:40 +00:00
order by ref.year,ref.title";
$rs=$this->db->query($sql);
2013-09-28 03:22:48 +00:00
return $rs->fetchAll();
2013-09-28 03:21:40 +00:00
}
//单条文献的信息
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();
2013-09-28 02:17:33 +00:00
if ($row['attid'])
{
$files = new Files();
$attfile = $files->getOne($row['attid']);
2013-09-28 02:17:33 +00:00
$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
2013-09-28 06:16:55 +00:00
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";
return $this->db->exec($sql);
}else{
}
}
2013-09-28 02:17:33 +00:00
}