2013-09-26 08:26:29 +00:00
< ? 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 ;
2013-09-29 10:06:49 +00:00
public $keyword ;
2013-09-30 03:09:21 +00:00
public $order ;
public $sort = " DESC " ;
2013-11-04 07:08:58 +00:00
public $field ;
2013-10-09 03:22:27 +00:00
public $reftype ;
2013-09-26 08:26:29 +00:00
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 (
2013-09-29 10:06:49 +00:00
'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' )),
2013-09-30 02:16:30 +00:00
'note' => trim ( $request -> getParam ( 'note' )),
2013-10-15 06:30:51 +00:00
'attid' => ( int ) $request -> getParam ( 'attid' ),
'abstract' => $request -> getParam ( 'abstract' ),
'type' => $request -> getParam ( 'type' ),
'language' => $request -> getParam ( 'language' ),
'doi' => $request -> getParam ( 'doi' ),
2013-09-26 08:26:29 +00:00
);
return $data ;
}
//上传文献PDF
2013-09-29 10:06:49 +00:00
public function uploadReferencePdf ( $file , $autoread = false )
2013-09-26 08:26:29 +00:00
{
$files = new Files ();
2013-09-28 06:16:55 +00:00
$file_info = $files -> upload ( $file , 'literature/' , true );
2013-09-26 08:26:29 +00:00
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' ,
2013-09-26 08:26:29 +00:00
'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 ;
2013-09-29 10:06:49 +00:00
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 );
}
2013-09-26 08:26:29 +00:00
return $file_data ;
}
2013-09-29 10:06:49 +00:00
//通过文件名自动提取文章标题
public function getReferenceTitleFromFilenName ( $filename )
{
$file = new Files ();
$title = str_replace ( " . " . $file -> getFileTextExt ( $filename ), " " , $filename );
return $title ;
}
2013-09-26 08:26:29 +00:00
//删除文献文件
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 ()
{
2013-09-30 03:09:21 +00:00
$wheresql = array ();
2013-09-29 10:06:49 +00:00
if ( ! empty ( $this -> keyword ))
{
2013-09-30 03:09:21 +00:00
$wheresql [] = " ( { $this -> table -> reference } .title LIKE '% { $this -> keyword } %' OR { $this -> table -> reference } .reference LIKE '% { $this -> keyword } %') " ;
2013-09-29 10:06:49 +00:00
}
2013-11-04 07:08:58 +00:00
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
}
2013-09-30 03:09:21 +00:00
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 } " ;
2013-09-26 08:26:29 +00:00
$rs = $this -> db -> query ( $sql );
return $rs -> fetchAll ();
}
2013-09-28 03:21:40 +00:00
//获取专题数据的文献
public function fetchThemeReferences ( $code )
{
2013-09-30 03:09:21 +00:00
$wheresql = array ();
2014-05-15 16:02:23 +00:00
//$wheresql[] = " s.code='$code' ";
2013-09-30 03:09:21 +00:00
if ( ! empty ( $this -> keyword ))
{
2014-05-15 16:02:23 +00:00
$wheresql [] = " (ref.title iLIKE '% { $this -> keyword } %' OR ref.reference iLIKE '% { $this -> keyword } %') " ;
2013-09-30 03:09:21 +00:00
}
if ( count ( $wheresql ) > 0 )
{
2014-05-15 16:02:23 +00:00
$wheresql = " and " . join ( " AND " , $wheresql );
2013-09-30 03:09:21 +00:00
} else {
$wheresql = " " ;
}
if ( empty ( $this -> order ))
{
$order = " ref.year,ref.title " ;
} else {
$order = " ref. { $this -> order } { $this -> sort } " ;
}
2014-05-15 16:02:23 +00:00
$sql = " select distinct ref.* from { $this -> table -> reference } ref where ref.id in (select r.refid from mdref r
2013-09-29 10:06:49 +00:00
left join datasource ds on r . uuid = ds . uuid left join { $this -> table -> source } s on s . id = ds . sourceid
2014-05-15 16:02:23 +00:00
where s . code = '$code' )
$wheresql
2013-09-30 03:09:21 +00:00
ORDER BY $order " ;
2013-09-28 03:21:40 +00:00
$rs = $this -> db -> query ( $sql );
2013-09-28 03:22:48 +00:00
return $rs -> fetchAll ();
2013-09-28 03:21:40 +00:00
}
2013-10-27 15:33:14 +00:00
2013-10-27 06:04:30 +00:00
//Get WestDC references
public function fetchWestdcReferences ()
{
2013-10-27 15:33:14 +00:00
$wheresql = array ();
2013-10-27 06:04:30 +00:00
$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 ();
2013-10-27 15:33:14 +00:00
}
2013-10-22 13:40:41 +00:00
//Get references which need to deal with
public function fetchTodoReferences ()
{
2013-10-27 15:33:14 +00:00
$wheresql = array ();
2013-10-22 14:42:10 +00:00
$wheresql [] = " ref.id not in (select distinct refid from mdref) " ;
2013-10-22 13:40:41 +00:00
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 } " ;
}
2013-10-22 14:42:10 +00:00
$sql = " select distinct ref.* from { $this -> table -> reference } ref
2013-10-22 13:40:41 +00:00
$wheresql
ORDER BY $order " ;
$rs = $this -> db -> query ( $sql );
return $rs -> fetchAll ();
2013-10-27 15:33:14 +00:00
}
2013-09-28 03:21:40 +00:00
2014-05-20 01:17:54 +00:00
//Get data author references which need to deal with
public function fetchAuthorReferences ()
{
$wheresql = array ();
$wheresql [] = " ref.ris is NULL " ;
$wheresql [] = " ref.id in (select distinct refid from mdref where reftype=0) " ;
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 ();
}
2014-05-16 14:48:02 +00:00
//Get references by data UUID
public function fetchReferencesByUUID ( $uuid )
{
$wheresql = array ();
$wheresql [] = " r.uuid=' $uuid ' " ;
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 } " ;
}
2014-05-18 04:49:12 +00:00
$sql = " select distinct ref.*,r.reftype,r.place,r.id as mrid from { $this -> table -> reference } ref left join { $this -> table -> metadata_reference } r on ref.id=r.refid
$wheresql
ORDER BY $order " ;
$rs = $this -> db -> query ( $sql );
return $rs -> fetchAll ();
}
//Get references with data UUID
public function fetchReferencesWithUUID ( $uuid )
{
$wheresql = array ();
//$wheresql[]=" r.uuid='$uuid' ";
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.*,r.reftype,r.place,r.id as mrid from { $this -> table -> reference } ref left join
( select * from { $this -> table -> metadata_reference } r where uuid = '$uuid' ) r on ref . id = r . refid
2014-05-16 14:48:02 +00:00
$wheresql
ORDER BY $order " ;
$rs = $this -> db -> query ( $sql );
return $rs -> fetchAll ();
}
2013-09-26 08:26:29 +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-26 08:26:29 +00:00
2013-09-28 02:17:33 +00:00
$row [ 'file' ] = $attfile ;
}
2013-09-26 08:26:29 +00:00
return $row ;
}
2013-09-27 07:21:14 +00:00
//获得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' " ;
2013-09-27 07:21:14 +00:00
$rs = $this -> db -> query ( $sql );
$rows = $rs -> fetchAll ();
return $rows ;
}
2013-09-29 10:06:49 +00:00
//删除文献
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 " ;
2013-10-15 01:36:49 +00:00
@ $this -> db -> exec ( $sql );
2013-10-09 10:02:11 +00:00
$sql = " DELETE FROM { $this -> table -> metadata_reference } WHERE refid= $id " ;
2013-10-15 01:36:49 +00:00
@ $this -> db -> exec ( $sql );
$this -> deleteReferenceAuthor ( $id );
$this -> deleteReferenceTag ( $id );
2013-10-09 10:02:11 +00:00
return true ;
2013-09-29 10:06:49 +00:00
} else {
}
}
2013-10-09 03:22:27 +00:00
2013-10-15 01:36:49 +00:00
//删除作者信息
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 );
}
2013-10-09 03:22:27 +00:00
//建立文献与数据的关系
2013-10-10 02:19:46 +00:00
public function createRelationFromReferenceToData ( $refid , $uuid , $reftype , $place , $id = NULL )
2013-10-09 03:22:27 +00:00
{
2013-10-09 10:02:11 +00:00
if ( empty ( $refid ) || ! is_numeric ( $refid ))
2013-10-09 03:22:27 +00:00
{
return " 参数错误 " ;
}
if ( ! view :: isUuid ( $uuid ))
{
return " 参数错误 " ;
}
$data = array (
'uuid' => $uuid ,
'refid' => $refid ,
'reftype' => $reftype ,
'place' => $place
);
$dbh = new dbh ();
2013-10-10 02:19:46 +00:00
if ( empty ( $id ))
2013-10-09 03:22:27 +00:00
{
2013-10-10 02:19:46 +00:00
$id = $dbh -> insert ( $this -> table -> metadata_reference , $data , true );
if ( is_numeric ( $id ))
{
return $id ;
} else {
return " 关系写入失败,请检查是否已经存在 " ;
}
2013-10-09 03:22:27 +00:00
} else {
2013-10-10 02:19:46 +00:00
$status = $dbh -> update ( $this -> table -> metadata_reference , $data , " id= $id " );
if ( $status === true )
{
return $id ;
} else {
return " 修改失败 " ;
}
}
2013-10-09 03:22:27 +00:00
}
//获得某个文献关联的数据 (根据文献获得数据)
public function getDataByReference ( $id )
{
if ( empty ( $id ) || ! is_numeric ( $id ))
{
return " 参数错误 " ;
}
2013-10-10 02:19:46 +00:00
$sql = " SELECT mr.id,mr.refid,mr.reftype,mr.place,md.title,md.uuid FROM { $this -> table -> metadata_reference } mr
2013-10-09 03:22:27 +00:00
LEFT JOIN { $this -> table -> metadata } md ON mr . uuid = md . uuid
2013-10-10 02:19:46 +00:00
WHERE mr . refid = $id
ORDER BY mr . place ASC " ;
2013-10-09 03:22:27 +00:00
$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 (
2013-11-01 09:13:35 +00:00
0 => '相关文献' , //作者建议的文献或数据中心建议的文献
2013-10-12 04:10:22 +00:00
1 => '施引文献' ,
2 => '参考文献' ,
2013-10-27 15:33:14 +00:00
3 => '多篇文献' ,
4 => '专题文献'
2013-10-09 03:22:27 +00:00
);
}
2013-10-09 10:02:11 +00:00
//数据文献参数
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 ;
}
//写入数据文献
2013-10-10 02:19:46 +00:00
public function makeMdref ( $id = NULL )
2013-10-09 10:02:11 +00:00
{
2013-10-10 02:19:46 +00:00
$data = $this -> getMdrefParam ();
2013-10-09 10:02:11 +00:00
$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 ();
2013-10-10 02:19:46 +00:00
$id = $this -> createRelationFromReferenceToData ( $data [ 'refid' ], $data [ 'uuid' ], $data [ 'reftype' ], $data [ 'place' ], $id );
2013-10-09 10:02:11 +00:00
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 " 删除失败 " ;
}
}
2013-10-12 09:26:10 +00:00
2013-11-04 07:08:58 +00:00
//按年份获得文献数量
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 ;
}
2013-11-04 09:04:07 +00:00
//获得标签
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 ;
}
2013-09-28 02:17:33 +00:00
}