Ticket #190 增加了附件下载功能

This commit is contained in:
Li Jianxuan 2011-09-28 09:55:52 +00:00
parent 3e4e1351ef
commit d76c555ea1
1 changed files with 245 additions and 57 deletions

View File

@ -294,6 +294,7 @@ class DataController extends Zend_Controller_Action
*/
function categoryAction()
{
$page = $this->_request->getParam('page');
$code = (int)$this->_request->getParam('code');
$md=new CategoryCodeTable();
$db=$md->getAdapter();
@ -308,11 +309,12 @@ class DataController extends Zend_Controller_Action
$rows = $state->fetchAll();
$paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(10);
$paginator->setCurrentPageNumber($page);//($this->_getParam('page'));
$paginator->setItemCountPerPage($this->view->config->page->max);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator;
} else {
//提供全部分类列表
}
@ -1201,6 +1203,97 @@ class DataController extends Zend_Controller_Action
}
}
/*
*附件下载
*/
function attachAction(){
$uuid = $this->_request->getParam('uuid');
if(!empty($uuid))
{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
/*
$user = $auth->getIdentity();
$userid = $user->id;
$sql = "select d.* from dataorder d
left join users u on u.id=d.userid
left join metadata m on m.uuid=d.uuid
where (d.status=5 and d.userid='$userid' and d.uuid='$uuid') or u.usertype='administrator'";
$re= $this->db->query($sql);
$row=$re->fetch();
if(!empty($row['uuid']))
{
$this->messenger->addMessage('您没有权限下载该附件');
$this->_redirect("/data");
}
else
{*/
//由于涉及多个文件下载所以将附件添加到zip压缩文件再输出
$sql="select * from mdattach m left join attachments a on m.id=a.id where m.uuid='$uuid'";
$re=$this->db->query($sql);
$atts = $re->fetchAll();
$zip = new ZipArchive();
$url=Zend_Registry::get('upload')."tmp/attachments_$uuid.zip";//创建临时文件
$opened=$zip->open($url, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
if( $opened !== true ){
die("cannot open {$url} for writing.");
}
$updates = array(); //统计被下载的附件ID
foreach ($atts as $k=>$v)
{
if (is_file(Zend_Registry::get('upload').$v['filename'])) {
$updates[]=$v['id'];
$zip->addFile(Zend_Registry::get('upload').$v['filename'],'/'.basename($v['filename']));
}
}
$zip->close();
//更新统计
$ids=join(',',$updates);
$sql = "update attachments set downtimes=downtimes+1 where id in ($ids)";
@$this->db->exec($sql);
//输出下载
$content=file_get_contents($url);
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->setHeader('Content-Type', 'application/octet-stream')
->setHeader('Content-Disposition','attachment; filename="'.basename($url).'"')
->setHeader('Content-Length', strlen($content))
->setHeader('Content-Type','application/force-download')
->setHeader('Content-Type','application/download')
->setHeader('Content-Type','application/zip')
->setHeader('Content-Description','File Transfer')
->setHeader('Content-Transfer-Encoding','binary')
->setHeader('Expires',0)
->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
->setHeader('Pragma','public')
->setBody($content);
// }
}//end if
else
{
$this->messenger->addMessage('您没有权限下载该附件');
$this->_redirect("/data");
}//未登陆
}//end if
else
{
$this->messenger->addMessage('您没有权限下载该附件');
$this->_redirect("/data");
}//无权限
}
/*
* 西部计划项目及其数据产出
*/
@ -1325,6 +1418,101 @@ class DataController extends Zend_Controller_Action
->setHeader('Pragma','public')
->setBody($content);
}
/*
*附件下载
*/
function attachAction(){
$uuid = $this->_request->getParam('uuid');
if(!empty($uuid))
{
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
/*
$user = $auth->getIdentity();
$userid = $user->id;
$sql = "select d.* from dataorder d
left join users u on u.id=d.userid
left join metadata m on m.uuid=d.uuid
where (d.status=5 and d.userid='$userid' and d.uuid='$uuid') or u.usertype='administrator'";
$re= $this->db->query($sql);
$row=$re->fetch();
if(!empty($row['uuid']))
{
$this->messenger->addMessage('您没有权限下载该附件');
$this->_redirect("/data");
}
else
{*/
//由于涉及多个文件下载所以将附件添加到zip压缩文件再输出
$sql="select * from mdattach m left join attachments a on m.id=a.id where m.uuid='$uuid'";
$re=$this->db->query($sql);
$atts = $re->fetchAll();
$zip = new ZipArchive();
$url=Zend_Registry::get('upload')."tmp/attachments_$uuid.zip";//创建临时文件
$opened=$zip->open($url, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
if( $opened !== true ){
die("cannot open {$url} for writing.");
}
$updates = array(); //统计被下载的附件ID
foreach ($atts as $k=>$v)
{
if (is_file(Zend_Registry::get('upload').$v['filename'])) {
$updates[]=$v['id'];
$zip->addFile(Zend_Registry::get('upload').$v['filename'],'/'.basename($v['filename']));
}
}
$zip->close();
//更新统计
$ids=join(',',$updates);
$sql = "update attachments set downtimes=downtimes+1 where id in ($ids)";
@$this->db->exec($sql);
//输出下载
$content=file_get_contents($url);
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->setHeader('Content-Type', 'application/octet-stream')
->setHeader('Content-Disposition','attachment; filename="'.basename($url).'"')
->setHeader('Content-Length', strlen($content))
->setHeader('Content-Type','application/force-download')
->setHeader('Content-Type','application/download')
->setHeader('Content-Type','application/zip')
->setHeader('Content-Description','File Transfer')
->setHeader('Content-Transfer-Encoding','binary')
->setHeader('Expires',0)
->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
->setHeader('Pragma','public')
->setBody($content);
// }
}//end if
else
{
$this->messenger->addMessage('您没有权限下载该附件');
$this->_redirect("/data");
}//未登陆
}//end if
else
{
$this->messenger->addMessage('您没有权限下载该附件');
$this->_redirect("/data");
}//无权限
}
/**
* parse the wiki syntax used to render tables, code modified from mediawiki
*