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();
}
function indexAction()
{
//其他连接
}
//提供和GEONETWORK中的元数据的同步功能
//提供双向同步功能,但只处理ISO 19115格式的元数据?
function syncAction()
{
set_time_limit(0);
$sql="select * from (select count(*) as westdccount from metadata) as t1,(select count(uuid) as gncount from geonetworkmetadata where schemaid='iso19115') as t2";
$this->db->setFetchMode(Zend_Db::FETCH_OBJ);
$this->view->mdcount=$this->db->fetchRow($sql);
$source=$this->_request->getParam('source');
$delete=$this->_request->getParam('delete');
$thumb=$this->_request->getParam('thumb');
$list=$this->_request->getParam('list');
$adminuser=$this->_request->getParam('adminuser');
$uuid=trim($this->_request->getParam('uuid'));
if ($source=="geonetwork" && empty($uuid) ) {
//从geonetwork向WESTDC同步
//对所有的新数据增加评审状态
$sql="insert into mdstatus (uuid,status,ts_accepted,userid) select uuid,1,now(),?
from geonetworkmetadata gn where uuid not in (select uuid from mdstatus)";
$userid = Zend_Auth::getInstance()->getIdentity()->id;
$this->db->query($sql,array($userid));
$sql="select uuid,data,source from geonetworkmetadata where schemaid='iso19115'";
$rs=$this->db->fetchAll($sql);
foreach($rs as $gmd) {
$this->import($gmd->data);
$this->db->query("update metadata set source=? where uuid=?",array($gmd->source,$gmd->uuid));
}
$this->view->msg='成功同步元数据!';
} elseif ($source=="watergn" && empty($uuid) ) {
//首先删除所有的黑河试验数据
//$sql="delete from metadata where source='e3ad32dc-f573-11e0-aa7b-b768cfd88d80'";
$sql="delete from metadata where uuid in (select uuid from watergn)";
$this->db->query($sql);
$sql="delete from responsible where id not in (select distinct(resid) from role)";
$this->db->query($sql);
//对所有的新数据增加评审状态
$sql="insert into mdstatus (uuid,status,ts_accepted,userid) select uuid,1,now(),?
from watergn where uuid not in (select uuid from mdstatus)";
$userid = Zend_Auth::getInstance()->getIdentity()->id;
$this->db->query($sql,array($userid));
//同步黑河遥感实验的元数据
$sql="select uuid,data,source from watergn where schemaid='iso19115'";
$rs=$this->db->fetchAll($sql);
foreach($rs as $gmd) {
$this->import($gmd->data);
$this->db->query("update metadata set source=? where uuid=?",array($gmd->source,$gmd->uuid));
}
$this->view->msg='成功同步黑河遥感综合实验元数据!';
}elseif ($source=="glacier" && empty($uuid) ) {
//首先删除所有的对应专题数据
$sql="delete from metadata where uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='glacier')";
$this->db->query($sql);
$sql="delete from responsible where id not in (select distinct(resid) from role)";
$this->db->query($sql);
//对所有的新数据增加评审状态
/*$sql="insert into mdstatus (uuid,status,ts_accepted,userid) select uuid,1,now(),?
from watergn where uuid not in (select uuid from mdstatus)";
$userid = Zend_Auth::getInstance()->getIdentity()->id;
$this->db->query($sql,array($userid));*/
//同步专题元数据
$sql="select uuid,data,source from geonetworkmetadata where schemaid='iso19115' and uuid in (select d.uuid from datasource d left join source s on d.sourceid=s.id where s.code='glacier')";
$rs=$this->db->fetchAll($sql);
foreach($rs as $gmd) {
$this->import($gmd->data);
//source in unused now.
//$this->db->query("update metadata set source=? where uuid=?",array($gmd->source,$gmd->uuid));
}
$this->view->msg='成功同步专题元数据!';
} elseif ($source=="westdc" && empty($uuid) ) {
//从westdc向geonetwork同步
$sql="select m.uuid,m.source,x.data from metadata m left join xml x on m.id=x.id where m.uuid not in (select uuid from geonetworkmetadata)";
$rs=$this->db->fetchAll($sql);
foreach($rs as $gmd) {
$sql="insert into geonetworkmetadata (uuid,source,data) values('".$gmd->uuid."','".$gmd->source."',".$this->db->quote($gmd->data).")";
$this->db->exec($sql);
}
$sql="select m.uuid,m.source,x.data from metadata m left join xml x on m.id=x.id where m.uuid in (select uuid from geonetworkmetadata)";
$rs=$this->db->fetchAll($sql);
foreach($rs as $gmd) {
$sql="update geonetworkmetadata set source='".$gmd->source."',data=".$this->db->quote($gmd->data)." where uuid='".$gmd->uuid."'";
$this->db->exec($sql);
}
$this->view->msg='成功同步WESTDC元数据到GEONETWORK!';
} elseif (!empty($uuid) && isset($_POST['gnsubmit'])) {
//同步单条元数据到geonetwork
$sql=$this->db->quoteInto("select m.uuid,m.source,x.data,g.id from metadata m left join xml x on m.id=x.id left join geonetworkmetadata g on g.uuid=m.uuid where m.uuid=?",$uuid);
if ($gmd=$this->db->fetchRow($sql)) {
if (!empty($gmd->id)) {
$sql="update geonetworkmetadata set source='".$gmd->source."',data=".$this->db->quote($gmd->data)." where uuid='".$gmd->uuid."'";
} else {
$sql="insert into geonetworkmetadata (uuid,source,data) values('".$gmd->uuid."','".$gmd->source."',".$this->db->quote($gmd->data).")";
}
$this->db->exec($sql);
$this->view->msg='成功同步元数据:'.$uuid;
} else $this->view->msg='不存在此元数据:'.$uuid;
} elseif (!empty($uuid) && isset($_POST['watersubmit'])) {
//同步单条元数据
$sql=$this->db->quoteInto("select data,source from watergn where uuid=?",$uuid);
if ($rs=$this->db->fetchRow($sql)) {
$this->import($rs->data);
$this->db->query("update metadata set source=? where uuid=?",array($rs->source,$uuid));
$this->view->msg='成功同步元数据:'.$uuid;
} else $this->view->msg='不存在此元数据:'.$uuid;
} elseif ($list=='westdc') {
$sql="select uuid,title from metadata where uuid not in (select uuid from geonetworkmetadata where schemaid='iso19115')";
$this->view->metadata=$this->db->query($sql);
} elseif ($list=='geonetwork') {
$sql="select id,uuid,(regexp_matches(data,'(.*)'))[1] as title from geonetworkmetadata
where uuid not in (select uuid from metadata)";
$this->view->metadata=$this->db->query($sql);
} elseif ($delete=='westdc') {
$sql="delete from metadata where uuid not in (select uuid from geonetworkmetadata where schemaid='iso19115')";
$this->db->query($sql);
$this->view->msg='已删除WESTDC中多出的元数据!';
} elseif ($delete=='geonetwork') {
//dblink view中删除多条有问题,暂时用单条删除替代
//$sql="delete from geonetworkmetadata where uuid not in (select uuid from metadata)";
$sql="select uuid from geonetworkmetadata where uuid not in (select uuid from metadata)";
$rows=$this->db->fetchAll($sql);
foreach($rows as $row)
{
$this->db->exec($this->db->quoteInto("delete from geonetworkmetadata where uuid=?",$row->uuid));
}
$this->view->msg='已删除GEONETWORK中多出的元数据!';
} elseif ($delete=='water') {
$sql="delete from metadata where uuid not in (select uuid from watergn where schemaid='iso19115') and uuid not in (select uuid from geonetworkmetadata where schemaid='iso19115')";
$this->db->query($sql);
$this->view->msg='已删除WESTDC中多出的元数据!';
} elseif ($thumb=='geonetwork') {
$sql="select g.id as gid,m.uuid,t.* from thumbnail t left join metadata m on t.id=m.id left join geonetworkmetadata g on m.uuid=g.uuid where t.filetype is not null and t.filename is not null";
$rows=$this->db->fetchAll($sql);
foreach($rows as $row) {
$data=file_get_contents($this->view->config->geonetwork->url.'srv/cn/resources.get?access=public&id='.$row->gid.'&fname='.urlencode($row->filename));
$sql="update thumbnail set data=? where id=?";
$this->db->query($sql,array(base64_encode($data),$row->id));
}
$this->view->msg='已成功同步缩略图!';
} elseif ($thumb=='water') {
$sql="select g.id as gid,m.uuid,t.* from watergn g left join metadata m on m.uuid=g.uuid left join thumbnail t on t.id=m.id where t.filetype is not null and t.filename is not null";
$rows=$this->db->fetchAll($sql);
foreach($rows as $row) {
$data=file_get_contents($this->view->config->watergeonetwork->url.'srv/cn/resources.get?access=public&id='.$row->gid.'&fname='.urlencode($row->filename));
$sql="update thumbnail set data=? where id=?";
$this->db->query($sql,array(base64_encode($data),$row->id));
}
$this->view->msg='已成功同步缩略图!';
} elseif ($adminuser=='geonetwork') {
$sql="select id,username,password,email from users";
$rows=$this->db->fetchAll($sql);
foreach($rows as $row) {
$sql="select * from geonetworkusers where id=?";
$r=$this->db->fetchRow($sql,array($row->id));
if ($r) {
$sql="update geonetworkusers set username='$row->username',password=encode(digest(md5('$row->password'),'sha1'),'hex'),email='$row->email' where id=$row->id";
$this->db->query($sql);
} else {
$sql="insert into geonetworkusers (id,username,password,email) values($row->id,'$row->username',encode(digest(md5('$row->password'),'sha1'),'hex'),'$row->email')";
$this->db->query($sql);
}
}
$this->view->msg='已成功同步帐号!';
} elseif (!empty($uuid) && (isset($_POST['submit']) || (!isset($_POST['gnsubmit']) && !isset($_POST['watersubmit'])))) {
//同步单条元数据
$sql=$this->db->quoteInto("select data,source from geonetworkmetadata where uuid=?",$uuid);
if ($rs=$this->db->fetchRow($sql)) {
$this->import($rs->data);
$this->db->exec($this->db->quoteInto("update metadata set source=? where uuid='$uuid'",$rs->source));
$userid = Zend_Auth::getInstance()->getIdentity()->id;
$this->db->query("insert into mdstatus (uuid,status,ts_accepted,userid) select uuid,1,now(),?
from geonetworkmetadata gn where uuid not in (select uuid from mdstatus) and uuid=?",array($userid,$uuid));
$this->view->msg='成功同步元数据:'.$uuid;
//update search document
$search=new Search();
$sql="select * from xunsearch where uuid=?";
$sth = $this->db->prepare($sql);
$sth->setFetchMode(Zend_Db::FETCH_ASSOC);
$sth->execute(array($uuid));
$data = $sth->fetch();
$search->update($data);
} else $this->view->msg='不存在此元数据:'.$uuid;
}
}
function datasetcdAction()
{
$add=(int)$this->_getParam('add');
$edit=(int)$this->_getParam('edit');
$delete=(int)$this->_getParam('delete');
set_time_limit(0);
if ($add) {
$form=new DatasetcdForm();
$form->img->setRequired(true);
$form->document->setRequired(true);
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
if ($form->isValid($formdata)) {
$uploadedData = $form->getValues();
$img = '/'.$form->img->getFileName();
$document='/'.$form->document->getFileName();
$sql="insert into datasetcd (title,size,uuid,img,document,descript) values(?,?,?,?,?,?)";
$this->db->query($sql,array($formdata['title'],$formdata['size'],$formdata['uuid'],$img,$document,$formdata['descript']));
$this->messenger->addMessage('提示信息:您已经成功添加该特色数据集。');
$this->_redirect('/admin/data/datasetcd');
} else {
$form->populate($formdata);
}
}
$this->view->form=$form;
$this->_helper->viewRenderer('datasetcdadd');
} elseif ($edit){
$form=new DatasetcdForm();
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
if ($form->isValid($formdata)) {
$uploadedData = $form->getValues();
$img = '/'.$form->img->getFileName();
$document='/'.$form->document->getFileName();
$sql="update datasetcd set title=?,size=?,uuid=?,";
if ($form->img->isUploaded()) $sql.="img=?,";
if ($form->document->isUploaded()) $sql.="document=?,";
$sql.="descript=? where id=?";
$param=array($formdata['title'],$formdata['size'],$formdata['uuid']);
if ($form->img->isUploaded()) $param[]=$img;
if ($form->document->isUploaded()) $param[]=$document;
$param[]=$formdata['descript'];
$param[]=$edit;
$this->db->query($sql,$param);
$this->messenger->addMessage('提示信息:您已经编辑添加该特色数据集。');
$this->_redirect('/admin/data/datasetcd');
} else {
$form->populate($formdata);
}
} else {
$sql="select * from datasetcd where id=?";
$formdata=$this->db->fetchRow($sql,array($edit));
$form->populate($formdata);
}
$this->view->form=$form;
$this->_helper->viewRenderer('datasetcdadd');
} elseif ($delete) {
$sql="delete from datasetcd where id=?";
try {
$this->db->query($sql,array($delete));
$this->messenger->addMessage('提示信息:您已经成功删除该特色数据集。');
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage());
}
$this->_redirect("/admin/data/datasetcd");
}
$select=$this->db->select();
$select->from('datasetcd')->order('id desc');
$paginator = Zend_Paginator::factory($select);
$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()
* 元数据管理
*
*/
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');
if ($delete)
{
$sql="delete from metadata where uuid=?";
try {
$this->db->query("delete from mdstatus where uuid=?",array($delete));
$this->db->query("delete from mdauthor where uuid=?",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 normalmetadata 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,gen.id as genid,st.status as mdstatus, ds.id as datasetid from metadata 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 and ds.lang in ('','cn')
LEFT JOIN mdstatus st ON md.uuid=st.uuid
left join en.geonetworkmetadata gen on gen.uuid=md.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,gen.id as genid,st.status as mdstatus,ds.id as datasetid,spatial.stid as stid FROM metadata 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 and ds.lang in ('','cn')
left join en.geonetworkmetadata gen on gen.uuid=md.uuid
left join (select count(uuid) as stid,uuid from dataspatial group by uuid) as spatial on spatial.uuid=md.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=? and lang in ('','cn')";
$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');
$lang = $this->_getParam('lang');
$sql = "UPDATE dataset SET host=?,path=?,lang=? WHERE uuid=?";
$sth = $this->db->prepare($sql);
$ds = $sth ->execute(array($host,$path,$lang,$uuid));
if ($host=='ftp1.westgis.ac.cn')
{
file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1&lang=".$lang);
} else if ($host=='ftp2.westgis.ac.cn')
{
file_get_contents("http://ftp2.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1&lang=".$lang);
}
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');
$lang = $this->_getParam('lang');
$sql = "SELECT * FROM dataset WHERE uuid=? and lang=?";
$sth = $this->db->prepare($sql);
$sth ->execute(array($uuid,$lang));
$row = $sth->fetch();
if(!empty($row['id']))
{
$data = array("error"=>"该数据已经有存档信息,不能重复添加");
$this->jsonexit($data);
return true;
}
$sql = "INSERT INTO dataset (uuid,host,path,lang) VALUES (?,?,?,?)";
$sth = $this->db->prepare($sql);
$ds = $sth ->execute(array($uuid,$host,$path,$lang));
if ($host=='ftp1.westgis.ac.cn')
{
file_get_contents("http://ftp1.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1&lang=".$lang);
} else if ($host=='ftp2.westgis.ac.cn')
{
file_get_contents("http://ftp2.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1&lang=".$lang);
}
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=? and lang in ('','cn')";
$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&lang=".$row['lang']);
echo '
数据目录成功导入!
';
} else if ($row['host']=='ftp2.westgis.ac.cn')
{
file_get_contents("http://ftp2.westgis.ac.cn/proftp_upload.php?uuid=".$uuid."&filelist=1&lang=".$row['lang']);
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');
if ($delete)
{
$sql="delete from comments where id=?";
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 metadata 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
LEFT JOIN metadata 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 newsletterAction()
{
$form=new Zend_Form();
$form->setName('newsletter');
$form->setAttrib('enctype', 'multipart/form-data');
$nlf=new Zend_Form_Element_File('nlf');
$nlf->setLabel('数据通讯')
->setRequired(true)
->setDestination($this->view->config->paths->newsletter)
->addValidator('Count', false, 1) // ensure only 1 file
->addValidator('Size', false, 2048000) // limit to 2M
->addValidator('Extension', false, 'pdf'); // only JPEG, PNG, and GIFs
$submit = new Zend_Form_Element_Submit('submit');
$form->addElements(array($nlf,$submit));
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
if ($form->isValid($formdata)) {
$uploadedData = $form->getValues();
//自动生成JPG文件
$pdf = $form->nlf->getFileName();
$img = new Imagick($pdf.'[0]');
$img->thumbnailImage(200, 0);
$img->writeImage($this->view->config->paths->newsletter.basename($pdf,'.pdf').'.jpg');
$this->messenger->addMessage('提示信息:您已经成功添加该数据通讯。');
$this->_redirect('/admin/data/newsletter');
}
}
$l=new mydir($this->view->config->paths->newsletter,"newsletter_*.pdf");
$this->view->newsletters=$l->toArray();
$this->view->addHelperPath('helper','Zend_View_Helper_');
rsort($this->view->newsletters);
$this->view->form=$form;
}
function featureAction()
{
$add=(int)$this->_getParam('add');
$edit=(int)$this->_getParam('edit');
$delete=(int)$this->_getParam('delete');
if ($add) {
$form=new DatafeatureForm();
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
if ($form->isValid($formdata)) {
$uploadedData = $form->getValues();
if (!$form->imgurl) {
$imgurl = '/'.$form->img->getFileName();
} else
$imgurl=$formdata['imgurl'];
$sql="insert into datafeature (title,detailurl,imgurl,description) values(?,?,?,?)";
$this->db->query($sql,array($formdata['title'],$formdata['detailurl'],$imgurl,$formdata['description']));
$this->messenger->addMessage('提示信息:您已经成功添加该特色推荐。');
$this->_redirect('/admin/data/feature');
} else {
$form->populate($formdata);
}
}
$this->view->form=$form;
$this->_helper->viewRenderer('featureadd');
} elseif ($edit){
$form=new DatafeatureForm();
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
if ($form->isValid($formdata)) {
$uploadedData = $form->getValues();
if (!$form->imgurl) {
$imgurl = '/'.$form->img->getFileName();
} else
$imgurl=$form->imgurl;
$sql="update datafeature set title=?,detailurl=?,imgurl=?,description=? where id=?";
$param=array($formdata['title'],$formdata['detailurl'],$formdata['imgurl'],$formdata['description'],$edit);
$this->db->query($sql,$param);
$this->messenger->addMessage('提示信息:您已经编辑该特色推荐。');
$this->_redirect('/admin/data/feature');
} else {
$form->populate($formdata);
}
} else {
$sql="select * from datafeature where id=?";
$formdata=$this->db->fetchRow($sql,array($edit));
$form->populate($formdata);
}
$this->view->form=$form;
$this->_helper->viewRenderer('featureadd');
} elseif ($delete) {
$sql="delete from datafeature where id=?";
try {
$this->db->query($sql,array($delete));
$this->messenger->addMessage('提示信息:您已经成功删除该特色推荐。');
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage());
}
$this->_redirect("/admin/data/feature");
}
$select=$this->db->select();
$select->from('datafeature')->order('id desc');
$paginator = Zend_Paginator::factory($select);
$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;
}
/*
* 数据文献管理
*/
function referenceAction()
{
$add=(int)$this->_getParam('add');
$edit=(int)$this->_getParam('edit');
$delete=(int)$this->_getParam('delete');
$uuid=$this->_getParam('uuid');
$search=$this->_getParam('search');
$keyword=$this->_getParam('keyword');
$import=(int)$this->_getParam('import');
$show = $this->_getParam('show');
if ($add) {
$type = $this->_getParam('type');
if($type == 'dc')
{
$this->_helper->viewRenderer('referenceadd');
$this->view->type = "dc";
}else{
$form=new ReferenceForm();
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
if ($form->isValid($formdata)) {
$sql="select id from reference where reference=?";
$row=$this->db->fetchRow($sql,array(trim($formdata['reference'])));
if (!$row)
{
$sql="insert into reference (reference,link) values(?,?)";
$this->db->query($sql,array(trim($formdata['reference']),trim($formdata['link'])));
$sql="select id from reference where reference=?";
$row=$this->db->fetchRow($sql,array(trim($formdata['reference'])));
}
$sql="insert into mdref (uuid,refid,reftype) values(?,?,?)";
$this->db->query($sql,array(trim($formdata['uuid']),$row['id'],$formdata['reftype']));
$this->messenger->addMessage('提示信息:您已经成功添加该数据文献。');
$this->_redirect('/admin/data/reference/show/'.$formdata['uuid']);
}else {
$form->populate($formdata);
}
} else {
if ($uuid)
{
$formdata['uuid']=$uuid;
$form->populate($formdata);
}
}
$this->view->form=$form;
$this->_helper->viewRenderer('referenceadd');
}
} //添加
else if($show){
$sql = "select mr.id as mdid,mr.refid,r.reference,md.title,md.uuid,mr.place FROM mdref mr
left join metadata md on md.uuid=mr.uuid
left join reference r on r.id=mr.refid
where mr.uuid='$show';
";
$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.phtml');
$this->view->paginator=$paginator;
} //按UUID查看
elseif ($edit){
$form=new ReferenceForm();
if ($this->_request->isPost()) {
$formdata=$this->_request->getPost();
if ($form->isValid($formdata)) {
$sql="select id from reference where reference=? order by id desc";
$rs=$this->db->query($sql,array($formdata['reference']));
$row = $rs->fetch();
if ($row['id']!='')
{
//更新文献信息
$sql="update mdref set refid=?,reftype=? where uuid=? and id=?";
$td = $this->db->query($sql,array($row['id'],$formdata['reftype'],$formdata['uuid'],$edit));
$sql="update reference set reference=?,link=? where id=?";
$this->db->query($sql,array($formdata['reference'],$formdata['link'],$row['id']));
if($td)
{
$this->messenger->addMessage('提示信息:您已经编辑该数据文献。');
$this->_redirect('/admin/data/reference/show/'.$formdata['uuid']);
}
}else
{
$sql = "INSERT INTO reference (reference,link) VALUES ('".$formdata['reference']."','".$formdata['link']."') RETURNING id";
$sth = $this->db->prepare($sql);
if($sth->execute())
{
$row = $sth->fetch(PDO::FETCH_ASSOC);
$sql="update mdref set refid=?,reftype=? where uuid=? and id=?";
$td = $this->db->query($sql,array($row['id'],$formdata['reftype'],$formdata['uuid'],$edit));
$this->messenger->addMessage('提示信息:您已经编辑该数据文献。');
$this->_redirect('/admin/data/reference/show/'.$formdata['uuid']);
}else
{
$this->messenger->addMessage('提示信息:文献创建失败,请重试');
$this->_redirect('/admin/data/reference/edit/'.$edit);
}
}
} else {
$form->populate($formdata);
}
} else {
$sql="select m.uuid,r.reference,r.link,m.reftype from reference r left join mdref m on r.id=m.refid where m.id=?";
$formdata=$this->db->fetchRow($sql,array($edit));
$form->populate($formdata);
}
$this->view->form=$form;
$this->_helper->viewRenderer('referenceadd');
} //编辑相关文献
elseif ($delete) {
$sql = "select uuid from mdref where id=$delete";
$rs = $this->db->query($sql);
$row = $rs->fetch();
$sql="delete from mdref where id=?";
try {
$this->db->query($sql,array($delete));
$this->messenger->addMessage('提示信息:您已经成功删除该数据文献。');
} catch (Exception $e) {
$this->messenger->addMessage($e->getMessage());
}
$this->_redirect('/admin/data/reference/show/'.$row['uuid']);
} //删除相关文献
elseif ($import) {
if ($this->_request->isPost()) {
if ($_FILES["ref"]["error"] == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["ref"]["tmp_name"];
$fp = fopen($tmp_name, "rb");
$ref=fread($fp, filesize($tmp_name));
fclose($fp);
$lines=explode("\n",$ref);
foreach($lines as $line)
{
$data=explode(";",$line);
if (count($data)==2)
$link='';
else
$link=$data[2];
$link=$this->db->quote($link);
$sql="insert into reference (reference,link) values(?,".$link.")";
try {
$this->db->exec($this->db->quoteInto($sql,trim($data[1])));
} catch (Exception $e) {}
$sql="select id from reference where reference=?";
$row=$this->db->fetchRow($this->db->quoteInto($sql,trim($data[1])));
$sql="insert into mdref (uuid,refid) values(?,".$row['id'].")";
try {
$this->db->exec($this->db->quoteInto($sql,trim($data[0])));
} catch (Exception $e) {}
}
}//end if
}
$this->_helper->viewRenderer('referenceimport');
} //引用
else if($search){
if(!empty($keyword))
{
$sql="select rf.id as mdid,rf.refid,m.*,r.* from mdref rf
left join metadata m on m.uuid=rf.uuid
left join reference r on r.id=rf.refid
where m.title like '%$keyword%'
order by m.title";
$re=$this->db->query($sql);
$row=$re->fetchAll();
$paginator = Zend_Paginator::factory($row);
$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->messenger->addMessage($keyword.' 的搜索结果');
}
else
{
$this->messenger->addMessage('请输入搜索关键字');
$this->_redirect("/admin/data/reference");
}
} //搜索
else{
$sql="select m.title as mdtitle,m.uuid,mr.id as mdid,mr.refid,mr.place,r.reference
from mdref mr right join metadata m on mr.uuid=m.uuid
left join reference r on mr.refid=r.id
order by m.ts_created desc,mr.place";
$re=$this->db->query($sql);
$row=$re->fetchAll();
$paginator = Zend_Paginator::factory($row);
$paginator->setCurrentPageNumber($this->_getParam('page'));
$paginator->setItemCountPerPage(30);
$paginator->setView($this->view);
Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
$this->view->paginator=$paginator;
}//列表
}
//文献管理
public function refAction()
{
$this->view->ac = $ac = $this->_getParam('ac');
$submit = $this->_getParam('submit');
$keyword = $this->view->q = trim($this->_getParam('q'));
$order = $this->view->search_order = trim($this->_getParam('order'));
$sort = $this->view->search_sort = trim($this->_getParam('sort'));
$field = $this->view->search_field = $this->_getParam('field');
$this->view->uuid = $uuid = $this->_getParam('uuid');
$reference = new Reference();
if(!empty($keyword))
{
$reference->keyword = $keyword;
}
if(!empty($field))
{
$reference->field = $field;
}
if(!empty($order))
{
$reference->order = $order;
}
if(!empty($sort))
{
$reference->sort = $sort;
}
$this->view->reference = $reference;
$this->view->page = $this->_getParam('page');
$this->view->pagelimit = 12;
//文献首页
if(empty($ac) && empty($uuid))
{
$this->view->referenceType = $reference->referenceType();
view::addPaginator($reference->fetchReferences(),$this, $this->view->pagelimit);
$this->view->years = $reference->countByYear();
return true;
}
else if (empty($ac) && !empty($uuid))
{
$sql="select * from metadata where uuid='$uuid'";
$this->view->md=$this->db->fetchRow($sql);
$this->view->referenceType = $reference->referenceType();
if (empty($keyword))
{
$this->_helper->viewRenderer('ref-data-add');
unset($reference->keyword);
view::addPaginator($reference->fetchReferencesByUUID($uuid),$this, $this->view->pagelimit);
} else {
$this->_helper->viewRenderer('ref-data-search');
view::addPaginator($reference->fetchReferencesWithUUID($uuid),$this, $this->view->pagelimit);
}
return true;
}
else if ($ac == "water")
{
$this->view->referenceType = $reference->referenceType();
view::addPaginator($reference->fetchThemeReferences('water'),$this,$this->view->pagelimit);
return true;
}
else if ($ac == "westdc")
{
$this->view->referenceType = $reference->referenceType();
view::addPaginator($reference->fetchWestdcReferences(),$this,$this->view->pagelimit);
return true;
}
else if ($ac == "todo")
{
$this->view->referenceType = $reference->referenceType();
view::addPaginator($reference->fetchTodoReferences(),$this,$this->view->pagelimit);
return true;
}
//数据作者文献(缺少RIS)
else if ($ac=="author")
{
$this->view->referenceType = $reference->referenceType();
view::addPaginator($reference->fetchAuthorReferences(),$this,$this->view->pagelimit);
return true;
}
unset($this->view->reference);
unset($this->view->page);
unset($this->view->pagelimit);
//添加
if($ac == "add")
{
$this->_helper->viewRenderer('ref-add');
$id = $this->_getParam('id');
$attid = $this->_getParam('attid');
if(!empty($submit))
{
$this->view->data = $reference->getReferenceParam();
$attid = $this->view->data['attid'];
if(empty($id) || !is_numeric($id))
{
$status = $reference->reference();
}else{
$status = $reference->reference($id);
}
if($status !== true)
{
$this->view->error = view::Error($status);
}else{
if(!empty($id))
{
$msg = "文献修改成功!";
view::Post($this,$msg,-2);
return true;
}else{
$msg = "文献添加成功!";
view::Post($this,$msg,"/admin/data/ref/ac/add");
return true;
}
}
}else{
$this->view->data = array();
if(!empty($id) && is_numeric($id))
{
$this->view->data = $reference->getOneReferenceData($id);
}
}
if(!empty($attid))
{
$files = new Files();
$attfile = $files->getOne($attid);
$this->view->data['attid'] = $attid;
$this->view->data['file'] = $attfile;
}
return true;
}
//上传pdf
if($ac == "pdfupload")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$autoread = false;
if($this->_getParam('multi'))
{
$autoread = true;
}
$statu = $reference->uploadReferencePdf($_FILES['Filedata'],$autoread);
$this->jsonexit($statu);
return true;
}
//删除已上传的文件
if($ac == "delete")
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$statu = $reference->deleteReferenceAttchment($this->_getParam('attid'));
$this->jsonexit($statu);
return true;
}
//批量上传
if($ac == "multiupload")
{
$this->_helper->viewRenderer('ref-multiupload');
return true;
}
//文件管理
if($ac == "files")
{
$this->_helper->viewRenderer('ref-files');
view::addPaginator($reference->getReferenceFiles(),$this,10);
return true;
}
//删除文献
if($ac == "deleteref")
{
$refid = $this->_getParam('id');
if($reference->deleteReference($refid))
{
view::Post($this,"删除成功!",-1);
}else{
view::Post($this,"删除失败!",-1);
}
return true;
}
//删除文献数据对应关系
if($ac == "deletemdref")
{
if($reference->delMdref($this->_getParam('mrid')))
{
view::Post($this,"删除成功!",-1);
}else{
view::Post($this,"删除失败!",-1);
}
return true;
}
//相关数据
if($ac == "data")
{
$refid = $this->view->refid = $this->_getParam('id');
if(view::isXmlHttpRequest($this)){
$this->jsonexit($reference->getDataByReference($refid));
return true;
}else{
$this->_helper->viewRenderer('ref-metadatas');
$this->view->referenceType = $reference->referenceType();
view::addPaginator($reference->getDataByReference($refid),$this,10);
return true;
}
}
//添加数据文献
if($ac == "mdref" && view::isXmlHttpRequest($this))
{
$mdrefid = $this->_getParam('id');
$status = $reference->makeMdref($mdrefid);
if($status !== true)
{
$this->jsonexit(array('error'=>$status));
}else{
$this->jsonexit(array('success' => 1));
}
return true;
}
//移除数据文献
if($ac == "delmdref")
{
$mdrefid = $this->_getParam('id');
$status = $reference->delMdref($mdrefid);
if($status === true)
{
view::Post($this,"移除成功",-1);
}else{
view::Post($this,$status,-1);
}
return true;
}
//ris
if($ac == "ris")
{
$this->_helper->viewRenderer('ref-ris');
$submit = $this->_getParam('submit');
if(!empty($submit))
{
$ris = new \Reference\Ris();
$this->view->data = $ris->loadout();
try{
$ris->pushToDataTable($this->view->data);
}catch(Exception $e)
{
view::Dump($e->getMessage(),false);
}
}
}
//ris更新单篇
if($ac == "singleris")
{
$this->_helper->viewRenderer('ref-singleris');
$id = (int)$this->_getParam('id');
$this->view->ristext = $this->_getParam('ristext');
if($id < 1)
{
view::Post($this,"参数错误!",-1);
return true;
}
$this->view->referenceData = $reference->getOneReferenceData($id);
if(!empty($submit))
{
$ris = new Ris();
$data = $ris->loadout();
if(count($data) < 1)
{
view::Post($this,"输入的RIS信息有误".'查看详细',-1);
return true;
}
if($ris->updateWithRis($id,$data[0]) === true)
{
view::Post($this,"编辑成功!".'查看详细',-2);
return true;
}else{
view::Post($this,"编辑失败!".'查看详细',-1);
return true;
}
}
return true;
}//ris单篇更新
//ris导出
if($ac == "risoutput")
{
$this->_helper->viewRenderer('ref-risoutput');
$this->view->years = $reference->countByYear();
if(!empty($submit))
{
$mode = $this->_getParam('mode');
$risOutput = new RisOutput();
$preData = $risOutput->preRead($mode);
$risData = $risOutput->processArrayDataToRisData($preData);
$risText = $risOutput->output($risData);
echo "