westdc-zf1/application/default/controllers/MetadataController.php

598 lines
18 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class MetadataController extends Zend_Controller_Action
{
function __call($id, $arguments)
{
Zend_Debug::dump($arguments,'argu');
Zend_Debug::dump($id,'id');
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
}
function postDispatch()
{
$this->view->config = Zend_Registry::get('config');
}
function indexAction()
{
$md = new MetadataTable();
$db=$md->getAdapter();
//序列
$state=$db->query('select s.id,name,count(*) from series s,dataseries d where d.sid=s.id group by s.id,s.name');
$this->view->serie=$state->fetchAll();
//分类
$state=$db->query('select c.code,name,name_zh,count(*) from category c,categorycode cc where c.code=cc.code group by c.code,cc.name,cc.name_zh');
$this->view->category=$state->fetchAll();
//关键词
$state=$db->query('select keyword,count(*),keytype from keyword group by keyword,keytype order by keytype,count desc');
$this->view->keywords=$state->fetchAll();
//最新10个入库数据
$state=$db->query('select id,title from metadata order by id desc limit 10');
$this->view->metadata = $state->fetchAll();
//特色数据
//其他连接
}
/*
* 数据浏览
*/
function browseAction()
{
$md=new MetadataTable();
$db=$md->getAdapter();
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$limit=10;
$offset=$limit*($page-1);
$state=$db->query('select count(*) from metadata');
$row=$state->fetchAll();
$sum=$row[0]['count'];
$select=$db->select();
$select->from('metadata','*')->order('id desc')->limitPage($page,$limit);
$this->view->metadata = $db->fetchAll($select);
$this->view->page=new Pagination($sum,$page,$limit);
}
function tagAction()
{
$id = (int)$this->_request->getParam('id');
$key = $this->_request->getParam('key');
$md=new KeywordTable();
$db=$md->getAdapter();
$state=$db->query('select keyword,count(*),keytype from keyword group by keyword,keytype order by keytype,count desc');
$this->view->keywords=$state->fetchAll();
if ($id>0 or !empty($key)) {
if (empty($key)) {
$where=$db->quoteInto('id = ?',$id);
$row=$md->fetchRow($where);
$key=$row->name;
}
$this->view->codename=$key;
$sql=$db->quoteInto('select m.* from metadata m,keyword k where m.id=k.id and k.keyword=?',$key);
$state=$db->query($sql);
$this->view->metadata=$state->fetchAll();
}
}
/*
* 数据集序列浏览
*/
function seriesAction()
{
$id = (int)$this->_request->getParam('id');
$md=new SeriesTable();
$db=$md->getAdapter();
$state=$db->query('select s.id,name,count(*) from series s,dataseries d where d.sid=s.id group by s.id,s.name');
$this->view->serie=$state->fetchAll();
if ($id>0) {
$where=$db->quoteInto('id = ?',$id);
$row=$md->fetchRow($where);
$this->view->codename=$row->name;
$sql='select m.* from metadata m,dataseries d where m.id=d.id and d.sid=?';
$state=$db->query($sql,array($id));
$this->view->metadata = $state->fetchAll();
} else {
//提供全部数据集序列列表
}
}
/*
* 分类浏览模式
*/
function categoryAction()
{
$code = (int)$this->_request->getParam('code');
$md=new CategoryCodeTable();
$db=$md->getAdapter();
$state=$db->query('select c.code,name,name_zh,count(*) from category c,categorycode cc where c.code=cc.code group by c.code,cc.name,cc.name_zh');
$this->view->category=$state->fetchAll();
if ($code>0 && $code<20) {
$where=$db->quoteInto('code = ?',$code);
$row=$md->fetchRow($where);
$this->view->codename=(empty($row->name_zh)?$row->name:$row->name_zh);
$sql='select m.* from metadata m,category c where m.id=c.id and c.code=?';
$state=$db->query($sql,array($code));
$this->view->metadata = $state->fetchAll();
} else {
//提供全部分类列表
}
}
/*
* 时间浏览方式
* todo:实现xml的缓存
*/
function timelineAction()
{
$dateformat="D M j Y G:i:s O";
$md = new MetadataTable();
$db=$md->getAdapter();
$state=$db->query('select id,title,description,timebegin,timeend from metadata where timebegin is not null');
$rows=$state->fetchAll();
$timexml='<data>';
foreach($rows as $row) {
$timexml.='<event start="'.date($dateformat,strtotime($row['timebegin'])).'" ';
if ($row['timeend']!='') $timexml.=' end="'.date($dateformat,strtotime($row['timeend'])).'" ';
$timexml.= ' title="'.$row['title'].'" image="/images/westdc_40w.gif" link="/metadata/view/id/'.$row['id'].'">'.$row['description'].'</event>';
}
$timexml.='</data>';
$fp=fopen('time.xml','w');
fwrite($fp,$timexml);
fclose($fp);
$this->_helper->layout->disableLayout();
}
/*
* todo:空间浏览
*/
function mapAction()
{
//use json & openlayers?
$id=(int)$this->_request->getParam('id');
if ($id>0) $this->view->params="/id/$id";
}
/*
* 产生geojson数据
*/
function jsonAction()
{
$md=new MetadataTable();
$db=$md->getAdapter();
$id = (int)$this->_request->getParam('id');
$where='';
if (!empty($id)) { $where=' where id='.$id; }
$sql='select id,west,south,north,east,title from metadata'.$where;
$state=$db->query($sql);
$rows=$state->fetchAll();
$geomd=new GeoMetaData();
foreach($rows as $row){
if ($id)
$box=new GeoBox($row['west'],$row['south'],$row['east'],$row['north']);
else
$box=new GeoBoxLine($row['west'],$row['south'],$row['east'],$row['north']);
$feature=new Geofeature();
$feature->id=$row['id'];
$feature->addProperties('title',$row['title']);
$feature->geometry=$box;
$geomd->addFeature($feature);
}
$this->_helper->json($geomd);
}
/*
* 返回XML源文件
*/
function xmlAction()
{
$id=(int)$this->_request->getParam('id');
$xml=new XmlTable();
$db=$xml->getAdapter();
$where=$db->quoteInto('id=?',$id);
$order='id desc';
$row=$xml->fetchRow($where,$order);
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->setHeader('Content-Type', 'text/xml')
->setBody($row->data);
}
function detailAction()
{
$id=(int)$this->_request->getParam('id');
$xml=new XmlTable();
$db=$xml->getAdapter();
$where=$db->quoteInto('id=?',$id);
$order='id desc';
$row=$xml->fetchRow($where,$order);
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
//Zend_Debug::dump($row->data,'xml');
$this->getResponse()->setHeader('Content-Type', 'text/xml')
->setBody($row->data);
}
/*
* todo: search
*/
function searchAction()
{
$md=new MetadataTable();
$db=$md->getAdapter();
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$limit=10;
$offset=$limit*($page-1);
$key=$this->_request->getParam('key');
$searchtype=$this->_request->getParam('type');
if (!empty($key)) {
$sql=$db->quoteInto('select count(*) from metadata where title ilike ? or description ilike ?','%'.$key.'%','%'.$key.'%');
$state=$db->query($sql);
$row=$state->fetchAll();
$sum=$row[0]['count'];
$select=$db->select();
$select->from('metadata','*')->where('title ilike ? or description ilike ?','%'.$key.'%','%'.$key.'%')->order('id desc')->limit($limit,$offset);
$this->view->metadata = $db->fetchAll($select);
$this->view->page=new Pagination($sum,$page);
$this->view->key=$key;
} else if (!empty($searchtype)) {
//form
//实现高级搜索的viewRender?
}
}
/*
* todo: 高级搜索
*/
function advancesearchAction()
{
$md=new MetadataTable();
$db=$md->getAdapter();
$page=(int)$this->_request->getParam('page');
if (empty($page)) $page=1;
$limit=10;
$offset=$limit*($page-1);
$key=$this->_request->getParam('key');
$searchtype=$this->_request->getParam('type');
if (!empty($key)) {
$sql=$db->quoteInto('select count(*) from metadata where title ilike ? or description ilike ?','%'.$key.'%','%'.$key.'%');
$state=$db->query($sql);
$row=$state->fetchAll();
$sum=$row[0]['count'];
$select=$db->select();
$select->from('metadata','*')->where('title ilike ? or description ilike ?','%'.$key.'%','%'.$key.'%')->order('id desc')->limit($limit,$offset);
$this->view->metadata = $db->fetchAll($select);
$this->view->page=new Pagination($sum,$page);
$this->view->key=$key;
}
}
/*
* 根据id或uuid来查看元数据
*/
function viewAction()
{
$md=new MetadataTable();
$db=$md->getAdapter();
$id = (int)$this->_request->getParam('id');
if (empty($id)) {
$uuid = (int)$this->_request->getParam('uuid');
if (empty($uuid)) $this->_redirect('/metadata');
$where=$db->quoteInto('uuid = ?',$uuid);
$row=$md->fetchRow($where);
} else {
$where=$db->quoteInto('id = ?',$id);
$row=$md->fetchRow($where);
}
if (!$row) $this->_redirect('/metadata');
$id=$row->id;
$this->view->metadata=$row;
$where=$db->quoteInto('id= ?',$id);
$order='keytype';
$keyt=new KeywordTable();
$this->view->keys=$keyt->fetchAll($where,$order);
$sql='select c.code,cc.name,cc.name_zh from category c,categorycode cc where c.code=cc.code and c.id= ?';
$state=$db->query($sql,array($id));
$this->view->category=$state->fetchAll();
$sql='select s.name from dataseries d, series s where d.sid=s.id and d.id= ?';
$state=$db->query($sql,array($id));
$this->view->series=$state->fetchAll();
//metadata,keyword,series,category,
}
/*
* 删除元数据,删除前有确认
*/
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('/metadata');
} else {
$id = (int)$this->_request->getParam('id');
if ($id > 0) {
$mdt = new MetadataTable();
$this->view->md = $mdt->fetchRow('id='.$id);
}
}
}
/*
* 查看数据缩略图
*/
function thumbAction()
{
$id = (int)$this->_request->getParam('id');
if ($id>0) {
$md=new ThumbnailTable();
$this->view->thumb=$md->fetchRow('id='.$id);
$this->_helper->layout->disableLayout();
}
}
/*
* 导入本地元数据
*/
function importAction()
{
$md=new MetadataTable();
$thumb=new ThumbnailTable();
$xmlt=new XmlTable();
$cgt=new CategoryTable();
$cgct=new CategoryCodeTable();
$keyt=new KeywordTable();
$dst=new DatasetSeriesTable();
$seriestable=new SeriesTable();
if ($this->_request->isPost()) {
foreach ($_FILES["xmlfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["xmlfile"]["tmp_name"][$key];
$name = $_FILES["xmlfile"]["name"][$key];
$fp = fopen($tmp_name, "rb");
$xml=fread($fp, filesize($tmp_name));
fclose($fp);
$this->import($xml);
/*
$iso=new ISO19115();
$iso->load($tmp_name);
$row=$md->createRow();
$trow=$thumb->createRow();
$row->uuid=$iso->uuid;
$row->title=$iso->resTitle;
$row->description=$iso->idAbs;
$row->timebegin=$iso->timebegin;
$row->timeend=$iso->timeend;
$row->west=$iso->geoBox['w'];
$row->south=$iso->geoBox['s'];
$row->north=$iso->geoBox['n'];
$row->east=$iso->geoBox['e'];
try {
$id=$row->save();
//处理缩略图
if (!empty($iso->thumbnail)) {
$trow->id=$id;
$trow->data=$iso->thumbnail;
$trow->save();
}
//todo:处理关键词
//1:N relation
foreach($iso->keyword as $keytype=>$keys)
{
foreach($keys as $key)
{
$row=$keyt->createRow();
$row->id=$id;
$row->keyword=$key;
$row->keytype=$keytype;
$row->save();
}
}
//todo:处理数据集序列
//M:N relation
$db=$seriestable->getAdapter();
foreach($iso->datasetSeries as $ds)
{
$where=$db->quoteInto('name = ?',$ds['seriesName']);
$trow=$seriestable->fetchRow($where);
if ($trow)
$sid=$trow->id;
else {
$trow=$seriestable->createRow();
$trow->name=$ds['seriesName'];
$sid=$trow->save();
}
$row=$dst->createRow();
$row->id=$id;
$row->sid=$sid;
$row->save();
}
//处理XML入库
$row=$xmlt->createRow();
$row->id=$id;
$row->data=$xml;
$row->save();
//处理数据分类
foreach($iso->tpCat as $cat){
if (is_numeric($cat)) {
$row=$cgt->createRow();
$row->id=$id;
$row->code=(int)$cat;
$row->save();
} else {
//是字符串geonetwork会采用这种模式
//从categorycode表中查找其对应的code
$where=$db->quoteInto('name= ?',trim($cat));
$trow=$cgct->fetchRow($where);
if ($trow) {
$row=$cgt->createRow();
$row->id=$id;
$row->code=$trow->code;
$row->save();
}
//$db=$cgt->getAdapter();
//$sql="insert into category (id,code) values($id,(select code from categorycode where name='$cat'))";
//$db->query($sql);
}
}
} catch (Exception $e) {
//数据重复插入,此处忽略所有错误
}
$fp=fopen('../data/import/$iso->uuid','w');
fwrite($fp,$iso->saveXML());
fclose($fp);
*/
//采用UUID为文件名
//move_uploaded_file($tmp_name, "../data/import/$name");
}//end if
}//foreach
$this->_redirect('/metadata');
} else {
//do nothing now.
}
}
/*
* 批量处理元数据(元数据保存在服务器的一个目录上)
*/
function batchimportAction()
{
if ($this->_request->isPost()) {
$dir = $this->_request->getPost('directory');
$subdir = $this->_request->getPost('subdir');
$xmlfiles=$this->ls($dir,"*.xml");
foreach($xmlfiles as $xmlfile)
{
$fp=fopen($dir.'/'.$xmlfile,'rb');
$xml=fread($fp,filesize($dir.'/'.$xmlfile));
fclose($fp);
$this->import($xml);
}
$this->_redirect('/metadata');
}
}
/*
* 保存XML数据到数据库
*/
private function import($xml)
{
$iso=new ISO19115();
$iso->loadXML($xml);
$md=new MetadataTable();
$thumb=new ThumbnailTable();
$xmlt=new XmlTable();
$cgt=new CategoryTable();
$cgct=new CategoryCodeTable();
$keyt=new KeywordTable();
$dst=new DatasetSeriesTable();
$seriestable=new SeriesTable();
//save metadata
$row=$md->createRow();
$trow=$thumb->createRow();
$row->uuid=$iso->uuid;
$row->title=$iso->resTitle;
$row->description=$iso->idAbs;
$row->timebegin=$iso->timebegin;
$row->timeend=$iso->timeend;
$row->west=$iso->geoBox['w'];
$row->south=$iso->geoBox['s'];
$row->north=$iso->geoBox['n'];
$row->east=$iso->geoBox['e'];
try {
$id=$row->save();
//处理缩略图
if (!empty($iso->thumbnail)) {
$trow->id=$id;
$trow->data=$iso->thumbnail;
$trow->save();
}
//todo:处理关键词
//1:N relation
foreach($iso->keyword as $keytype=>$keys)
{
foreach($keys as $key)
{
$row=$keyt->createRow();
$row->id=$id;
//统一转换为大写
$row->keyword=strtoupper($key);
$row->keytype=$keytype;
$row->save();
}
}
//todo:处理数据集序列
//M:N relation
$db=$seriestable->getAdapter();
foreach($iso->datasetSeries as $ds)
{
$where=$db->quoteInto('name = ?',$ds['seriesName']);
$trow=$seriestable->fetchRow($where);
if ($trow)
$sid=$trow->id;
else {
$trow=$seriestable->createRow();
$trow->name=$ds['seriesName'];
$sid=$trow->save();
}
$row=$dst->createRow();
$row->id=$id;
$row->sid=$sid;
$row->save();
}
//处理XML入库
$row=$xmlt->createRow();
$row->id=$id;
$row->data=$xml;
$row->save();
//处理数据分类
foreach($iso->tpCat as $cat){
if (is_numeric($cat)) {
$row=$cgt->createRow();
$row->id=$id;
$row->code=(int)$cat;
$row->save();
} else {
//是字符串geonetwork会采用这种模式
//从categorycode表中查找其对应的code
$where=$db->quoteInto('name= ?',trim($cat));
$trow=$cgct->fetchRow($where);
if ($trow) {
$row=$cgt->createRow();
$row->id=$id;
$row->code=$trow->code;
$row->save();
}
//$db=$cgt->getAdapter();
//$sql="insert into category (id,code) values($id,(select code from categorycode where name='$cat'))";
//$db->query($sql);
}
}
$fp=fopen("../data/import/$iso->uuid",'w');
fwrite($fp,$iso->saveXML());
fclose($fp);
} catch (Exception $e) {
//数据重复插入,此处忽略所有错误
}
}
/*
* ls(dir,pattern) return file list in "dir" folder matching "pattern"
* ls("path","module.php?") search into "path" folder for module.php3, module.php4, ...
* ls("images/","*.jpg") search into "images" folder for JPG images
*/
function ls($__dir="./",$__pattern="*.*")
{
settype($__dir,"string");
settype($__pattern,"string");
$__ls=array();
$__regexp=preg_quote($__pattern,"/");
$__regexp=preg_replace("/[\\x5C][\x2A]/",".*",$__regexp);
$__regexp=preg_replace("/[\\x5C][\x3F]/",".", $__regexp);
if(is_dir($__dir))
if(($__dir_h=@opendir($__dir))!==FALSE)
{
while(($__file=readdir($__dir_h))!==FALSE)
if(preg_match("/^".$__regexp."$/",$__file))
array_push($__ls,$__file);
closedir($__dir_h);
sort($__ls,SORT_STRING);
}
return $__ls;
}
}