2013-05-02 09:54:38 +00:00
< ? php
class Doi extends Zend_Controller_Plugin_Abstract
{
private $db ; //传入PDO对象.
private $auth = NULL ; //Zend_Auth 对象
//使用到的公共变量
public $tbl_doi = " datadoi " ;
function __construct ( $db )
{
$this -> db = $db ;
}
2013-06-29 16:15:23 +00:00
function fetch ( $uid = 0 , $keyword = '' )
2013-05-02 09:54:38 +00:00
{
2013-06-29 16:15:23 +00:00
$wheresql = ' 1=1 ' ;
if ( ! empty ( $keyword ))
{
if ( preg_match ( " / \ '/ " , $keyword ))
{
$keyword = preg_replace ( " / \ '/ " , " '' " , $keyword );
}
$wheresql .= " and d.title like '% $keyword %' " ;
}
2013-05-02 09:54:38 +00:00
if ( empty ( $uid ))
{
2015-01-26 06:36:48 +00:00
$sql = " SELECT *,array_to_json(organization) as org_json,array_to_json(organization_en) as orgen_json FROM " . $this -> tbl_doi . " d where " . $wheresql . " ORDER BY d.ts_published desc,d.ts_submitted desc,d.ts_created DESC " ;
2013-05-02 09:54:38 +00:00
} else {
2015-01-26 06:36:48 +00:00
$sql = " SELECT d.*,array_to_json(d.organization) as org_json,array_to_json(d.organization_en) as orgen_json FROM " . $this -> tbl_doi . " d
2013-06-29 16:15:23 +00:00
LEFT JOIN mdauthor a ON d . uuid = a . uuid
WHERE a . userid = " . $uid . " AND a . status > 0 and " . $wheresql . "
ORDER BY d . ts_published desc , d . ts_submitted desc , d . ts_created DESC
2013-05-02 09:54:38 +00:00
" ;
}
$rs = $this -> db -> query ( $sql );
$rows = $rs -> fetchAll ();
return $rows ;
}
function add ( $data ){
$this -> data_process_in ( $data );
2015-01-26 06:36:48 +00:00
$sql = " insert into " . $this -> tbl_doi . " (doi,authors,organization,publisher,uuid,url,title,title_en,publisher_en,author_en,organization_en) values(' " .
$data [ 'doi' ] . " ',' " . $data [ 'authors' ] . " ',array[ " . $data [ 'organization' ] . " ],' " . $data [ 'publisher' ] . " ',' " . $data [ 'uuid' ] .
" ',' " . $data [ 'url' ] . " ',' " . $data [ 'title' ] . " ',' " . $data [ 'title_en' ] . " ',' " . $data [ 'publisher_en' ] . " ',' " . $data [ 'author_en' ] .
" ',array[ " . $data [ 'organization_en' ] . " ]) " ;
return $this -> db -> exec ( $sql );
2013-05-02 09:54:38 +00:00
}
function update ( $data , $uuid , $uid = 0 ){
2013-05-09 03:55:46 +00:00
if ( ! empty ( $uid ))
2013-05-02 09:54:38 +00:00
{
2013-05-09 03:55:46 +00:00
include_once ( " data/Author.php " );
$author = new Author ( $this -> db );
if ( $author -> checkAuthor ( $uuid , $uid ) == false )
{
return " 您没有权限 " ;
}
2013-05-02 09:54:38 +00:00
}
$doi_info = $this -> view ( $uuid );
if ( $doi_info == false )
{
return $this -> add ( $data );
} else {
$this -> data_process_in ( $data );
//include_once("helper/view.php");
//view::Dump($data);
include_once ( " helper/dbh.php " );
$dbh = new dbh ( $this -> db );
if ( is_numeric ( $uuid ))
{
$condition = " id= $uuid " ;
} else {
$condition = " uuid=' $uuid ' " ;
}
2015-01-26 06:36:48 +00:00
$sql = " update $this->tbl_doi set doi=' " . $data [ 'doi' ] . " ',uuid=' " . $data [ 'uuid' ] . " ',publisher=' " . $data [ 'publisher' ] . " ',
url = '".$data[' url ']."' , title = '".$data[' title ']."' , title_en = '".$data[' title_en ']."' , publisher_en = '".$data[' publisher_en ']."' ,
authors = '".$data[' authors ']."' , author_en = '".$data[' author_en ']."' , organization = array [ " . $data['organization'] . " ],
organization_en = array [ " . $data['organization_en'] . " ] where " . $condition ;
$state = $this -> db -> exec ( $sql );
2013-05-02 09:54:38 +00:00
if ( $state == true )
{
return true ;
} else {
return $state ;
}
}
}
function data_process_in ( & $data ){
$authors = array ();
$orgs = array ();
$authors_en = array ();
$orgs_en = array ();
foreach ( $data [ 'info' ] as $k => $v )
{
2015-01-26 06:36:48 +00:00
$authors [( int ) $v [ 'order' ]] = $v [ 'author' ];
$orgs [( int ) $v [ 'order' ]] = $v [ 'organization' ];
$authors_en [( int ) $v [ 'order' ]] = $v [ 'author_en' ];
$orgs_en [( int ) $v [ 'order' ]] = $v [ 'organization_en' ];
2013-05-02 09:54:38 +00:00
}
2015-01-26 06:36:48 +00:00
ksort ( $authors );
ksort ( $orgs );
ksort ( $authors_en );
ksort ( $orgs_en );
2013-05-02 09:54:38 +00:00
$authors = " { " . join ( " , " , $authors ) . " } " ;
2015-01-26 06:36:48 +00:00
$orgs = " ' " . join ( " ',' " , $orgs ) . " ' " ;
2013-05-02 09:54:38 +00:00
$authors_en = " { " . join ( " , " , $authors_en ) . " } " ;
2015-01-26 06:36:48 +00:00
$orgs_en = " ' " . join ( " ',' " , $orgs_en ) . " ' " ;
2013-05-02 09:54:38 +00:00
$data [ 'authors' ] = $authors ;
$data [ 'organization' ] = $orgs ;
$data [ 'author_en' ] = $authors_en ;
$data [ 'organization_en' ] = $orgs_en ;
unset ( $data [ 'info' ]);
}
function data_process_out ( & $data ){
$authors = $this -> array_split ( $data [ 'authors' ]);
2015-01-26 06:36:48 +00:00
$orgs = json_decode ( $data [ 'org_json' ]);
2013-05-02 09:54:38 +00:00
$authors_en = $this -> array_split ( $data [ 'author_en' ]);
2015-01-26 06:36:48 +00:00
$orgs_en = json_decode ( $data [ 'orgen_json' ]);
2013-05-02 09:54:38 +00:00
$info = array ();
foreach ( $authors as $k => $v )
{
$info [ $k ] = array (
2013-05-09 08:20:44 +00:00
'author' => str_replace ( " \" " , " " , $authors [ $k ]),
'organization' => str_replace ( " \" " , " " , $orgs [ $k ]),
'author_en' => str_replace ( " \" " , " " , $authors_en [ $k ]),
'organization_en' => str_replace ( " \" " , " " , $orgs_en [ $k ]),
2015-01-26 06:36:48 +00:00
'order' => count ( $authors ) + $k
2013-05-02 09:54:38 +00:00
);
}
return $info ;
}
function array_split ( $a ){
2013-05-08 13:30:16 +00:00
return explode ( ',' , substr ( $a , 1 , - 1 ));
2013-05-02 09:54:38 +00:00
}
function view ( $id )
{
if ( is_numeric ( $id ))
{
2015-01-26 06:36:48 +00:00
$sql = " SELECT *,array_to_json(organization) as org_json,array_to_json(organization_en) as orgen_json FROM " . $this -> tbl_doi . " WHERE id= $id " ;
2013-05-02 09:54:38 +00:00
} else {
2015-01-26 06:36:48 +00:00
$sql = " SELECT *,array_to_json(organization) as org_json,array_to_json(organization_en) as orgen_json FROM " . $this -> tbl_doi . " WHERE uuid=' $id ' " ;
2013-05-02 09:54:38 +00:00
}
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
return $row ;
}
function delete ( $id , $uid = 0 ){
if ( ! is_numeric ( $id ))
{
return " 参数错误 " ;
}
if ( $uid != 0 ){
$sql = " SELECT uuid FROM " . $this -> tbl_doi . " WHERE id= $id " ;
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
if ( isset ( $row [ 'uuid' ]) && ! empty ( $row [ 'uuid' ])){
include_once ( " data/Author.php " );
$author = new Author ( $this -> db );
if ( $author -> checkAuthor ( $row [ 'uuid' ], $uid ) == false )
{
return " 您没有权限 " ;
}
$condition = " id= $id " ;
$sql = " DELETE FROM " . $this -> tbl_doi . " WHERE $condition " ;
return $this -> db -> exec ( $sql );
} else {
return " 该记录不存在 " ;
}
} else {
$condition = " id= $id " ;
$sql = " DELETE FROM " . $this -> tbl_doi . " WHERE $condition " ;
return $this -> db -> exec ( $sql );
}
}
function _getParams ( Zend_Controller_Request_Abstract $request )
{
$data = array (
'doi' => trim ( $request -> getParam ( 'doi' )),
'uuid' => trim ( $request -> getParam ( 'uuid' )),
'publisher' => trim ( $request -> getParam ( 'publisher' )),
'url' => trim ( $request -> getParam ( 'url' )),
'title' => trim ( $request -> getParam ( 'title' )),
'title_en' => trim ( $request -> getParam ( 'title_en' )),
'publisher_en' => trim ( $request -> getParam ( 'publisher_en' )),
'info' => $_POST [ 'info' ]
);
return $data ;
}
function checkinfo ( $info ){
if ( ! is_array ( $info )){
return NULL ;
}
foreach ( $info as $k => $v )
{
if ( empty ( $v [ 'author' ]) && empty ( $v [ 'organization' ]) && empty ( $v [ 'order' ]) && empty ( $v [ 'author_en' ]) && empty ( $v [ 'organization_en' ]) )
{
unset ( $info [ $k ]);
} else {
if ( empty ( $v [ 'author' ]))
{
return " 请输入 $k 中的作者 " ;
}
if ( empty ( $v [ 'organization' ]))
{
return " 请输入 $k 中的单位 " ;
}
if ( empty ( $v [ 'author_en' ]))
{
return " 请输入 $k 中的作者英文 " ;
}
if ( empty ( $v [ 'organization_en' ]))
{
return " 请输入 $k 中的单位英文 " ;
}
if ( empty ( $v [ 'order' ]))
{
return " 请输入 $k 中的排序 " ;
}
}
}
return $info ;
}
function sksort ( $array , $key , $type = SORT_DESC ) {
$sortArray = array ();
foreach ( $array as $v ){
foreach ( $v as $key => $value ){
if ( ! isset ( $sortArray [ $key ])){
$sortArray [ $key ] = array ();
}
$sortArray [ $key ][] = $value ;
}
}
if ( array_multisort ( $sortArray [ $key ], $type , $array ))
{
return $array ;
} else {
return $array ;
}
}
2015-01-26 06:36:48 +00:00
//为未注册DOI的数据准备初始信息
function prepare ( $uuid )
{
$sql = " select substring(doi from 'sjy.(.*).db') as max from datadoi where doi like '10.3972/sjy.%.db' order by doi desc limit 1 " ;
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
if ( $row )
{
$i = ( int ) $row [ 'max' ];
$i ++ ;
$doi = '10.3972/sjy.' . sprintf ( " %'.03d " , $i ) . '.db' ;
} else
$doi = '10.3972/sjy.001.db' ;
$sql = " select m.uuid,m.title,m.title_en,m.ts_published,res.organisation from metadata m left join role r on m.uuid=r.uuid left join responsible res on r.resid=res.id where r.role='resourceProvider' and m.uuid=' $uuid ' " ;
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
if ( ! $row ) return false ;
2015-01-26 06:58:17 +00:00
$org_english = array ( '青海省生态环境遥感监测中心' => 'Qinghai Ecosystem Remote Sensing Monitoring Center' ,
2015-01-26 06:36:48 +00:00
'青海省环境监测中心站' => 'Qinghai Environmental Monitoring Center' ,
'青海省气象科学研究所' => 'Qinghai Institute of Meteorological Science' ,
'青海省草原总站' => '' ,
'青海省水土保持局' => '' ,
'青海省林业调查规划院' => '' ,
'青海省水文水资源勘测局' => '' );
2013-05-02 09:54:38 +00:00
2015-01-26 06:36:48 +00:00
$info = array (
'author' => str_replace ( " \" " , " " , $row [ 'organisation' ]),
'organization' => str_replace ( " \" " , " " , $row [ 'organisation' ]),
'author_en' => str_replace ( " \" " , " " , $org_english [ $row [ 'organisation' ]]),
'organization_en' => str_replace ( " \" " , " " , $org_english [ $row [ 'organisation' ]]),
'order' => 1
);
$result = array ( 'uuid' => $uuid ,
'title' => $row [ 'title' ],
'doi' => $doi ,
'url' => " http:// " . $_SERVER [ 'HTTP_HOST' ] . '/data/' . $uuid ,
'publisher' => '三江源生态监测综合服务平台' ,
'title_en' => $row [ 'title_en' ],
'publisher_en' => 'Ecological data platform for Sanjiangyuan National Nature Reserve' ,
'info' => array ( $info ),
);
return $result ;
}
2013-05-02 09:54:38 +00:00
}