westdc-zf1/application/module/Order/Mount/PdfOperate.php

178 lines
4.6 KiB
PHP
Raw Normal View History

2013-10-28 14:54:21 +00:00
<?php
namespace Order\Mount;
2013-10-28 14:54:21 +00:00
use \Helpers\View as view;
use \Helpers\dbh;
use \Mail\Mail;
2013-10-28 14:54:21 +00:00
//事件中存在的操作
class PdfOperate implements \Order\Listener\PdfEvents
2013-10-28 14:54:21 +00:00
{
private $db;
private $config;
//!!!!!!important!!!!!
//不同项目使用时是否要修改此项??
public $tbl_metadata = "en.normalmetadata";
2013-10-28 14:54:21 +00:00
public $tbl_dataorder = "dataorder";
public $tbl_offlineapp = "offlineapp";
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 updateUserInfo(\Zend_EventManager_Event $e)
{
//根据pdflink字段以判断是否已经提交
//在数据库中创建rules在更新offlineapp表时同时更新users表中对应的信息
$formData = $e->getParam('formData');
$uid = $e->getParam('uid');
$returnid = $e->getParam('returnid');
try{
$sql="SELECT id FROM ".$this->tbl_offlineapp."
WHERE userid=? AND (pdflink IS null OR pdflink='') AND (ts_approved IS null)";
$row = $this->db->fetchRow($sql,array($uid));
$dbh = new dbh($this->db);
$formData['username'] = $formData['realname'];
$formData['userid'] = $uid;
unset($formData['realname']);
unset($formData['save']);
unset($formData['submit']);
unset($formData['id']);
if ($row) {
$s = $dbh->update($this->tbl_offlineapp,$formData," id={$row['id']} ");
$this->updateToUserTable($formData,$row['id']);
if(empty($returnid))
{
return $s;
}else{
return $row['id'];
}
} else {
$id = $dbh->insert($this->tbl_offlineapp,$formData,true);
$this->updateToUserTable($formData,$uid);
if(empty($returnid))
{
if($id > 0)
{
return true;
}else{
return "保存失败,请重试";
}
}else{
return $id;
}
}
}catch(Exception $e)
{
return $e->getMessage();
}
return true;
}
//将项目信息更新到用户表
public function updateToUserTable($formData,$id)
{
$data = array(
'address' => $formData['address'],
'phone' => $formData['phone'],
'postcode' => $formData['postcode'],
'unit' => $formData['unit'],
'project_id' => $formData['project_id'],
'project_type' => $formData['project_type'],
'project_title' => $formData['project_title'],
'project_leader' => $formData['project_leader'],
'project' => $formData['project']
);
$dbh = new dbh($this->db);
@$dbh->update('users',$data," id=$id ");
}
//申请成功提交之后
public function orderSubmited(\Zend_EventManager_Event $e)
{
$formData = $e->getParam('formData');
$uid = (int)$e->getParam('uid');
$oid = (int)$e->getParam('oid');
$pdf = $e->getParam('pdf');
try{
$sql="update dataorder set offlineappid=$oid where status=2 and userid=$uid";
if($this->db->exec($sql) < 0)
{
return "数据篮状态更新失败,请重试";
}
$sql = "UPDATE offlineapp SET pdflink='".$pdf->pdflink."',status=2 WHERE id=$oid ";
if($this->db->exec($sql) < 0)
{
return "数据篮状态更新有误,请联系管理员";
}
$this->sendEmail($formData,$pdf);
}catch(Exception $e)
{
return $e->getMessage();
}
return true;
}
//检查生成pdf的数量
public function checkPdfOrderNum(\Zend_EventManager_Event $e)
{
$uid = (int)$e->getParam('uid');
if(empty($uid))
{
$uid = view::User('id');
}
$sql = "select count(*) as datacount from {$this->tbl_dataorder} where (ts_approved is null) and userid=? and status=2 AND offlineappid=-1";
$rs = $this->db->fetchRow($this->db->quoteInto($sql,$uid));
$inorder = $rs['datacount'];
if($inorder <= $this->config->download->max)
{
return true;
}else{
return "本次订单将使申请数量超过限制所以无法生成PDF请在数据篮-待提交订单中移除相应数据确保待提交的数据申请条数小于等于5条然后再重新生成。";
}
}
//发送邮件通知
public function sendEmail($formData,$pdf)
{
$data = array(
"user"=>$formData['realname'],
"datalist"=>str_replace(";","\n",$formData['datalist']),
"orderlink"=> view::getHostLink() . '/data/order/ac/offline2'
);
$mail = new Mail();
$mail->loadTemplate("offline-email",$data);
$mail->addTo($formData['email'],$formData['realname']);
$attach = $mail->mail->createAttachment($pdf->Output('applicant','S'));
$attach->filename = '数据申请-'.$formData['realname'].'.pdf';
$mail->send();
}
}