2013-10-28 14:54:21 +00:00
< ? php
namespace Order ;
2013-11-07 11:24:49 +00:00
use \Helpers\View as view ;
use \Mail\Mail ;
use \Order\Listener\OrderListener ;
use \Files\Output ;
2013-10-28 14:54:21 +00:00
class Order
{
private $db ;
private $config ;
protected $events = NULL ;
public $projectType = array (
2013-11-11 08:25:44 +00:00
" National Funding " => " National Funding " ,
" Thesis or Dissertation " => " Thesis or Dissertation " ,
" Others " => " Others " ,
2013-10-28 14:54:21 +00:00
); //申请中的项目类型
public $pdfData ;
function __construct ( $db = NULL )
{
if ( empty ( $db ))
{
$this -> db = \Zend_Registry :: get ( 'db' );
} else {
$this -> db = $db ;
}
$this -> config = \Zend_Registry :: get ( 'config' );
$Listener = new OrderListener ();
@ $this -> events () -> attachAggregate ( $Listener );
}
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 ;
}
/*************
status :
1 开始进入离线申请申请程序中
2 填写并提交离线申请表
3 邮寄离线申请表
4 收到离线申请表
5 处理离线申请表
10 : 离线申请完成?
- 1 : 取消了在线下载进程
**************/
//添加到数据篮
public function addOrder ( $uuid , $selection = NULL , $uid = 0 ){
if ( empty ( $uid ))
{
$uid = view :: User ( 'id' );
}
if ( ! is_numeric ( $uid ))
{
$uid = view :: User ( 'id' );
}
if ( ! 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 ))
{
2014-03-03 02:29:04 +00:00
return " Wrong parameter. " ;
2013-10-28 14:54:21 +00:00
}
$results = $this -> events () -> trigger ( 'submit' , $this , compact ( 'uuid' , 'uid' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
if ( empty ( $selection ))
{
$results = $this -> events () -> trigger ( 'checksource' , $this , compact ( 'uuid' ));
$data = $results -> bottom ();
if ( ! empty ( $data ))
{
return $data ;
}
if ( $this -> pushToDataorder ( $uuid , $uid ) === true )
{
return true ;
} else {
2014-03-03 02:29:04 +00:00
return " Errors, please retry. " ;
2013-10-28 14:54:21 +00:00
}
} else {
if ( $this -> pushToDataorder ( $uuid , $uid , $selection ) === true )
{
return true ;
} else {
2014-03-03 02:29:04 +00:00
return " Errors, please retry. " ;
2013-10-28 14:54:21 +00:00
}
}
} //addOrder
//放到数据篮中
public function pushToDataorder ( $uuid , $uid = 0 , $selection = NULL )
{
if ( empty ( $uid ))
{
$uid = view :: User ( 'id' );
}
if ( empty ( $selection ))
{
$sql = " insert into dataorder (uuid,ts_created,userid,status) values(?,now(),?,?) " ;
$rs = $this -> db -> query ( $sql , array ( $uuid , $uid , 1 ));
} else {
$sql = " insert into dataorder (uuid,ts_created,userid,status,selection) values(?,now(),?,?,?) " ;
$rs = $this -> db -> query ( $sql , array ( $uuid , $uid , 1 , $selection ));
}
if ( $rs )
{
return true ;
} else {
return false ;
}
} //pushToDataorder
//提交
public function apply ( $id , $uid = 0 )
{
if ( ! is_numeric ( $id ) || ! is_numeric ( $uid ))
{
2014-03-03 02:29:04 +00:00
return " Wrong parameter. " ;
2013-10-28 14:54:21 +00:00
}
if ( empty ( $uid ))
{
$uid = view :: User ( 'id' );
}
if ( $id == - 1 )
{
$sql = $this -> db -> quoteInto ( " update dataorder set status=2 where status=1 and userid=? " , $uid );
if ( $this -> db -> exec ( $sql ))
{
return true ;
} else {
2014-03-03 02:29:04 +00:00
return " Errors, please retry. " ;
2013-10-28 14:54:21 +00:00
}
}
if ( $id > 0 )
{
$sql = $this -> db -> quoteInto ( " update dataorder set status=2 where status=1 and userid= $uid and id=? " , $id );
if ( $this -> db -> exec ( $sql ))
{
return true ;
} else {
2014-03-03 02:29:04 +00:00
return " Errors, please retry. " ;
2013-10-28 14:54:21 +00:00
}
}
2014-03-03 02:29:04 +00:00
return " Wrong parameter. " ;
2013-10-28 14:54:21 +00:00
} //apply()
//删除
public function del ( $id , $uid = 0 )
{
if ( ! is_numeric ( $id ) || ! is_numeric ( $uid ))
{
2014-03-03 02:29:04 +00:00
return " Wrong parameter. " ;
2013-10-28 14:54:21 +00:00
}
if ( empty ( $uid ))
{
$uid = view :: User ( 'id' );
}
if ( $id > 0 )
{
$sql = $this -> db -> quoteInto ( " delete from dataorder where userid= $uid and status in (1,2) and id=? " , $id );
if ( $this -> db -> exec ( $sql ))
{
return true ;
} else {
2014-03-03 02:29:04 +00:00
return " Errors, please retry. " ;
2013-10-28 14:54:21 +00:00
}
}
2014-03-03 02:29:04 +00:00
return " Wrong parameter. " ;
2013-10-28 14:54:21 +00:00
}
//设置PDF上显示的内容 getOrderItemForPdf() 的返回结果
public function setPdfData ( $data )
{
$this -> pdfData = $data ;
}
//保存预览
public function SaveOrder ( $formData , $uid = 0 )
{
if ( empty ( $uid ) || ! is_numeric ( $uid ))
{
$uid = view :: User ( 'id' );
}
$results = $this -> events () -> trigger ( 'order.formcheck' , $this , compact ( 'formData' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
$results = $this -> events () -> trigger ( 'order.pdfNumCheck' , $this , compact ( 'uid' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
$results = $this -> events () -> trigger ( 'order.onUpdate' , $this , compact ( 'formData' , 'uid' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
$this -> pdfPrint ( $formData , $uid );
return true ;
} //SaveOrder
//生成并下载PDF
public function SubmitOrder ( $formData , $uid = 0 )
{
if ( empty ( $uid ) || ! is_numeric ( $uid ))
{
$uid = view :: User ( 'id' );
}
$results = $this -> events () -> trigger ( 'order.formcheck' , $this , compact ( 'formData' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
$results = $this -> events () -> trigger ( 'order.pdfNumCheck' , $this , compact ( 'uid' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
$pdf = $this -> pdfPrint ( $formData , $uid , true , true );
$returnid = true ;
$results = $this -> events () -> trigger ( 'order.onUpdate' , $this , compact ( 'formData' , 'uid' , 'returnid' ));
$oid = $results -> bottom ();
if ( ! is_numeric ( $oid ) || $oid < 1 )
{
2014-03-03 02:29:04 +00:00
return " Wrong parameter. " ;
2013-10-28 14:54:21 +00:00
}
$results = $this -> events () -> trigger ( 'order.onSubmited' , $this , compact ( 'formData' , 'uid' , 'oid' , 'pdf' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
$this -> pushPdfDownload ( $pdf -> pdflink );
return true ;
} //SubmitOrder()
public function pushPdfDownload ( $fn )
{
$output = new Output ();
$path = realpath ( $this -> config -> offline -> savepath );
$content = file_get_contents ( $path . DIRECTORY_SEPARATOR . $fn );
2014-03-03 02:29:04 +00:00
$output -> pushDownload ( $content , " Application.pdf " , 'pdf' );
2013-10-28 14:54:21 +00:00
exit ();
}
//生成pdf
public function pdfPrint ( $formData , $userid = 0 , $save = false , $returnpdf = false )
{
2014-03-03 07:50:27 +00:00
/* $sql = " SELECT m.title||'('||m.filesize::text||'MB)' as title FROM dataorder d
2013-10-28 14:54:21 +00:00
RIGHT JOIN heihemetadata m ON d . uuid = m . uuid
WHERE d . status = 2 AND d . userid = ? " ;
$list = $this -> db -> fetchAll ( $sql , array ( $userid ));
foreach ( $list as $i => $row ){
@ $formData [ 'heihelist' ] .= ( $i + 1 ) . " . " . $row [ 'title' ] . " ; " ;
2014-03-03 07:50:27 +00:00
} */
2013-10-28 14:54:21 +00:00
$sql = " SELECT m.title||'('||m.filesize::text||'MB)' as title FROM dataorder d
2014-03-03 07:50:27 +00:00
RIGHT JOIN en . normalmetadata m ON d . uuid = m . uuid WHERE d . status = 2 AND d . userid = ? " ;
2013-10-28 14:54:21 +00:00
$list = $this -> db -> fetchAll ( $sql , array ( $userid ));
foreach ( $list as $i => $row ){
@ $formData [ 'westdclist' ] .= ( $i + 1 ) . " . " . $row [ 'title' ] . " ; " ;
}
$pdf = new \ApplicantPDF ();
$pdf -> template = $this -> config -> offline -> template ;
$pdf -> heihetemplate = $this -> config -> offline -> heihetemplate ;
$formData [ 'project' ] .= '[' . $formData [ 'project_title' ] . ' | ' . $formData [ 'project_type' ] . ' | ' . $formData [ 'project_id' ] . ' | ' . $formData [ 'project_leader' ] . ']' ;
$pdf -> data = $formData ;
if ( isset ( $formData [ 'westdclist' ])) $pdf -> drawWestdc ();
2014-03-03 07:50:27 +00:00
//if (isset($formData['heihelist'])) $pdf->drawHeihe();
2013-10-28 14:54:21 +00:00
$pdf -> addRef ( $this -> pdfData );
$pdf -> addSelection ( $this -> pdfData );
2014-03-03 07:50:27 +00:00
//$pdf->addSecurity($this->config->offline->security);
2013-10-28 14:54:21 +00:00
if ( $save === false )
{
header ( " Content-Disposition: inline; filename=westdc-data-apply.pdf " );
header ( " Content-Type:application/pdf " );
//header("Content-Length: " . strlen($pdfstring));
echo $pdf -> Output ( 'westdc-data-apply.pdf' , 'S' );
} else {
//$fn = $formData['realname'].date('YmdHis').".pdf";
$fn = date ( 'YmdHis' ) . " .pdf " ;
$path = realpath ( $this -> config -> offline -> savepath );
$pdf -> pdflink = $fn ;
@ $pdf -> Output ( $path . DIRECTORY_SEPARATOR . $fn , 'F' );
}
if ( $returnpdf === true )
{
return $pdf ;
}
}
//获得要生成pdf的信息
public function getOrderItemForPdf ( $uid = 0 , $statu = 2 )
{
if ( empty ( $uid ))
{
$uid = view :: User ( 'id' );
}
$sql = " select m.title||'('||m.filesize::text||'MB)' as title,m.ts_published,date_part('year',doi.ts_published) as publish_year,m.citation,m.suppinfo,d.selection,
array_to_string ( ARRAY (
select r . reference from mdref mr left join reference r on mr . refid = r . id
where mr . reftype = 3 and mr . uuid = d . uuid order by mr . place ), '\n' :: text ) as reference ,
array_to_string ( array (
2013-11-11 08:25:44 +00:00
select fund . fund_type_en || ':' || fund . title_en || '(No: ' || fund . fund_id || ')'
2013-10-28 14:54:21 +00:00
from fund left join mdfund on fund . id = mdfund . fid where mdfund . uuid = d . uuid order by mdfund . place ), '\n' :: text ) as fund ,
doi . doi as datadoi , doi . authors , doi . publisher , doi . title as doititle , doi . author_en , doi . publisher_en , doi . title_en
2013-11-11 08:25:44 +00:00
from dataorder d right join en . metadata m on d . uuid = m . uuid left join datadoi doi on doi . uuid = d . uuid
2013-10-28 14:54:21 +00:00
where d . status = $statu and d . userid = ? order by d . ts_created desc
" ;
$rows = $this -> db -> fetchAll ( $sql , array ( $uid ));
return $rows ;
}
//确认申请
public function commitApplicationForm ( $offlineappid )
{
$user = view :: User ();
if ( $user === false )
{
return false ;
}
$uid = $user -> id ;
$results = $this -> events () -> trigger ( 'order.pdfNumCheck' , $this , compact ( 'uid' ));
$data = $results -> bottom ();
if ( $data !== true )
{
return $data ;
}
$sql = " update dataorder set status=3 where status=2 and userid= $uid and offlineappid= $offlineappid " ;
if ( $this -> db -> exec ( $sql ) < 0 )
{
2014-03-03 02:29:04 +00:00
return " Could not update the status of the your shopping cart, please retry. " ;
2013-10-28 14:54:21 +00:00
}
$sql = " UPDATE offlineapp SET status=3 WHERE id= $offlineappid " ;
if ( $this -> db -> exec ( $sql ) < 0 )
{
2014-03-03 02:29:04 +00:00
return " Error exist when update the status of the shopping card, please contact our service group by email. " ;
2013-10-28 14:54:21 +00:00
}
$data = array (
" user " => empty ( $user -> realname ) ? $user -> username : $user -> realname ,
" link " => view :: getHostLink () . '/admin/down/offlineapp/ac/view/offlineappid/' . $offlineappid
);
$mail = new Mail ();
$mail -> loadTemplate ( " offline-pdf-commited " , $data );
$mail -> setDefaultTo ();
$mail -> send ();
return true ;
} //确认申请
//service_type 选择
public function serviceTypeTest ( $type )
{
if ( empty ( $type ) || ! is_numeric ( $type ))
{
return false ;
}
switch ( $type )
{
case 0 :
2014-03-03 02:29:04 +00:00
return " This data is online. " ;
2013-10-28 14:54:21 +00:00
break ;
case 1 :
2014-03-03 02:29:04 +00:00
return " This data has subset. " ;
2013-10-28 14:54:21 +00:00
break ;
case 2 :
2014-03-03 02:29:04 +00:00
return " This data has multiple subsets. " ;
2013-10-28 14:54:21 +00:00
break ;
case 3 :
return " " ;
break ;
case 4 :
return " " ;
break ;
case 5 :
return " " ;
break ;
case 6 :
return " " ;
break ;
}
return false ;
} //serviceTypeTest
}