From 66c37925dd312a6c4485d2ce1f5725e88066ec33 Mon Sep 17 00:00:00 2001 From: Li Jianxuan Date: Tue, 22 Jul 2014 03:33:26 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=97=B6=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controllers/DataController.php | 5730 ++++++++--------- 1 file changed, 2865 insertions(+), 2865 deletions(-) diff --git a/application/admin/controllers/DataController.php b/application/admin/controllers/DataController.php index e199295d..a1a9f384 100755 --- a/application/admin/controllers/DataController.php +++ b/application/admin/controllers/DataController.php @@ -1,2865 +1,2865 @@ -db=Zend_Registry::get('db'); - $this->view->config = Zend_Registry::get('config'); - $this->messenger=$this->_helper->getHelper('FlashMessenger'); - $this->view->messages = $this->messenger->getMessages(); - $this->debug = 0; //1:debug, 0:release - $this->debug_email='wangliangxu@lzb.ac.cn'; - $this->view->theme = new Theme(); - $this->_helper->layout->setLayout('administry'); - } - function postDispatch() - { - $this->view->messages = $this->messenger->getMessages(); - } - - public function indexAction() - { - $statistics = new Metadata\Statistics; - $this->view->total = $statistics->total(); - $this->view->review_accept = $statistics->review("accept"); - $this->view->review_finish = $statistics->review('finish'); - $this->view->visual = $statistics->visual(); - - if(view::isXmlHttpRequest($this)) - { - $this->jsonexit($statistics->getMetadataCountByDay(true)); - } - //其他连接 - } - - /* - * - * mdAction() - * 元数据管理 - * - */ - function mdAction() - { - $delete=$this->_getParam('delete'); - $down=(int)$this->_getParam('down'); - $search = $this->_getParam('search'); - $keyword = $this->_getParam('keyword'); - $att=$this->_getParam('att'); - $attupdate = $this->_getParam('attupdate'); - - $submd=$this->view->config->sub->metadata; - - if ($delete) - { - $sql="delete from metadata where uuid=? and uuid in (select uuid from ".$this->view->config->sub->meatadata.")"; - try { - $this->db->query("delete from mdstatus where uuid=? and uuid in (select uuid from ".$this->view->config->sub->meatadata.")",array($delete)); - $this->db->query("delete from mdauthor where uuid=? and uuid in (select uuid from ".$this->view->config->sub->meatadata.")",array($delete)); - $this->db->query($sql,array($delete)); - $this->messenger->addMessage('提示信息:您已经成功删除该数据。'); - $search=new Search(); - $search->del($delete,'uuid'); - } catch (Exception $e) { - $this->messenger->addMessage($e->getMessage()); - } - $this->_redirect("/admin/data/md"); - }//删除 - - elseif($att>0){ - $submit=$this->_request->getParam('submit'); - $uuid=$this->_request->getParam('uuid'); - $atts=$this->_request->getParam('ids'); - $addatts=$this->_request->getParam('addatts'); - - $this->view->id = $att; - $this->view->uuid = $uuid; - - if(!empty($addatts)) - { - - if(empty($submit)) - { - $sql = "select title from metadata where uuid='$uuid'"; - $re = $this->db->query($sql); - $rows = $re->fetch(); - - - $this->view->id = $att; - $this->view->uuid = $uuid; - $this->view->mdtitle = $rows['title']; - $this->view->thisatt = $rows; - - $this->_redirect("/admin/data/attachments/uuid/$uuid/mdtitle/{$rows['title']}"); - } - else - { - if(!empty($uuid)) - { - foreach($atts as $v) - { - $sql = "insert into mdattach (uuid,id) values ('$uuid','$v')"; - try{ - $this->db->exec($sql); - $this->messenger->addMessage('成功添加附件:'.$v); - }catch (Exception $e) - { - $this->messenger->addMessage('添加附件失败:'.$v); - } - } - $this->_redirect("/admin/data/md/att/1/uuid/$uuid"); - } - } - }//empty($addatts) - else - { - if(!empty($uuid)) - { - - $sql = "select m.*,a.*,d.title from mdattach m - left join attachments a on m.id = a.id - left join metadata d on m.uuid=d.uuid where m.uuid='$uuid'"; - $re = $this->db->query($sql); - - $rows = $re->fetchAll(); - - $sql = "select title from metadata where uuid='$uuid'"; - $re = $this->db->query($sql); - $title = $re->fetch(); - - $this->view->atts=$rows; - $this->view->mdtitle = $title['title']; - - $this->_helper->viewRenderer('attmanager'); - } - - - } - - }//编辑附件 - - /* - * 输出打包下载的xml文件 - * - * 文件量大时可能出现超时,需要修改超时时间为无限 - */ - elseif ($down) { - - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - //临时zip文件名 - $tmpname="dataxml"; - - //xml文件存放的缓存目录 - $dirName = '../data/import/'; - - //查询需要创建的文件 - $sql = "SELECT md.title,md.uuid,x.* from xml x - LEFT JOIN $submd md ON md.id=x.id"; - - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); //将结果储存,但不使用 - - //创建zip文件,创建成功后再使用查询结果 - $zip = new ZipArchive(); - $url = tempnam($this->config->temp->path,$tmpname);//创建临时文件 - - if( $zip->open($url, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true ) - { - throw new Exception("cannot open {$url} for writing."); - } - - foreach($rows as $k=>$v) - { - - $filename = $dirName.$v['uuid'].'.xml'; - - //创建xml文件 - $handle = fopen($filename,"w"); - fwrite($handle,$v['data']); - fclose($handle); - - //添加到zip文件 - //zip localname 直接使用UUID作为文件名 - $zip->addFile($filename,$v['uuid'].".xml"); - - } - $zip->close(); - - //zip文件创建完成后删除服务器上的缓存文件,防止发生冗余 - foreach($rows as $k=>$v) - { - $filename = $dirName.$v['uuid'].'.xml'; - unlink($filename); - } - - //输出下载 - $content=file_get_contents($url); - $this->getResponse()->setHeader('Content-Type', 'application/octet-stream') - ->setHeader('Content-Disposition','attachment; filename="dataxml.zip"') - ->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); - }//down - else if($search){ - - if(!empty($keyword)) - { - $sql = "select m.*,md.viewed,g.id as gid, ds.id as datasetid from $submd m - left join mdstat md on m.uuid=md.uuid - left join geonetworkmetadata g on g.uuid=m.uuid - left join dataset ds on m.uuid=ds.uuid - where m.title like '%$keyword%' - order by m.id desc - "; - - $re=$this->db->query($sql); - $row=$re->fetchAll(); - $paginator = Zend_Paginator::factory($row); - $paginator->setCurrentPageNumber($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; - $this->messenger->addMessage($keyword.' 的搜索结果'); - } - else - { - $this->messenger->addMessage('请输入搜索关键字'); - $this->_redirect("/admin/data/md"); - } - - }//search - else{ - - $sql = "SELECT md.*,s.viewed,g.id as gid,st.status as mdstatus,ds.id as datasetid FROM $submd md - LEFT JOIN mdstat s ON md.uuid=s.uuid - LEFT JOIN geonetworkmetadata g ON g.uuid=md.uuid - LEFT JOIN mdstatus st ON md.uuid=st.uuid - LEFT JOIN dataset ds ON md.uuid=ds.uuid - ORDER BY md.id DESC"; - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage($this->view->config->page->max); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); - $this->view->paginator=$paginator; - } - - }//mdAction 元数据管理 - - /* - * datasetAction() - * 数据路径:即数据的物理主目录 - * - */ - function datasetAction() - { - - $ac = $this->_request->getParam('ac'); - - if($ac == "getdataset") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer('md-dataset'); - - $uuid = $this->_request->getParam('uuid'); - $sql = "SELECT * FROM dataset WHERE uuid=?"; - $sth = $this->db->prepare($sql); - $sth ->execute(array($uuid)); - $row = $sth->fetch(); - - $this->view->dataset = $row; - $this->view->uuid = $uuid; - } - - else if($ac == "update") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $uuid = $this->_request->getParam('uuid'); - $host = $this->_getParam('host'); - $path = $this->_getParam('path'); - - $sql = "UPDATE dataset SET host=?,path=? WHERE uuid=?"; - $sth = $this->db->prepare($sql); - $ds = $sth ->execute(array($host,$path,$uuid)); - if ($host=='ftp1.westgis.ac.cn') - { - file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); - } else if ($host=='ftp.sanjiangyuan.org.cn') - { - file_get_contents("http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); - } - - if($ds) - { - $data = array("ok"=>1); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>"处理中出现错误"); - $this->jsonexit($data); - return true; - } - } - - else if($ac == "add") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $uuid = $this->_request->getParam('uuid'); - $host = $this->_getParam('host'); - $path = $this->_getParam('path'); - - $sql = "SELECT * FROM dataset WHERE uuid=?"; - $sth = $this->db->prepare($sql); - $sth ->execute(array($uuid)); - $row = $sth->fetch(); - if(!empty($row['id'])) - { - $data = array("error"=>"该数据已经有存档信息,不能重复添加"); - $this->jsonexit($data); - return true; - } - - $sql = "INSERT INTO dataset (uuid,host,path) VALUES (?,?,?)"; - $sth = $this->db->prepare($sql); - $ds = $sth ->execute(array($uuid,$host,$path)); - if ($host=='ftp1.westgis.ac.cn') - { - file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); - } else if ($host=='ftp.sanjiangyuan.org.cn') - { - file_get_contents("http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); - } - - if($ds) - { - $data = array("ok"=>1); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>"处理中出现错误"); - $this->jsonexit($data); - return true; - } - } - else if ($ac=="import") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $uuid = $this->_request->getParam('uuid'); - $sql = "SELECT * FROM dataset WHERE uuid=?"; - $sth = $this->db->prepare($sql); - $sth ->execute(array($uuid)); - $row = $sth->fetch(); - - if ($row['host']=='ftp1.westgis.ac.cn') - { - file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); - echo '

数据目录成功导入!

'; - } else if ($row['host']=='ftp.sanjiangyuan.org.cn') - { - file_get_contents("http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); - echo '

数据目录成功导入!

'; - } else { - echo '

数据目录未导入!

'; - } - } - - }//datasetAction存档管理 - - /* - * commentAction() - * 反馈管理 - * ALTER TABLE comments ADD COLUMN reply integer NOT NULL DEFAULT 0; - */ - function commentAction() - { - $delete=(int)$this->_getParam('delete'); - $uuid = $this->_getParam('uuid'); - $reply = $this->_getParam('reply'); - $replylist = $this->_getParam('replylist'); - $delreply = $this->_getParam('delreply'); - $submd=$this->view->config->sub->metadata; - - if ($delete) - { - $sql="delete from comments where id=? and uuid in (select uuid from $submd)"; - try { - $this->db->query($sql,array($delete)); - $this->messenger->addMessage('提示信息:您已经成功删除该评论。'); - } catch (Exception $e) { - $this->messenger->addMessage($e->getMessage()); - } - $this->_redirect("/admin/data/comment"); - } - - if($reply) - { - - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $body = $this->_getParam('content'); - if(empty($body)) - { - $data = array('error'=>"请输入回复内容"); - $this->jsonexit($data); - return true; - } - - $auth = Zend_Auth::getInstance(); - if($auth->hasIdentity()) - { - $user = $auth->getIdentity(); - $uid = $user->id; - $email = $user->email; - } - - $ipaddr = $_SERVER['REMOTE_ADDR']; - - $sql = "INSERT INTO comments (uuid,author,reply,userid,content,email,ip) VALUES (?,?,?,?,?,?,?)"; - $sth = $this->db->prepare($sql); - $rs = $sth->execute(array($uuid,$user->username,$reply,$uid,$body,$email,$ipaddr)); - - if($rs) - { - $sql="select c.author,c.email,c.uuid from comments c where c.id=?"; - $sth=$this->db->prepare($sql); - $sth->execute(array($reply)); - $row=$sth->fetch(); - - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"comment-admin-reply",array( - 'user' => $row['author'], - 'uuid' => $row['uuid'], - 'email'=> $row['email'] - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - $mail->addTo($row['email']); - $mail->addCc($this->view->config->service->email); - @$mail->send(); - - $data = array('status'=>1,'msg'=>'回复成功!'); - $this->jsonexit($data); - return true; - }else{ - $data = array('error'=>"回复失败,请重试"); - $this->jsonexit($data); - return true; - } - return true; - } - - if($uuid) - { - $sql = "SELECT c.*,md.title,md.uuid FROM comments c - LEFT JOIN $submd md ON md.uuid=c.uuid - WHERE c.uuid=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($uuid)); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage($this->view->config->page->max); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); - $this->view->paginator=$paginator; - - $sql = "SELECT title FROM metadata WHERE uuid=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($uuid)); - $row = $sth->fetch(); - - $this->view->title = $row['title']; - - return true; - } - - if($replylist) - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $sql = "SELECT cr.id,cr.content as body,cr.reply,u.username,cr.ts_created FROM comments cr - LEFT JOIN users u ON cr.userid=u.id WHERE cr.reply=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($replylist)); - $rows = $sth->fetchAll(); - - $this->jsonexit($rows); - return true; - - } - - if($delreply) - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $sql = "DELETE FROM comments WHERE id=?"; - $sth = $this->db->prepare($sql); - $rs = $sth->execute(array($delreply)); - - if($rs) - { - $this->jsonexit( - array('deleted'=>1) - ); - return true; - }else{ - $this->jsonexit( - array('error'=> '处理中出现错误,请重新尝试') - ); - return true; - } - - - } - - - $sql = "SELECT cm.*,md.title,(SELECT count(id) as counts FROM comments cms WHERE cms.reply=cm.id AND cms.reply!=0) as reply_count FROM comments cm - right JOIN $submd md ON md.uuid=cm.uuid - WHERE cm.reply=0 - ORDER BY cm.ts_created DESC,cm.id DESC"; - - $sth = $this->db->query($sql); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage($this->view->config->page->max); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); - $this->view->paginator=$paginator; - - }//comment - - - - - /* - * 删除元数据,删除前有确认 - */ - function deleteAction() - { - if ($this->_request->isPost()) { - $id = (int)$this->_request->getPost('id'); - $del = $this->_request->getPost('del'); - if ($del == 'Yes' && $id > 0) { - $md = new MetadataTable(); - $where = 'id = ' . $id; - $md->delete($where); - } - $this->_redirect('/data'); - } else { - $id = (int)$this->_request->getParam('id'); - if ($id > 0) { - $mdt = new MetadataTable(); - $this->view->md = $mdt->fetchRow('id='.$id); - } - } - } - - function sourceAction() - { - $do = $this->_request->getParam('do'); - $uuid = $this->_request->getParam('uuid'); - $id = $this->_request->getParam('id'); - $q = $this->_request->getParam('q'); - $search = $this->_request->getParam('search'); - - if ($do == 'add') { - - $redirect = "/admin/data/source/"; - - if(!empty($_POST['submit'])) - { - try{ - $title = $this->_request->getParam('title'); - $uuid = $this->_request->getParam('uuid'); - $code = $this->_request->getParam('code'); - $description = $this->_request->getParam('description'); - $has_pages = $this->_request->getParam('has_pages'); - $has_agreement = $this->_request->getParam('has_agreement'); - - $data = array( - 'title' => $title, - 'uuid' => $uuid, - 'code' => $code, - 'description' => $description, - 'has_pages' => $has_pages, - 'has_agreement' => $has_agreement - ); - - if($this->db->insert('source',$data)) - { - $this->messenger->addMessage('添加来源信息成功'); - $this->_redirect($redirect); - } - }catch(Exception $e){ - $this->messenger->addMessage('添加来源信息失败:'.$e->getMessage()); - $this->_redirect('/admin/data/source/do/add'); - } - - } - - $this->_helper->viewRenderer('sourceadd'); - }// 添加项目来源 - - else if($do == 'edit' && !empty($id)) - { - - if(!empty($_POST['submit'])) - { - try{ - $title = $this->_request->getParam('title'); - $uuid = $this->_request->getParam('uuid'); - $code = $this->_request->getParam('code'); - $description = $this->_request->getParam('description'); - $has_pages = $this->_request->getParam('has_pages'); - $has_agreement = $this->_request->getParam('has_agreement'); - - $sql = "update source set title='$title',uuid='$uuid',code='$code',description='$description',has_pages='$has_pages',has_agreement='$has_agreement' where id='$id'"; - - if($this->db->exec($sql)) - { - $this->messenger->addMessage('修改来源信息成功'); - $this->_redirect("/admin/data/source/do/edit/id/$id"); - } - }catch(Exception $e){ - $this->messenger->addMessage('修改来源信息失败:'.$e->getMessage()); - $this->_redirect("/admin/data/source/do/edit/id/$id"); - } - - } - - $sql = "select * from source where id='$id'"; - $rs = $this->db->query($sql); - $row = $rs->fetch(); - - $this->view->info = $row; - - $this->_helper->viewRenderer('sourceadd'); - - }// 编辑单条信息 - - else if($do == 'datasource' && !empty($uuid)) - { - $redirect = "/admin/data/source/do/datasource/uuid/$uuid"; - - $this->view->uuid = $uuid; - $sql = "select md.title,ds.id,ds.sourceid,s.title as stitle,s.code from metadata md - left join datasource ds on ds.uuid=md.uuid - left join source s on s.id=ds.sourceid - where md.uuid='$uuid'"; - $rs = $this->db->query($sql); - $row = $rs->fetch(); - - $this->view->info = $row; - - if(!empty($_POST['submit'])) - { - $sourceid = $this->_request->getParam('sourceid'); - if(empty($sourceid)) - { - $this->messenger->addMessage('请选择项目来源'); - $this->_redirect($redirect); - } - - $sql=""; - if(empty($row['id'])) - { - $sql = "insert into datasource (uuid,sourceid) values ('$uuid','$sourceid')"; - }else - { - $sql = "update datasource set uuid='$uuid',sourceid='$sourceid' where id='{$row['id']}'"; - } - try{ - if($this->db->exec($sql)) - { - $this->messenger->addMessage('修改项目来源成功'); - $this->_redirect($redirect); - } - else - { - $this->messenger->addMessage('修改项目来源失败'); - $this->_redirect($redirect); - } - }catch (Exception $e){ - $this->messenger->addMessage('修改项目来源失败:'.$e->getMessage()); - $this->_redirect($redirect); - } - - } - - - $wheresql = array(); - - if(!empty($q) && !empty($search)) - { - $this->view->q = $q; - $wheresql[] = " title like '%$q%' "; - } - - if(count($wheresql>0))$wheresql = join(' and ',$wheresql); - else $wheresql=''; - - if($wheresql!='') - { - $wheresql = 'where '.$wheresql; - } - - $sql = "select * from source $wheresql order by id desc"; - $rs = $this->db->query($sql); - $rows = $rs->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(30); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - - $this->_helper->viewRenderer('sourceselect'); - }// 为元数据选择项目来源 - - else if($do == 'del' && !empty($id)) - { - $redirect = "/admin/data/source/"; - $sql = "delete from source where id='$id'"; - try{ - if($this->db->exec($sql)) - { - $this->messenger->addMessage('删除成功'); - $this->_redirect($redirect); - }else{ - $this->messenger->addMessage('删除失败,可能该数据已不存在'); - $this->_redirect($redirect); - } - }catch (Exception $e){ - $this->messenger->addMessage('删除失败:'.$e->getMessage()); - $this->_redirect($redirect); - } - }// 删除项目来源 - - else if($do == 'fetch' && !empty($id)) - { - - $wheresql = array(); - - $wheresql[] = " ds.sourceid='$id' "; - - if(!empty($q) && !empty($search)) - { - $this->view->q = $q; - $wheresql[] = " title like '%$q%' "; - } - - if(count($wheresql>0))$wheresql = join(' and ',$wheresql); - else $wheresql=''; - - if($wheresql!='') - { - $wheresql = 'where '.$wheresql; - } - - $sql = "select md.title,md.uuid,ds.id,ds.sourceid,s.title as stitle,s.code from metadata md - left join datasource ds on ds.uuid=md.uuid - left join source s on s.id=ds.sourceid - $wheresql - order by id desc"; - $rs = $this->db->query($sql); - $rows = $rs->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(30); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - - $this->_helper->viewRenderer('sourcefetchone'); - - }//查看某项目来源中的所有元数据 - - else if($do == 'delsource' && !empty($id)) - { - $redirect = "/admin/data/source/do/datasource/uuid/$uuid"; - - $sql = "delete from datasource where id='$id'"; - - try{ - if($this->db->exec($sql)) - { - $this->messenger->addMessage('删除成功'); - $this->_redirect($redirect); - }else{ - $this->messenger->addMessage('删除失败,可能该数据已不存在'); - $this->_redirect($redirect); - } - }catch (Exception $e){ - $this->messenger->addMessage('删除失败:'.$e->getMessage()); - $this->_redirect($redirect); - } - - - }// 清除元数据来源记录 - elseif ($do=='sync') { //同步数据来源到metadata表 - $redirect = "/admin/data/source/"; - $sql = "update metadata m set source=s.uuid from source s right join datasource d on s.id=d.sourceid where m.uuid=d.uuid"; - if($this->db->exec($sql)) - { - $this->messenger->addMessage('成功同步数据来源'); - $this->_redirect($redirect); - }else{ - $this->messenger->addMessage('同步数据来源失败'); - $this->_redirect($redirect); - } - } - - else - { - $wheresql = array(); - - if(!empty($q) && !empty($search)) - { - $this->view->q = $q; - $wheresql[] = " title like '%$q%' "; - } - - if(count($wheresql>0))$wheresql = join(' and ',$wheresql); - else $wheresql=''; - - if($wheresql!='') - { - $wheresql = 'where '.$wheresql; - } - - $sql = "select * from source $wheresql order by id desc"; - $rs = $this->db->query($sql); - $rows = $rs->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(30); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - }// 项目来源管理 - - - }//function sourceAction - - - function attachmentsAction() - { - $submit = $this->_request->getParam('submit'); - $add = $this->_request->getParam('add'); - $search = $this->_request->getParam('search'); - $delete = $this->_request->getParam('delete'); - $edit = $this->_request->getParam('edit'); - $down = $this->_request->getParam('down'); - $uuid = $this->_request->getParam('uuid'); - $mdtitle = $this->_request->getParam('mdtitle'); - $mdattdel = $this->_request->getParam('mdattdel'); - $attupdate = $this->_getParam('attupdate'); - - if(!empty($uuid)&&!empty($mdtitle)) - { - $this->view->uuid= $uuid; - $this->view->mdtitle=$mdtitle; - } - - if($add) - { - $this->_helper->viewRenderer('attachmentsadd'); - if(!empty($uuid)) - { - $this->view->uuid = $uuid; - } - }//附件添加 - - elseif($attupdate) - { - $submit = $this->_getParam('submit'); - if(!empty($submit)) - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $id = $this->_getParam('attupdate'); - - $files=new Files(); - $msg = $files -> upload($_FILES['Filedata'],empty($uuid)?'file/':'md/',true); - - if(empty($msg['error'])) - { - $msg['error']=""; - $filename = $msg['db_path']; - $filesize = $msg['file_size']; - $filedesc = $this->_request->getParam('filedesc'); - $filetype = 'md'; - $realname = $msg['realname']; - - - $sql = "UPDATE attachments SET filename=?,filetype=?,filesize=?,realname=? WHERE id=?"; - $sth = $this->db->prepare($sql); - $ds = $sth->execute(array($filename,$filetype,$filesize,$realname,$id)); - - if($ds) - { - $msg['html'] = $realname.'['. round($filesize/1024,2) .' kb]'; - $msg['ok']= 1; - echo Zend_Json::encode($msg); - exit(); - }else{ - @unlink($filename); - $data = array("error"=>'附件上传失败:写入附件表出错'); - $this->jsonexit($data); - return true; - } - }else{ - @unlink($filename); - $data = array("error"=>'附件上传失败:'.$msg['error']); - $this->jsonexit($data); - return true; - } - - - $data = array("error"=>"处理中出现错误".$id); - $this->jsonexit($data); - return true; - }else{ - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer('attachments-update'); - $id = $this->_getParam('attupdate'); - $this->view->id = $id; - } - - }//附件文件更新 - - else if($mdattdel) - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - - $sql = "delete from mdattach where uuid=? AND id=?"; - $sth = $this->db->prepare($sql); - $ds = $sth->execute(array($uuid,$mdattdel)); - if($ds) - { - $data = array("status"=>1); //操作状态代码 : 1=>成功 2=>失败 - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>"处理中出现错误"); - $this->jsonexit($data); - return true; - } - } - - else if($delete) - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - - //需要删除文件,通过Zend_Registry::get('upload')获得上传文件的根目录 - - $basepath = $this->view->config->upload; - - //从数据库获取文件路径 - - $info = $this->getFileinfo($delete); - - $filepath = $basepath.$info['filename']; - - $sql = "SELECT * FROM mdattach WHERE id=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($delete)); - $rows = $sth->fetchAll(); - - if(count($rows)>0) - { - $data = array("error"=>'删除失败!该文件有元数据附件信息,不能直接删除',"status"=>0); - $this->jsonexit($data); - return true; - } - - $sql = "SELECT * FROM mdreviewattach WHERE attachid=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($delete)); - $rows = $sth->fetchAll(); - - if(count($rows)>0) - { - $data = array("error"=>'删除失败!该文件有对应评审附件信息,不能直接删除',"status"=>0); - $this->jsonexit($data); - return true; - } - - $sql = "SELECT link FROM reference WHERE link!=''"; - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - $ids = array(); - foreach($rows as $k=>$v) - { - if(preg_match("/service/i",$v['link'])) - { - $ids[] = str_replace("/service/attach/id/","",$v['link']); - } - unset($rows[$k]); - } - unset($rows); - - if(in_array($delete,$ids)) - { - $data = array("error"=>'删除失败!该文件有对应文献附件信息,不能直接删除',"status"=>0); - $this->jsonexit($data); - return true; - } - - if(unlink($filepath)) - { - $sql = "delete from attachments where id='$delete'"; - if($this->db->exec($sql)>0) - { - $data = array("status"=>1); - $this->jsonexit($data); - return true; - } - } - else - { - $sql = "delete from attachments where id='$delete'"; - if($this->db->exec($sql)>0) - { - $data = array("error"=>'文件删除失败,仅删除数据库记录,请手动删除文件:'.$info['filename'],"status"=>0); - $this->jsonexit($data); - return true; - } - } - - }//删除 - - else if($edit>0) - { - - if(empty($submit)) - { - - $this->view->info = $this->getFileinfo($edit); - - $this->_helper->viewRenderer('attachmentsadd'); - - } - else - { - $filedesc = $this->_request->getParam('filedesc'); - - $sql="update attachments set filedesc='$filedesc' where id='$edit'"; - - if($this->db->exec($sql)>0) - { - $this->messenger->addMessage('编辑成功'); - $this->_redirect("/admin/data/attachments/edit/$edit"); - } - } - - }//编辑 - - else if($search && $search!='my') - { - $keyword = $this->_request->getParam('keyword'); - if(empty($keyword)) - { - $this->messenger->addMessage('请输入关键词'); - $this->_redirect("/admin/data/attachments/search/1"); - } - else - { - $sql="select * from attachments where filedesc like '%$keyword%'"; - $re = $this->db->query($sql); - $rows=$re->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(30); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - } - - }//搜索 - - else if($search && $search=='my') - { - $auth = Zend_Auth::getInstance(); - if($auth->hasIdentity()) - { - $user = $auth->getIdentity(); - $userid = $user->id; - $sql="select * from attachments where userid='$userid'"; - $re= $this->db->query($sql); - $rows = $re->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(30); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); - $this->view->paginator=$paginator; - } - }//我的附件列表 - - else if ($down>0) - { - - $sql = "select * from attachments where id='$down'"; - - $re = $this->db->query($sql); - - $row = $re->fetch(); - - $file = new files(); - - $fullPath = $this->view->config->upload.$row['filename']; - - - // Parse Info / Get Extension - $fsize = filesize($fullPath); - $path_parts = pathinfo($fullPath); - $ext = strtolower($path_parts["extension"]); - - // Determine Content Type - switch ($ext) { - case "pdf": $ctype="application/pdf"; break; - case "exe": $ctype="application/octet-stream"; break; - case "zip": $ctype="application/zip"; break; - case "doc": $ctype="application/msword"; break; - case "xls": $ctype="application/vnd.ms-excel"; break; - case "ppt": $ctype="application/vnd.ms-powerpoint"; break; - case "gif": $ctype="image/gif"; break; - case "png": $ctype="image/png"; break; - case "jpeg": - case "jpg": $ctype="image/jpg"; break; - default: $ctype="application/force-download"; - } - - $content=file_get_contents($fullPath); - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - $this->getResponse()->setHeader('Content-Type', 'application/octet-stream') - ->setHeader('Content-Disposition','attachment; filename="'.basename($fullPath).'"') - ->setHeader('Content-Length', $fsize) - ->setHeader('Content-Type','application/force-download') - ->setHeader('Content-Type','application/download') - ->setHeader('Content-Type',$ctype) - ->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); - - //$sql = "update attachments set downtimes=downtimes+1 where id='$down'"; - //$this->db->exec($sql); - - }//附件下载 - - else - { - $submd=$this->view->config->sub->metadata; - $sql="select * from attachments where id in (select id from mdattach where uuid in (select uuid from $submd)) order by id desc"; - $re= $this->db->query($sql); - $rows = $re->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(30); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); - $this->view->paginator=$paginator; - }//所有附件 - }//attachments 附件 - - function uploadAction(){ - $this->_helper->layout()->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $uuid = $this->_getParam('uuid'); - - try{ - $auth = Zend_Auth::getInstance(); - if($auth->hasIdentity()) - { - $user = $auth->getIdentity(); - $userid = $user->id; - } - - if(empty($userid)||!is_numeric($userid)){ - $msg['error'] = "请先登录"; - echo Zend_Json::encode($msg); - exit(); - } - - if($user->usertype!='administrator') - { - $msg['error'] = "您无权使用此功能"; - echo Zend_Json::encode($msg); - exit(); - } - - $files=new Files(); - $msg = $files -> upload($_FILES['Filedata'],empty($uuid)?'file/':'md/',true); - - if(empty($msg['error'])) - { - $msg['error']=""; - $filename = $msg['db_path']; - $filesize = $msg['file_size']; - $filedesc = $this->_request->getParam('filedesc'); - $filetype = 'md'; - $realname = $msg['realname']; - - - $sql = "insert into attachments (filename,filetype,filedesc,userid,filesize,realname) values ('$filename','$filetype','$filedesc','$userid','$filesize','$realname') RETURNING id"; - $sth = $this->db->prepare($sql); - $sth->execute(); - $att = $sth->fetch(PDO::FETCH_ASSOC); - $msg['attid'] = $attid = $att['id']; - - $sql = "insert into mdattach (uuid,id) values ('$uuid','$attid')"; - if(empty($uuid) || $this->db->exec($sql)) - { - $msg['html'] = $realname.'['. round($filesize/1024,2) .' kb]
'; - echo Zend_Json::encode($msg); - exit(); - }else{ - $msg['error'] = '附件上传失败:写入附件表出错'; - @unlink($filename); - echo Zend_Json::encode($msg); - exit(); - } - - - }else{ - $msg['error'] = '附件上传失败:'.$msg['error']; - @unlink($filename); - echo Zend_Json::encode($msg); - exit(); - } - - }catch(Exception $e){ - $msg['error'] = "错误:".$e->getMessage(); - echo Zend_Json::encode($msg); - exit(); - } - }// uploadAction ajax上传附件 - - function getattsAction(){ - - $this->_helper->layout()->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $uuid = $this->_request->getParam('uuid'); - - if($uuid!='') - { - $auth = Zend_Auth::getInstance(); - if($auth->hasIdentity()) - { - $user = $auth->getIdentity(); - $userid = $user->id; - $sql = "select m.*,a.*,d.title from mdattach m - left join attachments a on m.id = a.id - left join metadata d on m.uuid=d.uuid where m.uuid='$uuid' - ORDER BY a.ts_created ASC"; - $rs = $this->db->query($sql); - $atts = $rs->fetchAll(); - - echo Zend_Json::encode($atts); - exit(); - }else - { - exit(); - } - }else{ - exit(); - } - //不输出错误 - }//获取附件 - - - /* - versionAction 版本管理 - */ - - public function versionAction(){ - - $ac = $this->_request->getParam('ac'); - $uuid = $this->_request->getParam('uuid'); - $submd=$this->view->config->sub->metadata; - set_time_limit(0); - - $auth = Zend_Auth::getInstance(); - if($auth->hasIdentity()) - { - $user = $auth->getIdentity(); - $u_id = $user->id; - } - - //查看单条数据的所有版本 - if (!empty($uuid) && empty($ac)) - { - //view the versions of the data - $sql = "SELECT md.title,md.uuid,v.ts_created,v.changelog,v.userid,v.id,u.username,u.realname,g.id as gid FROM mdversion v - LEFT JOIN $submd md ON md.uuid=v.uuid - left join users u on v.userid=u.id - left join geonetworkmetadata g on md.uuid=g.uuid - WHERE md.title IS NOT NULL AND v.uuid=? - order by v.ts_created desc - "; - $sth = $this->db->prepare($sql); - $sth->execute(array($uuid)); - $rows = $sth->fetchAll(); - @$this->view->mdtitle=$rows[0]['title']; - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(15); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - } - - //查看所有版本列表 - else if((empty($ac) && empty($uuid))|| $ac=='list') - { - $sql = "SELECT md.title,md.uuid,v.ts_created,v.changelog,v.userid,v.id,u.username,u.realname FROM mdversion v - LEFT JOIN $submd md ON md.uuid=v.uuid - left join users u on v.userid=u.id - WHERE md.title IS NOT NULL - order by v.ts_created desc - "; - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(15); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - } - - //按数据浏览 - else if($ac=="bydata") - { - $keywords = $this->_request->getParam('q'); - if(!empty($keywords)) - $this->view->q = $keywords; - $sql = "SELECT md.title,md.uuid,count(v.id) as c FROM mdversion v - LEFT JOIN $submd md ON md.uuid=v.uuid - WHERE md.title IS NOT NULL"; - if(!empty($keywords)) - { - $search=new Search($keywords); - $where=$search->sql_expr(array("md.title","md.description")); - $sql.=' and '.$where; - } - $sql.=" group by md.uuid,md.title"; - - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(10); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - - $this->_helper->viewRenderer('version-bydata'); - } - - - //删除某个版本 - else if($ac=="delete") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $data = ""; - - try{ - $id = $this->_request->getParam('id'); - if(empty($id) || !is_numeric($id)) - { - $data = array("error"=>"参数错误"); - $this->jsonexit($data); - return true; - } - - $sql = "DELETE FROM mdversion WHERE id=?"; - $sth = $this->db->prepare($sql); - $ex = $sth -> execute(array($id)); - - if($ex) - { - $data = array("deleted"=>$id, "error"=>"删除成功"); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>'删除失败,请确认权限后重试'); - $this->jsonexit($data); - return true; - } - }catch(Exception $e) { - $msg = "删除失败,请确认权限后重试"; - if($this->debug>0) - {$msg .= $e->getMessage();} - $data = array("error"=>$msg); - $this->jsonexit($data); - return true; - } - } - - //恢复到geonetwork - else if($ac == "restore") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $data = ""; - try{ - $id = $this->_request->getParam('id'); - if(empty($id) || !is_numeric($id)) - { - $data = array("error"=>"参数错误"); - $this->jsonexit($data); - return true; - } - - $this->wdb=Zend_Db::factory($this->view->config->geonetwork); - $sql = "SELECT v.xml,v.uuid FROM mdversion v - WHERE v.id=?"; - $sth = $this->db->prepare($sql); - $sth ->execute(array($id)); - $row = $sth->fetch(); - - $sql = "SELECT data FROM metadata WHERE uuid=?"; - $sth = $this->wdb->prepare($sql); - $sth ->execute(array($row['uuid'])); - $row_geo = $sth->fetch(); - - if($row['xml']==$row_geo['data']) - { - $data = array("error"=>'无须恢复,元数据相同'); - $this->jsonexit($data); - return true; - } - - $sql = "UPDATE metadata SET data=? WHERE uuid=?"; - $sth = $this->wdb->prepare($sql); - $ex = $sth ->execute(array($row['xml'],$row['uuid'])); - - if($ex) - { - $data = array("error"=>'恢复成功'); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>'恢复失败,请确认权限后重试'); - $this->jsonexit($data); - return true; - } - }catch(Exception $e) { - $msg = "恢复失败,请确认权限后重试"; - if($this->debug>0) - {$msg .= $e->getMessage();} - $data = array("error"=>$msg); - $this->jsonexit($data); - return true; - } - } - - //发布到评审 - else if($ac == "commit") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $data = ""; - try{ - $id = $this->_request->getParam('id'); - if(empty($id) || !is_numeric($id)) - { - $data = array("error"=>"参数错误"); - $this->jsonexit($data); - return true; - } - - $changelog = $this->_request->getParam('changelog'); - if(empty($changelog)) - { - $data = array("error"=>'请输入变更信息'); - $this->jsonexit($data); - return true; - } - - // 1. 权限认定 --skip - // 2. 保存变化记录 save changelog & userid - $sql = "UPDATE mdversion SET changelog=?,userid=? WHERE id=?"; - $this->db->query($sql,array($changelog,$u_id,$id)); - - // 3. 获取数据评审状态 - $sql = "SELECT s.*,v.xml,m.title FROM mdstatus s left join mdversion v on s.uuid=v.uuid - left join metadata m on s.uuid=m.uuid WHERE v.id=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($id)); - $row = $sth->fetch(); - - $sql="select * from xunsearch where uuid=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($row['uuid'])); - $data_search = $sth->fetch(); - $search=new Search(); - $search->update($data_search); - - if (empty($row)) //无对应记录 - { - $sql="select m.id from metadata m left join mdversion v on m.uuid=v.uuid where v.id=?"; - $sth=$this->db->prepare($sql); - $sth->execute(array($id)); - $mrow=$sth->fetch(); - if (empty($mrow)) //说明是新数据 - { - //导入元数据 - $iso=new ISO19115(); - $iso->saveDB($this->db,$row['xml']); - //进入评审库 - $sql="insert into mdstatus (uuid,status,userid) select uuid,0,? from mdversion where id=?"; - $this->db->query($sql,array($u_id,$id)); - - //email to admin - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"metadata-new-admin",array( - 'user' => $user->username, - 'uuid' => $iso->uuid, - 'email'=> $user->email, - //元数据标题 - 'title'=> $iso->resTitle, - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - $mail->send(); - - unset($mail); - unset($mailtp); - //email to author - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"metadata-new-author",array( - 'user' => $user->username, - 'uuid' => $iso->uuid, - 'email'=> $user->email, - //元数据标题 - 'title'=> $iso->resTitle, - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($user->email); - $mail->addCc($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - @$mail->send(); - - $data = array("commited"=>1,"error"=>'该版本已经成功提交,请等待数据中心进一步处理!'); - $this->jsonexit($data); - return true; - } else { //说明是已发布的数据且数据不存在评审信息 - //同步元数据 - $iso=new ISO19115(); - $iso->saveDB($this->db,$row['xml']); - //移除中间版本 - $sql="delete from mdversion where uuid in (select uuid from mdversion where id=?) and changelog is null"; - $this->db->query($sql,array($id)); - //修改评审状态为发布,且由其提交的用户进行管理 - $sql="insert into mdstatus (uuid,status,userid) select uuid,6,? from mdversion where id=?"; - $this->db->query($sql,array($u_id,$id)); - - //email to admin & author - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"metadata-release",array( - 'user' => $user->username, - 'uuid' => $row['uuid'], - 'email'=> $user->email, - //元数据标题 - 'title'=> $row['title'], - 'changelog'=>$changelog, - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($this->view->config->service->email); - $mail->addCc($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - @$mail->send(); - - $data = array("commited"=>1,"error"=>'该版本已经成功发布!'); - $this->jsonexit($data); - return true; - } - } - else if ($row['status']==-1 || $row['status']==0 || $row['status']==1) //取消发布的数据,初始状态,已接收 - { - //同步元数据 - $iso=new ISO19115(); - $iso->saveDB($this->db,$row['xml']); - //email to admin - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"version-commit-admin",array( - 'user' => $user->username, - 'uuid' => $row['uuid'], - 'email'=> $user->email, - //元数据标题 - 'title'=> $row['title'], - 'changelog'=>$changelog, - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - $mail->send(); - - unset($mail); - unset($mailtp); - //email to author - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'数据服务组'); - $mailtp=new EmailText($this->db,"version-commit-author",array( - 'user' => $user->username, - 'uuid' => $row['uuid'], - 'email'=> $user->email, - //元数据标题 - 'title'=> $row['title'], - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($user->email); - $mail->addCc($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - @$mail->send(); - - $data = array("commited"=>1,"error"=>'该版本已经成功提交并同步,请等待数据中心进一步处理!'); - $this->jsonexit($data); - return true; - } - else if ($row['status']==2 || $row['status']==3 || $row['status']==4)//已发送过外审邮件,需由编辑告知变化信息 - { - //同步元数据 - $iso=new ISO19115(); - $iso->saveDB($this->db,$row['xml']); - - //email to admin - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"version-commit-admin",array( - 'user' => $user->username, - 'uuid' => $row['uuid'], - 'email'=> $user->email, - //元数据标题 - 'title'=> $row['title'], - 'changelog'=>$changelog, - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - $mail->send(); - - unset($mail); - unset($mailtp); - //email to author - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'数据服务组'); - $mailtp=new EmailText($this->db,"version-commit-author",array( - 'user' => $user->username, - 'uuid' => $row['uuid'], - 'email'=> $user->email, - //元数据标题 - 'title'=> $row['title'], - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($user->email); - $mail->addCc($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - @$mail->send(); - - //email to experts - $sql="select u.username,u.email,u.id from mdexpertreview e left join users u on e.id=u.id where e.status in (0,1) and e.uuid=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($row['uuid'])); - $experts = $sth->fetchAll(); - - unset($mail); - unset($mailtp); - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"version-commit-expert",array( - 'user' => $user->username, - 'uuid' => $row['uuid'], - 'email'=> $user->email, - //元数据标题 - 'title'=> $row['title'], - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - $filecontent=file_get_contents("http://" . $_SERVER['HTTP_HOST'].'/service/doc/uuid/'.$row['uuid']); - $mail->createAttachment($filecontent,'application/octet-stream',Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, $row['title'].'.doc'); - $filecontent=file_get_contents("http://" . $_SERVER['HTTP_HOST'].'/service/pdf/uuid/'.$row['uuid']); - $mail->createAttachment($filecontent,'application/octet-stream',Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, $row['title'].'.pdf'); - if($this->debug==0) - { - foreach ($experts as $expert) $mail->addTo($expert['email']); - $mail->addCc($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - @$mail->send(); - - $data = array("commited"=>1,"error"=>'该版本已经成功提交,请等待数据中心进一步处理!'); - $this->jsonexit($data); - return true; - } - else if ($row['status']>=5)//数据已经发布,再次修改后将只通知管理员,保留发布状态 - { - //同步元数据 - $iso=new ISO19115(); - $iso->saveDB($this->db,$row['xml']); - //移除中间版本 - $sql="delete from mdversion where uuid in (select uuid from mdversion where id=?) and changelog is null"; - $this->db->query($sql,array($id)); - - //email to admin & author - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'数据服务组'); - $mailtp=new EmailText($this->db,"metadata-release",array( - 'user' => $user->username, - 'uuid' => $row['uuid'], - 'email'=> $user->email, - //元数据标题 - 'title'=> $row['title'], - 'changelog'=>$changelog, - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($this->view->config->service->email); - //$mail->addCc($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - //@$mail->send(); - - $data = array("commited"=>1,"error"=>'该版本已经成功发布!'); - $this->jsonexit($data); - return true; - } - - }catch(Exception $e) { - $msg = "提交失败,请确认权限后重试"; - if($this->debug>0) - {$msg .= $e->getMessage();} - $data = array("error"=>$msg); - $this->jsonexit($data); - return true; - } - }//发布到评审 - - //与前一个版本对比 - else if($ac == "diff" || $ac=="diff1") - { - $this->_helper->viewRenderer('version-diff'); - $id = $this->_request->getParam('id'); - if(empty($id) || !is_numeric($id)) - { - $this->view->error = "参数错误"; - return true; - } - - $sql = "SELECT v.uuid,md.title FROM mdversion v - LEFT JOIN metadata md ON v.uuid=md.uuid - WHERE v.id=?"; - $sth = $this->db->prepare($sql); - $sth -> execute(array($id)); - - $row = $sth->fetch(); - if ($ac=='diff') - { - $sql = "SELECT v.* FROM mdversion v - WHERE v.uuid=? and v.ts_created<=(select ts_created from mdversion where id=?) - ORDER BY v.ts_created DESC - LIMIT ?"; - $sth = $this->db->prepare($sql); - $sth -> execute(array($row['uuid'],$id,2)); - } else { - $sql = "SELECT v.* FROM mdversion v - WHERE v.uuid=? and (v.id=? or (v.ts_created<(select ts_created from mdversion where id=?) and changelog is not null)) - ORDER BY v.ts_created DESC - LIMIT ?"; - $sth = $this->db->prepare($sql); - $sth -> execute(array($row['uuid'],$id,$id,2)); - } - - $rows = $sth->fetchAll(); - - if(count($rows)<2) - { - $this->view->error = "对比失败:之前没有版本可以对比"; - return true; - } - - $this->view->info = $row; - $this->view->data = $rows; - - } - - - }//versionAction 版本控制 - - - /* - * authorAction() 数据作者 - * - * param string $ac //动作 - =add 添加 - =edit 编辑 - =update 更新 - =del 删除 - =list 列出所有数据作者 - - * param string $uuid //UUID - * - * return view|application-json - */ - public function authorAction(){ - - $ac = $this->_getParam('ac'); - $uuid = $this->_getParam('uuid'); - $submd=$this->view->config->sub->metadata; - - if(empty($ac) && empty($uuid)) - { - - $keywords = $this->_request->getParam('q'); - if(!empty($keywords)) - $this->view->q = $keywords; - - $sql = "select md.id,md.title,md.uuid,count(a.userid) as c from $submd md - right JOIN mdauthor a ON md.uuid=a.uuid - "; - - if(!empty($keywords)) - { - $search=new SimpleSearch($keywords); - $where=$search->sql_expr(array("md.title")); - $sql.=' and ('.$where.")"; - } - - $sql .= "GROUP by md.id,md.title,md.uuid,md.ts_created - ORDER BY md.ts_created DESC"; - - $sth = $this->db->prepare($sql); - $sth-> execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(15); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - - }//列表 - - - else if($ac == 'add') - { - - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $username = $this->_getParam('username'); - $uuid = $this->_getParam('uuid'); - - 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)) - { - $data = array("error"=>"uuid参数错误"); - $this->jsonexit($data); - return true; - } - - if(empty($username)) - { - $data = array("error"=>'请输入要添加为该数据作者的用户名'); - $this->jsonexit($data); - return true; - } - - $sql = "SELECT * FROM users WHERE username=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($username)); - $row = $sth->fetch(); - - if(empty($row['id']) || !isset($row['id'])) - { - $data = array("error"=>'您输入的用户名无任何对应用户'); - $this->jsonexit($data); - return true; - } - - $uid = $row['id']; - - $sql = "SELECT * FROM mdauthor WHERE userid=? AND uuid=?"; - $sth = $this->db->prepare($sql); - $sth->execute(array($row['id'],$uuid)); - $row = $sth->fetch(); - - if(!empty($row['id']) && $row['status']==1) - { - $data = array("error"=>'该用户已经是此数据的作者'); - $this->jsonexit($data); - return true; - } - - if(!empty($row['id']) && in_array($row['status'],array(0,-1)) ) - { - $sql = "UPDATE mdauthor SET status=1 WHERE uuid=? AND userid=?"; - $sth = $this->db->prepare($sql); - $ds = $sth->execute(array($uuid,$uid)); - if($ds) - { - $data = array("msg"=>'该用户已被提升为数据作者','added'=>1); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>'处理中出现错误'); - $this->jsonexit($data); - return true; - } - } - - if(empty($row['id'])) - { - $sql = "INSERT INTO mdauthor (uuid,userid,status) VALUES (?,?,?)"; - $sth = $this->db->prepare($sql); - $ds = $sth->execute(array($uuid,$uid,1)); - if($ds) - { - $data = array("msg"=>'该用户已被添加为数据作者','added'=>1); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>"处理中出现错误"); - $this->jsonexit($data); - return true; - } - } - - - $data = array("error"=>"处理中出现错误"); - $this->jsonexit($data); - return true; - - }//ajax 添加作者 - - - else if(($ac == "edit" || $ac=="") && !empty($uuid)) - { - - $window = $this->_getParam('window'); - - if($window == "iframe") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer('author-edit-iframe'); - }else{ - $this->_helper->viewRenderer('author-edit'); - } - - $sql = "SELECT a.*,md.title,u.realname,u.username FROM mdauthor a - LEFT JOIN $submd md ON a.uuid=md.uuid - LEFT JOIN users u ON a.userid=u.id - WHERE md.uuid=? - ORDER BY a.id DESC - "; - - $sth = $this->db->prepare($sql); - $sth->execute(array($uuid)); - $rows = $sth->fetchAll(); - - $this->view->authors = $rows; - - }// 作者管理弹窗 - - else if($ac == "del") - { - - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $id = $this->_getParam('id'); - - $sql = "DELETE FROM mdauthor WHERE id=?"; - $sth = $this->db->prepare($sql); - $ds = $sth->execute(array($id)); - if($ds) - { - $data = array("deleted"=>$id); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>"处理中出现错误"); - $this->jsonexit($data); - return true; - } - - - }//移除作者 - - else if($ac == 'update') - { - - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $id = $this->_getParam('id'); - - $sql = "UPDATE mdauthor SET status=1 WHERE id=?"; - $sth = $this->db->prepare($sql); - $ds = $sth->execute(array($id)); - if($ds) - { - $data = array("updated"=>$id,'msg'=>'认证成功!'); - $this->jsonexit($data); - return true; - }else{ - $data = array("error"=>"处理中出现错误"); - $this->jsonexit($data); - return true; - } - - - }//认证作者 - - else if($ac =="list") - { - $this->_helper->viewRenderer('author-list'); - - $sql = "SELECT u.id,u.realname,u.email,count(a.id) as d FROM mdauthor a - LEFT JOIN users u ON a.userid=u.id - WHERE a.status>0 and uuid in (select uuid from $submd) - GROUP BY u.id,u.realname,u.email - ORDER BY u.id DESC"; - - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(15); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - }//所有作者 - - else if($ac == "userdatas") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $id = $this->_getParam('id'); - - $sql = "SELECT md.title,md.uuid FROM mdauthor a - LEFT JOIN $submd md ON a.uuid=md.uuid - WHERE a.userid=? AND a.status>0 - ORDER BY a.ts_created"; - $sth = $this->db->prepare($sql); - $sth->execute(array($id)); - $rows = $sth->fetchAll(); - - $data = array("datas"=>$rows); - $this->jsonexit($data); - return true; - - }//用户数据 - - else if($ac == "datas") - { - $this->_helper->viewRenderer('author-datas'); - - $keywords = $this->_request->getParam('q'); - if(!empty($keywords)) - $this->view->q = $keywords; - - $sql = "select md.id,md.title,md.uuid,count(a.userid) as c from $submd md - left JOIN mdauthor a ON md.uuid=a.uuid - "; - - if(!empty($keywords)) - { - $search=new SimpleSearch($keywords); - $where=$search->sql_expr(array("md.title")); - $sql.=' WHERE '.$where; - } - - $sql .= "GROUP by md.id,md.title,md.uuid,md.ts_created - ORDER BY md.ts_created DESC"; - - $sth = $this->db->prepare($sql); - $sth-> execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(15); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - - } - - }//authorAction() 数据作者管理 - - - //newdataAction() 新建元数据 - public function newdataAction(){ - - $ac = $this->_request->getParam('ac'); - $id = $this->_request->getParam('id'); - $this->wdb=Zend_Db::factory($this->view->config->geonetwork); - - $auth = Zend_Auth::getInstance(); - if($auth->hasIdentity()) - { - $user = $auth->getIdentity(); - $u_id = $user->id; - } - - //在geonetwork中查看单条数据 - if (!empty($id) && empty($ac)) - { - $this->view->url='metadata.show?id='.$id; - $this->_helper->viewRenderer('newdata-view'); - } - //查看所有的未提交数据列表 - else if((empty($ac) && empty($uuid))|| $ac=='list') - { - $sql = "SELECT (regexp_matches(gn.data,'(.*)'))[1] as title,gn.id,gn.uuid,u.username,u.realname FROM geonetworkmetadata gn left join users u on gn.owner=u.id - WHERE gn.uuid not in (select uuid from metadata) - order by gn.id desc - "; - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(15); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - } - //元数据导入 - else if($ac=="import") - { - $this->_helper->viewRenderer('newdata-import'); - } - //从模板新建元数据 - else if($ac=="add-by-template") - { - $keywords = $this->_request->getParam('q'); - $sql="select id,(regexp_matches(data,'(.*)'))[1] as title,(owner-$u_id) as isowner from metadata where istemplate='y' and schemaid='iso19115'"; - if(!empty($keywords)) - { - $this->view->q = $keywords; - $search=new Search($keywords); - $where=$search->sql_expr(array("data")); - $sql.=' and '.$where; - } - $sql.=" order by changedate desc"; - $sth = $this->wdb->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(10); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - $this->_helper->viewRenderer('newdata-add'); - } - - else if($ac=="add-by-data") - { - $keywords = $this->_request->getParam('q'); - $sql = "SELECT md.title,md.uuid,md.description,gn.id as gid FROM normalmetadata md - left join geonetworkmetadata gn on md.uuid=gn.uuid - WHERE gn.id is not null"; - if(!empty($keywords)) - { - $this->view->q = $keywords; - $search=new Search($keywords); - $where=$search->sql_expr(array("md.title","md.description")); - $sql.=' and '.$where; - } - $sql.=" order by md.ts_created desc"; - $sth = $this->db->prepare($sql); - $sth->execute(); - $rows = $sth->fetchAll(); - - $paginator = Zend_Paginator::factory($rows); - $paginator->setCurrentPageNumber($this->_getParam('page')); - $paginator->setItemCountPerPage(10); - $paginator->setView($this->view); - Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); - $this->view->paginator=$paginator; - $this->_helper->viewRenderer('newdata-add-bydata'); - } - //元数据格式检查 - else if ($ac=="validate") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $data = ""; - $id = $this->_request->getParam('id'); - $sql="select uuid,data from geonetworkmetadata where id=?"; - $sth=$this->db->prepare($sql); - $sth->execute(array($id)); - $row=$sth->fetch(); - - $iso=new ISO19115(); - @$iso->loadXML($row['data']); - if ($iso->validate()) - { - $data=array("error"=>"元数据中发现错误。
".implode("
",$iso->error)); - $this->jsonexit($data); - return true; - } else { - $data=array("error"=>"元数据中没有发现错误。
"); - $this->jsonexit($data); - return true; - } - } - //提交数据 - else if($ac=="commit") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $data = ""; - $id = $this->_request->getParam('id'); - try{ - if(empty($id) || !is_numeric($id)) - { - $data = array("error"=>"参数错误"); - $this->jsonexit($data); - return true; - } - - $changelog = $this->_request->getParam('changelog'); - if(empty($changelog)) - { - $data = array("error"=>'请输入变更信息'); - $this->jsonexit($data); - return true; - } - - // 1. 权限认定:当前用户必须和其owner相同 - // 数据应当没有评审状态,没有作者信息 - $sql="select uuid,data from geonetworkmetadata where id=?"; - $sth=$this->db->prepare($sql); - $sth->execute(array($id)); - $row=$sth->fetch(); - if (empty($row)) - { - $data = array("error"=>'无权限修改数据'); - $this->jsonexit($data); - return true; - } - - // 保存数据作者信息 - - // 2. 保存变化记录 save changelog & userid for the latest version - $sql = "UPDATE mdversion SET changelog=?,userid=? WHERE id in (select v.id from mdversion v left join geonetworkmetadata g on v.uuid=g.uuid where g.id=? order by v.ts_created desc limit 1)"; - $this->db->query($sql,array($changelog,$u_id,$id)); - - // 3. 保存数据评审状态 - //导入元数据 - $iso=new ISO19115(); - @$iso->saveDB($this->db,$row['data']); - //进入评审库 - $sql="insert into mdstatus (uuid,status,userid) select uuid,0,? from geonetworkmetadata where id=?"; - $this->db->query($sql,array($u_id,$id)); - //email to admin - $mail=new WestdcMailer($this->view->config->smtp); - $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); - $mailtp=new EmailText($this->db,"metadata-new-admin",array( - 'user' => $user->username, - 'uuid' => $iso->uuid, - 'email'=> $user->email, - //元数据标题 - 'title'=> $iso->resTitle, - )); - $mail->setBodyText($mailtp->getBody()); - $mail->setSubject($mailtp->getSubject()); - if($this->debug==0) - { - $mail->addTo($this->view->config->service->email); - }else{ - $mail->addTo($this->debug_email); - } - $mail->send(); - - $data = array("commited"=>1,"error"=>'该版本已经成功提交,请等待数据中心进一步处理!'); - $this->jsonexit($data); - return true; - }catch(Exception $e) { - $sql="delete from mdstatus where uuid in (select uuid from geonetworkmetadata where id=?)"; - $this->db->query($sql,array($id)); - $msg = "提交失败,请确认权限后重试"; - if($this->debug>0) - {$msg .= $e->getMessage();} - $data = array("error"=>$msg); - $this->jsonexit($data); - return true; - } - } - - }// newdataAction() 新建元数据 - - - public function fundAction() - { - $this->_helper->layout->setLayout('administry'); - $ac = $this->_getParam('ac'); - $submit = $this->_getParam('submit'); - $this->view->q = $keyword = $this->_getParam('q'); - - include_once("helper/view.php"); - include_once("data/Fund.php"); - $fund = new Fund($this->db); - - $auth = Zend_Auth::getInstance(); - if($auth->hasIdentity()) - { - $user = $auth->getIdentity(); - $uid = $user->id; - } - - if($ac=='index' || empty($ac)) - { - $uuid = $this->_getParam('uuid'); - if(empty($uuid)) - { - $rows = $fund->fetch(NULL,true,0,$keyword); - view::addPaginator($rows,$this,10); - }else{ - include('data/Metadata.php'); - $md = new Metadata($this->db); - $this->view->md = $md->view($uuid); - $rows = $fund->fetch($uuid); - view::addPaginator($rows,$this,10); - } - return true; - } - - if($ac == 'datalist') - { - $this->_helper->viewRenderer('fund-data-list'); - $this->view->q = $q = $this->_getParam('q'); - $rows = $fund->fetchFromData(true,0,$q); - view::addPaginator($rows,$this,10); - return true; - } - - if($ac == 'dataview') - { - $this->_helper->viewRenderer('fund-data-view'); - $this->view->q = $q = $this->_getParam('q'); - $id = $this->_getParam('id'); - $this->view->fund = $fund->view($id); - $rows = $fund->fetchFromData($id,0,$q); - view::addPaginator($rows,$this,10); - return true; - } - - if($ac == "add") - { - $this->_helper->viewRenderer('fund-add'); - if(!empty($submit)) - { - $data = $fund->_getParams($this->_request); - $data['userid'] = $uid; - - if($fund->add($data) == true) - { - $this->view->AlertType = "alert-success"; - $this->view->msg = "添加成功!"; - $this->view->jump_url = "/admin/data/fund/"; - return true; - }else{ - $this->view->data = $data; - $this->view->error = "添加失败,请重试"; - return true; - } - } - return true; - }//add - - if($ac == "edit") - { - $this->_helper->viewRenderer('fund-add'); - $id = $this->_getParam('id'); - - if(empty($id)) - { - $this->view->AlertType = "alert-error"; - $this->view->msg = "参数错误"; - $this->view->jump_url = "/admin/data/fund/"; - } - - if(!empty($submit)) - { - $data = $fund->_getParams($this->_request); - $data['userid'] = $uid; - if($fund->update($data,$id) == true) - { - $this->view->AlertType = "alert-success"; - $this->view->msg = "修改成功!"; - $this->view->jump_url = "/admin/data/fund/"; - return true; - }else{ - $this->view->data = $data; - $this->view->error = "修改失败,请重试"; - return true; - } - }else{ - $this->view->data = $fund->view($id); - } - return true; - }//edit - - if($ac == "del") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $id = $this->_getParam('id'); - - if(empty($id)) - { - $this->jsonexit(array('error'=>'参数错误')); - return true; - } - - if($fund->delete($id) == true) - { - $this->jsonexit(array('success'=>$id)); - return true; - }else{ - $this->jsonexit(array('error'=>'删除失败')); - return true; - } - - }//del - - if($ac == "formd") - { - $uuid = $this->_getParam('uuid'); - - if(empty($uuid)) - { - $this->view->AlertType = "alert-error"; - $this->view->msg = "参数错误"; - $this->view->jump_url = "/admin/data/fund/"; - } - - $id = $this->_getParam('id'); - $order = $this->_getParam('order'); - - if(!empty($id)) - { - if($fund->addToMdfund($uuid,$id,$order)) - { - $this->view->AlertType = "alert-success"; - $this->view->error = "添加成功!可以继续选择并添加"; - }else{ - $this->view->AlertType = "alert-error"; - $this->view->error = "添加失败!该数据可能已被添加"; - } - } - - $mfid = $this->_getParam('mfid'); - - if(!empty($mfid)) - { - if($fund->changeorder($mfid,$order)) - { - $this->view->AlertType = "alert-success"; - $this->view->error = "排序修改成功!"; - }else{ - $this->view->AlertType = "alert-error"; - $this->view->error = "排序修改失败!"; - } - $rows = $fund->fetch($uuid); - }else{ - $this->view->ct = "ct"; - $rows = $fund->fetch($uuid,false,0,$this->_getParam('q')); - } - - include('data/Metadata.php'); - $md = new Metadata($this->db); - $this->view->md = $md->view($uuid); - - view::addPaginator($rows,$this,10); - - return true; - }//formd - - if($ac == "mdfunddel") - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $id = $this->_getParam('id'); - - if(empty($id)) - { - $this->jsonexit(array('error'=>'参数错误')); - return true; - } - - if($fund->mfdelete($id) == true) - { - $this->jsonexit(array('success'=>$id)); - return true; - }else{ - $this->jsonexit(array('error'=>'删除失败')); - return true; - } - }//mdfunddel - - if($ac == "data") - { - $this->_helper->viewRenderer('fund-data'); - - $id = $this->_getParam('id'); - $del = $this->_getParam('del'); - $this->view->info = $fund->view($id); - - if(!empty($del)) - { - $s = $fund->dataRelationDelete($del); - if($s) - { - $this->view->error = view::Error("删除成功!","alert-sccuess"); - }else{ - $this->view->error = view::Error("删除失败!","alert-error"); - } - } - - $rows = $fund->getData($id,$keyword); - view::addPaginator($rows,$this,10); - } - - }//fund - - //数据可是化管理 - public function visualAction() - { - $uuid = $this->_getParam('uuid'); - $submit = $this->_getParam('submit'); - $visual = new Visual\Visual; - - if(!empty($uuid)) - { - $this->_helper->viewRenderer('visual-add'); - $this->view->data = ['uuid' => $uuid]; - - if(empty($submit)) - { - $data = $visual->getVisualVars($uuid); - - if(!empty($data)) - { - $this->view->info = $data; - } - - return true; - } - - $data = array( - 'uuid' => $this->_getParam('uuid'), - 'vars' => $this->_getParam('var'), - 'data' => $this->_getParam('data'), - 'status' => $this->_getParam('status') - ); - - $status = $visual->add($data); - - if($status === true) - { - view::Post($this,"添加成功!",-2); - return true; - }else{ - $this->view->error = $status; - return true; - } - } - - view::addPaginator($visual->getVisualMetadata(),$this,10); - return; - } - - public function ftpAction() - { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - - $uuid = $this->_getParam('uuid'); - - $this->view->uuid = $uuid; - $submd=$this->view->config->sub->metadata; - - if(empty($uuid) || !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)) - { - $data = array( - 'error'=>"参数错误" - ); - $this->jsonexit($data); - return true; - } - //安全检查: uuid必须是当前子平台数据 - $sql="select * from $submd where uuid=?"; - $sth=$this->db->prepare($sql); - $sth->execute(array($uuid)); - $row=$sth->fetch(); - if (empty($row)) - { - $data = array( - 'error'=>"参数错误" - ); - $this->jsonexit($data); - return true; - } - - //ftp admin 用户名 - $auth = Zend_Auth::getInstance(); - $uid=$auth->getIdentity()->id; - $uname = $submd.'admin'.$uid; - - //ftp路径 - $homedir = "/data/upload/".$uuid."/"; - //ftp用户表 - $ftptable=' pureftp ';//ftp2.westgis.ac.cn - $uid = 1002; - $gid = 1002; - - $sql = "SELECT * FROM $ftptable WHERE userid='$uname' ORDER BY pkid DESC"; - $sth = $this->db->prepare($sql); - $sth->execute(); - $row = $sth->fetch(); - - //create directory for upload - //server is not localhost, so we need a trick - //$old=umask(0); - //@mkdir($homedir,0777); - //umask($old); - $page=file_get_contents('http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid='.$uuid); - if (!empty($page)) die($page);//there are errors in visit ftp page - - - if(!empty($row['pkid'])) - { - if(preg_match("/.*".$uuid.".*/",$row['homedir'])) - { - $data = array( - 'statu'=>1, - 'user'=>$row['userid'], - 'passwd'=>$row['passwd'] - ); - - $this->jsonexit($data); - return true; - - }else{ - $passwd = $this->genRandomString(16); - //$sql = "UPDATE proftpusers SET passwd=?,uid=?,gid=?,homedir=? WHERE userid=?"; - //$sth = $this->db->prepare($sql); - //$rs = $sth->execute(array($passwd,$uid,$gid,$homedir,$uname)); - $sql="update ".$ftptable." SET passwd='".$passwd."',uid=".$uid.",gid=".$gid.",homedir='".$homedir."' WHERE userid='".$uname."'"; - $rs=$this->db->query($sql); - if($rs) - { - $data = array( - 'statu'=>1, - 'user'=>$uname, - 'passwd'=>$passwd - ); - $this->jsonexit($data); - return true; - }else{ - $data = array( - 'error'=>"FTP信息更新失败,请重试" - ); - $this->jsonexit($data); - return true; - } - } - } - - else{ - $passwd = $this->genRandomString(16); - - //$sql = "INSERT INTO proftpusers (userid,passwd,uid,gid,homedir) VALUES (?,?,?,?,?)"; - //$sth = $this->db->prepare($sql); - //$rs = $sth->execute(array($uname,$passwd,$uid,$gid,$homedir)); - $sql="insert into ".$ftptable." (userid,passwd,uid,gid,homedir) values('".$uname."','".$passwd."',".$uid.",".$gid.",'".$homedir."')"; - $rs=$this->db->query($sql); - if($rs) - { - $data = array( - 'statu'=>1, - 'user'=>$uname, - 'passwd'=>$passwd - ); - $this->jsonexit($data); - return true; - }else{ - $data = array( - 'error'=>"FTP信息更新失败,请重试" - ); - $this->jsonexit($data); - return true; - } - }//end if - } - - /* - 获得单个文件的信息 - return array row - */ - public function getFileinfo($id){ - $sql = "select * from attachments where id='$id'"; - $re= $this->db->query($sql); - $row= $re->fetch(); - return $row; - } - - - /* - * jsonexit() 退出并返回json数据 - * - * param array $data 要返回的JSON数据,可以是任意数组 - * - * return JSON-response - */ - public function jsonexit($data){ - $this->getResponse()->setHeader('Content-Type', 'application/json')->appendBody(json_encode($data,JSON_NUMERIC_CHECK)); - return true; - }//jsonexit() 退出并返回json数据 - - private function genRandomString($len) - { - $chars = array( - "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", - "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", - "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", - "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", - "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", - "3", "4", "5", "6", "7", "8", "9" - ); - $charsLen = count($chars) - 1; - - shuffle($chars); // 将数组打乱 - - $output = ""; - for ($i=0; $i<$len; $i++) - { - $output .= $chars[mt_rand(0, $charsLen)]; - } - return $output; - } -} +db=Zend_Registry::get('db'); + $this->view->config = Zend_Registry::get('config'); + $this->messenger=$this->_helper->getHelper('FlashMessenger'); + $this->view->messages = $this->messenger->getMessages(); + $this->debug = 0; //1:debug, 0:release + $this->debug_email='wangliangxu@lzb.ac.cn'; + $this->view->theme = new Theme(); + $this->_helper->layout->setLayout('administry'); + } + function postDispatch() + { + $this->view->messages = $this->messenger->getMessages(); + } + + public function indexAction() + { + $statistics = new Metadata\Statistics; + $this->view->total = $statistics->total(); + $this->view->review_accept = $statistics->review("accept"); + $this->view->review_finish = $statistics->review('finish'); + $this->view->visual = $statistics->visual(); + + if(view::isXmlHttpRequest($this)) + { + $this->jsonexit($statistics->getMetadataCountByDay(true)); + } + //其他连接 + } + + /* + * + * mdAction() + * 元数据管理 + * + */ + function mdAction() + { + $delete=$this->_getParam('delete'); + $down=(int)$this->_getParam('down'); + $search = $this->_getParam('search'); + $keyword = $this->_getParam('keyword'); + $att=$this->_getParam('att'); + $attupdate = $this->_getParam('attupdate'); + + $submd=$this->view->config->sub->metadata; + + if ($delete) + { + $sql="delete from metadata where uuid=? and uuid in (select uuid from ".$this->view->config->sub->metadata.")"; + try { + $this->db->query("delete from mdstatus where uuid=? and uuid in (select uuid from ".$this->view->config->sub->metadata.")",array($delete)); + $this->db->query("delete from mdauthor where uuid=? and uuid in (select uuid from ".$this->view->config->sub->metadata.")",array($delete)); + $this->db->query($sql,array($delete)); + $this->messenger->addMessage('提示信息:您已经成功删除该数据。'); + $search=new Search(); + $search->del($delete,'uuid'); + } catch (Exception $e) { + $this->messenger->addMessage($e->getMessage()); + } + $this->_redirect("/admin/data/md"); + }//删除 + + elseif($att>0){ + $submit=$this->_request->getParam('submit'); + $uuid=$this->_request->getParam('uuid'); + $atts=$this->_request->getParam('ids'); + $addatts=$this->_request->getParam('addatts'); + + $this->view->id = $att; + $this->view->uuid = $uuid; + + if(!empty($addatts)) + { + + if(empty($submit)) + { + $sql = "select title from metadata where uuid='$uuid'"; + $re = $this->db->query($sql); + $rows = $re->fetch(); + + + $this->view->id = $att; + $this->view->uuid = $uuid; + $this->view->mdtitle = $rows['title']; + $this->view->thisatt = $rows; + + $this->_redirect("/admin/data/attachments/uuid/$uuid/mdtitle/{$rows['title']}"); + } + else + { + if(!empty($uuid)) + { + foreach($atts as $v) + { + $sql = "insert into mdattach (uuid,id) values ('$uuid','$v')"; + try{ + $this->db->exec($sql); + $this->messenger->addMessage('成功添加附件:'.$v); + }catch (Exception $e) + { + $this->messenger->addMessage('添加附件失败:'.$v); + } + } + $this->_redirect("/admin/data/md/att/1/uuid/$uuid"); + } + } + }//empty($addatts) + else + { + if(!empty($uuid)) + { + + $sql = "select m.*,a.*,d.title from mdattach m + left join attachments a on m.id = a.id + left join metadata d on m.uuid=d.uuid where m.uuid='$uuid'"; + $re = $this->db->query($sql); + + $rows = $re->fetchAll(); + + $sql = "select title from metadata where uuid='$uuid'"; + $re = $this->db->query($sql); + $title = $re->fetch(); + + $this->view->atts=$rows; + $this->view->mdtitle = $title['title']; + + $this->_helper->viewRenderer('attmanager'); + } + + + } + + }//编辑附件 + + /* + * 输出打包下载的xml文件 + * + * 文件量大时可能出现超时,需要修改超时时间为无限 + */ + elseif ($down) { + + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + //临时zip文件名 + $tmpname="dataxml"; + + //xml文件存放的缓存目录 + $dirName = '../data/import/'; + + //查询需要创建的文件 + $sql = "SELECT md.title,md.uuid,x.* from xml x + LEFT JOIN $submd md ON md.id=x.id"; + + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); //将结果储存,但不使用 + + //创建zip文件,创建成功后再使用查询结果 + $zip = new ZipArchive(); + $url = tempnam($this->config->temp->path,$tmpname);//创建临时文件 + + if( $zip->open($url, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true ) + { + throw new Exception("cannot open {$url} for writing."); + } + + foreach($rows as $k=>$v) + { + + $filename = $dirName.$v['uuid'].'.xml'; + + //创建xml文件 + $handle = fopen($filename,"w"); + fwrite($handle,$v['data']); + fclose($handle); + + //添加到zip文件 + //zip localname 直接使用UUID作为文件名 + $zip->addFile($filename,$v['uuid'].".xml"); + + } + $zip->close(); + + //zip文件创建完成后删除服务器上的缓存文件,防止发生冗余 + foreach($rows as $k=>$v) + { + $filename = $dirName.$v['uuid'].'.xml'; + unlink($filename); + } + + //输出下载 + $content=file_get_contents($url); + $this->getResponse()->setHeader('Content-Type', 'application/octet-stream') + ->setHeader('Content-Disposition','attachment; filename="dataxml.zip"') + ->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); + }//down + else if($search){ + + if(!empty($keyword)) + { + $sql = "select m.*,md.viewed,g.id as gid, ds.id as datasetid from $submd m + left join mdstat md on m.uuid=md.uuid + left join geonetworkmetadata g on g.uuid=m.uuid + left join dataset ds on m.uuid=ds.uuid + where m.title like '%$keyword%' + order by m.id desc + "; + + $re=$this->db->query($sql); + $row=$re->fetchAll(); + $paginator = Zend_Paginator::factory($row); + $paginator->setCurrentPageNumber($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; + $this->messenger->addMessage($keyword.' 的搜索结果'); + } + else + { + $this->messenger->addMessage('请输入搜索关键字'); + $this->_redirect("/admin/data/md"); + } + + }//search + else{ + + $sql = "SELECT md.*,s.viewed,g.id as gid,st.status as mdstatus,ds.id as datasetid FROM $submd md + LEFT JOIN mdstat s ON md.uuid=s.uuid + LEFT JOIN geonetworkmetadata g ON g.uuid=md.uuid + LEFT JOIN mdstatus st ON md.uuid=st.uuid + LEFT JOIN dataset ds ON md.uuid=ds.uuid + ORDER BY md.id DESC"; + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage($this->view->config->page->max); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); + $this->view->paginator=$paginator; + } + + }//mdAction 元数据管理 + + /* + * datasetAction() + * 数据路径:即数据的物理主目录 + * + */ + function datasetAction() + { + + $ac = $this->_request->getParam('ac'); + + if($ac == "getdataset") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer('md-dataset'); + + $uuid = $this->_request->getParam('uuid'); + $sql = "SELECT * FROM dataset WHERE uuid=?"; + $sth = $this->db->prepare($sql); + $sth ->execute(array($uuid)); + $row = $sth->fetch(); + + $this->view->dataset = $row; + $this->view->uuid = $uuid; + } + + else if($ac == "update") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $uuid = $this->_request->getParam('uuid'); + $host = $this->_getParam('host'); + $path = $this->_getParam('path'); + + $sql = "UPDATE dataset SET host=?,path=? WHERE uuid=?"; + $sth = $this->db->prepare($sql); + $ds = $sth ->execute(array($host,$path,$uuid)); + if ($host=='ftp1.westgis.ac.cn') + { + file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); + } else if ($host=='ftp.sanjiangyuan.org.cn') + { + file_get_contents("http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); + } + + if($ds) + { + $data = array("ok"=>1); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>"处理中出现错误"); + $this->jsonexit($data); + return true; + } + } + + else if($ac == "add") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $uuid = $this->_request->getParam('uuid'); + $host = $this->_getParam('host'); + $path = $this->_getParam('path'); + + $sql = "SELECT * FROM dataset WHERE uuid=?"; + $sth = $this->db->prepare($sql); + $sth ->execute(array($uuid)); + $row = $sth->fetch(); + if(!empty($row['id'])) + { + $data = array("error"=>"该数据已经有存档信息,不能重复添加"); + $this->jsonexit($data); + return true; + } + + $sql = "INSERT INTO dataset (uuid,host,path) VALUES (?,?,?)"; + $sth = $this->db->prepare($sql); + $ds = $sth ->execute(array($uuid,$host,$path)); + if ($host=='ftp1.westgis.ac.cn') + { + file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); + } else if ($host=='ftp.sanjiangyuan.org.cn') + { + file_get_contents("http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); + } + + if($ds) + { + $data = array("ok"=>1); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>"处理中出现错误"); + $this->jsonexit($data); + return true; + } + } + else if ($ac=="import") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $uuid = $this->_request->getParam('uuid'); + $sql = "SELECT * FROM dataset WHERE uuid=?"; + $sth = $this->db->prepare($sql); + $sth ->execute(array($uuid)); + $row = $sth->fetch(); + + if ($row['host']=='ftp1.westgis.ac.cn') + { + file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); + echo '

数据目录成功导入!

'; + } else if ($row['host']=='ftp.sanjiangyuan.org.cn') + { + file_get_contents("http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid=".$uuid."&filelist=1"); + echo '

数据目录成功导入!

'; + } else { + echo '

数据目录未导入!

'; + } + } + + }//datasetAction存档管理 + + /* + * commentAction() + * 反馈管理 + * ALTER TABLE comments ADD COLUMN reply integer NOT NULL DEFAULT 0; + */ + function commentAction() + { + $delete=(int)$this->_getParam('delete'); + $uuid = $this->_getParam('uuid'); + $reply = $this->_getParam('reply'); + $replylist = $this->_getParam('replylist'); + $delreply = $this->_getParam('delreply'); + $submd=$this->view->config->sub->metadata; + + if ($delete) + { + $sql="delete from comments where id=? and uuid in (select uuid from $submd)"; + try { + $this->db->query($sql,array($delete)); + $this->messenger->addMessage('提示信息:您已经成功删除该评论。'); + } catch (Exception $e) { + $this->messenger->addMessage($e->getMessage()); + } + $this->_redirect("/admin/data/comment"); + } + + if($reply) + { + + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $body = $this->_getParam('content'); + if(empty($body)) + { + $data = array('error'=>"请输入回复内容"); + $this->jsonexit($data); + return true; + } + + $auth = Zend_Auth::getInstance(); + if($auth->hasIdentity()) + { + $user = $auth->getIdentity(); + $uid = $user->id; + $email = $user->email; + } + + $ipaddr = $_SERVER['REMOTE_ADDR']; + + $sql = "INSERT INTO comments (uuid,author,reply,userid,content,email,ip) VALUES (?,?,?,?,?,?,?)"; + $sth = $this->db->prepare($sql); + $rs = $sth->execute(array($uuid,$user->username,$reply,$uid,$body,$email,$ipaddr)); + + if($rs) + { + $sql="select c.author,c.email,c.uuid from comments c where c.id=?"; + $sth=$this->db->prepare($sql); + $sth->execute(array($reply)); + $row=$sth->fetch(); + + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"comment-admin-reply",array( + 'user' => $row['author'], + 'uuid' => $row['uuid'], + 'email'=> $row['email'] + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + $mail->addTo($row['email']); + $mail->addCc($this->view->config->service->email); + @$mail->send(); + + $data = array('status'=>1,'msg'=>'回复成功!'); + $this->jsonexit($data); + return true; + }else{ + $data = array('error'=>"回复失败,请重试"); + $this->jsonexit($data); + return true; + } + return true; + } + + if($uuid) + { + $sql = "SELECT c.*,md.title,md.uuid FROM comments c + LEFT JOIN $submd md ON md.uuid=c.uuid + WHERE c.uuid=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($uuid)); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage($this->view->config->page->max); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); + $this->view->paginator=$paginator; + + $sql = "SELECT title FROM metadata WHERE uuid=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($uuid)); + $row = $sth->fetch(); + + $this->view->title = $row['title']; + + return true; + } + + if($replylist) + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $sql = "SELECT cr.id,cr.content as body,cr.reply,u.username,cr.ts_created FROM comments cr + LEFT JOIN users u ON cr.userid=u.id WHERE cr.reply=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($replylist)); + $rows = $sth->fetchAll(); + + $this->jsonexit($rows); + return true; + + } + + if($delreply) + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $sql = "DELETE FROM comments WHERE id=?"; + $sth = $this->db->prepare($sql); + $rs = $sth->execute(array($delreply)); + + if($rs) + { + $this->jsonexit( + array('deleted'=>1) + ); + return true; + }else{ + $this->jsonexit( + array('error'=> '处理中出现错误,请重新尝试') + ); + return true; + } + + + } + + + $sql = "SELECT cm.*,md.title,(SELECT count(id) as counts FROM comments cms WHERE cms.reply=cm.id AND cms.reply!=0) as reply_count FROM comments cm + right JOIN $submd md ON md.uuid=cm.uuid + WHERE cm.reply=0 + ORDER BY cm.ts_created DESC,cm.id DESC"; + + $sth = $this->db->query($sql); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage($this->view->config->page->max); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); + $this->view->paginator=$paginator; + + }//comment + + + + + /* + * 删除元数据,删除前有确认 + */ + function deleteAction() + { + if ($this->_request->isPost()) { + $id = (int)$this->_request->getPost('id'); + $del = $this->_request->getPost('del'); + if ($del == 'Yes' && $id > 0) { + $md = new MetadataTable(); + $where = 'id = ' . $id; + $md->delete($where); + } + $this->_redirect('/data'); + } else { + $id = (int)$this->_request->getParam('id'); + if ($id > 0) { + $mdt = new MetadataTable(); + $this->view->md = $mdt->fetchRow('id='.$id); + } + } + } + + function sourceAction() + { + $do = $this->_request->getParam('do'); + $uuid = $this->_request->getParam('uuid'); + $id = $this->_request->getParam('id'); + $q = $this->_request->getParam('q'); + $search = $this->_request->getParam('search'); + + if ($do == 'add') { + + $redirect = "/admin/data/source/"; + + if(!empty($_POST['submit'])) + { + try{ + $title = $this->_request->getParam('title'); + $uuid = $this->_request->getParam('uuid'); + $code = $this->_request->getParam('code'); + $description = $this->_request->getParam('description'); + $has_pages = $this->_request->getParam('has_pages'); + $has_agreement = $this->_request->getParam('has_agreement'); + + $data = array( + 'title' => $title, + 'uuid' => $uuid, + 'code' => $code, + 'description' => $description, + 'has_pages' => $has_pages, + 'has_agreement' => $has_agreement + ); + + if($this->db->insert('source',$data)) + { + $this->messenger->addMessage('添加来源信息成功'); + $this->_redirect($redirect); + } + }catch(Exception $e){ + $this->messenger->addMessage('添加来源信息失败:'.$e->getMessage()); + $this->_redirect('/admin/data/source/do/add'); + } + + } + + $this->_helper->viewRenderer('sourceadd'); + }// 添加项目来源 + + else if($do == 'edit' && !empty($id)) + { + + if(!empty($_POST['submit'])) + { + try{ + $title = $this->_request->getParam('title'); + $uuid = $this->_request->getParam('uuid'); + $code = $this->_request->getParam('code'); + $description = $this->_request->getParam('description'); + $has_pages = $this->_request->getParam('has_pages'); + $has_agreement = $this->_request->getParam('has_agreement'); + + $sql = "update source set title='$title',uuid='$uuid',code='$code',description='$description',has_pages='$has_pages',has_agreement='$has_agreement' where id='$id'"; + + if($this->db->exec($sql)) + { + $this->messenger->addMessage('修改来源信息成功'); + $this->_redirect("/admin/data/source/do/edit/id/$id"); + } + }catch(Exception $e){ + $this->messenger->addMessage('修改来源信息失败:'.$e->getMessage()); + $this->_redirect("/admin/data/source/do/edit/id/$id"); + } + + } + + $sql = "select * from source where id='$id'"; + $rs = $this->db->query($sql); + $row = $rs->fetch(); + + $this->view->info = $row; + + $this->_helper->viewRenderer('sourceadd'); + + }// 编辑单条信息 + + else if($do == 'datasource' && !empty($uuid)) + { + $redirect = "/admin/data/source/do/datasource/uuid/$uuid"; + + $this->view->uuid = $uuid; + $sql = "select md.title,ds.id,ds.sourceid,s.title as stitle,s.code from metadata md + left join datasource ds on ds.uuid=md.uuid + left join source s on s.id=ds.sourceid + where md.uuid='$uuid'"; + $rs = $this->db->query($sql); + $row = $rs->fetch(); + + $this->view->info = $row; + + if(!empty($_POST['submit'])) + { + $sourceid = $this->_request->getParam('sourceid'); + if(empty($sourceid)) + { + $this->messenger->addMessage('请选择项目来源'); + $this->_redirect($redirect); + } + + $sql=""; + if(empty($row['id'])) + { + $sql = "insert into datasource (uuid,sourceid) values ('$uuid','$sourceid')"; + }else + { + $sql = "update datasource set uuid='$uuid',sourceid='$sourceid' where id='{$row['id']}'"; + } + try{ + if($this->db->exec($sql)) + { + $this->messenger->addMessage('修改项目来源成功'); + $this->_redirect($redirect); + } + else + { + $this->messenger->addMessage('修改项目来源失败'); + $this->_redirect($redirect); + } + }catch (Exception $e){ + $this->messenger->addMessage('修改项目来源失败:'.$e->getMessage()); + $this->_redirect($redirect); + } + + } + + + $wheresql = array(); + + if(!empty($q) && !empty($search)) + { + $this->view->q = $q; + $wheresql[] = " title like '%$q%' "; + } + + if(count($wheresql>0))$wheresql = join(' and ',$wheresql); + else $wheresql=''; + + if($wheresql!='') + { + $wheresql = 'where '.$wheresql; + } + + $sql = "select * from source $wheresql order by id desc"; + $rs = $this->db->query($sql); + $rows = $rs->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(30); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + + $this->_helper->viewRenderer('sourceselect'); + }// 为元数据选择项目来源 + + else if($do == 'del' && !empty($id)) + { + $redirect = "/admin/data/source/"; + $sql = "delete from source where id='$id'"; + try{ + if($this->db->exec($sql)) + { + $this->messenger->addMessage('删除成功'); + $this->_redirect($redirect); + }else{ + $this->messenger->addMessage('删除失败,可能该数据已不存在'); + $this->_redirect($redirect); + } + }catch (Exception $e){ + $this->messenger->addMessage('删除失败:'.$e->getMessage()); + $this->_redirect($redirect); + } + }// 删除项目来源 + + else if($do == 'fetch' && !empty($id)) + { + + $wheresql = array(); + + $wheresql[] = " ds.sourceid='$id' "; + + if(!empty($q) && !empty($search)) + { + $this->view->q = $q; + $wheresql[] = " title like '%$q%' "; + } + + if(count($wheresql>0))$wheresql = join(' and ',$wheresql); + else $wheresql=''; + + if($wheresql!='') + { + $wheresql = 'where '.$wheresql; + } + + $sql = "select md.title,md.uuid,ds.id,ds.sourceid,s.title as stitle,s.code from metadata md + left join datasource ds on ds.uuid=md.uuid + left join source s on s.id=ds.sourceid + $wheresql + order by id desc"; + $rs = $this->db->query($sql); + $rows = $rs->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(30); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + + $this->_helper->viewRenderer('sourcefetchone'); + + }//查看某项目来源中的所有元数据 + + else if($do == 'delsource' && !empty($id)) + { + $redirect = "/admin/data/source/do/datasource/uuid/$uuid"; + + $sql = "delete from datasource where id='$id'"; + + try{ + if($this->db->exec($sql)) + { + $this->messenger->addMessage('删除成功'); + $this->_redirect($redirect); + }else{ + $this->messenger->addMessage('删除失败,可能该数据已不存在'); + $this->_redirect($redirect); + } + }catch (Exception $e){ + $this->messenger->addMessage('删除失败:'.$e->getMessage()); + $this->_redirect($redirect); + } + + + }// 清除元数据来源记录 + elseif ($do=='sync') { //同步数据来源到metadata表 + $redirect = "/admin/data/source/"; + $sql = "update metadata m set source=s.uuid from source s right join datasource d on s.id=d.sourceid where m.uuid=d.uuid"; + if($this->db->exec($sql)) + { + $this->messenger->addMessage('成功同步数据来源'); + $this->_redirect($redirect); + }else{ + $this->messenger->addMessage('同步数据来源失败'); + $this->_redirect($redirect); + } + } + + else + { + $wheresql = array(); + + if(!empty($q) && !empty($search)) + { + $this->view->q = $q; + $wheresql[] = " title like '%$q%' "; + } + + if(count($wheresql>0))$wheresql = join(' and ',$wheresql); + else $wheresql=''; + + if($wheresql!='') + { + $wheresql = 'where '.$wheresql; + } + + $sql = "select * from source $wheresql order by id desc"; + $rs = $this->db->query($sql); + $rows = $rs->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(30); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + }// 项目来源管理 + + + }//function sourceAction + + + function attachmentsAction() + { + $submit = $this->_request->getParam('submit'); + $add = $this->_request->getParam('add'); + $search = $this->_request->getParam('search'); + $delete = $this->_request->getParam('delete'); + $edit = $this->_request->getParam('edit'); + $down = $this->_request->getParam('down'); + $uuid = $this->_request->getParam('uuid'); + $mdtitle = $this->_request->getParam('mdtitle'); + $mdattdel = $this->_request->getParam('mdattdel'); + $attupdate = $this->_getParam('attupdate'); + + if(!empty($uuid)&&!empty($mdtitle)) + { + $this->view->uuid= $uuid; + $this->view->mdtitle=$mdtitle; + } + + if($add) + { + $this->_helper->viewRenderer('attachmentsadd'); + if(!empty($uuid)) + { + $this->view->uuid = $uuid; + } + }//附件添加 + + elseif($attupdate) + { + $submit = $this->_getParam('submit'); + if(!empty($submit)) + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $id = $this->_getParam('attupdate'); + + $files=new Files(); + $msg = $files -> upload($_FILES['Filedata'],empty($uuid)?'file/':'md/',true); + + if(empty($msg['error'])) + { + $msg['error']=""; + $filename = $msg['db_path']; + $filesize = $msg['file_size']; + $filedesc = $this->_request->getParam('filedesc'); + $filetype = 'md'; + $realname = $msg['realname']; + + + $sql = "UPDATE attachments SET filename=?,filetype=?,filesize=?,realname=? WHERE id=?"; + $sth = $this->db->prepare($sql); + $ds = $sth->execute(array($filename,$filetype,$filesize,$realname,$id)); + + if($ds) + { + $msg['html'] = $realname.'['. round($filesize/1024,2) .' kb]'; + $msg['ok']= 1; + echo Zend_Json::encode($msg); + exit(); + }else{ + @unlink($filename); + $data = array("error"=>'附件上传失败:写入附件表出错'); + $this->jsonexit($data); + return true; + } + }else{ + @unlink($filename); + $data = array("error"=>'附件上传失败:'.$msg['error']); + $this->jsonexit($data); + return true; + } + + + $data = array("error"=>"处理中出现错误".$id); + $this->jsonexit($data); + return true; + }else{ + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer('attachments-update'); + $id = $this->_getParam('attupdate'); + $this->view->id = $id; + } + + }//附件文件更新 + + else if($mdattdel) + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + + $sql = "delete from mdattach where uuid=? AND id=?"; + $sth = $this->db->prepare($sql); + $ds = $sth->execute(array($uuid,$mdattdel)); + if($ds) + { + $data = array("status"=>1); //操作状态代码 : 1=>成功 2=>失败 + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>"处理中出现错误"); + $this->jsonexit($data); + return true; + } + } + + else if($delete) + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + + //需要删除文件,通过Zend_Registry::get('upload')获得上传文件的根目录 + + $basepath = $this->view->config->upload; + + //从数据库获取文件路径 + + $info = $this->getFileinfo($delete); + + $filepath = $basepath.$info['filename']; + + $sql = "SELECT * FROM mdattach WHERE id=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($delete)); + $rows = $sth->fetchAll(); + + if(count($rows)>0) + { + $data = array("error"=>'删除失败!该文件有元数据附件信息,不能直接删除',"status"=>0); + $this->jsonexit($data); + return true; + } + + $sql = "SELECT * FROM mdreviewattach WHERE attachid=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($delete)); + $rows = $sth->fetchAll(); + + if(count($rows)>0) + { + $data = array("error"=>'删除失败!该文件有对应评审附件信息,不能直接删除',"status"=>0); + $this->jsonexit($data); + return true; + } + + $sql = "SELECT link FROM reference WHERE link!=''"; + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + $ids = array(); + foreach($rows as $k=>$v) + { + if(preg_match("/service/i",$v['link'])) + { + $ids[] = str_replace("/service/attach/id/","",$v['link']); + } + unset($rows[$k]); + } + unset($rows); + + if(in_array($delete,$ids)) + { + $data = array("error"=>'删除失败!该文件有对应文献附件信息,不能直接删除',"status"=>0); + $this->jsonexit($data); + return true; + } + + if(unlink($filepath)) + { + $sql = "delete from attachments where id='$delete'"; + if($this->db->exec($sql)>0) + { + $data = array("status"=>1); + $this->jsonexit($data); + return true; + } + } + else + { + $sql = "delete from attachments where id='$delete'"; + if($this->db->exec($sql)>0) + { + $data = array("error"=>'文件删除失败,仅删除数据库记录,请手动删除文件:'.$info['filename'],"status"=>0); + $this->jsonexit($data); + return true; + } + } + + }//删除 + + else if($edit>0) + { + + if(empty($submit)) + { + + $this->view->info = $this->getFileinfo($edit); + + $this->_helper->viewRenderer('attachmentsadd'); + + } + else + { + $filedesc = $this->_request->getParam('filedesc'); + + $sql="update attachments set filedesc='$filedesc' where id='$edit'"; + + if($this->db->exec($sql)>0) + { + $this->messenger->addMessage('编辑成功'); + $this->_redirect("/admin/data/attachments/edit/$edit"); + } + } + + }//编辑 + + else if($search && $search!='my') + { + $keyword = $this->_request->getParam('keyword'); + if(empty($keyword)) + { + $this->messenger->addMessage('请输入关键词'); + $this->_redirect("/admin/data/attachments/search/1"); + } + else + { + $sql="select * from attachments where filedesc like '%$keyword%'"; + $re = $this->db->query($sql); + $rows=$re->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(30); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + } + + }//搜索 + + else if($search && $search=='my') + { + $auth = Zend_Auth::getInstance(); + if($auth->hasIdentity()) + { + $user = $auth->getIdentity(); + $userid = $user->id; + $sql="select * from attachments where userid='$userid'"; + $re= $this->db->query($sql); + $rows = $re->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(30); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); + $this->view->paginator=$paginator; + } + }//我的附件列表 + + else if ($down>0) + { + + $sql = "select * from attachments where id='$down'"; + + $re = $this->db->query($sql); + + $row = $re->fetch(); + + $file = new files(); + + $fullPath = $this->view->config->upload.$row['filename']; + + + // Parse Info / Get Extension + $fsize = filesize($fullPath); + $path_parts = pathinfo($fullPath); + $ext = strtolower($path_parts["extension"]); + + // Determine Content Type + switch ($ext) { + case "pdf": $ctype="application/pdf"; break; + case "exe": $ctype="application/octet-stream"; break; + case "zip": $ctype="application/zip"; break; + case "doc": $ctype="application/msword"; break; + case "xls": $ctype="application/vnd.ms-excel"; break; + case "ppt": $ctype="application/vnd.ms-powerpoint"; break; + case "gif": $ctype="image/gif"; break; + case "png": $ctype="image/png"; break; + case "jpeg": + case "jpg": $ctype="image/jpg"; break; + default: $ctype="application/force-download"; + } + + $content=file_get_contents($fullPath); + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + $this->getResponse()->setHeader('Content-Type', 'application/octet-stream') + ->setHeader('Content-Disposition','attachment; filename="'.basename($fullPath).'"') + ->setHeader('Content-Length', $fsize) + ->setHeader('Content-Type','application/force-download') + ->setHeader('Content-Type','application/download') + ->setHeader('Content-Type',$ctype) + ->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); + + //$sql = "update attachments set downtimes=downtimes+1 where id='$down'"; + //$this->db->exec($sql); + + }//附件下载 + + else + { + $submd=$this->view->config->sub->metadata; + $sql="select * from attachments where id in (select id from mdattach where uuid in (select uuid from $submd)) order by id desc"; + $re= $this->db->query($sql); + $rows = $re->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(30); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml'); + $this->view->paginator=$paginator; + }//所有附件 + }//attachments 附件 + + function uploadAction(){ + $this->_helper->layout()->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $uuid = $this->_getParam('uuid'); + + try{ + $auth = Zend_Auth::getInstance(); + if($auth->hasIdentity()) + { + $user = $auth->getIdentity(); + $userid = $user->id; + } + + if(empty($userid)||!is_numeric($userid)){ + $msg['error'] = "请先登录"; + echo Zend_Json::encode($msg); + exit(); + } + + if($user->usertype!='administrator') + { + $msg['error'] = "您无权使用此功能"; + echo Zend_Json::encode($msg); + exit(); + } + + $files=new Files(); + $msg = $files -> upload($_FILES['Filedata'],empty($uuid)?'file/':'md/',true); + + if(empty($msg['error'])) + { + $msg['error']=""; + $filename = $msg['db_path']; + $filesize = $msg['file_size']; + $filedesc = $this->_request->getParam('filedesc'); + $filetype = 'md'; + $realname = $msg['realname']; + + + $sql = "insert into attachments (filename,filetype,filedesc,userid,filesize,realname) values ('$filename','$filetype','$filedesc','$userid','$filesize','$realname') RETURNING id"; + $sth = $this->db->prepare($sql); + $sth->execute(); + $att = $sth->fetch(PDO::FETCH_ASSOC); + $msg['attid'] = $attid = $att['id']; + + $sql = "insert into mdattach (uuid,id) values ('$uuid','$attid')"; + if(empty($uuid) || $this->db->exec($sql)) + { + $msg['html'] = $realname.'['. round($filesize/1024,2) .' kb]
'; + echo Zend_Json::encode($msg); + exit(); + }else{ + $msg['error'] = '附件上传失败:写入附件表出错'; + @unlink($filename); + echo Zend_Json::encode($msg); + exit(); + } + + + }else{ + $msg['error'] = '附件上传失败:'.$msg['error']; + @unlink($filename); + echo Zend_Json::encode($msg); + exit(); + } + + }catch(Exception $e){ + $msg['error'] = "错误:".$e->getMessage(); + echo Zend_Json::encode($msg); + exit(); + } + }// uploadAction ajax上传附件 + + function getattsAction(){ + + $this->_helper->layout()->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $uuid = $this->_request->getParam('uuid'); + + if($uuid!='') + { + $auth = Zend_Auth::getInstance(); + if($auth->hasIdentity()) + { + $user = $auth->getIdentity(); + $userid = $user->id; + $sql = "select m.*,a.*,d.title from mdattach m + left join attachments a on m.id = a.id + left join metadata d on m.uuid=d.uuid where m.uuid='$uuid' + ORDER BY a.ts_created ASC"; + $rs = $this->db->query($sql); + $atts = $rs->fetchAll(); + + echo Zend_Json::encode($atts); + exit(); + }else + { + exit(); + } + }else{ + exit(); + } + //不输出错误 + }//获取附件 + + + /* + versionAction 版本管理 + */ + + public function versionAction(){ + + $ac = $this->_request->getParam('ac'); + $uuid = $this->_request->getParam('uuid'); + $submd=$this->view->config->sub->metadata; + set_time_limit(0); + + $auth = Zend_Auth::getInstance(); + if($auth->hasIdentity()) + { + $user = $auth->getIdentity(); + $u_id = $user->id; + } + + //查看单条数据的所有版本 + if (!empty($uuid) && empty($ac)) + { + //view the versions of the data + $sql = "SELECT md.title,md.uuid,v.ts_created,v.changelog,v.userid,v.id,u.username,u.realname,g.id as gid FROM mdversion v + LEFT JOIN $submd md ON md.uuid=v.uuid + left join users u on v.userid=u.id + left join geonetworkmetadata g on md.uuid=g.uuid + WHERE md.title IS NOT NULL AND v.uuid=? + order by v.ts_created desc + "; + $sth = $this->db->prepare($sql); + $sth->execute(array($uuid)); + $rows = $sth->fetchAll(); + @$this->view->mdtitle=$rows[0]['title']; + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(15); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + } + + //查看所有版本列表 + else if((empty($ac) && empty($uuid))|| $ac=='list') + { + $sql = "SELECT md.title,md.uuid,v.ts_created,v.changelog,v.userid,v.id,u.username,u.realname FROM mdversion v + LEFT JOIN $submd md ON md.uuid=v.uuid + left join users u on v.userid=u.id + WHERE md.title IS NOT NULL + order by v.ts_created desc + "; + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(15); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + } + + //按数据浏览 + else if($ac=="bydata") + { + $keywords = $this->_request->getParam('q'); + if(!empty($keywords)) + $this->view->q = $keywords; + $sql = "SELECT md.title,md.uuid,count(v.id) as c FROM mdversion v + LEFT JOIN $submd md ON md.uuid=v.uuid + WHERE md.title IS NOT NULL"; + if(!empty($keywords)) + { + $search=new Search($keywords); + $where=$search->sql_expr(array("md.title","md.description")); + $sql.=' and '.$where; + } + $sql.=" group by md.uuid,md.title"; + + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(10); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + + $this->_helper->viewRenderer('version-bydata'); + } + + + //删除某个版本 + else if($ac=="delete") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $data = ""; + + try{ + $id = $this->_request->getParam('id'); + if(empty($id) || !is_numeric($id)) + { + $data = array("error"=>"参数错误"); + $this->jsonexit($data); + return true; + } + + $sql = "DELETE FROM mdversion WHERE id=?"; + $sth = $this->db->prepare($sql); + $ex = $sth -> execute(array($id)); + + if($ex) + { + $data = array("deleted"=>$id, "error"=>"删除成功"); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>'删除失败,请确认权限后重试'); + $this->jsonexit($data); + return true; + } + }catch(Exception $e) { + $msg = "删除失败,请确认权限后重试"; + if($this->debug>0) + {$msg .= $e->getMessage();} + $data = array("error"=>$msg); + $this->jsonexit($data); + return true; + } + } + + //恢复到geonetwork + else if($ac == "restore") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $data = ""; + try{ + $id = $this->_request->getParam('id'); + if(empty($id) || !is_numeric($id)) + { + $data = array("error"=>"参数错误"); + $this->jsonexit($data); + return true; + } + + $this->wdb=Zend_Db::factory($this->view->config->geonetwork); + $sql = "SELECT v.xml,v.uuid FROM mdversion v + WHERE v.id=?"; + $sth = $this->db->prepare($sql); + $sth ->execute(array($id)); + $row = $sth->fetch(); + + $sql = "SELECT data FROM metadata WHERE uuid=?"; + $sth = $this->wdb->prepare($sql); + $sth ->execute(array($row['uuid'])); + $row_geo = $sth->fetch(); + + if($row['xml']==$row_geo['data']) + { + $data = array("error"=>'无须恢复,元数据相同'); + $this->jsonexit($data); + return true; + } + + $sql = "UPDATE metadata SET data=? WHERE uuid=?"; + $sth = $this->wdb->prepare($sql); + $ex = $sth ->execute(array($row['xml'],$row['uuid'])); + + if($ex) + { + $data = array("error"=>'恢复成功'); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>'恢复失败,请确认权限后重试'); + $this->jsonexit($data); + return true; + } + }catch(Exception $e) { + $msg = "恢复失败,请确认权限后重试"; + if($this->debug>0) + {$msg .= $e->getMessage();} + $data = array("error"=>$msg); + $this->jsonexit($data); + return true; + } + } + + //发布到评审 + else if($ac == "commit") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $data = ""; + try{ + $id = $this->_request->getParam('id'); + if(empty($id) || !is_numeric($id)) + { + $data = array("error"=>"参数错误"); + $this->jsonexit($data); + return true; + } + + $changelog = $this->_request->getParam('changelog'); + if(empty($changelog)) + { + $data = array("error"=>'请输入变更信息'); + $this->jsonexit($data); + return true; + } + + // 1. 权限认定 --skip + // 2. 保存变化记录 save changelog & userid + $sql = "UPDATE mdversion SET changelog=?,userid=? WHERE id=?"; + $this->db->query($sql,array($changelog,$u_id,$id)); + + // 3. 获取数据评审状态 + $sql = "SELECT s.*,v.xml,m.title FROM mdstatus s left join mdversion v on s.uuid=v.uuid + left join metadata m on s.uuid=m.uuid WHERE v.id=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($id)); + $row = $sth->fetch(); + + $sql="select * from xunsearch where uuid=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($row['uuid'])); + $data_search = $sth->fetch(); + $search=new Search(); + $search->update($data_search); + + if (empty($row)) //无对应记录 + { + $sql="select m.id from metadata m left join mdversion v on m.uuid=v.uuid where v.id=?"; + $sth=$this->db->prepare($sql); + $sth->execute(array($id)); + $mrow=$sth->fetch(); + if (empty($mrow)) //说明是新数据 + { + //导入元数据 + $iso=new ISO19115(); + $iso->saveDB($this->db,$row['xml']); + //进入评审库 + $sql="insert into mdstatus (uuid,status,userid) select uuid,0,? from mdversion where id=?"; + $this->db->query($sql,array($u_id,$id)); + + //email to admin + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"metadata-new-admin",array( + 'user' => $user->username, + 'uuid' => $iso->uuid, + 'email'=> $user->email, + //元数据标题 + 'title'=> $iso->resTitle, + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + $mail->send(); + + unset($mail); + unset($mailtp); + //email to author + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"metadata-new-author",array( + 'user' => $user->username, + 'uuid' => $iso->uuid, + 'email'=> $user->email, + //元数据标题 + 'title'=> $iso->resTitle, + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($user->email); + $mail->addCc($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + @$mail->send(); + + $data = array("commited"=>1,"error"=>'该版本已经成功提交,请等待数据中心进一步处理!'); + $this->jsonexit($data); + return true; + } else { //说明是已发布的数据且数据不存在评审信息 + //同步元数据 + $iso=new ISO19115(); + $iso->saveDB($this->db,$row['xml']); + //移除中间版本 + $sql="delete from mdversion where uuid in (select uuid from mdversion where id=?) and changelog is null"; + $this->db->query($sql,array($id)); + //修改评审状态为发布,且由其提交的用户进行管理 + $sql="insert into mdstatus (uuid,status,userid) select uuid,6,? from mdversion where id=?"; + $this->db->query($sql,array($u_id,$id)); + + //email to admin & author + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"metadata-release",array( + 'user' => $user->username, + 'uuid' => $row['uuid'], + 'email'=> $user->email, + //元数据标题 + 'title'=> $row['title'], + 'changelog'=>$changelog, + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($this->view->config->service->email); + $mail->addCc($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + @$mail->send(); + + $data = array("commited"=>1,"error"=>'该版本已经成功发布!'); + $this->jsonexit($data); + return true; + } + } + else if ($row['status']==-1 || $row['status']==0 || $row['status']==1) //取消发布的数据,初始状态,已接收 + { + //同步元数据 + $iso=new ISO19115(); + $iso->saveDB($this->db,$row['xml']); + //email to admin + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"version-commit-admin",array( + 'user' => $user->username, + 'uuid' => $row['uuid'], + 'email'=> $user->email, + //元数据标题 + 'title'=> $row['title'], + 'changelog'=>$changelog, + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + $mail->send(); + + unset($mail); + unset($mailtp); + //email to author + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'数据服务组'); + $mailtp=new EmailText($this->db,"version-commit-author",array( + 'user' => $user->username, + 'uuid' => $row['uuid'], + 'email'=> $user->email, + //元数据标题 + 'title'=> $row['title'], + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($user->email); + $mail->addCc($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + @$mail->send(); + + $data = array("commited"=>1,"error"=>'该版本已经成功提交并同步,请等待数据中心进一步处理!'); + $this->jsonexit($data); + return true; + } + else if ($row['status']==2 || $row['status']==3 || $row['status']==4)//已发送过外审邮件,需由编辑告知变化信息 + { + //同步元数据 + $iso=new ISO19115(); + $iso->saveDB($this->db,$row['xml']); + + //email to admin + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"version-commit-admin",array( + 'user' => $user->username, + 'uuid' => $row['uuid'], + 'email'=> $user->email, + //元数据标题 + 'title'=> $row['title'], + 'changelog'=>$changelog, + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + $mail->send(); + + unset($mail); + unset($mailtp); + //email to author + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'数据服务组'); + $mailtp=new EmailText($this->db,"version-commit-author",array( + 'user' => $user->username, + 'uuid' => $row['uuid'], + 'email'=> $user->email, + //元数据标题 + 'title'=> $row['title'], + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($user->email); + $mail->addCc($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + @$mail->send(); + + //email to experts + $sql="select u.username,u.email,u.id from mdexpertreview e left join users u on e.id=u.id where e.status in (0,1) and e.uuid=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($row['uuid'])); + $experts = $sth->fetchAll(); + + unset($mail); + unset($mailtp); + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"version-commit-expert",array( + 'user' => $user->username, + 'uuid' => $row['uuid'], + 'email'=> $user->email, + //元数据标题 + 'title'=> $row['title'], + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + $filecontent=file_get_contents("http://" . $_SERVER['HTTP_HOST'].'/service/doc/uuid/'.$row['uuid']); + $mail->createAttachment($filecontent,'application/octet-stream',Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, $row['title'].'.doc'); + $filecontent=file_get_contents("http://" . $_SERVER['HTTP_HOST'].'/service/pdf/uuid/'.$row['uuid']); + $mail->createAttachment($filecontent,'application/octet-stream',Zend_Mime::DISPOSITION_ATTACHMENT, Zend_Mime::ENCODING_BASE64, $row['title'].'.pdf'); + if($this->debug==0) + { + foreach ($experts as $expert) $mail->addTo($expert['email']); + $mail->addCc($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + @$mail->send(); + + $data = array("commited"=>1,"error"=>'该版本已经成功提交,请等待数据中心进一步处理!'); + $this->jsonexit($data); + return true; + } + else if ($row['status']>=5)//数据已经发布,再次修改后将只通知管理员,保留发布状态 + { + //同步元数据 + $iso=new ISO19115(); + $iso->saveDB($this->db,$row['xml']); + //移除中间版本 + $sql="delete from mdversion where uuid in (select uuid from mdversion where id=?) and changelog is null"; + $this->db->query($sql,array($id)); + + //email to admin & author + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'数据服务组'); + $mailtp=new EmailText($this->db,"metadata-release",array( + 'user' => $user->username, + 'uuid' => $row['uuid'], + 'email'=> $user->email, + //元数据标题 + 'title'=> $row['title'], + 'changelog'=>$changelog, + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($this->view->config->service->email); + //$mail->addCc($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + //@$mail->send(); + + $data = array("commited"=>1,"error"=>'该版本已经成功发布!'); + $this->jsonexit($data); + return true; + } + + }catch(Exception $e) { + $msg = "提交失败,请确认权限后重试"; + if($this->debug>0) + {$msg .= $e->getMessage();} + $data = array("error"=>$msg); + $this->jsonexit($data); + return true; + } + }//发布到评审 + + //与前一个版本对比 + else if($ac == "diff" || $ac=="diff1") + { + $this->_helper->viewRenderer('version-diff'); + $id = $this->_request->getParam('id'); + if(empty($id) || !is_numeric($id)) + { + $this->view->error = "参数错误"; + return true; + } + + $sql = "SELECT v.uuid,md.title FROM mdversion v + LEFT JOIN metadata md ON v.uuid=md.uuid + WHERE v.id=?"; + $sth = $this->db->prepare($sql); + $sth -> execute(array($id)); + + $row = $sth->fetch(); + if ($ac=='diff') + { + $sql = "SELECT v.* FROM mdversion v + WHERE v.uuid=? and v.ts_created<=(select ts_created from mdversion where id=?) + ORDER BY v.ts_created DESC + LIMIT ?"; + $sth = $this->db->prepare($sql); + $sth -> execute(array($row['uuid'],$id,2)); + } else { + $sql = "SELECT v.* FROM mdversion v + WHERE v.uuid=? and (v.id=? or (v.ts_created<(select ts_created from mdversion where id=?) and changelog is not null)) + ORDER BY v.ts_created DESC + LIMIT ?"; + $sth = $this->db->prepare($sql); + $sth -> execute(array($row['uuid'],$id,$id,2)); + } + + $rows = $sth->fetchAll(); + + if(count($rows)<2) + { + $this->view->error = "对比失败:之前没有版本可以对比"; + return true; + } + + $this->view->info = $row; + $this->view->data = $rows; + + } + + + }//versionAction 版本控制 + + + /* + * authorAction() 数据作者 + * + * param string $ac //动作 + =add 添加 + =edit 编辑 + =update 更新 + =del 删除 + =list 列出所有数据作者 + + * param string $uuid //UUID + * + * return view|application-json + */ + public function authorAction(){ + + $ac = $this->_getParam('ac'); + $uuid = $this->_getParam('uuid'); + $submd=$this->view->config->sub->metadata; + + if(empty($ac) && empty($uuid)) + { + + $keywords = $this->_request->getParam('q'); + if(!empty($keywords)) + $this->view->q = $keywords; + + $sql = "select md.id,md.title,md.uuid,count(a.userid) as c from $submd md + right JOIN mdauthor a ON md.uuid=a.uuid + "; + + if(!empty($keywords)) + { + $search=new SimpleSearch($keywords); + $where=$search->sql_expr(array("md.title")); + $sql.=' and ('.$where.")"; + } + + $sql .= "GROUP by md.id,md.title,md.uuid,md.ts_created + ORDER BY md.ts_created DESC"; + + $sth = $this->db->prepare($sql); + $sth-> execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(15); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + + }//列表 + + + else if($ac == 'add') + { + + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $username = $this->_getParam('username'); + $uuid = $this->_getParam('uuid'); + + 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)) + { + $data = array("error"=>"uuid参数错误"); + $this->jsonexit($data); + return true; + } + + if(empty($username)) + { + $data = array("error"=>'请输入要添加为该数据作者的用户名'); + $this->jsonexit($data); + return true; + } + + $sql = "SELECT * FROM users WHERE username=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($username)); + $row = $sth->fetch(); + + if(empty($row['id']) || !isset($row['id'])) + { + $data = array("error"=>'您输入的用户名无任何对应用户'); + $this->jsonexit($data); + return true; + } + + $uid = $row['id']; + + $sql = "SELECT * FROM mdauthor WHERE userid=? AND uuid=?"; + $sth = $this->db->prepare($sql); + $sth->execute(array($row['id'],$uuid)); + $row = $sth->fetch(); + + if(!empty($row['id']) && $row['status']==1) + { + $data = array("error"=>'该用户已经是此数据的作者'); + $this->jsonexit($data); + return true; + } + + if(!empty($row['id']) && in_array($row['status'],array(0,-1)) ) + { + $sql = "UPDATE mdauthor SET status=1 WHERE uuid=? AND userid=?"; + $sth = $this->db->prepare($sql); + $ds = $sth->execute(array($uuid,$uid)); + if($ds) + { + $data = array("msg"=>'该用户已被提升为数据作者','added'=>1); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>'处理中出现错误'); + $this->jsonexit($data); + return true; + } + } + + if(empty($row['id'])) + { + $sql = "INSERT INTO mdauthor (uuid,userid,status) VALUES (?,?,?)"; + $sth = $this->db->prepare($sql); + $ds = $sth->execute(array($uuid,$uid,1)); + if($ds) + { + $data = array("msg"=>'该用户已被添加为数据作者','added'=>1); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>"处理中出现错误"); + $this->jsonexit($data); + return true; + } + } + + + $data = array("error"=>"处理中出现错误"); + $this->jsonexit($data); + return true; + + }//ajax 添加作者 + + + else if(($ac == "edit" || $ac=="") && !empty($uuid)) + { + + $window = $this->_getParam('window'); + + if($window == "iframe") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer('author-edit-iframe'); + }else{ + $this->_helper->viewRenderer('author-edit'); + } + + $sql = "SELECT a.*,md.title,u.realname,u.username FROM mdauthor a + LEFT JOIN $submd md ON a.uuid=md.uuid + LEFT JOIN users u ON a.userid=u.id + WHERE md.uuid=? + ORDER BY a.id DESC + "; + + $sth = $this->db->prepare($sql); + $sth->execute(array($uuid)); + $rows = $sth->fetchAll(); + + $this->view->authors = $rows; + + }// 作者管理弹窗 + + else if($ac == "del") + { + + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $id = $this->_getParam('id'); + + $sql = "DELETE FROM mdauthor WHERE id=?"; + $sth = $this->db->prepare($sql); + $ds = $sth->execute(array($id)); + if($ds) + { + $data = array("deleted"=>$id); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>"处理中出现错误"); + $this->jsonexit($data); + return true; + } + + + }//移除作者 + + else if($ac == 'update') + { + + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $id = $this->_getParam('id'); + + $sql = "UPDATE mdauthor SET status=1 WHERE id=?"; + $sth = $this->db->prepare($sql); + $ds = $sth->execute(array($id)); + if($ds) + { + $data = array("updated"=>$id,'msg'=>'认证成功!'); + $this->jsonexit($data); + return true; + }else{ + $data = array("error"=>"处理中出现错误"); + $this->jsonexit($data); + return true; + } + + + }//认证作者 + + else if($ac =="list") + { + $this->_helper->viewRenderer('author-list'); + + $sql = "SELECT u.id,u.realname,u.email,count(a.id) as d FROM mdauthor a + LEFT JOIN users u ON a.userid=u.id + WHERE a.status>0 and uuid in (select uuid from $submd) + GROUP BY u.id,u.realname,u.email + ORDER BY u.id DESC"; + + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(15); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + }//所有作者 + + else if($ac == "userdatas") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $id = $this->_getParam('id'); + + $sql = "SELECT md.title,md.uuid FROM mdauthor a + LEFT JOIN $submd md ON a.uuid=md.uuid + WHERE a.userid=? AND a.status>0 + ORDER BY a.ts_created"; + $sth = $this->db->prepare($sql); + $sth->execute(array($id)); + $rows = $sth->fetchAll(); + + $data = array("datas"=>$rows); + $this->jsonexit($data); + return true; + + }//用户数据 + + else if($ac == "datas") + { + $this->_helper->viewRenderer('author-datas'); + + $keywords = $this->_request->getParam('q'); + if(!empty($keywords)) + $this->view->q = $keywords; + + $sql = "select md.id,md.title,md.uuid,count(a.userid) as c from $submd md + left JOIN mdauthor a ON md.uuid=a.uuid + "; + + if(!empty($keywords)) + { + $search=new SimpleSearch($keywords); + $where=$search->sql_expr(array("md.title")); + $sql.=' WHERE '.$where; + } + + $sql .= "GROUP by md.id,md.title,md.uuid,md.ts_created + ORDER BY md.ts_created DESC"; + + $sth = $this->db->prepare($sql); + $sth-> execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(15); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + + } + + }//authorAction() 数据作者管理 + + + //newdataAction() 新建元数据 + public function newdataAction(){ + + $ac = $this->_request->getParam('ac'); + $id = $this->_request->getParam('id'); + $this->wdb=Zend_Db::factory($this->view->config->geonetwork); + + $auth = Zend_Auth::getInstance(); + if($auth->hasIdentity()) + { + $user = $auth->getIdentity(); + $u_id = $user->id; + } + + //在geonetwork中查看单条数据 + if (!empty($id) && empty($ac)) + { + $this->view->url='metadata.show?id='.$id; + $this->_helper->viewRenderer('newdata-view'); + } + //查看所有的未提交数据列表 + else if((empty($ac) && empty($uuid))|| $ac=='list') + { + $sql = "SELECT (regexp_matches(gn.data,'(.*)'))[1] as title,gn.id,gn.uuid,u.username,u.realname FROM geonetworkmetadata gn left join users u on gn.owner=u.id + WHERE gn.uuid not in (select uuid from metadata) + order by gn.id desc + "; + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(15); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + } + //元数据导入 + else if($ac=="import") + { + $this->_helper->viewRenderer('newdata-import'); + } + //从模板新建元数据 + else if($ac=="add-by-template") + { + $keywords = $this->_request->getParam('q'); + $sql="select id,(regexp_matches(data,'(.*)'))[1] as title,(owner-$u_id) as isowner from metadata where istemplate='y' and schemaid='iso19115'"; + if(!empty($keywords)) + { + $this->view->q = $keywords; + $search=new Search($keywords); + $where=$search->sql_expr(array("data")); + $sql.=' and '.$where; + } + $sql.=" order by changedate desc"; + $sth = $this->wdb->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(10); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + $this->_helper->viewRenderer('newdata-add'); + } + + else if($ac=="add-by-data") + { + $keywords = $this->_request->getParam('q'); + $sql = "SELECT md.title,md.uuid,md.description,gn.id as gid FROM normalmetadata md + left join geonetworkmetadata gn on md.uuid=gn.uuid + WHERE gn.id is not null"; + if(!empty($keywords)) + { + $this->view->q = $keywords; + $search=new Search($keywords); + $where=$search->sql_expr(array("md.title","md.description")); + $sql.=' and '.$where; + } + $sql.=" order by md.ts_created desc"; + $sth = $this->db->prepare($sql); + $sth->execute(); + $rows = $sth->fetchAll(); + + $paginator = Zend_Paginator::factory($rows); + $paginator->setCurrentPageNumber($this->_getParam('page')); + $paginator->setItemCountPerPage(10); + $paginator->setView($this->view); + Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination_param.phtml'); + $this->view->paginator=$paginator; + $this->_helper->viewRenderer('newdata-add-bydata'); + } + //元数据格式检查 + else if ($ac=="validate") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $data = ""; + $id = $this->_request->getParam('id'); + $sql="select uuid,data from geonetworkmetadata where id=?"; + $sth=$this->db->prepare($sql); + $sth->execute(array($id)); + $row=$sth->fetch(); + + $iso=new ISO19115(); + @$iso->loadXML($row['data']); + if ($iso->validate()) + { + $data=array("error"=>"元数据中发现错误。
".implode("
",$iso->error)); + $this->jsonexit($data); + return true; + } else { + $data=array("error"=>"元数据中没有发现错误。
"); + $this->jsonexit($data); + return true; + } + } + //提交数据 + else if($ac=="commit") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $data = ""; + $id = $this->_request->getParam('id'); + try{ + if(empty($id) || !is_numeric($id)) + { + $data = array("error"=>"参数错误"); + $this->jsonexit($data); + return true; + } + + $changelog = $this->_request->getParam('changelog'); + if(empty($changelog)) + { + $data = array("error"=>'请输入变更信息'); + $this->jsonexit($data); + return true; + } + + // 1. 权限认定:当前用户必须和其owner相同 + // 数据应当没有评审状态,没有作者信息 + $sql="select uuid,data from geonetworkmetadata where id=?"; + $sth=$this->db->prepare($sql); + $sth->execute(array($id)); + $row=$sth->fetch(); + if (empty($row)) + { + $data = array("error"=>'无权限修改数据'); + $this->jsonexit($data); + return true; + } + + // 保存数据作者信息 + + // 2. 保存变化记录 save changelog & userid for the latest version + $sql = "UPDATE mdversion SET changelog=?,userid=? WHERE id in (select v.id from mdversion v left join geonetworkmetadata g on v.uuid=g.uuid where g.id=? order by v.ts_created desc limit 1)"; + $this->db->query($sql,array($changelog,$u_id,$id)); + + // 3. 保存数据评审状态 + //导入元数据 + $iso=new ISO19115(); + @$iso->saveDB($this->db,$row['data']); + //进入评审库 + $sql="insert into mdstatus (uuid,status,userid) select uuid,0,? from geonetworkmetadata where id=?"; + $this->db->query($sql,array($u_id,$id)); + //email to admin + $mail=new WestdcMailer($this->view->config->smtp); + $mail->setFrom($this->view->config->service->email,'西部数据中心服务组'); + $mailtp=new EmailText($this->db,"metadata-new-admin",array( + 'user' => $user->username, + 'uuid' => $iso->uuid, + 'email'=> $user->email, + //元数据标题 + 'title'=> $iso->resTitle, + )); + $mail->setBodyText($mailtp->getBody()); + $mail->setSubject($mailtp->getSubject()); + if($this->debug==0) + { + $mail->addTo($this->view->config->service->email); + }else{ + $mail->addTo($this->debug_email); + } + $mail->send(); + + $data = array("commited"=>1,"error"=>'该版本已经成功提交,请等待数据中心进一步处理!'); + $this->jsonexit($data); + return true; + }catch(Exception $e) { + $sql="delete from mdstatus where uuid in (select uuid from geonetworkmetadata where id=?)"; + $this->db->query($sql,array($id)); + $msg = "提交失败,请确认权限后重试"; + if($this->debug>0) + {$msg .= $e->getMessage();} + $data = array("error"=>$msg); + $this->jsonexit($data); + return true; + } + } + + }// newdataAction() 新建元数据 + + + public function fundAction() + { + $this->_helper->layout->setLayout('administry'); + $ac = $this->_getParam('ac'); + $submit = $this->_getParam('submit'); + $this->view->q = $keyword = $this->_getParam('q'); + + include_once("helper/view.php"); + include_once("data/Fund.php"); + $fund = new Fund($this->db); + + $auth = Zend_Auth::getInstance(); + if($auth->hasIdentity()) + { + $user = $auth->getIdentity(); + $uid = $user->id; + } + + if($ac=='index' || empty($ac)) + { + $uuid = $this->_getParam('uuid'); + if(empty($uuid)) + { + $rows = $fund->fetch(NULL,true,0,$keyword); + view::addPaginator($rows,$this,10); + }else{ + include('data/Metadata.php'); + $md = new Metadata($this->db); + $this->view->md = $md->view($uuid); + $rows = $fund->fetch($uuid); + view::addPaginator($rows,$this,10); + } + return true; + } + + if($ac == 'datalist') + { + $this->_helper->viewRenderer('fund-data-list'); + $this->view->q = $q = $this->_getParam('q'); + $rows = $fund->fetchFromData(true,0,$q); + view::addPaginator($rows,$this,10); + return true; + } + + if($ac == 'dataview') + { + $this->_helper->viewRenderer('fund-data-view'); + $this->view->q = $q = $this->_getParam('q'); + $id = $this->_getParam('id'); + $this->view->fund = $fund->view($id); + $rows = $fund->fetchFromData($id,0,$q); + view::addPaginator($rows,$this,10); + return true; + } + + if($ac == "add") + { + $this->_helper->viewRenderer('fund-add'); + if(!empty($submit)) + { + $data = $fund->_getParams($this->_request); + $data['userid'] = $uid; + + if($fund->add($data) == true) + { + $this->view->AlertType = "alert-success"; + $this->view->msg = "添加成功!"; + $this->view->jump_url = "/admin/data/fund/"; + return true; + }else{ + $this->view->data = $data; + $this->view->error = "添加失败,请重试"; + return true; + } + } + return true; + }//add + + if($ac == "edit") + { + $this->_helper->viewRenderer('fund-add'); + $id = $this->_getParam('id'); + + if(empty($id)) + { + $this->view->AlertType = "alert-error"; + $this->view->msg = "参数错误"; + $this->view->jump_url = "/admin/data/fund/"; + } + + if(!empty($submit)) + { + $data = $fund->_getParams($this->_request); + $data['userid'] = $uid; + if($fund->update($data,$id) == true) + { + $this->view->AlertType = "alert-success"; + $this->view->msg = "修改成功!"; + $this->view->jump_url = "/admin/data/fund/"; + return true; + }else{ + $this->view->data = $data; + $this->view->error = "修改失败,请重试"; + return true; + } + }else{ + $this->view->data = $fund->view($id); + } + return true; + }//edit + + if($ac == "del") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $id = $this->_getParam('id'); + + if(empty($id)) + { + $this->jsonexit(array('error'=>'参数错误')); + return true; + } + + if($fund->delete($id) == true) + { + $this->jsonexit(array('success'=>$id)); + return true; + }else{ + $this->jsonexit(array('error'=>'删除失败')); + return true; + } + + }//del + + if($ac == "formd") + { + $uuid = $this->_getParam('uuid'); + + if(empty($uuid)) + { + $this->view->AlertType = "alert-error"; + $this->view->msg = "参数错误"; + $this->view->jump_url = "/admin/data/fund/"; + } + + $id = $this->_getParam('id'); + $order = $this->_getParam('order'); + + if(!empty($id)) + { + if($fund->addToMdfund($uuid,$id,$order)) + { + $this->view->AlertType = "alert-success"; + $this->view->error = "添加成功!可以继续选择并添加"; + }else{ + $this->view->AlertType = "alert-error"; + $this->view->error = "添加失败!该数据可能已被添加"; + } + } + + $mfid = $this->_getParam('mfid'); + + if(!empty($mfid)) + { + if($fund->changeorder($mfid,$order)) + { + $this->view->AlertType = "alert-success"; + $this->view->error = "排序修改成功!"; + }else{ + $this->view->AlertType = "alert-error"; + $this->view->error = "排序修改失败!"; + } + $rows = $fund->fetch($uuid); + }else{ + $this->view->ct = "ct"; + $rows = $fund->fetch($uuid,false,0,$this->_getParam('q')); + } + + include('data/Metadata.php'); + $md = new Metadata($this->db); + $this->view->md = $md->view($uuid); + + view::addPaginator($rows,$this,10); + + return true; + }//formd + + if($ac == "mdfunddel") + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $id = $this->_getParam('id'); + + if(empty($id)) + { + $this->jsonexit(array('error'=>'参数错误')); + return true; + } + + if($fund->mfdelete($id) == true) + { + $this->jsonexit(array('success'=>$id)); + return true; + }else{ + $this->jsonexit(array('error'=>'删除失败')); + return true; + } + }//mdfunddel + + if($ac == "data") + { + $this->_helper->viewRenderer('fund-data'); + + $id = $this->_getParam('id'); + $del = $this->_getParam('del'); + $this->view->info = $fund->view($id); + + if(!empty($del)) + { + $s = $fund->dataRelationDelete($del); + if($s) + { + $this->view->error = view::Error("删除成功!","alert-sccuess"); + }else{ + $this->view->error = view::Error("删除失败!","alert-error"); + } + } + + $rows = $fund->getData($id,$keyword); + view::addPaginator($rows,$this,10); + } + + }//fund + + //数据可是化管理 + public function visualAction() + { + $uuid = $this->_getParam('uuid'); + $submit = $this->_getParam('submit'); + $visual = new Visual\Visual; + + if(!empty($uuid)) + { + $this->_helper->viewRenderer('visual-add'); + $this->view->data = ['uuid' => $uuid]; + + if(empty($submit)) + { + $data = $visual->getVisualVars($uuid); + + if(!empty($data)) + { + $this->view->info = $data; + } + + return true; + } + + $data = array( + 'uuid' => $this->_getParam('uuid'), + 'vars' => $this->_getParam('var'), + 'data' => $this->_getParam('data'), + 'status' => $this->_getParam('status') + ); + + $status = $visual->add($data); + + if($status === true) + { + view::Post($this,"添加成功!",-2); + return true; + }else{ + $this->view->error = $status; + return true; + } + } + + view::addPaginator($visual->getVisualMetadata(),$this,10); + return; + } + + public function ftpAction() + { + $this->_helper->layout->disableLayout(); + $this->_helper->viewRenderer->setNoRender(); + + $uuid = $this->_getParam('uuid'); + + $this->view->uuid = $uuid; + $submd=$this->view->config->sub->metadata; + + if(empty($uuid) || !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)) + { + $data = array( + 'error'=>"参数错误" + ); + $this->jsonexit($data); + return true; + } + //安全检查: uuid必须是当前子平台数据 + $sql="select * from $submd where uuid=?"; + $sth=$this->db->prepare($sql); + $sth->execute(array($uuid)); + $row=$sth->fetch(); + if (empty($row)) + { + $data = array( + 'error'=>"参数错误" + ); + $this->jsonexit($data); + return true; + } + + //ftp admin 用户名 + $auth = Zend_Auth::getInstance(); + $uid=$auth->getIdentity()->id; + $uname = $submd.'admin'.$uid; + + //ftp路径 + $homedir = "/data/upload/".$uuid."/"; + //ftp用户表 + $ftptable=' pureftp ';//ftp2.westgis.ac.cn + $uid = 1002; + $gid = 1002; + + $sql = "SELECT * FROM $ftptable WHERE userid='$uname' ORDER BY pkid DESC"; + $sth = $this->db->prepare($sql); + $sth->execute(); + $row = $sth->fetch(); + + //create directory for upload + //server is not localhost, so we need a trick + //$old=umask(0); + //@mkdir($homedir,0777); + //umask($old); + $page=file_get_contents('http://ftp.sanjiangyuan.org.cn/proftp_upload.php?uuid='.$uuid); + if (!empty($page)) die($page);//there are errors in visit ftp page + + + if(!empty($row['pkid'])) + { + if(preg_match("/.*".$uuid.".*/",$row['homedir'])) + { + $data = array( + 'statu'=>1, + 'user'=>$row['userid'], + 'passwd'=>$row['passwd'] + ); + + $this->jsonexit($data); + return true; + + }else{ + $passwd = $this->genRandomString(16); + //$sql = "UPDATE proftpusers SET passwd=?,uid=?,gid=?,homedir=? WHERE userid=?"; + //$sth = $this->db->prepare($sql); + //$rs = $sth->execute(array($passwd,$uid,$gid,$homedir,$uname)); + $sql="update ".$ftptable." SET passwd='".$passwd."',uid=".$uid.",gid=".$gid.",homedir='".$homedir."' WHERE userid='".$uname."'"; + $rs=$this->db->query($sql); + if($rs) + { + $data = array( + 'statu'=>1, + 'user'=>$uname, + 'passwd'=>$passwd + ); + $this->jsonexit($data); + return true; + }else{ + $data = array( + 'error'=>"FTP信息更新失败,请重试" + ); + $this->jsonexit($data); + return true; + } + } + } + + else{ + $passwd = $this->genRandomString(16); + + //$sql = "INSERT INTO proftpusers (userid,passwd,uid,gid,homedir) VALUES (?,?,?,?,?)"; + //$sth = $this->db->prepare($sql); + //$rs = $sth->execute(array($uname,$passwd,$uid,$gid,$homedir)); + $sql="insert into ".$ftptable." (userid,passwd,uid,gid,homedir) values('".$uname."','".$passwd."',".$uid.",".$gid.",'".$homedir."')"; + $rs=$this->db->query($sql); + if($rs) + { + $data = array( + 'statu'=>1, + 'user'=>$uname, + 'passwd'=>$passwd + ); + $this->jsonexit($data); + return true; + }else{ + $data = array( + 'error'=>"FTP信息更新失败,请重试" + ); + $this->jsonexit($data); + return true; + } + }//end if + } + + /* + 获得单个文件的信息 + return array row + */ + public function getFileinfo($id){ + $sql = "select * from attachments where id='$id'"; + $re= $this->db->query($sql); + $row= $re->fetch(); + return $row; + } + + + /* + * jsonexit() 退出并返回json数据 + * + * param array $data 要返回的JSON数据,可以是任意数组 + * + * return JSON-response + */ + public function jsonexit($data){ + $this->getResponse()->setHeader('Content-Type', 'application/json')->appendBody(json_encode($data,JSON_NUMERIC_CHECK)); + return true; + }//jsonexit() 退出并返回json数据 + + private function genRandomString($len) + { + $chars = array( + "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", + "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", + "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", + "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", + "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", + "3", "4", "5", "6", "7", "8", "9" + ); + $charsLen = count($chars) - 1; + + shuffle($chars); // 将数组打乱 + + $output = ""; + for ($i=0; $i<$len; $i++) + { + $output .= $chars[mt_rand(0, $charsLen)]; + } + return $output; + } +}