westdc-zf1/application/models/MetaData.php

305 lines
6.3 KiB
PHP
Raw Normal View History

<?php
/**
* Metadata 元数据
*/
class Metadata
{
public $db; //PDO
public $tbl_Metadata = "metadata"; //元数据数据表
public $tbl_Metadata_temp = "metadata_temp"; //新元数据缓存表
public $SavePath = "../data/metadata_temp/"; //临时元数据保存地址
public $SaveMethod = "JSON"; //临时元数据保存格式 XML or JSON
public $XMLNamespace = "metadata_temp"; //临时元数据XML的根域名称
public $MetadataFields; //元数据涉及的字段
function __construct($db)
{
$this->db = $db;
$this->_init_Fields();
}
//初始化简化版元数据的字段
function _init_Fields(){
$this->MetadataFields = array(
//中文标题
2012-12-21 09:17:32 +00:00
'title' => array(
'Title' => '中文标题',
2012-12-21 09:17:32 +00:00
'FieldName' => 'title', //元数据表的字段名称
'FieldName_temp' => 'title', //临时元数据字段名称和表单收集名称
'Type' => 'varchar',
'MaxLength' => 200,
'Required' => true
),
//英文标题
2012-12-21 09:17:32 +00:00
'title_en' => array(
'Title' => '英文标题',
'FieldName' => 'title_en',
'FieldName_temp' => 'title_en',
'Type' => 'varchar',
'MaxLength' => 200,
'Required' => false
),
//摘要
2012-12-21 09:17:32 +00:00
'description' => array(
'Title' => '摘要',
'FieldName' => 'description',
'FieldName_temp' => 'description',
'Type' => 'text',
'MaxLength' => 200,
'Required' => true
),
//开始时间
2012-12-21 09:17:32 +00:00
'timebegin' => array(
'Title' => '开始时间',
'FieldName' => 'timebegin',
'FieldName_temp' => 'timebegin',
'Type' => 'varchar',
'MaxLength' => 24,
'Required' => true
),
//结束时间
2012-12-21 09:17:32 +00:00
'timeend' => array(
'Title' => '结束时间',
'FieldName' => 'timeend',
'FieldName_temp' => 'timeend',
'Type' => 'varchar',
'MaxLength' => 24,
2013-01-29 08:05:45 +00:00
'Required' => false
),
//空间范围-东
2012-12-21 09:17:32 +00:00
'east' => array(
'Title' => '空间范围<b>东</b>',
'FieldName' => 'east',
'FieldName_temp' => 'east',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-南
2012-12-21 09:17:32 +00:00
'south' => array(
'Title' => '空间范围<b>南</b>',
'FieldName' => 'south',
'FieldName_temp' => 'south',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-西
2012-12-21 09:17:32 +00:00
'west' => array(
'Title' => '空间范围<b>西</b>',
'FieldName' => 'west',
'FieldName_temp' => 'west',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-北
2012-12-21 09:17:32 +00:00
'north' => array(
'Title' => '空间范围<b>北</b>',
'FieldName' => 'north',
'FieldName_temp' => 'north',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
2012-12-21 09:17:32 +00:00
//数据引用方式
'citation' => array(
'Title' => '数据引用方式',
'FieldName' => 'citation',
'FieldName_temp' => 'citation',
'Type' => 'varchar',
'MaxLength' => 200,
'Required' => true
),
//参考文献
'reference' => array(
'Title' => '数据参考文献',
'FieldName' => 'reference',
'FieldName_temp' => 'reference',
'Type' => 'array',
'MaxLength' => 200,
'Required' => false
2012-12-21 09:17:32 +00:00
),
//联系人
'contact' => array(
'Title' => '联系人',
'FieldName' => 'contact',
'FieldName_temp' => 'contact',
'Type' => 'array',
'MaxLength' => 200,
'Required' => true,
'options' => array(
"数据作者",
"资源提供者",
"数据服务联系人"
)
),
);
}//_init_Fields()
//记录
function Record($uid,$data,$status,$uuid=''){
if(!is_array($data) || count($data)<1)
{
return false;
}
$content = "";
if($this->SaveMethod == "XML")
{
$content = $this->RecordXML($data);
}
else if($this->SaveMethod == "JSON")
{
$content = $this->RecordJSON($data);
}
else{
return false;
}
$update = 0;
if(empty($uuid))
{
include_once("uuid.php");
@$unqid = new uuid();
$uuid = @$unqid->toString();
}else{
$update = 1;
}
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))
{
return false;
}
//添加一条新记录
if($update == 0)
{
$temp = array(
"uuid" => $uuid,
"userid" => $uid,
"content" => $content,
"status"=>$status
);
if($this->db->insert($this->tbl_Metadata_temp,$temp))
{
return $uuid;
}else{
return false;
}
}//update == 0
//编辑或者更新已存在的记录
else{
$temp = array(
"content" => $content,
"ts_update" => 'now()',
"status" => $status
);
$whereSql = "userid=$uid AND uuid='$uuid'";
if($this->db->update($this->tbl_Metadata_temp,$temp,$whereSql))
{
return $uuid;
}else{
return false;
}
}//update == 1
}//Record()
//返回XML内容
function RecordXML($data){
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement =$dom->createElement($this->XMLNamespace);
foreach($data as $dk=>$dv)
{
foreach($this->MetadataFields as $k=>$v)
{
if($dk == $v['FieldName'])
{
$nod = $dom->createElement($v['FieldName'], $dv);
$rootelement->appendChild($nod);
}
}
}
$dom->appendChild($rootelement);
return $dom->saveHTML();
}//RecordXML
//返回JSON内容
function RecordJSON($data)
{
$content = json_encode($data,JSON_NUMERIC_CHECK);
return $content;
}//RecordJSON
//获得某个用户的元数据草稿
//或者根据某个用户的uid和uuid获得某一条草稿的数据
function getRecord($uid,$uuid=''){
if(empty($uid) || !is_numeric($uid))
{
return false;
}
if(empty($uuid))
{
$sql = "SELECT * FROM ".$this->tbl_Metadata_temp." WHERE userid=$uid ORDER BY ts_created DESC";
$sth = $this->db->query($sql);
$rows = $sth->fetchAll();
return $rows;
}
else{
$sql = "SELECT * FROM ".$this->tbl_Metadata_temp." WHERE userid=$uid AND uuid='$uuid'";
$sth = $this->db->query($sql);
$row = $sth->fetch();
return $row;
}
}//getRecord()
}