2012-04-17 10:19:43 +00:00
< ? php
class SubmitController extends Zend_Controller_Action
{
private $limit = 10 ;
function preDispatch ()
{
$this -> view -> config = Zend_Registry :: get ( 'config' );
$this -> db = Zend_Registry :: get ( 'db' );
}
2012-04-22 09:19:23 +00:00
2012-04-17 10:19:43 +00:00
function indexAction ()
{
}
2012-04-21 12:39:35 +00:00
//新建元数据
function newdataAction ()
{
$ac = $this -> _request -> getParam ( 'ac' );
$id = $this -> _request -> getParam ( 'id' );
$this -> wdb = Zend_Db :: factory ( $this -> view -> config -> geonetwork );
2012-04-17 10:19:43 +00:00
2012-04-21 12:39:35 +00:00
$auth = Zend_Auth :: getInstance ();
2012-04-17 10:19:43 +00:00
if ( $auth -> hasIdentity ())
{
$user = $auth -> getIdentity ();
2012-04-22 09:19:23 +00:00
$u_id = $user -> id ;
$this -> view -> isadmin = false ;
2012-04-21 13:40:54 +00:00
if ( $user -> usertype == 'administrator' ) $this -> view -> isadmin = true ;
2012-04-17 10:19:43 +00:00
}
2012-04-21 13:40:54 +00:00
//根据已有元数据模板创建元数据
if ( empty ( $ac ))
{
2012-04-22 09:19:23 +00:00
$keywords = $this -> _request -> getParam ( 'q' );
$sql = " select id,(regexp_matches(data,'<resTitle>(.*)</resTitle>'))[1] as title,(owner- $u_id ) as isowner from metadata where istemplate='y' and schemaid='iso19115' " ;
if ( ! empty ( $keywords ))
{
$this -> view -> q = $keywords ;
$search = new Search ( $keywords );
$where = $search -> sql_expr ( array ( " data " ));
$sql .= ' and ' . $where ;
}
$sql .= " order by changedate desc " ;
$sth = $this -> wdb -> prepare ( $sql );
$sth -> execute ();
$rows = $sth -> fetchAll ();
$paginator = Zend_Paginator :: factory ( $rows );
$paginator -> setCurrentPageNumber ( $this -> _getParam ( 'page' ));
$paginator -> setItemCountPerPage ( 10 );
$paginator -> setView ( $this -> view );
Zend_View_Helper_PaginationControl :: setDefaultViewPartial ( 'pagination_param.phtml' );
$this -> view -> paginator = $paginator ;
2012-04-21 13:40:54 +00:00
}
//根据已有数据创建元数据
else if ( $ac == " add " )
{
$keywords = $this -> _request -> getParam ( 'q' );
2012-04-22 09:19:23 +00:00
$sql = " SELECT md.title,md.uuid,md.description,gn.id as gid FROM normalmetadata md
left join geonetworkmetadata gn on md . uuid = gn . uuid
WHERE gn . id is not null " ;
2012-04-20 12:56:50 +00:00
if ( ! empty ( $keywords ))
2012-04-17 10:19:43 +00:00
{
$this -> view -> q = $keywords ;
$search = new Search ( $keywords );
$where = $search -> sql_expr ( array ( " md.title " , " md.description " ));
2012-04-22 09:19:23 +00:00
$sql .= ' and ' . $where ;
2012-04-20 12:56:50 +00:00
}
2012-04-21 13:40:54 +00:00
$sql .= " order by md.ts_created desc " ;
$sth = $this -> db -> prepare ( $sql );
2012-04-20 12:56:50 +00:00
$sth -> execute ();
2012-04-21 13:40:54 +00:00
$rows = $sth -> fetchAll ();
$paginator = Zend_Paginator :: factory ( $rows );
$paginator -> setCurrentPageNumber ( $this -> _getParam ( 'page' ));
$paginator -> setItemCountPerPage ( 10 );
$paginator -> setView ( $this -> view );
Zend_View_Helper_PaginationControl :: setDefaultViewPartial ( 'pagination_param.phtml' );
$this -> view -> paginator = $paginator ;
2012-04-17 10:19:43 +00:00
$this -> _helper -> viewRenderer ( 'newdata-add' );
2012-04-22 09:19:23 +00:00
}
2012-04-21 13:40:54 +00:00
}
2012-04-17 10:19:43 +00:00
2012-04-22 09:19:23 +00:00
//未提交数据列表
function unsubmitAction ()
{
$ac = $this -> _request -> getParam ( 'ac' );
$id = $this -> _request -> getParam ( 'id' );
$this -> wdb = Zend_Db :: factory ( $this -> view -> config -> geonetwork );
$auth = Zend_Auth :: getInstance ();
if ( $auth -> hasIdentity ())
{
$user = $auth -> getIdentity ();
$u_id = $user -> id ;
}
//提交数据
if ( $ac == " commit " )
{
$this -> _helper -> layout -> disableLayout ();
$this -> _helper -> viewRenderer -> setNoRender ();
$data = " " ;
try {
$id = $this -> _request -> getParam ( 'id' );
if ( empty ( $id ) || ! is_numeric ( $id ))
{
$data = array ( " error " => " 参数错误 " );
$this -> jsonexit ( $data );
return true ;
}
$changelog = $this -> _request -> getParam ( 'changelog' );
if ( empty ( $changelog ))
{
$data = array ( " error " => $this -> alertbox ( 'warning' , '请输入变更信息' ));
$this -> jsonexit ( $data );
return true ;
}
// 1. 权限认定: 当前用户必须和其owner相同
// 数据应当没有评审状态,没有作者信息
$sql = " select gn.id from geonetworkmetadata gn
left join mdstatus s on gn . uuid = s . uuid
left join mdauthor a on s . uuid = a . uuid
where s . id is not null and a . id is not null and gn . id = ? " ;
$sth = $this -> db -> prepare ( $sql );
$sth -> execute ( array ( $id ));
$row = $sth -> fetch ();
if ( ! emtpy ( $row ))
{
$data = array ( " error " => '错误的入口' );
$this -> jsonexit ( $data );
return true ;
}
$sql = " select uuid from metadata where id=? and owner=? " ;
$sth = $this -> wdb -> prepare ( $sql );
$sth -> execute ( array ( $id , $u_id ));
$row = $sth -> fetch ();
if ( empty ( $row ))
{
$data = array ( " error " => '无权限修改数据' );
$this -> jsonexit ( $data );
return true ;
}
// 保存数据作者信息
$sql = " insert into mdauthor (uuid,userid,ts_activated,status) values(?,?,now(),1) " ;
$sth = $this -> db -> query ( $sql , array ( $row [ 'uuid' ], $u_id ));
// 2. 保存变化记录 save changelog & userid for the latest version
$sql = " UPDATE mdversion SET changelog=?,userid=? WHERE id in (select id from mdversion where uuid=? order by ts_created desc limit 1) " ;
$this -> db -> query ( $sql , array ( $changelog , $u_id , $row [ 'uuid' ]));
// 3. 保存数据评审状态
//导入元数据
$iso = new ISO19115 ();
$iso -> saveDB ( $this -> db , $row [ 'xml' ]);
//进入评审库
$sql = " insert into mdstatus (uuid,status,userid) select uuid,0,? from mdversion where id=? " ;
$this -> db -> query ( $sql , array ( $u_id , $id ));
//email to admin
$mail = new WestdcMailer ( $this -> view -> config -> smtp );
$mail -> setFrom ( $this -> view -> config -> service -> email , '西部数据中心服务组' );
$mailtp = new EmailText ( $this -> db , " metadata-new-admin " , array (
'user' => $user -> username ,
'uuid' => $iso -> uuid ,
'email' => $user -> email ,
//元数据标题
'title' => $iso -> resTitle ,
));
$mail -> setBodyText ( $mailtp -> getBody ());
$mail -> setSubject ( $mailtp -> getSubject ());
$mail -> addTo ( $this -> view -> config -> service -> email );
$mail -> send ();
unset ( $mail );
unset ( $mailtp );
//email to author
$mail = new WestdcMailer ( $this -> view -> config -> smtp );
$mail -> setFrom ( $this -> view -> config -> service -> email , '西部数据中心服务组' );
$mailtp = new EmailText ( $this -> db , " metadata-new-author " , array (
'user' => $user -> username ,
'uuid' => $iso -> uuid ,
'email' => $user -> email ,
//元数据标题
'title' => $iso -> resTitle ,
));
$mail -> setBodyText ( $mailtp -> getBody ());
$mail -> setSubject ( $mailtp -> getSubject ());
$mail -> addTo ( $user -> email );
$mail -> addCc ( $this -> view -> config -> service -> email );
@ $mail -> send ();
$data = array ( " commited " => 1 , " error " => $this -> alertbox ( 'ok' , '该版本已经成功提交,请等待数据中心进一步处理!' ));
$this -> jsonexit ( $data );
return true ;
} catch ( Exception $e ) {
$msg = " 提交失败,请确认权限后重试 " ;
if ( $this -> debug > 0 )
{ $msg .= $e -> getMessage ();}
$data = array ( " error " => $this -> alertbox ( 'error' , $msg ));
$this -> jsonexit ( $data );
return true ;
}
}
else
{
$sql = " SELECT (regexp_matches(gn.data,'<resTitle>(.*)</resTitle>'))[1] as title,gn.id,gn.uuid FROM geonetworkmetadata gn
WHERE gn . uuid not in ( select uuid from metadata ) and gn . owner = ?
order by gn . id desc
" ;
$sth = $this -> db -> prepare ( $sql );
$sth -> execute ( array ( $u_id ));
$rows = $sth -> fetchAll ();
$paginator = Zend_Paginator :: factory ( $rows );
$paginator -> setCurrentPageNumber ( $this -> _getParam ( 'page' ));
$paginator -> setItemCountPerPage ( 15 );
$paginator -> setView ( $this -> view );
Zend_View_Helper_PaginationControl :: setDefaultViewPartial ( 'pagination_param.phtml' );
$this -> view -> paginator = $paginator ;
}
2012-04-21 12:39:35 +00:00
}
2012-04-22 09:19:23 +00:00
function uploadAction ()
2012-04-21 12:39:35 +00:00
{
2012-04-21 06:15:17 +00:00
$this -> _helper -> layout () -> disableLayout ();
2012-04-20 12:56:50 +00:00
$uuid = $this -> _request -> getParam ( 'uuid' );
$this -> view -> uuid = $uuid ;
$ac = $this -> _request -> getParam ( 'ac' );
2012-04-22 09:19:23 +00:00
$dataFilePath = " ../data/datafiles/ " ;
2012-04-20 12:56:50 +00:00
if ( $ac == 'submit' )
{
2012-04-21 12:39:35 +00:00
$this -> _helper -> viewRenderer -> setNoRender ();
2012-04-20 12:56:50 +00:00
$auth = Zend_Auth :: getInstance ();
if ( $auth -> hasIdentity ())
{
$user = $auth -> getIdentity ();
$userid = $user -> id ;
}
if ( empty ( $userid ))
{
$data = array ( " error " => '请先登录后进行操作' );
$this -> jsonexit ( $data );
return true ;
}
if ( empty ( $uuid ))
{
$data = array ( " error " => '参数错误' . $uuid );
$this -> jsonexit ( $data );
return true ;
}
$files = $_REQUEST [ 'files' ];
if ( empty ( $files ) || ! is_array ( $files ))
{
$data = array ( " error " => '请先上传文件' );
$this -> jsonexit ( $data );
return true ;
}
//sql
$data = array ( " error " => '数据文件保存成功' );
$this -> jsonexit ( $data );
return true ;
}
if ( empty ( $ac ) && ! empty ( $_FILES [ 'Filedata' ]))
{
$this -> _helper -> layout () -> disableLayout ();
$this -> _helper -> viewRenderer -> setNoRender ();
$data = " " ;
try {
$auth = Zend_Auth :: getInstance ();
if ( $auth -> hasIdentity ())
{
$user = $auth -> getIdentity ();
$userid = $user -> id ;
}
if ( empty ( $userid ) ||! is_numeric ( $userid )){
$data = array ( " error " => '请先登录' );
$this -> jsonexit ( $data );
return true ;
}
include ( " files.php " );
$msg = files :: dataFilesUpload ( $dataFilePath , $_FILES [ 'Filedata' ], 'datafiles' , $uuid );
if ( empty ( $msg [ 'error' ]))
{
$filename = $msg [ 'db_path' ];
$filesize = $msg [ 'file_size' ];
$filedesc = $this -> _request -> getParam ( 'filedesc' );
$filetype = $this -> _request -> getParam ( 'dir' );
$realname = $msg [ 'realname' ];
$fileurl = $msg [ 'file_url' ];
$sql = " insert into attachments (filename,filetype,filedesc,userid,filesize,realname) values (' $filename ','datafiles',' $filedesc ',' $userid ',' $filesize ',' $realname ') RETURNING id " ;
$sth = $this -> db -> prepare ( $sql );
$sth -> execute ();
$att = $sth -> fetch ( PDO :: FETCH_ASSOC );
$attid = $att [ 'id' ];
$html = $realname . '[已完成]<input type="hidden" name="files[]" value="' . $attid . '" /><div class="cancel"><a href="javascript:;" id="deletebtn_' . $attid . '"><img border="0" src="/static/js/uploadify/cancel.png" /></a></div>' ;
$data = array (
'html' => $html ,
'attid' => $attid ,
'error' => ''
);
echo Zend_Json :: encode ( $data );
exit ();
} else {
@ unlink ( $filename );
$data = array (
'error' => '附件上传失败:' . $msg [ 'error' ],
);
echo Zend_Json :: encode ( $data );
exit ();
}
} catch ( Exception $e ){
if ( $this -> debug > 0 )
{
$error = " 错误: " . $e -> getMessage ();
} else {
$error = " 处理中发生错误 " ;
}
$data = array (
'error' => $error ,
);
echo Zend_Json :: encode ( $data );
exit ();
}
}
if ( $ac == 'del' )
{
$this -> _helper -> layout () -> disableLayout ();
$this -> _helper -> viewRenderer -> setNoRender ();
$id = $this -> _request -> getParam ( 'id' );
$info = $this -> getFileinfo ( $id );
$filepath = $dataFilePath . $info [ 'filename' ];
try {
$auth = Zend_Auth :: getInstance ();
if ( $auth -> hasIdentity ())
{
$user = $auth -> getIdentity ();
$userid = $user -> id ;
$sql = " delete from attachments where id=' $id ' and userid=' $userid ' " ;
if ( $this -> db -> exec ( $sql ) > 0 )
{
@ unlink ( $filepath );
echo " ok " ;
}
}
} catch ( Exception $e ){}
//不输出任何错误
}
}
2012-04-22 09:19:23 +00:00
function filesAction (){
$this -> _helper -> layout () -> disableLayout ();
$uuid = $this -> _request -> getParam ( 'uuid' );
$this -> view -> uuid = $uuid ;
$ac = $this -> _request -> getParam ( 'ac' );
$dataFilePath = " ../data/datafiles " ;
$auth = Zend_Auth :: getInstance ();
if ( $auth -> hasIdentity ())
{
$user = $auth -> getIdentity ();
$userid = $user -> id ;
}
if ( empty ( $ac ) || $ac == 'list' )
{
$sql = " SELECT * FROM attachments WHERE filetype='datafiles' AND userid=? ORDER BY id DESC " ;
$sth = $this -> db -> prepare ( $sql );
$sth -> execute ( array ( $userid ));
$rows = $sth -> fetchAll ();
$paginator = Zend_Paginator :: factory ( $rows );
$paginator -> setCurrentPageNumber ( $this -> _getParam ( 'page' ));
$paginator -> setItemCountPerPage ( 8 );
$paginator -> setView ( $this -> view );
Zend_View_Helper_PaginationControl :: setDefaultViewPartial ( 'pagination.phtml' );
$this -> view -> paginator = $paginator ;
}
if ( $ac == " editname " )
{
$this -> _helper -> viewRenderer -> setNoRender ();
$name = $this -> _request -> getParam ( 'name' );
$id = $this -> _request -> getParam ( 'id' );
if ( empty ( $name ))
{
$data = array ( " error " => '请输入文件名' );
$this -> jsonexit ( $data );
return true ;
}
if ( empty ( $id ))
{
$data = array ( " error " => '参数错误' );
$this -> jsonexit ( $data );
return true ;
}
try {
$sql = " UPDATE attachments SET realname=? WHERE id=? " ;
$sth = $this -> db -> prepare ( $sql );
$ex = $sth -> execute ( array ( $name , $id ));
if ( $ex )
{
$data = array ( " success " => '1' );
$this -> jsonexit ( $data );
return true ;
}
else
{
$data = array ( " error " => '遇到错误请重试' );
$this -> jsonexit ( $data );
return true ;
}
} catch ( Exception $e ){
$data = array ( " error " => '遇到错误请重试' . $e -> getMessage ());
$this -> jsonexit ( $data );
return true ;
}
}
} //文件管理
2012-04-20 12:56:50 +00:00
public function getFileinfo ( $id ){
$sql = " select * from attachments where id=' $id ' " ;
$re = $this -> db -> query ( $sql );
$row = $re -> fetch ();
return $row ;
}
2012-04-17 10:19:43 +00:00
//成为作者后的后继处理工作
private function author_first ( $uuid , $author )
{
$sql = " insert into mdversion (xml,ts_created,uuid,changelog,userid)
select x . data , m . ts_created , ? , ? , ? from metadata m left join xml x on m . id = x . id
left join mdversion v on m . uuid = v . uuid
where m . uuid = ? and v . changelog is null " ;
$sth = $this -> db -> prepare ( $sql );
try
{
$sth -> execute ( array ( $uuid , '初始版本 version 1.0' , $author , $uuid ));
} catch ( Exception $e ){
// do nothing here.
// 说明之前已经有对应数据
}
$this -> wdb = Zend_Db :: factory ( $this -> view -> config -> geonetwork );
$sql = " update metadata set owner=? where uuid=? " ;
$sth = $this -> wdb -> prepare ( $sql );
$sth -> execute ( array ( $author , $uuid ));
}
/*
* jsonexit () 退出并返回json数据
*
* param array $data 要返回的JSON数据, 可以是任意数组
*
* return JSON - response
*/
public function jsonexit ( $data ){
$this -> getResponse () -> setHeader ( 'Content-Type' , 'application/json' ) -> appendBody ( Zend_Json :: encode ( $data ));
return true ;
} //jsonexit() 退出并返回json数据
//ajax 提示框
public function alertbox ( $type = '' , $body ){
if ( $type == " error " )
{
$img = '<img src="/images/alert_big_error.png" />' ;
$text = '<h4>' . $body . '</h4>' ;
return $img . $text ;
}
if ( $type == " ok " )
{
$img = '<img src="/images/alert_big_ok.png" />' ;
$text = '<h4>' . $body . '</h4>' ;
return $img . $text ;
}
if ( $type == " warning " )
{
$img = '<img src="/images/alert_big_warning.png" />' ;
$text = '<h4>' . $body . '</h4>' ;
return $img . $text ;
}
if ( empty ( $type ))
{
$text = '<h4>' . $body . '</h4>' ;
return $text ;
}
}
}