2013-05-03 06:41:06 +00:00
< ? php
class Review extends Zend_Controller_Plugin_Abstract
{
private $db ; //传入PDO对象.
2013-06-09 09:39:16 +00:00
protected $events = NULL ; //事件
2013-05-03 06:41:06 +00:00
2013-10-26 09:24:03 +00:00
//相关数据表
2013-05-10 08:53:24 +00:00
public $tbl_reviewexp = " mdexpertreview " ;
public $tbl_mdreview = " mdreview " ;
public $tbl_user = " users " ;
2013-05-03 06:41:06 +00:00
2013-07-15 03:44:50 +00:00
function __construct ( $db = NULL )
2013-05-03 06:41:06 +00:00
{
2013-07-15 03:44:50 +00:00
if ( empty ( $db ))
{
$this -> db = Zend_Registry :: get ( 'db' );
} else {
$this -> db = $db ;
}
2013-05-03 06:41:06 +00:00
}
2013-10-26 09:24:03 +00:00
/*
* events () 事件枚举
*
* @ param $events Zend_EventManager_EventCollection default null
*
* return Zend_EventManager_EventManager
*
* 使用Zend_EventManager_EventManager枚举评审操作中的事件
*/
2013-06-09 09:39:16 +00:00
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 ;
}
2013-10-26 09:24:03 +00:00
/*
* invite () 接受或者拒绝评审
*
* @ param $id int
* @ param $uuid uuid
* @ param $uid int
* @ param $status string
*
* return bool / string
*
* 处理评审邀请,接受或者拒绝
*/
2013-05-03 06:41:06 +00:00
function invite ( $id , $uuid , $uid , $status )
{
if ( empty ( $id ) || empty ( $uuid ) || ! is_numeric ( $id ) ||! preg_match ( " /^[0-9A-Za-z] { 8}-[0-9A-Za-z] { 4}-[0-9A-Za-z] { 4}-[0-9A-Za-z] { 4}-[0-9A-Za-z] { 12} $ / " , $uuid ))
{
return " 参数错误 " ;
}
if ( $id != $uid )
{
return " 您无权使用此通知 " ;
} //非本人操作
try {
2013-05-10 08:53:24 +00:00
$sql = " update " . $this -> tbl_reviewexp . " set status= $status where id=' $id ' and uuid=' $uuid ' " ;
2013-05-03 06:41:06 +00:00
if ( $this -> db -> exec ( $sql ))
{
return true ;
} else {
return " 您无权限进行此操作 " ;
}
} catch ( Exception $e ){
return " 处理中出现错误 " ;
}
2013-05-10 08:53:24 +00:00
}
2013-10-26 09:24:03 +00:00
/*
* getReviews () 根据UUID获得评审意见
*
* @ param $uuid uuid
*
* return array
*
* 传入评审数据的UUID, 返回评审意见记录
*/
2013-05-10 08:53:24 +00:00
function getReviews ( $uuid )
{
$sql = " SELECT mr.*,u.username,u.realname FROM " . $this -> tbl_mdreview . " mr
LEFT JOIN " . $this->tbl_user . " u ON mr . userid = u . id
WHERE mr . uuid = '$uuid' " ;
$sth = $this -> db -> query ( $sql );
$reviews = $sth -> fetchAll ();
2013-05-03 06:41:06 +00:00
2013-05-10 08:53:24 +00:00
return $reviews ;
2013-05-03 06:41:06 +00:00
}
2013-05-14 08:57:30 +00:00
2013-10-26 09:24:03 +00:00
/*
* needEditor () 需要分配责任编辑的元数据评审
*
* @ param $filter array
*
* return array
*
* filter参数帮助生成sql语句中的where、order、sort条件
*/
2013-07-15 03:44:50 +00:00
function needEditor ( $filter = " " )
{
$wheresql = array ();
$ordersql = array ();
$wheresql [] = " m.status in (1,2,3,4) " ;
$wheresql [] = " (m.userid IS NULL OR u.usertype != 'administrator') " ;
if ( isset ( $filter [ 'keyword' ]) && ! empty ( $filter [ 'keyword' ]))
{
$wheresql [] = " (md.title like '% " . $filter [ 'keyword' ] . " %' OR u.username LIKE '% " . $filter [ 'keyword' ] . " %' OR u.realname LIKE '% " . $filter [ 'keyword' ] . " %') " ;
}
if ( count ( $wheresql ) > 0 )
{
$wheresql = " WHERE " . join ( " AND " , $wheresql );
} else {
$wheresql = " " ;
}
if ( isset ( $filter [ 'order' ]) && ! empty ( $filter [ 'order' ]))
{
$sort = " DESC " ;
if ( isset ( $filter [ 'sort' ]) && ! empty ( $filter [ 'sort' ]) && in_array ( strtolower ( $filter [ 'sort' ]), array ( 'desc' , 'asc' )))
{
$sort = $filter [ 'sort' ];
}
$ordersql [] = " { $filter [ 'order' ] } $sort " ;
}
if ( count ( $ordersql ) > 0 )
{
$ordersql = " ORDER BY " . join ( ',' , $ordersql );
} else {
$ordersql = " ORDER BY m.ts_created desc " ;
}
$sql = " select m.*,md.title,u.username,u.realname from mdstatus m
right join metadata md on md . uuid = m . uuid
left join users u on u . id = m . userid
$wheresql
$ordersql " ;
$re = $this -> db -> query ( $sql );
$rows = $re -> fetchAll ();
return $rows ;
}
2013-10-26 09:24:03 +00:00
/*
* adminReviews () 我负责的评审
*
* @ param $filter array
*
* return array
*
* filter参数帮助生成sql语句中的where、order、sort条件
* 此函数返回当前登录的用户 ( 管理员 ) 负责的元数据评审
*/
2013-07-10 09:04:43 +00:00
function adminReviews ( $filter = " " )
2013-05-14 08:57:30 +00:00
{
include_once ( 'helper/view.php' );
2013-07-10 09:04:43 +00:00
$uid = \view :: User ( 'id' );
2013-05-14 08:57:30 +00:00
$wheresql = array ();
2013-07-10 09:04:43 +00:00
$ordersql = array ();
2013-05-14 08:57:30 +00:00
2013-05-29 07:36:44 +00:00
$wheresql [] = " m.status in (1,2,3,4) " ;
2013-05-14 08:57:30 +00:00
$wheresql [] = " u.id= $uid " ;
2013-06-13 08:06:11 +00:00
if ( isset ( $filter [ 'keyword' ]) && ! empty ( $filter [ 'keyword' ]))
2013-05-14 08:57:30 +00:00
{
2013-06-13 08:06:11 +00:00
$wheresql [] = " md.title like '% " . $filter [ 'keyword' ] . " %' " ;
}
if ( isset ( $filter [ 'code' ]) && ! empty ( $filter [ 'code' ]))
{
2013-06-16 15:28:28 +00:00
$wheresql [] = " s.sourceid= " . $filter [ 'code' ] . " " ;
2013-05-14 08:57:30 +00:00
}
2013-07-10 09:04:43 +00:00
if ( isset ( $filter [ 'order' ]) && ! empty ( $filter [ 'order' ]))
{
$sort = " DESC " ;
if ( isset ( $filter [ 'sort' ]) && ! empty ( $filter [ 'sort' ]) && in_array ( strtolower ( $filter [ 'sort' ]), array ( 'desc' , 'asc' )))
{
$sort = $filter [ 'sort' ];
}
$ordersql [] = " { $filter [ 'order' ] } $sort " ;
}
2013-05-14 08:57:30 +00:00
if ( count ( $wheresql ) > 0 )
{
$wheresql = " WHERE " . join ( " AND " , $wheresql );
} else {
$wheresql = " " ;
}
2013-07-10 09:04:43 +00:00
if ( count ( $ordersql ) > 0 )
{
$ordersql = " ORDER BY " . join ( ',' , $ordersql );
} else {
$ordersql = " ORDER BY m.status desc,m.ts_accepted desc " ;
}
$sql = " select m.id,g.id as gid, md.uuid,md.title,u.username,u.realname,m.status,md.id as mdid,md.author,m.ts_accepted,count(r.id) as reviews
2013-05-14 08:57:30 +00:00
FROM mdstatus m
2013-05-29 07:36:44 +00:00
right join metadata md on md . uuid = m . uuid
2013-05-24 00:35:07 +00:00
left join geonetworkmetadata g on m . uuid = g . uuid
2013-05-14 08:57:30 +00:00
left join users u on u . id = m . userid
2013-06-13 08:06:11 +00:00
left join datasource s on s . uuid = md . uuid
2013-07-10 09:04:43 +00:00
left join mdreview r ON r . uuid = md . uuid
2013-05-14 08:57:30 +00:00
$wheresql
2013-07-10 09:04:43 +00:00
GROUP BY m . id , g . id , md . uuid , md . title , u . username , u . realname , m . status , md . id , md . author , m . ts_accepted
$ordersql " ;
2013-05-14 08:57:30 +00:00
$re = $this -> db -> query ( $sql );
$rows = $re -> fetchAll ();
return $rows ;
}
2013-06-09 09:39:16 +00:00
2013-10-26 09:24:03 +00:00
/*
* post () 通过评审
*
* @ param $id int
* $param $emails array ()
*
* return bool
*
* 元数据通过评审且发布
*/
2013-06-09 09:39:16 +00:00
function post ( $id , $emails )
{
if ( is_numeric ( $id ))
{
$cid = ( int ) $id ;
}
if ( is_array ( $id ))
{
$cid = join ( " , " , $id );
}
include_once ( " helper/view.php " );
$userid = ( int ) view :: User ( 'id' );
if ( $this -> changestatus ( $cid , 5 ))
{
$results = $this -> events () -> trigger ( 'post.post' , $this , compact ( 'emails' , 'id' , 'userid' ));
return $results -> bottom ();
} else {
return false ;
}
} //发布
2013-10-26 09:24:03 +00:00
/*
* post () 检查管理员
*
* @ param $id int
* $param $emails array ()
*
* return bool
*
* 元数据通过评审且发布
*/
2013-06-09 09:39:16 +00:00
function checkAdmin ( $id , $userid = 0 )
{
$id = ( int ) $id ;
if ( empty ( $userid ))
{
include_once ( " helper/view.php " );
$userid = ( int ) view :: User ( 'id' );
}
$sql = " select id from mdstatus where id= $id and userid= $userid " ;
$rs = $this -> db -> query ( $sql );
$row = $rs -> fetch ();
if ( empty ( $row [ 'id' ]))
{
return false ;
} else {
return true ;
}
}
/*
* changestatus () 更改mdstatus中的status字段
*
* @ param int $id //要更改状态的mdstatus记录的ID
* @ param int $status //状态
*
* return bool
*/
function changestatus ( $id , $status ){
$stvalues = array (
- 1 , //取消评审
0 , //初始状态
1 , //接受元数据评审,进入评审阶段
2 , //开始邀请专家,送审阶段
3 , //专家接受邀请,在审阶段
4 , //专家反馈,在审
5 , //评审结束,发布
6 , 7
);
if ( empty ( $id ) || ! isset ( $status ) || ! in_array ( $status , $stvalues ))
{
return false ;
}
else
{
if ( $status == 1 )
{ $sql = " update mdstatus set status=' $status ',ts_accepted='now()' where id in ( $id ) " ; }
else if ( $status == 5 )
{ $sql = " update mdstatus set status=' $status ',ts_finished='now()' where id in ( $id ) " ;}
else
{ $sql = " update mdstatus set status=' $status ' where id in ( $id ) " ;}
try {
if ( $this -> db -> exec ( $sql ) > 0 )
{
return true ;
}
}
catch ( Exception $e )
{
return false ;
}
}
} //changestatus 更改状态
}
class ReviewListener implements Zend_EventManager_ListenerAggregate
{
private $db ; //传入PDO对象.
function __construct ( $db = NULL )
{
if ( empty ( $db ))
{
$this -> db = Zend_Registry :: get ( 'db' );
} else {
$this -> db = $db ;
}
$this -> config = Zend_Registry :: get ( 'config' );
}
public function attach ( Zend_EventManager_EventCollection $events )
{
$events -> attach ( 'post.post' , array ( $this , 'posted' ), 100 );
}
public function detach ( Zend_EventManager_EventCollection $events )
{
}
public function posted ( $e )
{
$id = $e -> getParam ( 'id' );
$emails = $e -> getParam ( 'emails' );
$userid = $e -> getParam ( 'userid' );
if ( is_numeric ( $id ))
{
//发布正式版本
$sql = " UPDATE mdversion SET changelog=?,userid=? WHERE id in (select id from mdversion where uuid in (select uuid from mdstatus where id=?) order by ts_created desc limit 1) " ;
$this -> db -> query ( $sql , array ( '发布第一个正式版本 version 1.0' , $userid , $id ));
//删除所有的中间版本
$sql = " delete from mdversion where changelog is null and uuid in (select uuid from mdstatus where id=?) " ;
$this -> db -> query ( $sql , array ( $id ));
//email message
if ( isset ( $emails ) && is_array ( $emails ) && count ( $emails ) > 0 )
2013-07-10 09:04:43 +00:00
{
2013-06-21 09:05:21 +00:00
$config = Zend_Registry :: get ( 'config' );
$mail = new WestdcMailer ( $config -> smtp );
2013-06-09 09:39:16 +00:00
$sql = " select m.uuid,m.title from metadata m left join mdstatus s on m.uuid=s.uuid where s.id=' $id ' " ;
$rs = $this -> db -> query ( $sql );
$res = $rs -> fetch ();
$mailtp = new EmailText ( $this -> db , 'metadata-publish' , array ( 'uuid' => $res [ 'uuid' ], 'title' => $res [ 'title' ]));
2013-07-10 09:04:43 +00:00
$mail -> setBodyText ( $mailtp -> getBody ());
$mail -> setFrom ( $config -> service -> email , '西部数据中心服务组' );
2013-06-21 08:49:38 +00:00
foreach ( $emails as $email ) $mail -> addTo ( $email );
2013-06-09 09:39:16 +00:00
$mail -> setSubject ( $mailtp -> getSubject ());
$mail -> send ();
}
}
if ( is_array ( $id ))
{
foreach ( $id as $v )
{
//发布正式版本
$sql = " UPDATE mdversion SET changelog=?,userid=? WHERE id in (select id from mdversion where uuid in (select uuid from mdstatus where id=?) order by ts_created desc limit 1) " ;
$this -> db -> query ( $sql , array ( '发布第一个正式版本 version 1.0' , $userid , $v ));
//删除所有的中间版本
$sql = " delete from mdversion where changelog is null and uuid in (select uuid from mdstatus where id=?) " ;
$this -> db -> query ( $sql , array ( $v ));
}
}
return true ;
}
2013-05-03 06:41:06 +00:00
}