db = $db;
$this->_init_Fields();
}
//初始化简化版元数据的字段
function _init_Fields(){
$this->MetadataFields = array(
//中文标题
'title' => array(
'Title' => '中文标题',
'FieldName' => 'title', //元数据表的字段名称
'FieldName_temp' => 'title', //临时元数据字段名称和表单收集名称
'Type' => 'varchar',
'MaxLength' => 200,
'Required' => true
),
//英文标题
'title_en' => array(
'Title' => '英文标题',
'FieldName' => 'title_en',
'FieldName_temp' => 'title_en',
'Type' => 'varchar',
'MaxLength' => 200,
'Required' => false
),
//摘要
'description' => array(
'Title' => '摘要',
'FieldName' => 'description',
'FieldName_temp' => 'description',
'Type' => 'text',
'MaxLength' => 200,
'Required' => true
),
//开始时间
'timebegin' => array(
'Title' => '开始时间',
'FieldName' => 'timebegin',
'FieldName_temp' => 'timebegin',
'Type' => 'varchar',
'MaxLength' => 24,
'Required' => true
),
//结束时间
'timeend' => array(
'Title' => '结束时间',
'FieldName' => 'timeend',
'FieldName_temp' => 'timeend',
'Type' => 'varchar',
'MaxLength' => 24,
'Required' => false
),
//空间范围-东
'east' => array(
'Title' => '空间范围东',
'FieldName' => 'east',
'FieldName_temp' => 'east',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-南
'south' => array(
'Title' => '空间范围南',
'FieldName' => 'south',
'FieldName_temp' => 'south',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-西
'west' => array(
'Title' => '空间范围西',
'FieldName' => 'west',
'FieldName_temp' => 'west',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//空间范围-北
'north' => array(
'Title' => '空间范围北',
'FieldName' => 'north',
'FieldName_temp' => 'north',
'Type' => 'varchar',
'MaxLength' => 20,
'Required' => true
),
//数据引用方式
'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
),
//联系人
'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()
}