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() function categoryAction()
{ {
$page = $this->_request->getParam('page');
$code = (int)$this->_request->getParam('code'); $code = (int)$this->_request->getParam('code');
$md=new CategoryCodeTable(); $md=new CategoryCodeTable();
$db=$md->getAdapter(); $db=$md->getAdapter();
@ -306,13 +307,14 @@ class DataController extends Zend_Controller_Action
$sql='select m.* from metadata m,category c where m.id=c.id and c.code=? order by m.title'; $sql='select m.* from metadata m,category c where m.id=c.id and c.code=? order by m.title';
$state=$db->query($sql,array($code)); $state=$db->query($sql,array($code));
$rows = $state->fetchAll(); $rows = $state->fetchAll();
$paginator = Zend_Paginator::factory($rows); $paginator = Zend_Paginator::factory($rows);
$paginator->setCurrentPageNumber($this->_getParam('page')); $paginator->setCurrentPageNumber($page);//($this->_getParam('page'));
$paginator->setItemCountPerPage(10); $paginator->setItemCountPerPage($this->view->config->page->max);
$paginator->setView($this->view); $paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml');
$this->view->paginator=$paginator; $this->view->paginator=$paginator;
} else { } else {
//提供全部分类列表 //提供全部分类列表
} }
@ -1200,6 +1202,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");
}//无权限
}
/* /*
* 西部计划项目及其数据产出 * 西部计划项目及其数据产出
@ -1256,63 +1349,63 @@ class DataController extends Zend_Controller_Action
print "<br>"; print "<br>";
} }
} }
/* /*
* 转换元数据为WORD DOC格式 * 转换元数据为WORD DOC格式
*/ */
public function docAction() public function docAction()
{ {
$this->_helper->layout->disableLayout(); $this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(); $this->_helper->viewRenderer->setNoRender();
$ua = $_SERVER["HTTP_USER_AGENT"]; $ua = $_SERVER["HTTP_USER_AGENT"];
$uuid=$this->_request->getParam('uuid'); $uuid=$this->_request->getParam('uuid');
if (!empty($uuid)) if (!empty($uuid))
{ {
$sql="select x.data,m.title,m.description,g.id from xml x left join metadata m on m.id=x.id left join geonetworkmetadata g on g.uuid=m.uuid where m.uuid=".$this->db->quote($uuid); $sql="select x.data,m.title,m.description,g.id from xml x left join metadata m on m.id=x.id left join geonetworkmetadata g on g.uuid=m.uuid where m.uuid=".$this->db->quote($uuid);
$row=$this->db->fetchRow($sql); $row=$this->db->fetchRow($sql);
} }
$dom = new DOMDocument(); $dom = new DOMDocument();
$dom->loadXML($row['data']); $dom->loadXML($row['data']);
//提前对表格进行预处理 //提前对表格进行预处理
$abs=$this->parseTable($this->view->escape($row["description"])); $abs=$this->parseTable($this->view->escape($row["description"]));
//处理外部链接 //处理外部链接
$abs=preg_replace('/\[\s*(http:\/\/.+?)\s+(.*?)\]/m','<a href="$1">$2</a>',$abs); $abs=preg_replace('/\[\s*(http:\/\/.+?)\s+(.*?)\]/m','<a href="$1">$2</a>',$abs);
$abs=str_replace(array("\r\n", "\n", "\r"),'</p><p>',$abs); $abs=str_replace(array("\r\n", "\n", "\r"),'</p><p>',$abs);
$abs=str_replace("'","&apos;",$abs);//not needed? $abs=str_replace("'","&apos;",$abs);//not needed?
$id = $row['id']; $id = $row['id'];
$thumburl=sprintf('%05d',floor(($id+0.1)/100)*100).'-'.sprintf('%05d',ceil(($id+0.1)/100)*100-1)."/".$id; $thumburl=sprintf('%05d',floor(($id+0.1)/100)*100).'-'.sprintf('%05d',ceil(($id+0.1)/100)*100-1)."/".$id;
$xslt = new XSLTProcessor(); $xslt = new XSLTProcessor();
$xslt->registerPHPFunctions(); $xslt->registerPHPFunctions();
$xslt->setParameter('', 'thumburl', $thumburl); $xslt->setParameter('', 'thumburl', $thumburl);
$xslt->setParameter('', 'abstract', $abs); $xslt->setParameter('', 'abstract', $abs);
$XSL = new DOMDocument(); $XSL = new DOMDocument();
$XSL->load( '../data/doc.xsl', LIBXML_NOCDATA); $XSL->load( '../data/doc.xsl', LIBXML_NOCDATA);
$xslt->importStylesheet($XSL); $xslt->importStylesheet($XSL);
$content='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> $content='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">'; <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
$content.='<title>'.$row['title'].'</title>'; $content.='<title>'.$row['title'].'</title>';
$content.='<style> $content.='<style>
body{MARGIN-RIGHT: auto;MARGIN-LEFT: auto;font-size:14px;line-height:22px;} body{MARGIN-RIGHT: auto;MARGIN-LEFT: auto;font-size:14px;line-height:22px;}
span{font-size:14px;} span{font-size:14px;}
div{clear: both;margin: 0 auto;width:100%;vertical-align:baseline;} div{clear: both;margin: 0 auto;width:100%;vertical-align:baseline;}
#uuid{text-align: right;} #uuid{text-align: right;}
h3{font-size: 18px;} h3{font-size: 18px;}
img {border:0 none;} img {border:0 none;}
a, a:visited {color:Blue;text-decoration:none;} a, a:visited {color:Blue;text-decoration:none;}
a:hover{text-decoration:underline;} a:hover{text-decoration:underline;}
ul{list-style: none;margin: 0;} ul{list-style: none;margin: 0;}
ul li{list-style:none;} ul li{list-style:none;}
#etitle{font-size:16px;margin-left:10px;} #etitle{font-size:16px;margin-left:10px;}
span{font-weight:bolder;} span{font-weight:bolder;}
#content{padding:5px 0 10px 0;border:1px solid #BF5008;} #content{padding:5px 0 10px 0;border:1px solid #BF5008;}
#divFooter {background-color:#BF5008;color:White;font-size:12px;padding:5px 15px;} #divFooter {background-color:#BF5008;color:White;font-size:12px;padding:5px 15px;}
#divFooter a:link, #divFooter a:visited, #divFooter a:active {color:White;font-family:Arial,Serif;text-decoration:none;} #divFooter a:link, #divFooter a:visited, #divFooter a:active {color:White;font-family:Arial,Serif;text-decoration:none;}
</style> </style>
</head> </head>
<body>'; <body>';
$content.=$xslt->transformToXML($dom); $content.=$xslt->transformToXML($dom);
$content.="</body></html>"; $content.="</body></html>";
$this->getResponse()->setHeader('Content-Type', 'application/vnd.ms-doc') $this->getResponse()->setHeader('Content-Type', 'application/vnd.ms-doc')
->setHeader('Content-Disposition','attachment; filename="'.$row['title'].'.doc"') ->setHeader('Content-Disposition','attachment; filename="'.$row['title'].'.doc"')
->setHeader('Content-Length', strlen($content)) ->setHeader('Content-Length', strlen($content))
@ -1322,9 +1415,104 @@ class DataController extends Zend_Controller_Action
->setHeader('Content-Transfer-Encoding','binary') ->setHeader('Content-Transfer-Encoding','binary')
->setHeader('Expires',0) ->setHeader('Expires',0)
->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0') ->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0')
->setHeader('Pragma','public') ->setHeader('Pragma','public')
->setBody($content); ->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 * parse the wiki syntax used to render tables, code modified from mediawiki
* *