diff --git a/application/default/controllers/DataController.php b/application/default/controllers/DataController.php index 7d2fe193..0fc20009 100755 --- a/application/default/controllers/DataController.php +++ b/application/default/controllers/DataController.php @@ -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(); @@ -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'; $state=$db->query($sql,array($code)); $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 { //提供全部分类列表 } @@ -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 "
"; } - } - /* - * 转换元数据为WORD DOC格式 + } + /* + * 转换元数据为WORD DOC格式 */ - public function docAction() - { + public function docAction() + { $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - $ua = $_SERVER["HTTP_USER_AGENT"]; - $uuid=$this->_request->getParam('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); - $row=$this->db->fetchRow($sql); - } - - $dom = new DOMDocument(); - $dom->loadXML($row['data']); + $this->_helper->viewRenderer->setNoRender(); + $ua = $_SERVER["HTTP_USER_AGENT"]; + $uuid=$this->_request->getParam('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); + $row=$this->db->fetchRow($sql); + } + + $dom = new DOMDocument(); + $dom->loadXML($row['data']); //提前对表格进行预处理 $abs=$this->parseTable($this->view->escape($row["description"])); //处理外部链接 - $abs=preg_replace('/\[\s*(http:\/\/.+?)\s+(.*?)\]/m','$2',$abs); + $abs=preg_replace('/\[\s*(http:\/\/.+?)\s+(.*?)\]/m','$2',$abs); $abs=str_replace(array("\r\n", "\n", "\r"),'

',$abs); - $abs=str_replace("'","'",$abs);//not needed? - $id = $row['id']; - $thumburl=sprintf('%05d',floor(($id+0.1)/100)*100).'-'.sprintf('%05d',ceil(($id+0.1)/100)*100-1)."/".$id; - $xslt = new XSLTProcessor(); - $xslt->registerPHPFunctions(); - $xslt->setParameter('', 'thumburl', $thumburl); - $xslt->setParameter('', 'abstract', $abs); - $XSL = new DOMDocument(); - $XSL->load( '../data/doc.xsl', LIBXML_NOCDATA); - $xslt->importStylesheet($XSL); - $content=' -'; - $content.=''.$row['title'].''; - $content.=' - - '; - $content.=$xslt->transformToXML($dom); - $content.=""; + $abs=str_replace("'","'",$abs);//not needed? + $id = $row['id']; + $thumburl=sprintf('%05d',floor(($id+0.1)/100)*100).'-'.sprintf('%05d',ceil(($id+0.1)/100)*100-1)."/".$id; + $xslt = new XSLTProcessor(); + $xslt->registerPHPFunctions(); + $xslt->setParameter('', 'thumburl', $thumburl); + $xslt->setParameter('', 'abstract', $abs); + $XSL = new DOMDocument(); + $XSL->load( '../data/doc.xsl', LIBXML_NOCDATA); + $xslt->importStylesheet($XSL); + $content=' +'; + $content.=''.$row['title'].''; + $content.=' + + '; + $content.=$xslt->transformToXML($dom); + $content.=""; $this->getResponse()->setHeader('Content-Type', 'application/vnd.ms-doc') ->setHeader('Content-Disposition','attachment; filename="'.$row['title'].'.doc"') ->setHeader('Content-Length', strlen($content)) @@ -1322,9 +1415,104 @@ class DataController extends Zend_Controller_Action ->setHeader('Content-Transfer-Encoding','binary') ->setHeader('Expires',0) ->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0') - ->setHeader('Pragma','public') - ->setBody($content); - } + ->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 *